/* ==========================================================================
   Seefa ERP Assistant - visual system

   Built on Apple's interface principles: translucent material for chrome,
   springs for anything the user can touch, feedback on pointer-down, and
   size-specific typography. Reduced-motion / transparency / contrast are
   first-class, not afterthoughts.
   ========================================================================== */

/* ---------------------------------------------------------------------------
   PALETTE - "Apple Modern"

     #F5F5F7  mist      page background      (light)
     #1D1D1F  graphite  primary text         (light) / page background (dark)
     #AAAAAA  silver    tertiary text, fills
     #007AFF  azure     the single accent

   Four colours only. Everything else is one of these at an opacity, which is
   what keeps it feeling like one system rather than a collection of screens.
   Semantic colours (success/warning/danger) are kept but used sparingly -
   restraint is the point.

   THEME SWITCHING
     :root                        light by default
     @media (prefers-color-scheme: dark) + :root:not([data-theme="light"])
                                  follow the OS unless the user overrode it
     :root[data-theme="dark"]     explicit user choice, wins over the OS
   The guard on the media query is what lets a manual "light" choice survive
   on a machine set to dark.
--------------------------------------------------------------------------- */

:root {
  /* Type scale - tracking is size-specific; one letter-spacing is wrong somewhere */
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI Variable",
          "Segoe UI", system-ui, sans-serif;
  --font-mono: "SF Mono", ui-monospace, "Cascadia Code", Consolas, monospace;

  /* --- palette primitives --- */
  --mist:     #F5F5F7;
  --graphite: #1D1D1F;
  --silver:   #AAAAAA;
  --azure:    #007AFF;

  /* Surfaces */
  --bg:            var(--mist);
  --bg-elevated:   #FFFFFF;
  --material:      rgba(255, 255, 255, 0.72);
  --material-edge: rgba(255, 255, 255, 0.55);
  --scrim:         rgba(29, 29, 31, 0.28);

  /* Content - vibrancy: never flat grey over translucent surfaces */
  --text:           var(--graphite);
  --text-secondary: #55555A;
  --text-tertiary:  #86868B;

  --separator:   rgba(29, 29, 31, 0.09);
  --fill:        rgba(170, 170, 170, 0.18);
  --fill-strong: rgba(170, 170, 170, 0.32);

  --accent:       var(--azure);
  --accent-hover: #0A84FF;
  --on-accent:    #FFFFFF;

  --success: #1D8A4E;
  --warning: #B25000;
  --danger:  #D1293D;

  --radius-sm: 10px;
  --radius:    16px;
  --radius-lg: 22px;
  --radius-xl: 30px;

  /* Bigger surfaces read as thicker: stronger blur, deeper shadow */
  --shadow-sm: 0 1px 2px rgba(29,29,31,.05), 0 1px 1px rgba(29,29,31,.03);
  --shadow:    0 4px 16px rgba(29,29,31,.07), 0 1px 3px rgba(29,29,31,.04);
  --shadow-lg: 0 14px 44px rgba(29,29,31,.11), 0 2px 8px rgba(29,29,31,.05);

  /* Springs. Critically damped by default; bounce only after momentum. */
  --spring:        cubic-bezier(0.32, 0.72, 0, 1);
  --spring-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
  --dur-fast: 140ms;
  --dur:      280ms;
  --dur-slow: 420ms;

  /* Follows the OS while no explicit choice has been made. */
  color-scheme: light dark;
}

/* The in-app toggle has to move color-scheme too, not just the tokens.
   This property is what tells the browser how to paint the parts we do NOT
   style: the <option> list a <select> pops open, scrollbars, autofill, the
   date picker, the caret.

   Left on `light dark` alone it follows the OS and ignores the toggle, and
   the mismatch is unreadable rather than merely inconsistent: with the OS
   in light mode and the app switched to dark, our CSS gives the closed
   <select> light text on a dark field, then the browser paints the open
   dropdown white and inherits that same light text into it. White on
   white. Reported from the admin console, where every filter is a
   <select>. */
:root[data-theme="dark"]  { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }

/* Belt and braces for the dropdown list itself. color-scheme above is what
   actually fixes this on macOS and iOS, where the list is drawn by the OS
   and ignores author colours; on Windows and Android the list IS styleable
   and inherits from the <select>, so naming the pair here removes any
   dependence on inheritance. */
option {
  background-color: var(--bg-elevated);
  color: var(--text);
}

/* Dark tokens, shared by the OS preference and the explicit override. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:            #000000;
    --bg-elevated:   var(--graphite);
    --material:      rgba(29, 29, 31, 0.72);
    --material-edge: rgba(245, 245, 247, 0.10);
    --scrim:         rgba(0, 0, 0, 0.55);

    --text:           var(--mist);
    --text-secondary: #C6C6CB;
    --text-tertiary:  var(--silver);

    --separator:   rgba(245, 245, 247, 0.12);
    --fill:        rgba(170, 170, 170, 0.16);
    --fill-strong: rgba(170, 170, 170, 0.28);

    --accent:       #0A84FF;
    --accent-hover: #3D9BFF;

    --success: #30D158;
    --warning: #FF9F0A;
    --danger:  #FF453A;

    --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
    --shadow:    0 4px 16px rgba(0,0,0,.55);
    --shadow-lg: 0 14px 44px rgba(0,0,0,.65);
  }
}

:root[data-theme="dark"] {
  --bg:            #000000;
  --bg-elevated:   var(--graphite);
  --material:      rgba(29, 29, 31, 0.72);
  --material-edge: rgba(245, 245, 247, 0.10);
  --scrim:         rgba(0, 0, 0, 0.55);

  --text:           var(--mist);
  --text-secondary: #C6C6CB;
  --text-tertiary:  var(--silver);

  --separator:   rgba(245, 245, 247, 0.12);
  --fill:        rgba(170, 170, 170, 0.16);
  --fill-strong: rgba(170, 170, 170, 0.28);

  --accent:       #0A84FF;
  --accent-hover: #3D9BFF;

  --success: #30D158;
  --warning: #FF9F0A;
  --danger:  #FF453A;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
  --shadow:    0 4px 16px rgba(0,0,0,.55);
  --shadow-lg: 0 14px 44px rgba(0,0,0,.65);
}

/* ------------------------------ theme toggle ------------------------------
   Sun and moon cross-fade and rotate through each other rather than swapping,
   so the control reads as one object changing state, not two icons.
--------------------------------------------------------------------------- */
.theme-toggle { position: relative; overflow: hidden; }
.theme-toggle svg {
  position: absolute;
  transition: opacity var(--dur) var(--spring),
              transform var(--dur) var(--spring);
}
.theme-toggle .icon-sun  { opacity: 0; transform: rotate(-90deg) scale(.5); }
.theme-toggle .icon-moon { opacity: 1; transform: rotate(0deg) scale(1); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun  { opacity: 1; transform: rotate(0deg) scale(1); }
  :root:not([data-theme="light"]) .theme-toggle .icon-moon { opacity: 0; transform: rotate(90deg) scale(.5); }
}
:root[data-theme="dark"] .theme-toggle .icon-sun  { opacity: 1; transform: rotate(0deg) scale(1); }
:root[data-theme="dark"] .theme-toggle .icon-moon { opacity: 0; transform: rotate(90deg) scale(.5); }

* { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: var(--font);
  /* Body: comfortable leading, tracking near zero */
  font-size: 15px;
  line-height: 1.5;
  letter-spacing: -0.01em;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* ============================ Chrome: translucent material ================ */
.app-bar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--material);
  backdrop-filter: saturate(180%) blur(24px);
  -webkit-backdrop-filter: saturate(180%) blur(24px);
  /* A bright top edge reads as light catching a real material */
  border-bottom: 1px solid var(--separator);
}

.app-bar h1 {
  /* Large text wants negative tracking and tight leading */
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.022em;
  line-height: 1.2;
}

.app-bar .spacer { flex: 1; }

.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--success) 18%, transparent);
  flex: none;
}
.brand-dot[data-state="degraded"] { background: var(--warning);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--warning) 18%, transparent); }
.brand-dot[data-state="down"] { background: var(--danger);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 18%, transparent); }

.provider-chip {
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0;           /* small text: slightly positive, never negative */
  color: var(--text-secondary);
  background: var(--fill);
  padding: 5px 10px;
  border-radius: 100px;
  font-variant-numeric: tabular-nums;
}

/* ================================ Conversation ============================ */
.thread {
  flex: 1;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 24px 20px 200px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.msg { display: flex; flex-direction: column; gap: 6px; }
.msg[data-role="user"] { align-items: flex-end; }

/* Who is speaking, said once above the bubble. The colour and the side
   already carry it; this is what makes it unambiguous when an answer is
   long enough that the question has scrolled out of sight. */
.msg-role {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 12px; font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--text-tertiary);
  padding: 0 4px;
}
.msg[data-role="user"] .msg-who { color: var(--accent); }
.msg-time { font-weight: 500; font-variant-numeric: tabular-nums; opacity: 0.75; }

/* A question is a bubble; an answer is a full-width card. Sizing them alike
   was most of why the two were hard to tell apart. */
.msg[data-role="user"] .bubble { max-width: min(78%, 560px); }
.msg[data-role="assistant"] .bubble { width: 100%; }

/* Enter along the path it will leave by - spatial consistency */
.msg { animation: msg-in var(--dur) var(--spring) both; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(12px) scale(0.99); }
  to   { opacity: 1; transform: none; }
}

.bubble {
  max-width: 100%;
  padding: 13px 17px;
  border-radius: var(--radius-lg);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--separator);
  overflow-wrap: anywhere;
}

.msg[data-role="user"] .bubble {
  background: var(--accent);
  color: var(--on-accent);
  border-color: transparent;
  border-bottom-right-radius: var(--radius-sm);
}

.msg[data-role="assistant"] .bubble { border-bottom-left-radius: var(--radius-sm); }

.msg-meta {
  font-size: 12px;
  color: var(--text-tertiary);
  display: flex; align-items: center; gap: 8px;
  font-variant-numeric: tabular-nums;
}

/* ---- copy / edit ---- */
.msg-actions {
  display: flex; gap: 4px;
  padding: 0 2px;
  /* Present but quiet until the message is hovered or a button is focused,
     so a long thread is not a wall of buttons. Always visible on touch,
     where there is no hover to discover them with. */
  opacity: 0;
  transition: opacity 140ms ease;
}
.msg:hover .msg-actions,
.msg:focus-within .msg-actions { opacity: 1; }
@media (hover: none) { .msg-actions { opacity: 1; } }

.msg-action {
  font: inherit;
  font-size: 12px;
  font-weight: 550;
  line-height: 1;
  padding: 6px 10px;
  border-radius: 100px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, border-color 140ms ease;
}
.msg-action:hover { background: var(--bg-elevated); color: var(--text-primary); border-color: var(--separator); }
.msg-action:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.msg-action.is-done { color: var(--accent); }

/* ---- editing a sent question ---- */
.msg.is-editing .bubble { width: min(100%, 560px); }
.edit-box {
  width: 100%;
  font: inherit;
  color: inherit;
  background: transparent;
  border: 0;
  outline: none;
  resize: vertical;
  padding: 0;
  overflow-wrap: anywhere;
}
.edit-row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.edit-send, .edit-cancel {
  font: inherit; font-size: 13px; font-weight: 600;
  padding: 7px 14px;
  border-radius: 100px;
  cursor: pointer;
  border: 1px solid transparent;
}
/* Inside the accent-coloured user bubble, so these invert rather than
   borrow the page's button colours. */
.edit-send { background: var(--on-accent); color: var(--accent); }
.edit-cancel { background: transparent; color: inherit; border-color: currentColor; opacity: 0.8; }
.edit-send:focus-visible, .edit-cancel:focus-visible { outline: 2px solid var(--on-accent); outline-offset: 2px; }

/* ---- answer internals ---- */
.answer-value {
  font-size: 30px;
  font-weight: 650;
  letter-spacing: -0.03em;   /* large text: tighten */
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  margin-bottom: 4px;
}

.answer-label { font-size: 13px; color: var(--text-secondary); }

table.result {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin-top: 10px;
  font-variant-numeric: tabular-nums;
}
table.result th {
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 7px 10px;
  border-bottom: 1px solid var(--separator);
  position: sticky; top: 0;
  background: var(--bg-elevated);
}
table.result td { padding: 7px 10px; border-bottom: 1px solid var(--separator); }
table.result tr:last-child td { border-bottom: none; }
table.result td.num { text-align: right; }
.table-scroll { max-height: 340px; overflow: auto; border-radius: var(--radius-sm); }

/* ---- SQL provenance: every answer can be audited ---- */
details.sql-disclosure { margin-top: 12px; }
details.sql-disclosure summary {
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 6px 11px;
  background: var(--fill);
  border-radius: 100px;
  display: inline-flex; align-items: center; gap: 6px;
  list-style: none;
  user-select: none;
  transition: background var(--dur-fast) ease-out, transform var(--dur-fast) ease-out;
}
details.sql-disclosure summary::-webkit-details-marker { display: none; }
details.sql-disclosure summary:hover { background: var(--fill-strong); }
details.sql-disclosure summary:active { transform: scale(0.97); }
details.sql-disclosure pre {
  margin-top: 10px;
  padding: 13px 15px;
  background: var(--fill);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.55;
  overflow-x: auto;
  white-space: pre;
}

.caveat {
  margin-top: 10px; padding: 9px 13px;
  background: color-mix(in srgb, var(--warning) 10%, transparent);
  border-left: 3px solid var(--warning);
  border-radius: var(--radius-sm);
  font-size: 13px; color: var(--text-secondary);
}

.chart-holder { margin-top: 14px; height: 260px; position: relative; }

/* ================================ Composer ================================ */
.composer-dock {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  padding: 14px 20px calc(14px + env(safe-area-inset-bottom));
  background: var(--material);
  backdrop-filter: saturate(180%) blur(24px);
  -webkit-backdrop-filter: saturate(180%) blur(24px);
  border-top: 1px solid var(--separator);
}

/* Scroll edge effect: fade where content meets floating chrome,
   rather than a hard 1px divider */
.composer-dock::before {
  content: "";
  position: absolute; left: 0; right: 0; top: -28px; height: 28px;
  background: linear-gradient(to top, var(--bg), transparent);
  pointer-events: none;
}

.composer {
  max-width: 820px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--separator);
  border-radius: var(--radius-xl);
  padding: 7px 7px 7px 18px;
  box-shadow: var(--shadow);
  transition: border-color var(--dur) var(--spring),
              box-shadow var(--dur) var(--spring);
}
.composer:focus-within {
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  box-shadow: var(--shadow-lg),
              0 0 0 4px color-mix(in srgb, var(--accent) 12%, transparent);
}

.composer textarea {
  flex: 1;
  border: none; outline: none; resize: none; background: none;
  font-family: inherit; font-size: 15px; line-height: 1.45;
  color: var(--text);
  max-height: 160px; padding: 9px 0;
}
.composer textarea::placeholder { color: var(--text-tertiary); }

/* ---- buttons: feedback on pointer-DOWN, never on release ---- */
.btn-icon {
  flex: none;
  width: 38px; height: 38px;
  border: none; border-radius: 50%;
  background: var(--fill);
  color: var(--text-secondary);
  display: grid; place-items: center;
  cursor: pointer;
  transition: background var(--dur-fast) ease-out,
              transform var(--dur-fast) ease-out,
              color var(--dur-fast) ease-out;
}
.btn-icon:hover { background: var(--fill-strong); }
.btn-icon:active { transform: scale(0.92); }   /* instant, on press */
.btn-icon:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.btn-icon svg { width: 19px; height: 19px; }

.btn-send { background: var(--accent); color: var(--on-accent); }
.btn-send:hover { background: var(--accent-hover); }

.btn-mic[data-recording="true"] {
  background: var(--danger); color: var(--on-accent);
  animation: mic-pulse 1.6s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--danger) 45%, transparent); }
  50%      { box-shadow: 0 0 0 9px color-mix(in srgb, var(--danger) 0%, transparent); }
}

/* ======================= Voice: live transcript review ==================== */
/* Speech is a draft, never an action. It must be reviewable before it commits. */
.voice-panel {
  max-width: 820px; margin: 0 auto 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--separator);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  /* Materialize: blur + scale together, so it reads as a real surface arriving */
  animation: voice-in var(--dur) var(--spring) both;
}
@keyframes voice-in {
  from { opacity: 0; transform: translateY(10px) scale(0.985); filter: blur(6px); }
  to   { opacity: 1; transform: none; filter: blur(0); }
}
.voice-panel[hidden] { display: none; }

.voice-head {
  display: flex; align-items: center; gap: 9px;
  padding: 10px 15px;
  border-bottom: 1px solid var(--separator);
  font-size: 12px; font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: 0.01em; text-transform: uppercase;
}

.voice-body { padding: 13px 15px; font-size: 15px; line-height: 1.45; min-height: 46px; }
.voice-body .interim { color: var(--text-tertiary); }  /* not yet final */

.voice-actions {
  display: flex; gap: 8px; padding: 0 15px 13px; flex-wrap: wrap;
}
.btn-text {
  border: 1px solid var(--separator);
  background: var(--bg-elevated);
  color: var(--text);
  font-family: inherit; font-size: 13px; font-weight: 500;
  padding: 8px 15px; border-radius: 100px; cursor: pointer;
  transition: background var(--dur-fast) ease-out, transform var(--dur-fast) ease-out;
}
.btn-text:hover { background: var(--fill); }
.btn-text:active { transform: scale(0.96); }
.btn-text.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn-text.primary:hover { background: var(--accent-hover); }
.btn-text.quiet { color: var(--text-secondary); }

/* Level meter - continuous feedback during the interaction, not just at the end */
.level-meter { display: flex; align-items: center; gap: 3px; height: 15px; }
.level-meter span {
  width: 3px; border-radius: 2px; background: var(--danger);
  height: 20%;
  transition: height 90ms ease-out;   /* tracks the mic 1:1 */
}

/* ================================ Misc ==================================== */
.thinking { display: inline-flex; gap: 4px; align-items: center; padding: 3px 0; }
.thinking i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-tertiary);
  animation: blink 1.3s ease-in-out infinite;
}
.thinking i:nth-child(2) { animation-delay: 0.16s; }
.thinking i:nth-child(3) { animation-delay: 0.32s; }
@keyframes blink { 0%,80%,100% { opacity:.25; transform:scale(.8);} 40% {opacity:1; transform:scale(1);} }

.suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.suggestion {
  border: 1px solid var(--separator);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font: inherit; font-size: 13px;
  padding: 9px 15px; border-radius: 100px; cursor: pointer; text-align: left;
  transition: background var(--dur-fast) ease-out,
              transform var(--dur-fast) ease-out,
              color var(--dur-fast) ease-out;
}
.suggestion:hover { background: var(--fill); color: var(--text); }
.suggestion:active { transform: scale(0.97); }

.empty-state { text-align: center; padding: 56px 20px 24px; }
.empty-state h2 {
  font-size: 27px; font-weight: 650; letter-spacing: -0.028em;
  line-height: 1.15; margin-bottom: 9px;
}
.empty-state p { color: var(--text-secondary); max-width: 460px; margin: 0 auto; }

.toast {
  position: fixed; left: 50%; bottom: 108px; transform: translateX(-50%);
  z-index: 80; max-width: 460px;
  background: var(--bg-elevated);
  border: 1px solid var(--separator);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 12px 17px; font-size: 14px;
  animation: toast-in var(--dur) var(--spring-bounce) both;  /* momentum: slight bounce */
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(16px) scale(0.96); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}
.toast[data-kind="error"] { border-left: 3px solid var(--danger); }

/* ====================== Accessibility: three signals ====================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  /* Reduced motion means a gentler equivalent, not no feedback */
  .msg, .voice-panel, .toast { animation: fade-only 180ms ease both; }
  @keyframes fade-only { from { opacity: 0; } to { opacity: 1; } }
  .btn-icon:active, .btn-text:active, .suggestion:active { transform: none; }
}

@media (prefers-reduced-transparency: reduce) {
  .app-bar, .composer-dock {
    background: var(--bg-elevated);
    backdrop-filter: none; -webkit-backdrop-filter: none;
  }
}

@media (prefers-contrast: more) {
  :root { --separator: rgba(0,0,0,0.45); --text-secondary: var(--text); }
  .bubble, .composer, .voice-panel { border-width: 2px; }
}

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

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* Layout scales with the user's text size: spacing in rem/em, not fixed px */
@media (max-width: 640px) {
  .thread { padding: 16px 14px 190px; }
  .composer-dock { padding: 11px 14px calc(11px + env(safe-area-inset-bottom)); }
  .answer-value { font-size: 25px; }
  .empty-state h2 { font-size: 23px; }
}

/* ==========================================================================
   SIGN IN

   A single centred card on a quiet field. The restraint is deliberate: this
   screen has exactly one job, and anything that is not the form competes with
   it. Depth comes from one translucent card and one soft ambient glow, not
   from decoration.
   ========================================================================== */

.auth-page {
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}

/* One diffuse accent bloom behind the card. Large, low-opacity and blurred so
   it reads as light in the room rather than a graphic element. */
.auth-page::before {
  content: "";
  position: fixed;
  inset: -20% -20% auto -20%;
  height: 65vh;
  background:
    radial-gradient(60% 55% at 30% 20%,
      color-mix(in srgb, var(--accent) 22%, transparent), transparent 70%),
    radial-gradient(45% 45% at 78% 8%,
      color-mix(in srgb, var(--accent) 12%, transparent), transparent 70%);
  filter: blur(70px);
  pointer-events: none;
  z-index: 0;
}

.auth-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  padding: 40px 36px 30px;
  border-radius: var(--radius-xl);
  background: var(--material);
  backdrop-filter: saturate(180%) blur(30px);
  -webkit-backdrop-filter: saturate(180%) blur(30px);
  border: 1px solid var(--material-edge);
  box-shadow: var(--shadow-lg);
  /* Materialize: blur + scale together, so the card reads as a real surface
     arriving rather than a rectangle fading in. */
  animation: auth-in var(--dur-slow) var(--spring) both;
}

@keyframes auth-in {
  from { opacity: 0; transform: translateY(16px) scale(.975); filter: blur(8px); }
  to   { opacity: 1; transform: none; filter: blur(0); }
}

/* --- brand mark --- */
.auth-mark {
  width: 52px; height: 52px;
  border-radius: 15px;
  display: grid; place-items: center;
  margin-bottom: 22px;
  background: linear-gradient(150deg,
              color-mix(in srgb, var(--accent) 100%, white 6%),
              color-mix(in srgb, var(--accent) 82%, black 14%));
  box-shadow:
    0 6px 20px color-mix(in srgb, var(--accent) 34%, transparent),
    inset 0 1px 0 rgba(255,255,255,.28);
}
.auth-mark svg { width: 26px; height: 26px; color: #fff; }

/* --- type: large text tightens, small text does not --- */
.auth-card h1 {
  font-size: 26px;
  font-weight: 650;
  letter-spacing: -0.028em;
  line-height: 1.12;
}
.auth-sub {
  color: var(--text-secondary);
  font-size: 14.5px;
  line-height: 1.45;
  margin: 8px 0 28px;
}

/* --- fields --- */
.auth-field { margin-bottom: 16px; }
.auth-field label {
  display: block;
  font-size: 12.5px;
  font-weight: 560;
  letter-spacing: 0.005em;      /* small text: slightly positive */
  color: var(--text-secondary);
  margin-bottom: 7px;
}

.auth-input-wrap { position: relative; display: flex; align-items: center; }
.auth-input-wrap svg {
  position: absolute; left: 14px;
  width: 17px; height: 17px;
  color: var(--text-tertiary);
  pointer-events: none;
  transition: color var(--dur) var(--spring);
}
.auth-input-wrap:focus-within svg { color: var(--accent); }

.auth-card input[type="text"],
.auth-card input[type="password"] {
  width: 100%;
  font: inherit;
  font-size: 15px;
  padding: 12px 14px 12px 42px;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--separator);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color var(--dur) var(--spring),
              box-shadow var(--dur) var(--spring),
              background var(--dur) var(--spring);
}
.auth-card input::placeholder { color: var(--text-tertiary); }
.auth-card input:focus {
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 13%, transparent);
}

/* Reveal control. Feedback lands on pointer-down, as everywhere else. */
.auth-reveal {
  position: absolute; right: 6px;
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border: none; background: none; cursor: pointer;
  color: var(--text-tertiary);
  border-radius: 50%;
  transition: color var(--dur-fast) ease-out,
              background var(--dur-fast) ease-out,
              transform var(--dur-fast) ease-out;
}
.auth-reveal:hover { color: var(--text); background: var(--fill); }
.auth-reveal:active { transform: scale(.9); }
.auth-reveal svg { width: 17px; height: 17px; position: static; color: inherit; }

/* --- submit --- */
.auth-submit {
  width: 100%;
  font: inherit; font-size: 15px; font-weight: 560;
  padding: 13px;
  margin-top: 10px;
  color: var(--on-accent);
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 9px;
  box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 30%, transparent);
  transition: background var(--dur-fast) ease-out,
              transform var(--dur-fast) ease-out,
              box-shadow var(--dur-fast) ease-out;
}
.auth-submit:hover {
  background: var(--accent-hover);
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 38%, transparent);
}
.auth-submit:active { transform: scale(.985); }
.auth-submit[data-busy="true"] { pointer-events: none; opacity: .72; }

.auth-spinner {
  width: 15px; height: 15px;
  border: 2px solid rgba(255,255,255,.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  display: none;
}
.auth-submit[data-busy="true"] .auth-spinner { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- error --- */
.auth-error {
  display: flex; align-items: flex-start; gap: 10px;
  background: color-mix(in srgb, var(--danger) 11%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 26%, transparent);
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  line-height: 1.4;
  margin-bottom: 20px;
  animation: auth-shake 380ms var(--spring) both;
}
.auth-error svg { width: 17px; height: 17px; color: var(--danger); flex: none; margin-top: 1px; }

/* A short, damped shake - enough to register, not enough to be theatrical. */
@keyframes auth-shake {
  0%   { transform: translateX(0); }
  22%  { transform: translateX(-5px); }
  44%  { transform: translateX(4px); }
  66%  { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}

/* --- footer --- */
.auth-foot {
  margin-top: 26px; padding-top: 18px;
  border-top: 1px solid var(--separator);
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  font-size: 12px;
  color: var(--text-tertiary);
}
.auth-foot code {
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: var(--fill);
  padding: 3px 7px;
  border-radius: 6px;
  color: var(--text-secondary);
}
.auth-secure { display: inline-flex; align-items: center; gap: 5px; }
.auth-secure svg { width: 13px; height: 13px; }

/* Theme toggle sits in the corner, out of the form's way. */
.auth-theme {
  position: fixed;
  top: 18px; right: 18px;
  z-index: 2;
}

@media (max-width: 480px) {
  .auth-card { padding: 32px 24px 24px; border-radius: var(--radius-lg); }
  .auth-card h1 { font-size: 23px; }
}

@media (prefers-reduced-motion: reduce) {
  .auth-card, .auth-error { animation: fade-only 180ms ease both; }
  .auth-spinner { animation-duration: 1.6s; }
}

@media (prefers-reduced-transparency: reduce) {
  .auth-card { background: var(--bg-elevated); backdrop-filter: none;
               -webkit-backdrop-filter: none; }
  .auth-page::before { display: none; }
}

/* ==========================================================================
   INSIGHTS & FORECAST
   ========================================================================== */

.insight {
  border: 1px solid var(--separator);
  border-left: 3px solid var(--text-tertiary);
  border-radius: var(--radius-sm);
  padding: 13px 15px;
  margin-top: 10px;
  background: var(--bg-elevated);
}
.insight[data-severity="critical"] { border-left-color: var(--danger); }
.insight[data-severity="warning"]  { border-left-color: var(--warning); }
.insight[data-severity="info"]     { border-left-color: var(--accent); }

.insight-head {
  display: flex; align-items: center; gap: 9px;
  margin-bottom: 7px;
}
.insight-title { font-size: 14.5px; font-weight: 600; letter-spacing: -0.012em; }
.insight-badge {
  font-size: 10.5px; font-weight: 650;
  letter-spacing: 0.04em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 100px;
  background: var(--fill); color: var(--text-secondary);
}
.insight[data-severity="critical"] .insight-badge {
  background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.insight[data-severity="warning"] .insight-badge {
  background: color-mix(in srgb, var(--warning) 15%, transparent); color: var(--warning); }
.insight[data-severity="info"] .insight-badge {
  background: color-mix(in srgb, var(--accent) 13%, transparent); color: var(--accent); }

.insight-finding { font-size: 14px; line-height: 1.5; }
.insight-action {
  font-size: 13.5px; line-height: 1.5;
  color: var(--text-secondary);
  margin-top: 7px; padding-left: 13px;
  border-left: 2px solid var(--separator);
}
.insight-basis {
  font-size: 11.5px; color: var(--text-tertiary);
  margin-top: 9px; font-style: italic;
}

/* Forecast block - the range is the answer, so it is given equal weight to
   the midpoint rather than shown as a footnote. */
.forecast-box {
  margin-top: 12px; padding: 13px 15px;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--accent) 7%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 20%, transparent);
}
.forecast-head {
  font-size: 11.5px; font-weight: 640;
  letter-spacing: 0.045em; text-transform: uppercase;
  color: var(--accent); margin-bottom: 9px;
  display: flex; align-items: center; gap: 6px;
}
.forecast-head svg { width: 13px; height: 13px; }

.forecast-row {
  display: flex; align-items: baseline; gap: 10px;
  padding: 5px 0;
  font-variant-numeric: tabular-nums;
  border-bottom: 1px solid var(--separator);
}
.forecast-row:last-of-type { border-bottom: none; }
.forecast-period { font-size: 12.5px; color: var(--text-secondary); min-width: 68px; }
.forecast-value  { font-size: 15px; font-weight: 600; }
.forecast-range  { font-size: 12px; color: var(--text-tertiary); margin-left: auto; }

.forecast-method {
  font-size: 11.5px; color: var(--text-tertiary);
  margin-top: 9px; padding-top: 8px;
  border-top: 1px solid var(--separator);
  line-height: 1.45;
}

.trend-pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 550;
  padding: 4px 10px; border-radius: 100px;
  background: var(--fill); color: var(--text-secondary);
  margin-top: 9px;
}
.trend-pill svg { width: 12px; height: 12px; }
.trend-pill[data-dir="rising"]  { color: var(--success);
  background: color-mix(in srgb, var(--success) 12%, transparent); }
.trend-pill[data-dir="falling"] { color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* ==========================================================================
   PROGRESS, SKELETON, MODEL PANEL

   Apple: feedback must be continuous DURING the interaction, not only at the
   end. A spinner says "wait"; a stage says "here is what is happening, and it
   is going fine". The difference matters most exactly when things are slow.
   ========================================================================== */

.progress {
  display: flex; align-items: center; gap: 11px;
  min-height: 24px;
}
.progress-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); flex: none;
  animation: pulse-dot 1.4s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: .35; transform: scale(.8); }
  50%      { opacity: 1;   transform: scale(1.15); }
}

/* The stage label cross-fades and slides a little, so successive stages read
   as one thing progressing rather than text being replaced. */
.progress-text {
  font-size: 14px; color: var(--text-secondary);
  animation: stage-in 260ms var(--spring) both;
}
@keyframes stage-in {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: none; }
}

.progress-elapsed {
  margin-left: auto; font-size: 11.5px; color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
}

/* Trail of completed stages - reassurance that work is accumulating. */
.progress-trail {
  display: flex; flex-wrap: wrap; gap: 5px; margin-top: 9px;
}
.progress-trail span {
  font-size: 11px; color: var(--text-tertiary);
  background: var(--fill); padding: 2px 8px; border-radius: 100px;
  animation: stage-in 220ms var(--spring) both;
}

/* ---- skeleton: only appears once the query is actually running ---- */
.skeleton { margin-top: 12px; }
.skeleton-row {
  height: 13px; border-radius: 5px; margin-bottom: 9px;
  background: linear-gradient(90deg,
              var(--fill) 25%, var(--fill-strong) 37%, var(--fill) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
}
.skeleton-row:nth-child(1) { width: 92%; }
.skeleton-row:nth-child(2) { width: 78%; animation-delay: .1s; }
.skeleton-row:nth-child(3) { width: 85%; animation-delay: .2s; }
.skeleton-row:nth-child(4) { width: 64%; animation-delay: .3s; }
@keyframes shimmer {
  0%   { background-position: 100% 0; }
  100% { background-position: 0 0; }
}

/* ---- word-by-word reveal ---- */
.reveal-word {
  opacity: 0;
  animation: word-in 300ms var(--spring) forwards;
}
@keyframes word-in {
  from { opacity: 0; filter: blur(3px); }
  to   { opacity: 1; filter: blur(0); }
}

/* ============================== model panel ============================== */
/* An invisible scrim: it exists only to catch the click that dismisses the
   popover. Dimming would be wrong here - this is a parallel, non-blocking
   panel, not a modal task, so the flow behind it should stay legible. */
.sheet-scrim {
  position: fixed; inset: 0; z-index: 90;
  background: transparent;
}

/* Anchored popover, NOT a centred modal.
   It originates from the button that opened it, so the spatial relationship
   between control and content is obvious: it scales out of the trigger and
   collapses back into it. Position is set in JS from the button's rect. */
.sheet {
  position: fixed; z-index: 91;
  width: min(430px, calc(100vw - 28px));
  max-height: min(72vh, 620px);
  overflow-y: auto;
  background: var(--material);
  backdrop-filter: saturate(180%) blur(28px);
  -webkit-backdrop-filter: saturate(180%) blur(28px);
  border: 1px solid var(--material-edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 18px;
  /* Origin is overridden in JS to point at the trigger. */
  transform-origin: top right;
  animation: popover-in 240ms var(--spring) both;
}

@keyframes popover-in {
  from { opacity: 0; transform: scale(.94) translateY(-6px); filter: blur(5px); }
  to   { opacity: 1; transform: scale(1) translateY(0); filter: blur(0); }
}

/* A small notch tying the panel to its button. */
.sheet::before {
  content: "";
  position: absolute;
  left: var(--notch-x, 24px);
  width: 12px; height: 12px;
  background: var(--material);
  border-left: 1px solid var(--material-edge);
  border-top: 1px solid var(--material-edge);
  transform: rotate(45deg);
  border-radius: 2px 0 0 0;
}
.sheet[data-above="false"]::before { top: -7px; }
/* When the panel flips above the trigger the notch must flip too, or it
   points away from the button it belongs to. */
.sheet[data-above="true"]::before {
  top: auto; bottom: -7px;
  transform: rotate(225deg);
}

/* On phones an anchored popover has nowhere to go - become a bottom sheet,
   which is the right form factor there. */
@media (max-width: 560px) {
  .sheet {
    left: 10px !important; right: 10px !important;
    top: auto !important; bottom: 10px !important;
    width: auto; max-height: 78vh;
    transform-origin: bottom center !important;
    border-radius: var(--radius-lg);
  }
  .sheet::before { display: none; }
  @keyframes popover-in {
    from { opacity: 0; transform: translateY(18px); filter: blur(5px); }
    to   { opacity: 1; transform: translateY(0); filter: blur(0); }
  }
}

.sheet h2 {
  font-size: 16px; font-weight: 640; letter-spacing: -.02em;
  margin-bottom: 3px;
}
.sheet .sheet-sub {
  font-size: 12.5px; color: var(--text-secondary); margin-bottom: 14px;
  line-height: 1.45;
}

.model-card {
  border: 1.5px solid var(--separator);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  padding: 11px 13px; margin-bottom: 8px;
  cursor: pointer;
  transition: border-color var(--dur-fast) ease-out,
              background var(--dur-fast) ease-out,
              transform var(--dur-fast) ease-out;
}
.model-card:hover { background: var(--fill); }
.model-card:active { transform: scale(.99); }
.model-card[data-selected="true"] {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 6%, transparent);
}
.model-card[data-available="false"] { opacity: .55; }

.model-head { display: flex; align-items: center; gap: 9px; margin-bottom: 3px; }
.model-name { font-size: 14.5px; font-weight: 600; }
.model-tag {
  font-size: 10.5px; font-weight: 650; letter-spacing: .04em;
  text-transform: uppercase; padding: 2px 7px; border-radius: 100px;
  background: var(--fill); color: var(--text-secondary);
}
.model-tag[data-state="ok"]   { background: color-mix(in srgb, var(--success) 15%, transparent); color: var(--success); }
.model-tag[data-state="low"]  { background: color-mix(in srgb, var(--warning) 15%, transparent); color: var(--warning); }
.model-tag[data-state="out"]  { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.model-id { font-size: 11.5px; color: var(--text-tertiary); font-family: var(--font-mono); }

.quota-bar {
  height: 5px; border-radius: 3px; background: var(--fill);
  margin: 9px 0 7px; overflow: hidden;
}
.quota-fill {
  height: 100%; border-radius: 3px; background: var(--success);
  transition: width var(--dur-slow) var(--spring);
}
.quota-fill[data-state="low"] { background: var(--warning); }
.quota-fill[data-state="out"] { background: var(--danger); }

.quota-lines { display: grid; gap: 3px; }
.quota-line {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 11.5px; color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
}
.quota-line b { color: var(--text-secondary); font-weight: 600; }
.quota-line .reset { margin-left: auto; }

.sheet-actions {
  display: flex; gap: 8px; margin-top: 14px; padding-top: 12px;
  border-top: 1px solid var(--separator);
}
.sheet-actions .btn-text { flex: 1; }
.sheet-note {
  font-size: 11.5px; color: var(--text-tertiary); line-height: 1.5;
  margin-top: 12px; padding: 9px 11px;
  background: var(--fill); border-radius: var(--radius-sm);
}

@media (prefers-reduced-motion: reduce) {
  .progress-text, .progress-trail span, .reveal-word, .sheet {
    animation: fade-only 160ms ease both;
  }
  .skeleton-row, .progress-dot { animation: none; }
  .reveal-word { opacity: 1; }
}

/* ---- OpenRouter free models -------------------------------------------- */
.free-models {
  margin-top: 14px; padding-top: 14px;
  border-top: 1px solid var(--separator);
}
.free-models-head {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; font-weight: 620; margin-bottom: 4px;
}
.free-models-sub {
  font-size: 11.5px; color: var(--text-tertiary);
  line-height: 1.45; margin-bottom: 10px;
}

/* A scrollable column rather than a long list: the menu should not grow
   without bound just because a provider offers forty models. */
.free-model-list {
  display: grid; gap: 5px;
  max-height: 190px; overflow-y: auto;
  padding-right: 3px;
}

.free-model {
  display: flex; align-items: center; gap: 10px;
  width: 100%; text-align: left;
  font: inherit; font-size: 12.5px;
  padding: 7px 11px;
  border: 1px solid var(--separator);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  color: var(--text);
  cursor: pointer;
  transition: background var(--dur-fast) ease-out,
              border-color var(--dur-fast) ease-out,
              transform var(--dur-fast) ease-out;
}
.free-model:hover { background: var(--fill); }
.free-model:active { transform: scale(.985); }
.free-model[data-selected="true"] {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 7%, transparent);
}
.free-model-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.free-model-ctx {
  margin-left: auto; flex: none;
  font-size: 11px; color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
}
