/*
  FinRAG web UI styles.

  Design system (see docs/project/Design_System.md):
  - Calm, high-trust, data-dense-but-readable style for finance / compliance work.
  - Semantic colour tokens only; components never use raw hex values.
  - Light and dark themes are designed together; every text pair meets WCAG AA
    (4.5:1) and UI boundaries meet 3:1 (checked with a script, not by eye).
  - Status is never colour-only: each state pairs colour with an icon and text.
  - Spacing on a 4 px scale, touch targets >= 44 px, motion 150-300 ms and
    disabled entirely under prefers-reduced-motion.
*/

/* ------------------------------------------------------------------ tokens */
:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #14181f;
  --muted: #4a5565;
  --border: #cfd6df;
  --border-strong: #6b7686;
  --primary: #0b5cad;
  --on-primary: #ffffff;
  --primary-weak: #e6f0fa;
  --warning: #8a4b00;
  --warning-bg: #fff4e0;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --success: #05603a;
  --success-bg: #e6f6ee;
  --scrim: rgba(10, 14, 20, 0.55);

  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-md: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.5rem;
  --text-2xl: 2rem;

  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow-1: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
  --shadow-2: 0 8px 24px rgba(16, 24, 40, 0.16);

  --nav-h: 64px;
  --dur: 180ms;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --z-nav: 20;
  --z-top: 30;
  --z-toast: 100;
}

:root[data-theme="dark"] {
  --bg: #0e1217; --surface: #161b22; --surface-2: #1d242d; --text: #e8eaed; --muted: #a9b2bd;
  --border: #2f3844; --border-strong: #7d8896; --primary: #6cb2ff; --on-primary: #08111c;
  --primary-weak: #14283d; --warning: #ffb454; --warning-bg: #2d2008; --danger: #ff8a80;
  --danger-bg: #34171a; --success: #5fd39a; --success-bg: #0f2a1f; --scrim: rgba(0, 0, 0, 0.65);
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4); --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1217; --surface: #161b22; --surface-2: #1d242d; --text: #e8eaed; --muted: #a9b2bd;
    --border: #2f3844; --border-strong: #7d8896; --primary: #6cb2ff; --on-primary: #08111c;
    --primary-weak: #14283d; --warning: #ffb454; --warning-bg: #2d2008; --danger: #ff8a80;
    --danger-bg: #34171a; --success: #5fd39a; --success-bg: #0f2a1f; --scrim: rgba(0, 0, 0, 0.65);
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4); --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

/* -------------------------------------------------------------------- base */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 400 var(--text-md)/1.5 var(--font-sans);
  min-height: 100dvh;
}
h1, h2, h3 { line-height: 1.25; margin: 0; font-weight: 650; }
h1 { font-size: var(--text-xl); }
h2 { font-size: var(--text-lg); }
h3 { font-size: var(--text-md); }
p { margin: 0; }
a { color: var(--primary); }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 4px; }
h1:focus { outline: none; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.muted { color: var(--muted); }
.flex-1 { flex: 1; }
.noscript { padding: var(--s4); }
.small { font-size: var(--text-sm); }
.mono { font-family: var(--font-mono); }
.num { font-variant-numeric: tabular-nums; }

.skip-link {
  position: absolute; left: var(--s4); top: -100px; z-index: var(--z-toast);
  background: var(--primary); color: var(--on-primary); padding: var(--s3) var(--s4);
  border-radius: var(--radius-sm); text-decoration: none; font-weight: 600;
}
.skip-link:focus { top: var(--s2); }

.icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon.spin { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ----------------------------------------------------------------- buttons */
.btn {
  white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 44px; padding: 0 var(--s4); border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: 600 var(--text-sm)/1 var(--font-sans); cursor: pointer; touch-action: manipulation;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease),
    transform 120ms var(--ease);
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.98); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: var(--surface); }
.btn-danger:hover { background: var(--danger); filter: brightness(1.08); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-icon { width: 44px; padding: 0; }
.btn-sm { min-height: 44px; }

/* ------------------------------------------------------------------- forms */
.field { display: flex; flex-direction: column; gap: var(--s1); }
.field > label, legend { font-weight: 600; font-size: var(--text-sm); }
.req { color: var(--danger); margin-left: 2px; }
.help { color: var(--muted); font-size: var(--text-sm); }
.error-text { color: var(--danger); font-size: var(--text-sm); display: flex; gap: var(--s2);
  align-items: flex-start; }
input[type="text"], input[type="password"], input[type="number"], select, textarea {
  width: 100%; min-height: 44px; padding: var(--s2) var(--s3); background: var(--surface);
  color: var(--text); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  font: inherit; transition: border-color var(--dur) var(--ease);
}
textarea { resize: none; line-height: 1.5; }
input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] {
  border-color: var(--danger); border-width: 2px;
}
input:disabled, select:disabled, textarea:disabled { opacity: 0.5; cursor: not-allowed; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: var(--s4);
  margin: 0; min-width: 0; }
legend { padding: 0 var(--s2); }
.field-grid { display: grid; gap: var(--s4); grid-template-columns: 1fr; }
.input-with-button { display: flex; gap: var(--s2); }
.checkbox { display: flex; gap: var(--s3); align-items: flex-start; min-height: 44px;
  padding: var(--s2) 0; }
.checkbox input { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--primary); }

details > summary { cursor: pointer; min-height: 44px; display: list-item; padding: 10px 0;
  font-weight: 600; font-size: var(--text-sm); list-style-position: inside; }

/* ------------------------------------------------------------------ layout */
.login {
  min-height: 100dvh; display: grid; place-items: center; padding: var(--s4);
}
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-1); padding: var(--s5);
}
.login .card { width: 100%; max-width: 440px; display: flex; flex-direction: column; gap: var(--s4); }
.brand { display: flex; align-items: center; gap: var(--s3); font-weight: 700;
  font-size: var(--text-lg); }
.brand .icon { width: 28px; height: 28px; color: var(--primary); }

.shell { min-height: 100dvh; display: grid; grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr; }
.topbar {
  position: sticky; top: 0; z-index: var(--z-top); display: flex; align-items: center;
  gap: var(--s3); padding: var(--s2) var(--s4); background: var(--surface);
  border-bottom: 1px solid var(--border); min-height: 56px;
}
.topbar .spacer { flex: 1; }
.topbar .who { display: none; text-align: right; line-height: 1.25; }
.badge {
  display: inline-flex; align-items: center; gap: var(--s1); padding: 2px var(--s2);
  border-radius: 999px; border: 1px solid var(--border-strong); font-size: var(--text-xs);
  font-weight: 600; background: var(--surface-2); color: var(--text); white-space: nowrap;
}
.badge.primary { background: var(--primary-weak); border-color: var(--primary); color: var(--primary); }

.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: var(--z-nav); display: flex;
  background: var(--surface); border-top: 1px solid var(--border);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
}
.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; color: var(--muted); text-decoration: none; font-size: var(--text-xs);
  font-weight: 600; min-height: 44px; border-top: 3px solid transparent;
}
.nav a[aria-current="page"] { color: var(--primary); border-top-color: var(--primary);
  background: var(--primary-weak); }
.nav a:hover { color: var(--text); }

main { padding: var(--s4) var(--s4) calc(var(--nav-h) + var(--s6) + env(safe-area-inset-bottom));
  min-width: 0; }
.view.wide { max-width: 1040px; }
.view { max-width: 860px; margin: 0 auto; display: flex; flex-direction: column; gap: var(--s5); }
.view-head { display: flex; flex-direction: column; gap: var(--s1); }
.section { display: flex; flex-direction: column; gap: var(--s3); }

@media (min-width: 768px) {
  .topbar .who { display: block; }
  .field-grid.two { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 1024px) {
  .shell { grid-template-columns: 232px minmax(0, 1fr); }
  .topbar { grid-column: 1 / -1; }
  .nav {
    position: sticky; top: 56px; align-self: start; height: calc(100dvh - 56px);
    flex-direction: column; justify-content: flex-start; gap: var(--s1);
    padding: var(--s4) var(--s3); border-top: 0; border-right: 1px solid var(--border);
  }
  .nav a { flex: none; flex-direction: row; justify-content: flex-start; gap: var(--s3);
    padding: 0 var(--s3); font-size: var(--text-sm); border-top: 0; border-left: 3px solid transparent;
    border-radius: var(--radius-sm); }
  .nav a[aria-current="page"] { border-left-color: var(--primary); }
  main { padding: var(--s6) var(--s6) var(--s7); }
}

/* ------------------------------------------------------------------ banners */
.banner {
  display: flex; gap: var(--s3); align-items: flex-start; padding: var(--s3) var(--s4);
  border-radius: var(--radius-sm); border: 1px solid; font-size: var(--text-sm);
}
.banner .icon { margin-top: 1px; }
.banner strong { display: block; }
.banner ul { margin: var(--s1) 0 0; padding-left: var(--s5); }
.banner.warning { background: var(--warning-bg); border-color: var(--warning); }
.banner.warning .icon, .banner.warning strong { color: var(--warning); }
.banner.danger { background: var(--danger-bg); border-color: var(--danger); }
.banner.danger .icon, .banner.danger strong { color: var(--danger); }
.banner.success { background: var(--success-bg); border-color: var(--success); }
.banner.success .icon, .banner.success strong { color: var(--success); }
.banner.info { background: var(--primary-weak); border-color: var(--primary); }
.banner.info .icon, .banner.info strong { color: var(--primary); }

/* --------------------------------------------------------------- Ask / chat */
.thread { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column;
  gap: var(--s5); }
.turn { display: flex; flex-direction: column; gap: var(--s3); }
.question {
  align-self: flex-end; max-width: min(640px, 92%); background: var(--primary-weak);
  border: 1px solid var(--primary); border-radius: var(--radius); padding: var(--s3) var(--s4);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.answer { display: flex; flex-direction: column; gap: var(--s3); }
.answer-text { display: flex; flex-direction: column; gap: var(--s2); max-width: 70ch;
  overflow-wrap: anywhere; }
.answer-text ul { margin: 0; padding-left: var(--s5); display: flex; flex-direction: column;
  gap: var(--s2); }
.cite {
  display: inline-flex; align-items: center; justify-content: center; min-width: 28px;
  min-height: 24px; padding: 0 var(--s2); margin: 0 1px; border-radius: 6px;
  border: 1px solid var(--primary); background: var(--primary-weak); color: var(--primary);
  font: 700 var(--text-xs)/1 var(--font-sans); cursor: pointer; vertical-align: baseline;
}
.cite:hover { background: var(--primary); color: var(--on-primary); }
.cite.unknown { border-color: var(--warning); background: var(--warning-bg); color: var(--warning);
  cursor: default; }
.token {
  font-family: var(--font-mono); font-size: 0.85em; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; white-space: nowrap;
}
.meta-row { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.sources { border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); padding: 0 var(--s3); }
.sources ol { list-style: none; margin: 0; padding: 0 0 var(--s3); display: flex;
  flex-direction: column; gap: var(--s3); }
.source { border-left: 3px solid var(--border-strong); padding-left: var(--s3);
  scroll-margin: 96px; }
.source.flash { animation: flash 1.4s var(--ease); }
@keyframes flash { 0% { background: var(--primary-weak); } 100% { background: transparent; } }
.source blockquote { margin: var(--s1) 0 0; font-family: var(--font-mono); font-size: var(--text-xs);
  white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); max-height: 9em;
  overflow: auto; }
.disclosure { font-size: var(--text-xs); color: var(--muted); max-width: 70ch; }

.skeleton { display: flex; flex-direction: column; gap: var(--s2); }
.skeleton i { display: block; height: 14px; border-radius: 4px;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 50%, var(--surface-2) 75%);
  background-size: 200% 100%; animation: shimmer 1.4s linear infinite; }
.skeleton i:nth-child(2) { width: 85%; } .skeleton i:nth-child(3) { width: 60%; }
@keyframes shimmer { to { background-position: -200% 0; } }

.suggestions { display: flex; flex-wrap: wrap; gap: var(--s2); }
.chip { min-height: 44px; padding: 0 var(--s4); border-radius: 999px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font: 500 var(--text-sm)/1.2 var(--font-sans);
  cursor: pointer; text-align: left; }
.chip:hover { background: var(--primary-weak); border-color: var(--primary); }

.composer {
  position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + var(--s2));
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius);
  box-shadow: var(--shadow-2); padding: var(--s3); display: flex; flex-direction: column;
  gap: var(--s2); z-index: 5;
}
.composer-row { display: flex; gap: var(--s2); align-items: flex-end; }
.composer textarea { border: 0; min-height: 48px; max-height: 160px; padding: var(--s2); }
.composer textarea:focus-visible { outline-offset: 0; }
.composer .foot { display: flex; justify-content: space-between; gap: var(--s3); flex-wrap: wrap; }
@media (max-width: 767px) { .composer .hint { display: none; } .composer .foot { justify-content: flex-end; } }
@media (min-width: 1024px) { .composer { bottom: var(--s4); } }

/* -------------------------------------------------------------- documents */
.dropzone {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: var(--s2);
  text-align: center; padding: var(--s6) var(--s4); border: 2px dashed var(--border-strong);
  border-radius: var(--radius); background: var(--surface); cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.dropzone:hover, .dropzone.over { background: var(--primary-weak); border-color: var(--primary); }
.dropzone:focus-within { outline: 3px solid var(--primary); outline-offset: 2px; }
.dropzone .icon { width: 32px; height: 32px; color: var(--primary); }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }

.queue { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--s2); }
.queue li { display: flex; gap: var(--s3); align-items: flex-start; padding: var(--s3);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.queue .grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.status-ok { color: var(--success); } .status-bad { color: var(--danger); }
.status-warn { color: var(--warning); } .status-wait { color: var(--muted); }

.table-wrap { background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; }
table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
th { text-align: left; font-weight: 600; color: var(--muted); padding: var(--s3) var(--s4);
  border-bottom: 1px solid var(--border); background: var(--surface-2); }
td { padding: var(--s3) var(--s4); border-bottom: 1px solid var(--border); vertical-align: top;
  overflow-wrap: break-word; }
.fname { overflow-wrap: anywhere; }
@media (min-width: 768px) { td:first-child { min-width: 12rem; } .col-actions { width: 1%; white-space: nowrap; } }
tr:last-child td { border-bottom: 0; }
@media (max-width: 767px) {
  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  table, tbody, tr, td { display: block; }
  tr { padding: var(--s3) var(--s4); border-bottom: 1px solid var(--border); }
  tr:last-child { border-bottom: 0; }
  td { border: 0; padding: var(--s1) 0; }
  td[data-label]::before { content: attr(data-label) ": "; font-weight: 600; color: var(--muted); }
}
.empty { text-align: center; padding: var(--s6) var(--s4); display: flex; flex-direction: column;
  gap: var(--s3); align-items: center; background: var(--surface); border: 1px dashed var(--border-strong);
  border-radius: var(--radius); }
.empty .icon { width: 32px; height: 32px; color: var(--muted); }

pre.result { margin: 0; padding: var(--s3); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); font: var(--text-xs)/1.5 var(--font-mono); overflow: auto;
  max-height: 320px; white-space: pre-wrap; overflow-wrap: anywhere; }
dl.kv { display: grid; grid-template-columns: minmax(0, 11rem) minmax(0, 1fr); gap: var(--s2) var(--s4);
  margin: 0; font-size: var(--text-sm); }
dl.kv dt { font-weight: 600; color: var(--muted); } dl.kv dd { margin: 0; overflow-wrap: anywhere; }
dl.kv ul { margin: 0; padding-left: var(--s4); }
@media (max-width: 599px) { dl.kv { grid-template-columns: 1fr; } dl.kv dd { margin-bottom: var(--s2); } }
.actions { display: flex; flex-wrap: wrap; gap: var(--s2); }

/* ---------------------------------------------------------- dialog + toasts */
dialog { border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface);
  color: var(--text); padding: var(--s5); max-width: min(480px, calc(100vw - 32px)); box-shadow: var(--shadow-2); }
dialog::backdrop { background: var(--scrim); }
dialog h2 { margin-bottom: var(--s2); }
dialog .actions { justify-content: flex-end; margin-top: var(--s5); }

.toasts { position: fixed; z-index: var(--z-toast); left: var(--s4); right: var(--s4);
  bottom: calc(var(--nav-h) + var(--s4) + env(safe-area-inset-bottom)); display: flex;
  flex-direction: column; gap: var(--s2); pointer-events: none; align-items: center; }
.toast { pointer-events: auto; display: flex; gap: var(--s3); align-items: center; background: var(--text);
  color: var(--bg); padding: var(--s3) var(--s4); border-radius: var(--radius-sm); box-shadow: var(--shadow-2);
  max-width: 480px; font-size: var(--text-sm); animation: rise var(--dur) var(--ease); }
.toast button { color: inherit; background: transparent; border: 0; min-width: 44px; min-height: 44px;
  cursor: pointer; display: grid; place-items: center; margin: -8px -12px -8px 0; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (min-width: 1024px) { .toasts { bottom: var(--s5); align-items: flex-end; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
