:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --radius-lg: 28px;
  --radius-md: 18px;
  --sidebar-w: 260px;
}

html[data-theme="light"],
html:not([data-theme]) {
  --bg: #ffffff;
  --sidebar: #f9f9f9;
  --sidebar-hover: #ececec;
  --ink: #0d0d0d;
  --muted: #8f8f8f;
  --line: #e5e5e5;
  --user-bubble: #f4f4f4;
  --composer: #ffffff;
  --composer-border: #e5e5e5;
  --composer-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06), 0 8px 24px rgba(0, 0, 0, 0.06);
  --send-bg: #0d0d0d;
  --send-fg: #ffffff;
  --send-disabled: #d9d9d9;
  --avatar: #0d0d0d;
  --avatar-fg: #ffffff;
  --toast-bg: #f4f4f4;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #212121;
  --sidebar: #171717;
  --sidebar-hover: #2f2f2f;
  --ink: #ececec;
  --muted: #afafaf;
  --line: #2f2f2f;
  --user-bubble: #2f2f2f;
  --composer: #2f2f2f;
  --composer-border: #424242;
  --composer-shadow: 0 0 0 1px rgba(255, 255, 255, 0.06), 0 8px 24px rgba(0, 0, 0, 0.35);
  --send-bg: #ffffff;
  --send-fg: #0d0d0d;
  --send-disabled: #565656;
  --avatar: #ffffff;
  --avatar-fg: #0d0d0d;
  --toast-bg: #2f2f2f;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]):not([data-theme="dark"]) {
    --bg: #212121;
    --sidebar: #171717;
    --sidebar-hover: #2f2f2f;
    --ink: #ececec;
    --muted: #afafaf;
    --line: #2f2f2f;
    --user-bubble: #2f2f2f;
    --composer: #2f2f2f;
    --composer-border: #424242;
    --composer-shadow: 0 0 0 1px rgba(255, 255, 255, 0.06), 0 8px 24px rgba(0, 0, 0, 0.35);
    --send-bg: #ffffff;
    --send-fg: #0d0d0d;
    --send-disabled: #565656;
    --avatar: #ffffff;
    --avatar-fg: #0d0d0d;
    --toast-bg: #2f2f2f;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.shell {
  display: flex;
  height: 100%;
  min-height: 100dvh;
  overflow: hidden;
}

/* —— Sidebar —— */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  gap: 0.5rem;
  z-index: 30;
}

.sidebar-top,
.sidebar-mid,
.sidebar-bottom {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.sidebar-mid { flex: 1; min-height: 0; overflow: auto; }
.sidebar-bottom { margin-top: auto; padding-top: 0.5rem; border-top: 1px solid var(--line); }

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  text-decoration: none;
  color: var(--ink);
  padding: 0.45rem 0.5rem;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.95rem;
}

.brand:hover { background: var(--sidebar-hover); }

.brand-mark {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 999px;
  background: var(--avatar);
  color: var(--avatar-fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
}

.btn-ghost,
.nav-item {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 0.875rem;
  text-align: left;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  cursor: pointer;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  width: 100%;
}

.btn-ghost:hover,
.nav-item:hover { background: var(--sidebar-hover); }

.nav-item.is-active { background: var(--sidebar-hover); font-weight: 500; }

.sidebar-label {
  margin: 0.75rem 0.65rem 0.15rem;
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.sidebar-meta {
  margin: 0;
  padding: 0.15rem 0.65rem;
  font-size: 0.75rem;
  color: var(--muted);
}

#theme-toggle .icon-sun { display: none; }
#theme-toggle .icon-moon { display: inline; }
html[data-theme="dark"] #theme-toggle .icon-sun { display: inline; }
html[data-theme="dark"] #theme-toggle .icon-moon { display: none; }

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]):not([data-theme="dark"]) #theme-toggle .icon-sun { display: inline; }
  html:not([data-theme="light"]):not([data-theme="dark"]) #theme-toggle .icon-moon { display: none; }
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 20;
}

/* —— Main —— */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  min-height: 3.25rem;
}

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 10px;
  cursor: pointer;
  font-size: 1.1rem;
  display: none;
  align-items: center;
  justify-content: center;
}

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

.model-wrap {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.model-select {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  padding: 0.35rem 1.5rem 0.35rem 0.5rem;
  border-radius: 10px;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238f8f8f' d='M3 4.5L6 8l3-3.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.35rem center;
}

.model-select:hover { background-color: var(--sidebar-hover); }

.thread {
  max-width: 48rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 1rem 0 2rem;
}

.turn {
  display: flex;
  gap: 0.85rem;
  animation: fadeIn 0.3s ease;
}

.turn-user {
  justify-content: flex-end;
}

.bubble {
  max-width: min(85%, 36rem);
  background: var(--user-bubble);
  color: var(--ink);
  padding: 0.75rem 1.1rem;
  border-radius: var(--radius-md);
  font-size: 1rem;
  line-height: 1.55;
}

.turn-assistant .avatar {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 999px;
  background: var(--avatar);
  color: var(--avatar-fg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  margin-top: 0.15rem;
}

.turn-assistant .content {
  flex: 1;
  min-width: 0;
  font-size: 1rem;
  line-height: 1.65;
  padding-top: 0.1rem;
}

.turn-assistant .content p { margin: 0; }
.turn-assistant .content p + p { margin-top: 0.65rem; }

.msg-meta {
  font-size: 0.75rem !important;
  color: var(--muted);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* —— Composer —— */
.composer {
  padding: 0.5rem 1rem 1rem;
  max-width: 48rem;
  width: 100%;
  margin: 0 auto;
}

.compose-box {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  background: var(--composer);
  border: 1px solid var(--composer-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--composer-shadow);
  padding: 0.65rem 0.65rem 0.65rem 1rem;
}

#prompt {
  flex: 1;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  line-height: 1.45;
  min-height: 1.45rem;
  max-height: 10rem;
  padding: 0.35rem 0;
}

#prompt::placeholder { color: var(--muted); }

.send {
  appearance: none;
  border: 0;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 999px;
  background: var(--send-bg);
  color: var(--send-fg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.send:hover { opacity: 0.88; }
.send:active { transform: scale(0.96); }

.compose-hint {
  margin: 0.55rem 0 0;
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
}

.toast {
  margin: 0.65rem 0 0;
  padding: 0.7rem 0.9rem;
  border-radius: 12px;
  background: var(--toast-bg);
  color: var(--ink);
  font-size: 0.875rem;
  text-align: center;
  animation: fadeIn 0.25s ease;
}

.legal {
  margin: 0.75rem 0 0;
  text-align: center;
  font-size: 0.7rem;
  color: var(--muted);
}

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

.legal a:hover { color: var(--ink); }

/* —— Privacy doc —— */
.doc {
  max-width: 40rem;
  margin: 0 auto;
  padding: 2rem 1.25rem 3rem;
  line-height: 1.6;
}

.doc h1 {
  font-weight: 600;
  font-size: 1.75rem;
  color: var(--ink);
  margin: 0 0 1rem;
}

.doc a { color: var(--ink); }
.doc p { color: var(--muted); }
.doc-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.doc .theme-toggle,
.doc #theme-toggle {
  width: auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.4rem 0.85rem;
  background: var(--sidebar);
}

/* —— Mobile —— */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.12);
  }

  .shell.sidebar-open .sidebar { transform: translateX(0); }

  .sidebar-backdrop { display: block; }
  .shell.sidebar-open .sidebar-backdrop { display: block; }
  .sidebar-backdrop[hidden] { display: none !important; }
  .shell.sidebar-open .sidebar-backdrop[hidden] { display: block !important; }

  .sidebar-open { display: inline-flex; }

  .bubble { max-width: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  .turn, .toast, .sidebar { animation: none; transition: none; }
}
