:root {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 13px;
  color-scheme: light;
  --bg: #ffffff;
  --side: #f4f4f5;
  --ink: #18181b;
  --muted: #71717a;
  --line: #e4e4e7;
  --hover: #ececef;
  --user: #f4f4f5;
  --danger: #b91c1c;
  --panel: #ffffff;
  --on: #e7e7ea;
  --send: #18181b;
  --send-ink: #ffffff;
  --focus: #d4d4d8;
  --shadow: 0 8px 24px rgba(24, 24, 27, 0.06);
  --backdrop: rgba(24, 24, 27, 0.28);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #18181b;
  --side: #111113;
  --ink: #f4f4f5;
  --muted: #a1a1aa;
  --line: #3f3f46;
  --hover: #27272a;
  --user: #27272a;
  --danger: #fca5a5;
  --panel: #27272a;
  --on: #3f3f46;
  --send: #f4f4f5;
  --send-ink: #18181b;
  --focus: #52525b;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --backdrop: rgba(0, 0, 0, 0.55);
}

html[data-theme="paper"] {
  color-scheme: light;
  --bg: #f7f3ea;
  --side: #efe8da;
  --ink: #29241c;
  --muted: #7a7064;
  --line: #e2d8c6;
  --hover: #e7decc;
  --user: #efe8da;
  --danger: #9b2c2c;
  --panel: #fffdf8;
  --on: #e4d9c4;
  --send: #3f4f3a;
  --send-ink: #f7f3ea;
  --focus: #cbbfa8;
  --shadow: 0 8px 24px rgba(60, 45, 20, 0.08);
  --backdrop: rgba(41, 36, 28, 0.28);
}

html[data-theme="ocean"] {
  color-scheme: dark;
  --bg: #0f172a;
  --side: #0b1220;
  --ink: #e2e8f0;
  --muted: #94a3b8;
  --line: #1e293b;
  --hover: #1e293b;
  --user: #1e293b;
  --danger: #fda4af;
  --panel: #1e293b;
  --on: #334155;
  --send: #38bdf8;
  --send-ink: #0f172a;
  --focus: #334155;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --backdrop: rgba(2, 6, 23, 0.55);
}

@media (prefers-color-scheme: dark) {
  html[data-theme="system"],
  html:not([data-theme]) {
    color-scheme: dark;
    --bg: #18181b;
    --side: #111113;
    --ink: #f4f4f5;
    --muted: #a1a1aa;
    --line: #3f3f46;
    --hover: #27272a;
    --user: #27272a;
    --danger: #fca5a5;
    --panel: #27272a;
    --on: #3f3f46;
    --send: #f4f4f5;
    --send-ink: #18181b;
    --focus: #52525b;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    --backdrop: rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}
button, input, textarea { font: inherit; color: inherit; }
button, a { cursor: pointer; }
a { color: inherit; }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.gate {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background: var(--side);
}
.card {
  width: min(380px, 100%);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 22px 22px 18px;
  box-shadow: var(--shadow);
}
.mark {
  margin: 0;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 11px;
  color: var(--muted);
}
.card h1 { margin: 4px 0 14px; font-weight: 600; font-size: 18px; line-height: 1.35; }
.lede { margin: 0; color: var(--muted); line-height: 1.4; font-size: 13px; }
.card label { display: block; margin: 16px 0 6px; font-size: 12px; }
.card input[type="password"] {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 14px;
}
.card button {
  margin-top: 12px;
  border: 0;
  background: var(--send);
  color: var(--send-ink);
  border-radius: 999px;
  padding: 8px 14px;
}
.theme {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 8px;
  padding: 4px 6px;
  font-size: 12px;
}
.theme-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.card .theme-label { margin-top: 16px; }
.error { color: var(--danger); font-size: 12px; }

.app { height: 100%; overflow: hidden; }
.shell { display: flex; height: 100%; }
.chats {
  width: 260px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--side);
  border-right: 1px solid var(--line);
  min-height: 0;
}
.side-top { padding: 12px 12px 8px; display: flex; flex-direction: column; gap: 8px; }
.side-tools { display: flex; gap: 4px; align-items: center; }
.brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.new, .icon, .quiet, .row {
  border: 0;
  background: transparent;
  text-align: left;
}
.new {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 13px;
}
.new:hover, .row:hover, .quiet:hover { background: var(--hover); }
#find {
  width: 100%;
  border: 1px solid transparent;
  background: var(--panel);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 13px;
}
#find:focus { outline: none; border-color: var(--focus); }
#chat-list {
  flex: 1;
  overflow: auto;
  padding: 4px 8px 8px;
  min-height: 0;
}
.row {
  width: 100%;
  display: flex;
  gap: 6px;
  align-items: center;
  border-radius: 8px;
  padding: 2px 4px 2px 8px;
}
.row .name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 6px 0;
  font-size: 13px;
}
.pin {
  flex: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  padding: 4px;
}
.pin.on { color: var(--ink); font-weight: 600; }
.group {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  padding: 4px 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}
.group .count { font-size: 11px; }
.group-body { display: flex; flex-direction: column; }
.group-body[hidden] { display: none; }
.row.on { background: var(--on); }
.when { flex: none; color: var(--muted); font-size: 11px; }
.none { margin: 8px; color: var(--muted); font-size: 12px; }
.side-foot {
  position: relative;
  border-top: 1px solid var(--line);
  padding: 8px;
  font-size: 12px;
}
.account {
  display: flex;
  align-items: center;
  gap: 8px;
}
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: var(--on);
  font-size: 12px;
  font-weight: 600;
}
#account-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.account-pop {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 52px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: var(--shadow);
}
.account-pop[hidden] { display: none; }
.account-pop a, .account-pop .quiet {
  text-decoration: none;
  text-align: left;
  padding: 4px 0;
  background: transparent;
  border: 0;
  color: var(--ink);
}
#account-rename {
  width: 120px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  border-radius: 8px;
  padding: 4px 6px;
}
.side-foot a { text-decoration: none; padding: 6px 8px; border-radius: 8px; }
.side-foot a:hover { background: var(--hover); }
.quiet { color: var(--muted); padding: 6px 8px; border-radius: 8px; }

.stage {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg);
}
.brand-name { margin: 0; font-size: 14px; font-weight: 600; line-height: 1.2; }
.brand-foot, .about-model, .stage-foot {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.3;
}
.about-model { margin-top: 4px; }
.stage-foot { text-align: center; padding: 0 12px 10px; }
.brand-inline { min-width: 0; }
.top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 8px 14px;
  border-bottom: 1px solid transparent;
}
.top h1 {
  margin: 0;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread {
  flex: 1;
  overflow: auto;
  width: min(720px, 100%);
  margin: 0 auto;
  padding: 18px 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.thread[hidden] { display: none; }
article { max-width: 100%; }
article.user { align-self: end; max-width: min(520px, 92%); }
.bubble {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.45;
}
article.user .bubble {
  background: var(--user);
  border-radius: 14px;
  padding: 8px 12px;
}
.saved, .pending, .stamp {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 11px;
}
.stamp { display: block; }
article.user .stamp { text-align: right; }
.empty {
  position: absolute;
  inset: 44px 0 84px;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
  pointer-events: none;
}
.empty[hidden] { display: none; }
.composer {
  width: min(720px, calc(100% - 24px));
  margin: 0 auto 14px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 6px;
  align-items: end;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 8px 8px 8px 12px;
  box-shadow: var(--shadow);
  background: var(--panel);
}
.composer .error { grid-column: 1 / -1; margin: 0; }
.chips {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chips[hidden] { display: none; }
.chip {
  border: 1px solid var(--line);
  background: transparent;
  color: inherit;
  border-radius: 999px;
  font-size: 12px;
  padding: 2px 8px;
}
.attach {
  align-self: center;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 2px;
}
.attach input { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.composer textarea {
  width: 100%;
  border: 0;
  resize: none;
  padding: 6px 0;
  max-height: 160px;
  line-height: 1.4;
  font-size: 14px;
  background: transparent;
}
.composer textarea:focus { outline: none; }
#send {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 999px;
  background: var(--send);
  color: var(--send-ink);
  padding: 0;
}
#send:disabled { opacity: 0.45; }
.icon { width: 28px; height: 28px; border-radius: 8px; padding: 0; font-size: 16px; }
.mobile-only { display: none; }
#open-nav { display: none; }
body.side-collapsed .chats { display: none; }
body.side-collapsed #open-nav { display: inline-grid; place-items: center; }
.backdrop { display: none; }

.memory-page .bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  height: 44px;
  padding: 0 14px;
  border-bottom: 1px solid var(--line);
  background: var(--side);
}
.memory-page .bar strong { font-size: 13px; }
.memory-page .bar nav, .memory-page .bar form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.memory-page .bar a, .memory-page .quiet {
  text-decoration: none;
  font-size: 12px;
  border: 0;
  background: transparent;
  color: var(--ink);
}
.memory {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: calc(100% - 44px);
}
.memory aside {
  border-right: 1px solid var(--line);
  padding: 12px;
  overflow: auto;
  background: var(--side);
}
.memory ul { list-style: none; padding: 0; margin: 8px 0 0; }
.memory a {
  display: block;
  text-decoration: none;
  padding: 7px 0;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}
.memory a[aria-current="page"] { font-weight: 600; }
.memory a span, .memory .lede { display: block; color: var(--muted); font-size: 11px; }
.memory article { padding: 14px 16px 32px; min-width: 0; overflow: auto; }
.memory h1 { margin: 0 0 10px; font-size: 14px; }
.memory pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.45;
  background: var(--side);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin: 0;
}
.library {
  display: grid;
  grid-template-columns: minmax(240px, 360px) 1fr;
  height: calc(100% - 44px);
}
.library .files { overflow: auto; border-right: 1px solid var(--line); background: var(--side); }
.library .upload {
  display: grid;
  gap: 6px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}
.library .upload input, .library .upload button, .library .preview input, .library .preview button {
  font-size: 12px;
}
.library .row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 8px;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  color: inherit;
  padding: 8px 12px;
}
.library .row[aria-current="true"] { background: var(--panel); }
.library .row small, .library .meta { color: var(--muted); font-size: 11px; }
.library .preview { padding: 14px 16px 32px; overflow: auto; min-width: 0; }
.library .preview img { max-width: 100%; height: auto; border-radius: 12px; }
.library .preview pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
  line-height: 1.45;
}
.library .actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }

@media (max-width: 760px) {
  .mobile-only { display: inline-grid; place-items: center; }
  #open-nav { display: inline-grid; place-items: center; }
  body.side-collapsed .chats { display: flex; }
  .chats {
    position: fixed;
    z-index: 30;
    inset: 0 auto 0 0;
    width: min(280px, 88vw);
    transform: translateX(-105%);
    transition: transform 0.16s ease;
  }
  body.nav-open .chats { transform: none; }
  body.nav-open .backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 20;
    border: 0;
    background: var(--backdrop);
  }
  .memory, .library { grid-template-columns: 1fr; height: auto; }
  .library .files { border-right: 0; border-bottom: 1px solid var(--line); }
  .memory aside { border-right: 0; border-bottom: 1px solid var(--line); }
  .memory-page { height: auto; overflow: auto; }
}
