/* since: browser UI styles. Plain CSS, system fonts, no network assets. */

:root {
  color-scheme: light dark;
  --bg: #fafaf9;
  --surface: #ffffff;
  --surface-2: #f4f4f5;
  --sidebar-bg: #f4f4f3;
  --text: #18181b;
  --muted: #71717a;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --accent: #4f46e5;
  --accent-text: #4338ca;
  --accent-soft: #eef2ff;
  --on-accent: #ffffff;
  --user-bg: #efeff1;
  --code-bg: #f4f4f5;
  --good: #15803d;
  --good-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --danger: #b91c1c;
  --danger-bg: #fef2f2;
  --shadow: 0 1px 2px rgba(24, 24, 27, 0.06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0f12;
    --surface: #17171c;
    --surface-2: #202028;
    --sidebar-bg: #131317;
    --text: #ececf1;
    --muted: #9b9ba7;
    --border: #2b2b35;
    --border-strong: #3a3a46;
    --accent: #8b8cf8;
    --accent-text: #c7c8ff;
    --accent-soft: rgba(139, 140, 248, 0.16);
    --on-accent: #101014;
    --user-bg: #22222b;
    --code-bg: #1c1c23;
    --good: #4ade80;
    --good-bg: rgba(74, 222, 128, 0.14);
    --warn: #fbbf24;
    --warn-bg: rgba(251, 191, 36, 0.14);
    --danger: #f87171;
    --danger-bg: rgba(248, 113, 113, 0.12);
    --shadow: none;
  }
}

/* ---------- base ---------- */

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

html { height: 100%; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--font);
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, p { margin: 0; }

[hidden] { display: none !important; }

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

.muted { color: var(--muted); }
.small { font-size: 13px; }
.pad { padding: 8px; }
.center { text-align: center; }

@keyframes shimmer {
  from { background-position: 100% 0; }
  to { background-position: -100% 0; }
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ---------- layout ---------- */

.app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
}

.main {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  flex: none;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.title {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 15px;
  font-weight: 600;
}

.menu-btn {
  display: none;
  flex: none;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
}

.menu-btn svg { width: 20px; height: 20px; }

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

.thread-inner {
  min-height: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 28px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

/* ---------- sidebar ---------- */

.sidebar {
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
}

.brand { padding: 4px 8px 14px; }

.brand-name {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.tagline {
  font-size: 13px;
  color: var(--muted);
}

.new-chat {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
  font-weight: 600;
  text-align: left;
  box-shadow: var(--shadow);
}

.new-chat:hover { border-color: var(--border-strong); }

.conv-list {
  flex: 1;
  min-height: 0;
  margin-top: 8px;
  overflow-y: auto;
}

.group-heading {
  margin: 16px 8px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.conv-group {
  list-style: none;
  margin: 0;
  padding: 0;
}

.conv-item {
  position: relative;
  border-radius: 8px;
}

.conv-item:hover,
.conv-item.active { background: var(--surface-2); }

.conv-item.active { box-shadow: inset 2px 0 0 var(--accent); }

.conv-link {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  padding: 7px 30px 7px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
}

.conv-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.conv-time {
  flex-shrink: 0;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.conv-del {
  position: absolute;
  top: 50%;
  right: 4px;
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  opacity: 0;
}

.conv-item:hover .conv-del,
.conv-del:focus-visible { opacity: 1; }

.conv-del:hover {
  color: var(--danger);
  background: var(--surface);
}

@media (hover: none) {
  .conv-del { opacity: 1; }
}

/* ---------- messages ---------- */

.msg { min-width: 0; }

.msg.user {
  display: flex;
  justify-content: flex-end;
}

.bubble {
  max-width: min(85%, 560px);
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
  background: var(--user-bg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg.assistant {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg.assistant > :empty { display: none; }

/* Activity panel */

.activity {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
  color: var(--muted);
}

.activity-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 13px;
  text-align: left;
}

.activity-toggle:disabled { cursor: default; }

.activity-toggle::after {
  content: "\25BE";
  display: inline-block;
  transition: transform 0.15s ease;
}

.activity-toggle[aria-expanded="true"]::after { transform: rotate(180deg); }

.activity-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.activity-toggle[aria-expanded="true"] + .activity-body { margin-top: 8px; }

.act-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.act-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.act-label.pending::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 7px;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: middle;
  animation: pulse 1s ease-in-out infinite;
}

.pill {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 2px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.badge {
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 11px;
  line-height: 1.6;
}

.preamble {
  font-style: italic;
  font-size: 13.5px;
  color: var(--muted);
}

.recall-hits {
  margin: 0;
  padding-left: 18px;
  font-size: 13.5px;
  color: var(--text);
}

.recall-hits .when { color: var(--muted); }

.status-line {
  font-size: 13px;
  color: var(--muted);
}

/* Thinking shimmer */

.thinking {
  width: max-content;
  font-size: 15px;
  color: transparent;
  background: linear-gradient(90deg, var(--muted) 35%, var(--text) 50%, var(--muted) 65%) 0 0 / 250% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  animation: shimmer 1.8s linear infinite;
}

/* Answer typography */

.answer {
  min-width: 0;
  font-size: 16px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }

.answer p,
.answer ul,
.answer ol,
.answer blockquote,
.answer pre,
.answer .table-wrap { margin: 0.7em 0; }

.answer h1,
.answer h2,
.answer h3,
.answer h4 {
  margin: 1.2em 0 0.4em;
  line-height: 1.3;
  font-weight: 650;
}

.answer h1 { font-size: 1.15em; }
.answer h2 { font-size: 1.1em; }
.answer h3,
.answer h4 { font-size: 1.05em; }

.answer ul,
.answer ol { padding-left: 1.4em; }
.answer li + li { margin-top: 0.25em; }
.answer li > p { margin: 0.3em 0; }

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

.answer code {
  padding: 0.1em 0.35em;
  border-radius: 5px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 0.88em;
}

.answer pre {
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--code-bg);
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.5;
}

.answer pre code {
  padding: 0;
  background: none;
  font-size: inherit;
}

.answer blockquote {
  margin-left: 0;
  padding: 0 0 0 14px;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
}

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

.answer img { max-width: 100%; }

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.answer table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 15px;
}

.answer th,
.answer td {
  max-width: 42ch;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.answer th {
  background: var(--surface-2);
  font-weight: 600;
}

.answer tr:last-child td { border-bottom: 0; }

/* Citation chips */

.cite {
  position: relative;
  top: -0.5em;
  display: inline-block;
  min-width: 1.35em;
  margin: 0 0.1em;
  padding: 0.12em 0.4em;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 0.72em;
  font-weight: 650;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  white-space: nowrap;
}

.cite:hover,
.cite:focus-visible {
  background: var(--accent);
  color: var(--on-accent);
}

.stopped-note {
  margin: -4px 0 0;
  font-size: 13px;
  font-style: italic;
  color: var(--muted);
}

/* Sources */

.sources {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.source {
  min-width: 0;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  font-size: 13px;
}

.source[hidden] {
  display: none;
}

.sources-more {
  margin-top: 6px;
  font-size: 13px;
}

.source-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  color: var(--text);
  font-weight: 550;
  line-height: 1.4;
  text-decoration: none;
  overflow-wrap: anywhere;
}

.source-title:hover {
  color: var(--accent-text);
  text-decoration: underline;
}

.src-n {
  color: var(--accent-text);
  font-weight: 650;
}

.src-meta {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
}

/* Meta row and details */

.meta-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 10px;
  font-size: 13px;
  color: var(--muted);
}

.link-btn {
  padding: 2px 4px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--muted);
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.link-btn:hover { color: var(--text); }

.details {
  max-height: 320px;
  margin: 0;
  padding: 10px 12px;
  overflow: auto;
  border-radius: 10px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
}

/* Verification */

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

.verify-badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 13px;
}

button.verify-badge { cursor: pointer; }

.verify-badge.good {
  background: var(--good-bg);
  color: var(--good);
}

.verify-badge.warn {
  background: var(--warn-bg);
  color: var(--warn);
}

.flagged {
  align-self: stretch;
  margin: 0;
  padding: 10px 12px 10px 28px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
}

.flagged li + li { margin-top: 8px; }

.verdict {
  font-weight: 600;
  text-transform: capitalize;
}

.flagged .note {
  font-size: 13px;
  color: var(--muted);
}

/* Errors */

.turn-error {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 14px;
}

.retry {
  padding: 4px 12px;
  border: 1px solid currentColor;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font-size: 13px;
  font-weight: 600;
}

.retry:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Empty state */

.empty {
  margin: auto 0;
  padding: 6vh 0;
  text-align: center;
}

.empty-lead {
  max-width: 520px;
  margin: 0 auto 24px;
  font-size: 17px;
  line-height: 1.6;
  color: var(--muted);
}

.chips {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  text-align: left;
}

.chip {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-size: 14px;
  line-height: 1.45;
  box-shadow: var(--shadow);
}

.chip:hover { border-color: var(--accent); }

.example-offer {
  max-width: 520px;
  margin: 28px auto 0;
  display: grid;
  gap: 10px;
  justify-items: center;
}

.example-offer p { margin: 0; }

.example-btn {
  padding: 9px 16px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: transparent;
  color: var(--accent-text);
  font-size: 14px;
}

.example-btn:hover:not(:disabled) { background: var(--accent); color: var(--on-accent); }
.example-btn:disabled { opacity: 0.7; cursor: progress; }

/* ---------- composer ---------- */

.composer {
  flex: none;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
}

.composer-box {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

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

.composer-input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  max-height: 13.5rem;
  padding: 7px 0;
  border: 0;
  outline: none;
  background: transparent;
  font-size: 16px;
  line-height: 1.5;
  resize: none;
  overflow-y: auto;
}

.send-btn {
  flex: none;
  height: 36px;
  min-width: 64px;
  padding: 0 14px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 14px;
  font-weight: 600;
}

.send-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.send-btn.stop {
  border-color: var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
}

/* ---------- narrow screens: sidebar becomes an overlay ---------- */

.scrim { display: none; }

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

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    width: 260px;
    max-width: 82vw;
    transform: translateX(-102%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0.2s ease;
    box-shadow: 0 0 0 1px var(--border);
  }

  .nav-open .sidebar {
    transform: none;
    visibility: visible;
  }

  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 15;
    background: rgba(0, 0, 0, 0.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  .nav-open .scrim {
    opacity: 1;
    pointer-events: auto;
  }

  .menu-btn { display: inline-grid; }

  .thread-inner {
    padding: 20px 16px;
    gap: 26px;
  }

  .chips { grid-template-columns: 1fr; }
}
