/* =========================================================================
   GOC Operator — operatorAdmin
   Terminal-amber console. Dark, data-forward, WCAG AA (all pairs verified).
   Palette authored in OKLCH; every text/bg pair clears 4.5:1, non-text 3:1.
   ========================================================================= */

:root {
  /* — Surfaces: warm charcoal neutrals (hue ~68), terminal-dark — */
  --bg:          oklch(0.165 0.010 70);
  --surface:     oklch(0.210 0.012 68);
  --surface-2:   oklch(0.250 0.014 68);
  --user-tint:   oklch(0.290 0.030 72);
  --border:      oklch(0.320 0.016 68);
  --border-strong: oklch(0.400 0.018 68);

  /* — Ink — */
  --text:        oklch(0.950 0.008 85);
  --text-muted:  oklch(0.740 0.020 80);
  --text-dim:    oklch(0.630 0.022 78);   /* large / non-critical only */

  /* — Accent: terminal amber. Actions, focus, selection, status only. — */
  --accent:      oklch(0.800 0.145 78);
  --accent-hi:   oklch(0.855 0.140 82);    /* hover */
  --accent-ink:  oklch(0.220 0.030 70);    /* dark text on amber fill: 9.15:1 */
  --accent-soft: oklch(0.800 0.145 78 / 0.16);

  /* — Semantic state tokens — */
  --success:     oklch(0.800 0.140 158);
  --success-bg:  oklch(0.300 0.050 158);
  --error-text:  oklch(0.790 0.115 30);
  --error-bg:    oklch(0.300 0.070 30);
  --error-border:oklch(0.420 0.100 30);

  /* — Type — */
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;

  /* — Radii & spacing — */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --shadow-1: 0 1px 2px oklch(0 0 0 / 0.4);
  --shadow-2: 0 8px 30px oklch(0 0 0 / 0.35);

  /* — Motion — */
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);       /* ease-out-quart-ish */
  --dur-fast: 120ms;
  --dur-mid: 220ms;

  /* — z-index scale — */
  --z-sticky: 100;
  --z-backdrop: 250;
  --z-drawer: 300;
  --z-toast: 400;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
  border-radius: var(--r-sm);
}

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

.skip-link {
  position: absolute; left: 12px; top: -60px; z-index: var(--z-toast);
  background: var(--surface-2); color: var(--text);
  padding: 10px 14px; border-radius: var(--r-sm); border: 1px solid var(--border);
  text-decoration: none; transition: top var(--dur-fast) var(--ease-out);
}
.skip-link:focus { top: 12px; }

kbd {
  font-family: var(--font-mono); font-size: 0.82em;
  background: var(--surface-2); border: 1px solid var(--border);
  border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px;
  color: var(--text-muted);
}

/* ── App shell ──────────────────────────────────────────────────────────── */
.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr);
  /* Bound the single row to the viewport (min 0 so children can scroll instead
     of growing the row). Without this an auto row expands to the tallest column
     — a long sidebar or conversation — and overflows instead of scrolling. */
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden; /* app shell owns the viewport; regions scroll internally */
}
@supports not (height: 100dvh) { .app { height: 100vh; } }

/* Main column: header / scrollable messages / composer. min-height:0 lets the
   1fr messages row shrink and scroll instead of expanding the whole page.
   The gutter grows on wide screens so content sits in a centered column. */
.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
  --gutter: max(20px, calc((100% - 940px) / 2));
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
  display: flex;
  flex-direction: column;
  height: 100%;          /* pin to the grid row explicitly, not via stretch */
  min-height: 0;
  overflow: hidden;      /* the only scroller is .sessions inside */
  background: var(--bg);
  border-right: 1px solid var(--border);
}
.sidebar__head { flex: 0 0 auto; padding: 12px; border-bottom: 1px solid var(--border); }

.btn--new {
  width: 100%;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: transparent; color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  padding: 10px 12px; min-height: 42px; font-size: 13px; font-weight: 600;
  transition: background var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out);
}
.btn--new .icon { color: var(--accent); }
.btn--new:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }

.sessions {
  flex: 1 1 0;            /* explicit basis 0 so height comes from the flex track */
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px 8px 16px;
  display: flex; flex-direction: column; gap: 3px;
}
.sessions__label {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600;
  color: var(--text-dim); padding: 14px 14px 10px;
}
.sessions__empty { color: var(--text-dim); font-size: 13px; padding: 8px 14px; }
.session {
  position: relative;
  display: flex; flex-direction: column; gap: 2px; width: 100%; text-align: left;
  border: none; background: transparent; color: var(--text-muted);
  font: inherit; font-size: 13.5px; line-height: 1.4; cursor: pointer;
  padding: 9px 12px 9px 26px; border-radius: var(--r-sm);
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.session__title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session__time {
  font-family: var(--font-mono); font-size: 10px; color: var(--text-dim);
  letter-spacing: 0.01em;
}
/* Marker dot: hidden by default (keeps the list calm), shown on hover/active. */
.session::before {
  content: ""; position: absolute; left: 12px; top: 18px; transform: translateY(-50%);
  width: 5px; height: 5px; border-radius: 50%; background: var(--text-dim);
  opacity: 0; transition: opacity var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}
.session:hover { background: var(--surface-2); color: var(--text); }
.session:hover::before { opacity: 1; }
.session--active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.session--active::before { opacity: 1; background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ── Mobile drawer scaffolding (activated in the responsive block) ────────── */
.backdrop { display: none; }
.icon-btn {
  display: none;
  align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: var(--r-sm);
  background: transparent; border: 1px solid var(--border); color: var(--text-muted); cursor: pointer;
  transition: color var(--dur-fast), border-color var(--dur-fast);
}
.icon-btn:hover { color: var(--text); border-color: var(--border-strong); }

/* ── Topbar ─────────────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: var(--z-sticky);
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
  padding: 12px var(--gutter);
  background: color-mix(in oklab, var(--surface) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  transition: box-shadow var(--dur-mid) var(--ease-out);
}
/* Depth cue once the conversation scrolls under the header. */
.main.is-scrolled .topbar { box-shadow: 0 12px 24px -20px oklch(0 0 0 / 0.85); }
.topbar__left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand__dot {
  align-self: center; width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft);
}
.brand__name { font-size: 15px; font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; }

.topbar__actions { display: flex; align-items: center; gap: 12px; }

/* ── Connection status pill ─────────────────────────────────────────────── */
.status {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 4px 10px; border-radius: 999px;
  border: 1px solid var(--border); white-space: nowrap;
}
.status__dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-dim); }
.status--checking { color: var(--text-muted); }
.status--ok    { color: var(--success); border-color: color-mix(in oklab, var(--success) 40%, var(--border)); }
.status--ok    .status__dot { background: var(--success); box-shadow: 0 0 0 3px var(--success-bg); animation: pulse 2.4s var(--ease-out) infinite; }
.status--degraded { color: var(--accent); border-color: color-mix(in oklab, var(--accent) 40%, var(--border)); }
.status--degraded .status__dot { background: var(--accent); }
.status--bad   { color: var(--error-text); border-color: var(--error-border); }
.status--bad   .status__dot { background: var(--error-text); }

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

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn {
  font-family: inherit; font-size: 13px; font-weight: 600;
  border-radius: var(--r-sm); cursor: pointer; border: 1px solid transparent;
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn--ghost {
  background: transparent; color: var(--text-muted);
  border-color: var(--border); padding: 7px 12px; min-height: 34px;
}
.btn--ghost:hover:not(:disabled) { color: var(--text); border-color: var(--border-strong); background: var(--surface-2); }

/* ── Messages ───────────────────────────────────────────────────────────── */
.messages {
  min-height: 0;
  overflow-y: auto; overflow-x: hidden;
  overscroll-behavior: contain; /* don't chain scroll to the rest of the UI */
  padding: 24px var(--gutter) 8px;
  display: flex; flex-direction: column; gap: 20px;
  scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) { .messages { scroll-behavior: auto; } }

/* Restrained themed scrollbars — the OS default is jarring on a dark surface. */
.messages, .sessions {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.messages::-webkit-scrollbar, .sessions::-webkit-scrollbar { width: 11px; }
.messages::-webkit-scrollbar-track, .sessions::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb, .sessions::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
  border: 3px solid transparent;
  background-clip: padding-box;
}
.messages:hover::-webkit-scrollbar-thumb, .sessions:hover::-webkit-scrollbar-thumb {
  background: var(--text-dim); background-clip: padding-box;
}
/* Sidebar list: keep the scrollbar clearly visible so it reads as scrollable. */
.sessions { scrollbar-color: var(--text-dim) transparent; }
.sessions::-webkit-scrollbar-thumb { background: var(--text-dim); }

.msg { display: flex; flex-direction: column; gap: 6px; max-width: 100%; }
.msg__role {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--text-muted); padding-inline: 4px;
}
.msg__role::before {
  content: ""; width: 6px; height: 6px; border-radius: 2px; background: currentColor;
}
.msg--user { align-items: flex-end; }
.msg--user .msg__role { color: var(--accent); }
.msg__meta { display: inline-flex; align-items: center; gap: 8px; }
.msg__time {
  font-family: var(--font-mono); font-size: 10.5px; color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* Centered system note (e.g. resumed session) — not a chat bubble. */
.note { align-self: center; color: var(--text-dim); font-size: 12.5px; padding: 2px 0; text-align: center; }

.bubble {
  max-width: min(88%, 680px);
  padding: 12px 15px; border-radius: var(--r-lg);
  line-height: 1.55; font-size: 15px;
  overflow-wrap: break-word; overflow-x: auto;
}
.msg--user .bubble {
  background: var(--user-tint); color: var(--text);
  border: 1px solid color-mix(in oklab, var(--accent) 22%, var(--border));
  border-bottom-right-radius: 5px;
}
.msg--assistant .bubble {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 3px oklch(0 0 0 / 0.25);
}
.msg--error .bubble {
  background: var(--error-bg); color: var(--error-text);
  border: 1px solid var(--error-border);
}
.msg--error .bubble strong { color: var(--text); }

/* Entrance motion — enhances an already-visible default (no visibility gating) */
.msg { animation: rise var(--dur-mid) var(--ease-out) both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .msg { animation: fade var(--dur-fast) ease both; }
  @keyframes fade { from { opacity: 0; } to { opacity: 1; } }
}
/* History load: render past messages without the entrance animation. */
.msg--instant { animation: none !important; }

.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble strong { font-weight: 650; color: var(--text); }
.bubble em { font-style: italic; color: var(--text-muted); }
.bubble h3, .bubble h4 { margin: 2px 0 8px; line-height: 1.3; color: var(--text); text-wrap: balance; }
.bubble h3 { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; }
.bubble h4 { font-size: 14px; font-weight: 650; }
.bubble h3:first-child, .bubble h4:first-child { margin-top: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble ul:last-child, .bubble ol:last-child { margin-bottom: 0; }
.bubble li { margin: 4px 0; padding-left: 2px; }
.bubble li::marker { color: var(--text-dim); }

/* Inline + block code, data tables — the data-forward core */
.bubble code {
  font-family: var(--font-mono); font-size: 0.88em;
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 1px 5px; border-radius: 5px;
}
.code {
  position: relative; margin: 4px 0 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--r-md);
  overflow: hidden;
}
.code__head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; border-bottom: 1px solid var(--border);
  font-size: 11px; color: var(--text-dim); font-family: var(--font-mono);
}
.code pre { margin: 0; padding: 12px; overflow-x: auto; }
.code code {
  background: none; border: none; padding: 0; font-size: 12.5px;
  line-height: 1.6; color: var(--text); font-variant-ligatures: none;
}
.copy {
  font-family: var(--font-ui); font-size: 11px; font-weight: 600;
  color: var(--text-muted); background: transparent; border: 1px solid var(--border);
  border-radius: 6px; padding: 3px 8px; cursor: pointer; min-height: 24px;
  transition: color var(--dur-fast), border-color var(--dur-fast);
}
.copy:hover { color: var(--text); border-color: var(--border-strong); }
.copy[data-copied="true"] { color: var(--success); border-color: color-mix(in oklab, var(--success) 40%, var(--border)); }

.table-wrap { overflow-x: auto; margin: 4px 0 12px; border: 1px solid var(--border); border-radius: var(--r-md); }
.bubble table {
  border-collapse: collapse; width: 100%; font-size: 13px;
  font-family: var(--font-mono); font-variant-numeric: tabular-nums;
}
.bubble th, .bubble td { padding: 8px 12px; text-align: left; white-space: nowrap; }
.bubble th {
  background: var(--surface-2); color: var(--text-muted);
  font-weight: 650; border-bottom: 1px solid var(--border-strong);
  position: sticky; top: 0;
}
.bubble td { border-top: 1px solid var(--border); }
.bubble td.num, .bubble th.num { text-align: right; }
.bubble tbody tr:hover td { background: color-mix(in oklab, var(--surface) 60%, transparent); }

/* ── Typing indicator ───────────────────────────────────────────────────── */
.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted);
  animation: blink 1.3s var(--ease-out) infinite;
}
.typing span:nth-child(2) { animation-delay: 0.18s; }
.typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes blink { 0%, 75%, 100% { opacity: 0.3; transform: translateY(0); } 38% { opacity: 1; transform: translateY(-2px); } }
@media (prefers-reduced-motion: reduce) {
  .typing span { animation: none; opacity: 0.6; }
}

/* ── Welcome / empty state ──────────────────────────────────────────────── */
.welcome { margin: auto 0; padding: 8px 4px 20px; max-width: 620px; }
.welcome__title { font-size: 34px; font-weight: 700; letter-spacing: -0.025em; margin: 0 0 10px; text-wrap: balance; }
.welcome__sub { color: var(--text-muted); font-size: 15px; margin: 0; max-width: 48ch; text-wrap: pretty; }

/* ── Composer ───────────────────────────────────────────────────────────── */
.composer {
  padding: 12px var(--gutter) 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer__field { display: flex; align-items: flex-end; gap: 10px; }
.composer__input {
  flex: 1; resize: none; max-height: 168px; min-height: 46px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: var(--r-md); padding: 12px 14px;
  font-size: 15px; font-family: inherit; line-height: 1.45;
  transition: border-color var(--dur-fast) var(--ease-out);
}
.composer__input::placeholder { color: var(--text-muted); }
.composer__input:hover { border-color: var(--border-strong); }
.composer__input:focus-visible { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); border-color: transparent; }

.btn--send {
  background: var(--accent); color: var(--accent-ink);
  padding: 0 20px; min-height: 46px; min-width: 82px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn--send:hover:not(:disabled) { background: var(--accent-hi); }
.btn--send[aria-busy="true"] .btn__label { opacity: 0; }
.btn--send[aria-busy="true"] .spinner { display: block; }

.spinner {
  display: none; position: absolute; width: 16px; height: 16px;
  border: 2px solid color-mix(in oklab, var(--accent-ink) 40%, transparent);
  border-top-color: var(--accent-ink); border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.btn--send { position: relative; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 1.4s; } }

.composer__hint { margin: 8px 2px 0; font-size: 11.5px; color: var(--text-dim); }

/* ── Responsive ─────────────────────────────────────────────────────────── */
/* Sidebar collapses into an off-canvas drawer below this width. */
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .icon-btn { display: inline-flex; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: 284px; max-width: 84vw;
    z-index: var(--z-drawer);
    transform: translateX(-100%);
    transition: transform var(--dur-mid) var(--ease-out);
  }
  .app.nav-open .sidebar { transform: none; }
  .backdrop {
    display: block; position: fixed; inset: 0;
    background: oklch(0 0 0 / 0.5); z-index: var(--z-backdrop);
  }
  .backdrop[hidden] { display: none; }
}
@media (max-width: 820px) and (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}

@media (max-width: 600px) {
  .topbar { padding: 10px 14px; gap: 10px; }
  .messages { padding: 18px 14px 6px; }
  .bubble { max-width: 100%; }
  .composer { padding: 10px 14px 14px; }
}
@media (max-width: 380px) {
  .welcome__title { font-size: 22px; }
}
