:root {
  --mist: #F1F5F8;
  --panel: #E3EAF0;
  --ink: #14202B;
  --muted: #5A6B7A;
  --accent: #2F4FD8;
  --accent-ink: #FFFFFF;
  --fresh: #F2C14E;
  --line: #C9D4DD;
  --bubble: #FFFFFF;
  --danger: #B3321F;
}
@media (prefers-color-scheme: dark) {
  :root {
    --mist: #101820;
    --panel: #17222C;
    --ink: #E6EDF3;
    --muted: #8FA1B1;
    --accent: #6C86FF;
    --accent-ink: #0B1220;
    --fresh: #E0AE3A;
    --line: #263543;
    --bubble: #1A2631;
    --danger: #FF8A78;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Schibsted Grotesk", system-ui, sans-serif;
  background: var(--mist);
  color: var(--ink);
}
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
button {
  font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid var(--line);
  background: transparent; color: var(--ink); padding: 8px 14px;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 500; }
button:disabled { opacity: .5; cursor: wait; }
input, textarea {
  font: inherit; color: inherit; background: var(--bubble);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}

/* ---------- login ---------- */
body.auth { display: grid; place-items: center; padding: 24px; }
.auth-box { width: min(380px, 100%); }
.auth-box h1 { font-size: 28px; margin: 0 0 6px; }
.auth-box p.sub { margin: 0 0 24px; color: var(--muted); line-height: 1.5; }
.auth-box label { display: block; font-size: 14px; font-weight: 500; margin: 14px 0 6px; }
.auth-box button.primary { width: 100%; margin-top: 22px; padding: 11px; }
.auth-box .switch { margin-top: 18px; font-size: 14px; color: var(--muted); }
.auth-box .switch button { border: none; padding: 0; color: var(--accent); text-decoration: underline; }
.error { color: var(--danger); font-size: 14px; margin-top: 14px; min-height: 1.2em; }
.hidden { display: none !important; }

/* ---------- app layout ---------- */
body.app { display: grid; grid-template-columns: 264px minmax(0, 1fr) 300px; }
.sidebar, .mem { background: var(--panel); display: flex; flex-direction: column; height: 100vh; min-height: 0; }
.sidebar { border-right: 1px solid var(--line); }
.mem { border-left: 1px solid var(--line); }

.side-top { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.side-top h1 { font-size: 17px; margin: 0; }
#convList { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; flex: 1; }
#convList li { display: flex; align-items: center; border-radius: 8px; }
#convList li:hover, #convList li.active { background: var(--mist); }
#convList li.active .conv-title { font-weight: 700; }
.conv-title {
  flex: 1; min-width: 0; text-align: left; border: none; padding: 9px 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: none;
}
.conv-actions { display: none; padding-right: 4px; }
#convList li:hover .conv-actions, #convList li:focus-within .conv-actions { display: flex; }
.conv-actions button { border: none; padding: 4px 6px; font-size: 13px; color: var(--muted); }
.conv-actions button:hover { color: var(--ink); }
.conv-actions button.del:hover { color: var(--danger); }
.list-empty { color: var(--muted); font-size: 14px; padding: 10px; }
.side-bottom {
  padding: 12px 16px; border-top: 1px solid var(--line); display: flex;
  justify-content: space-between; align-items: center; gap: 8px; font-size: 14px;
}
.side-bottom span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-bottom button { font-size: 13px; padding: 6px 10px; }

.chat { display: flex; flex-direction: column; height: 100vh; min-width: 0; }
.chat header {
  padding: 14px 20px; display: flex; align-items: center; gap: 12px;
  border-bottom: 1px solid var(--line);
}
.chat header #title { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat header small { color: var(--muted); font-size: 13px; }
.only-narrow, .only-mid { display: none; }

#log { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 68ch; padding: 12px 16px; border-radius: 14px; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--bubble); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.error { align-self: flex-start; border: 1px solid var(--danger); color: var(--danger); background: transparent; }
.empty { margin: auto; color: var(--muted); text-align: center; max-width: 38ch; line-height: 1.5; }
.learned {
  align-self: flex-start; font-size: 13px; color: var(--muted);
  border-left: 3px solid var(--fresh); padding: 2px 0 2px 10px; margin-top: -6px;
}
#form { display: flex; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--line); align-items: flex-end; }
#form textarea { resize: none; max-height: 160px; }

.mem h2 { font-size: 15px; margin: 0; padding: 16px 20px 4px; }
.mem p.hint { margin: 0; padding: 0 20px 12px; color: var(--muted); font-size: 13px; line-height: 1.4; }
#memories { list-style: none; margin: 0; padding: 0 12px; overflow-y: auto; flex: 1; }
#memories li {
  display: flex; gap: 8px; justify-content: space-between; align-items: flex-start;
  padding: 9px 8px; border-bottom: 1px solid var(--line); font-size: 14px; line-height: 1.4;
}
#memories li.fresh { box-shadow: inset 3px 0 0 var(--fresh); padding-left: 12px; }
#memories li.none { color: var(--muted); }
#memories li button { border: none; padding: 0 6px; color: var(--muted); font-size: 18px; line-height: 1; }
#memories li button:hover { color: var(--danger); }
.mem-actions { padding: 14px 20px; border-top: 1px solid var(--line); }
.mem-actions button { width: 100%; font-size: 13px; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  body.app { grid-template-columns: 264px minmax(0, 1fr); }
  .only-mid { display: inline-block; }
  .mem { position: fixed; right: 0; top: 0; width: min(320px, 90vw); z-index: 20; transform: translateX(100%); transition: transform .2s; }
  .mem.open { transform: none; box-shadow: -8px 0 24px rgba(0,0,0,.2); }
}
@media (max-width: 760px) {
  body.app { grid-template-columns: minmax(0, 1fr); }
  .only-narrow { display: inline-block; }
  .sidebar { position: fixed; left: 0; top: 0; width: min(300px, 88vw); z-index: 20; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; box-shadow: 8px 0 24px rgba(0,0,0,.2); }
  .conv-actions { display: flex; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar, .mem { transition: none; }
}
