/* Kildechat – rolig, klinisk-profesjonelt grensesnitt. Bokmål. */

:root {
  /* Flater */
  --bg:            #f6f7f7;
  --bg-sidebar:    #eeefef;
  --surface:       #ffffff;
  --surface-2:     #f1f2f2;
  --surface-hover: #e6e8e8;

  /* Tekst */
  --text:          #1c2222;
  --text-muted:    #5b6666;
  --text-faint:    #7d8888;

  /* Linjer */
  --border:        #dcdfdf;
  --border-strong: #c4c9c9;

  /* Aksent: dempet blågrønn */
  --accent:        #2f7d78;
  --accent-hover:  #276a66;
  --accent-soft:   #e3efee;
  --accent-text:   #1f5c58;
  --on-accent:     #ffffff;

  /* Status */
  --danger:        #b3261e;
  --danger-soft:   #fbeae9;
  --danger-border: #edc4c1;
  --ok:            #2e7d4f;
  --warn:          #a8690b;

  /* Bobler */
  --user-bubble:   #e3efee;
  --user-border:   #c9e0de;

  --radius:        10px;
  --radius-sm:     7px;
  --shadow:        0 1px 2px rgba(20, 30, 30, .06), 0 4px 14px rgba(20, 30, 30, .05);

  --sidebar-w:     284px;
  --measure:       72ch;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          "Noto Sans", Arial, "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:            #14181a;
    --bg-sidebar:    #101416;
    --surface:       #1b2124;
    --surface-2:     #222a2d;
    --surface-hover: #2a3336;

    --text:          #e6eaea;
    --text-muted:    #a3adad;
    --text-faint:    #7f8a8a;

    --border:        #2b3437;
    --border-strong: #3b4649;

    --accent:        #5fb3ac;
    --accent-hover:  #74c4bd;
    --accent-soft:   #1d2e2d;
    --accent-text:   #8fd2cb;
    --on-accent:     #0f1718;

    --danger:        #f2938c;
    --danger-soft:   #2c1c1b;
    --danger-border: #4d2c2a;
    --ok:            #79c99b;
    --warn:          #e0b371;

    --user-bubble:   #1d2e2d;
    --user-border:   #2b4341;

    --shadow:        0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

/* Skal alltid vinne over display-regler under (.status-line, .drop-overlay, knapper …). */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, textarea { font: inherit; color: inherit; }

::selection { background: var(--accent-soft); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  height: 100%;
}

/* ============ INNLOGGING (sky-modus) ============ */

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  overflow-y: auto;
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(340px, 100%);
  padding: 22px 22px 20px;
  border: 1px solid var(--border);
  border-top: 3px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-brand { margin-bottom: 2px; }
.login-brand .brand-mark { width: 34px; height: 34px; border-radius: 9px; font-size: 18px; }
.login-brand .brand-text h1 { font-size: 18px; }

.login-lead {
  margin: 0 0 4px;
  font-size: 13.5px;
  color: var(--text-muted);
}

.login-label {
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-faint);
}

#login-password {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-size: 14px;
}

#login-password:focus { border-color: var(--accent); }

#login-submit { margin-top: 4px; }

.login-msg {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-muted);
}

.login-msg.error { color: var(--danger); }

/* ============ SIDEFELT ============ */

#sidebar {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-height: 0;
  padding: 16px 14px 14px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overscroll-behavior: contain;
}

.sb-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  font-size: 16px;
  flex: none;
}

.brand-text h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .01em;
  line-height: 1.2;
}

.model-badge {
  display: block;
  font-size: 11.5px;
  color: var(--text-faint);
  letter-spacing: .02em;
}

.sb-section { display: flex; flex-direction: column; gap: 7px; }

.sb-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px;
  margin: 0 0 2px;
  padding: 0 4px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.sb-count { font-weight: 400; letter-spacing: 0; text-transform: none; }

.sb-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }

/* Listene får hver sin rullesone, slik at «Legg til PDF» og «Innstillinger»
   holder seg synlige selv med mange samtaler og kilder. */
#conv-list { max-height: 26vh; overflow-y: auto; overscroll-behavior: contain; }
#src-list { max-height: 38vh; overflow-y: auto; overscroll-behavior: contain; }

.sb-empty {
  margin: 0;
  padding: 2px 6px 4px;
  font-size: 12.5px;
  color: var(--text-faint);
}

.sb-foot { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--border); }

.sb-note {
  margin: 6px 4px 0;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text-faint);
}

.sb-note code { font-family: var(--mono); font-size: .95em; }

/* Sky-modus: notatet står over «Logg ut» og trenger litt luft under seg. */
#cloud-note { margin-bottom: 10px; }

/* Rader i sidefeltet */
.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  cursor: pointer;
  text-align: left;
  color: var(--text);
}

.row:hover { background: var(--surface-hover); }
.row.active { background: var(--surface); border-color: var(--border); }

.row-main { min-width: 0; flex: 1; }

.row-title {
  display: block;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-sub {
  display: block;
  font-size: 11.5px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-key {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--accent-text);
}

.row-del {
  flex: none;
  width: 22px; height: 22px;
  display: grid; place-items: center;
  border: 0;
  border-radius: 5px;
  background: none;
  color: var(--text-faint);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;   /* usynlig knapp skal ikke kunne treffes ved uhell */
}

.row:hover .row-del, .row-del:focus-visible { opacity: 1; pointer-events: auto; }

/* Uten hover (berøringsskjerm) må sletteknappen være synlig for å kunne brukes. */
@media (hover: none) {
  .row-del { opacity: .55; pointer-events: auto; }
}
.row-del:hover { background: var(--danger-soft); color: var(--danger); }

.badge {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 10.5px;
  letter-spacing: .01em;
  white-space: nowrap;
}

.badge.ingesting { background: var(--accent-soft); color: var(--accent-text); }
.badge.error { background: var(--danger-soft); color: var(--danger); }

.info-dot {
  flex: none;
  width: 16px; height: 16px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 10.5px;
  font-style: normal;
  cursor: help;
}

.dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-faint);
  vertical-align: baseline;
}

.dot.ok { background: var(--ok); }
.dot.missing { background: var(--warn); }

.key-status {
  margin: 0 0 8px;
  padding: 0 4px;
  font-size: 12.5px;
  color: var(--text-muted);
}

/* Knapper */
.btn-primary, .btn-ghost {
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  padding: 8px 12px;
  transition: background .12s ease, border-color .12s ease;
}

.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
  border: 1px solid var(--accent);
  font-weight: 500;
}

.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:disabled { opacity: .55; cursor: default; }

.btn-ghost {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-ghost:hover { background: var(--surface-hover); }

.block { display: block; width: 100%; }

.icon-btn {
  border: 1px solid transparent;
  background: none;
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1;
}

.icon-btn:hover { background: var(--surface-hover); }

.upload-msg {
  margin: 2px 4px 0;
  font-size: 12px;
  color: var(--text-muted);
}

.upload-msg.error { color: var(--danger); }

/* Dra-og-slipp */
.drop-overlay {
  position: absolute;
  inset: 6px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 14px;
  font-weight: 500;
  z-index: 20;
  pointer-events: none;
}

/* ============ HOVEDOMRÅDE ============ */

#main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.conv-title {
  font-size: 13.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#scroller {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

#thread-wrap {
  max-width: calc(var(--measure) + 80px);
  margin: 0 auto;
  padding: 24px 24px 8px;
}

/* Oppsettkort for nøkkel */
.setup-card {
  margin-bottom: 22px;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.setup-card h2 { margin: 0 0 6px; font-size: 15px; font-weight: 600; }
.setup-card p { margin: 0 0 12px; font-size: 13.5px; color: var(--text-muted); }
.setup-card code { font-family: var(--mono); font-size: 12.5px; }

#setup-form { display: flex; gap: 8px; flex-wrap: wrap; }

#setup-key {
  flex: 1 1 240px;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg);
  font-family: var(--mono);
  font-size: 13px;
}

.setup-msg { margin: 10px 0 0 !important; font-size: 13px; }
.setup-msg.ok { color: var(--ok); }
.setup-msg.error { color: var(--danger); }

/* Tomtilstand */
.empty-state { padding: 36px 2px 10px; }
.empty-state h2 { margin: 0 0 6px; font-size: 21px; font-weight: 600; letter-spacing: -.01em; }
.empty-state > p { margin: 0 0 20px; color: var(--text-muted); max-width: 60ch; }

.suggestions { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }

.suggestion {
  max-width: 100%;
  padding: 9px 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}

.suggestion:hover { background: var(--surface-hover); border-color: var(--border-strong); }

/* Meldinger */
.thread { display: flex; flex-direction: column; gap: 22px; }

.msg { display: flex; flex-direction: column; gap: 5px; }
.msg.user { align-items: flex-end; }

.msg-role {
  font-size: 11px;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 0 2px;
}

.bubble {
  max-width: var(--measure);
  border-radius: var(--radius);
  overflow-wrap: break-word;
}

.msg.user .bubble {
  padding: 10px 14px;
  background: var(--user-bubble);
  border: 1px solid var(--user-border);
  white-space: pre-wrap;
}

.msg.assistant .bubble {
  max-width: var(--measure);
  padding: 0 2px;
}

.msg.error .bubble {
  padding: 10px 14px;
  background: var(--danger-soft);
  border: 1px solid var(--danger-border);
  color: var(--danger);
  font-size: 14px;
}

/* Markdown i svar */
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }

.md p { margin: 0 0 .85em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.4em 0 .5em; font-weight: 600; line-height: 1.3; }
.md h1 { font-size: 1.32em; }
.md h2 { font-size: 1.18em; }
.md h3 { font-size: 1.05em; }
.md h4 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .85em; padding-left: 1.4em; }
.md li { margin: .22em 0; }
.md li > ul, .md li > ol { margin: .25em 0; }

.md blockquote {
  margin: 0 0 .85em;
  padding: .1em 0 .1em 1em;
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
}

.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }

.md a { color: var(--accent-text); text-decoration: underline; text-underline-offset: 2px; }

.md code {
  font-family: var(--mono);
  font-size: .88em;
  padding: .12em .35em;
  border-radius: 4px;
  background: var(--surface-2);
}

.md pre {
  margin: 0 0 .95em;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  overflow-x: auto;
}

.md pre code { padding: 0; background: none; font-size: .85em; line-height: 1.5; }

.md .table-wrap { overflow-x: auto; margin: 0 0 1em; }

.md table {
  border-collapse: collapse;
  /* La tabellen bli så bred som innholdet krever og la .table-wrap rulle –
     ellers klemmes doseringstabeller sammen i stedet for å kunne rulles. */
  width: max-content;
  min-width: 100%;
  font-size: .93em;
  background: var(--surface);
}

.md th, .md td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  text-align: left;
  vertical-align: top;
}

.md th { background: var(--surface-2); font-weight: 600; }
.md tbody tr:nth-child(even) td { background: var(--surface-2); }

.md img { max-width: 100%; height: auto; }

/* Sitatklammer */
a.cite {
  /* inline (ikke inline-block) og lite sidepolstring, slik at punktumet etter
     en avsluttende henvisning ikke blir stående og flyte. */
  display: inline;
  padding: 0 3px;
  border: 1px solid var(--user-border);
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: .82em;
  font-weight: 500;
  line-height: 1.5;
  text-decoration: none;
  white-space: nowrap;
  vertical-align: baseline;
}

a.cite:hover { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* Statuslinje under strømming */
.status-line {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 14px;
  padding: 0 2px;
  font-size: 13px;
  color: var(--text-muted);
  min-height: 22px;
}

.spinner {
  flex: none;
  width: 13px; height: 13px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}

.badge .spinner { width: 9px; height: 9px; border-width: 1.5px; }

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
}

.caret {
  display: inline-block;
  width: .5em;
  height: 1em;
  margin-left: 1px;
  background: var(--accent);
  vertical-align: text-bottom;
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink { 50% { opacity: 0; } }

/* Komposer */
#composer-wrap {
  padding: 10px 24px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: calc(var(--measure) + 80px);
  margin: 0 auto;
  padding: 8px 8px 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.composer:focus-within { border-color: var(--accent); }

#input {
  flex: 1;
  min-height: 24px;
  max-height: 220px;
  padding: 4px 0;
  border: 0;
  background: none;
  resize: none;
  outline: none;
  line-height: 1.5;
  overflow-y: auto;
}

#input::placeholder { color: var(--text-faint); }

.btn-send, .btn-stop {
  flex: none;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 500;
}

.btn-send { background: var(--accent); color: var(--on-accent); border: 1px solid var(--accent); }
.btn-send:hover:not(:disabled) { background: var(--accent-hover); }
.btn-send:disabled { opacity: .45; cursor: default; }

.btn-stop { background: var(--surface); color: var(--text); border: 1px solid var(--border-strong); }
.btn-stop:hover { background: var(--surface-hover); }

.composer-hint {
  max-width: calc(var(--measure) + 80px);
  margin: 7px auto 0;
  padding: 0 2px;
  font-size: 11.5px;
  color: var(--text-faint);
  text-align: center;
}

/* ============ SMAL SKJERM ============ */

.only-narrow { display: none; }

@media (max-width: 860px) {
  #app { grid-template-columns: minmax(0, 1fr); }

  .only-narrow { display: block; }

  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, var(--sidebar-w));
    z-index: 30;
    box-shadow: var(--shadow);
    transform: translateX(-102%);
    transition: transform .18s ease;
  }

  body.sidebar-open #sidebar { transform: none; }

  body.sidebar-open::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .3);
    z-index: 25;
  }

  #thread-wrap { padding: 18px 16px 8px; }
  #composer-wrap { padding: 8px 12px 12px; }
}
