/* Colors: chart palette and chrome follow the validated dataviz reference palette
   (categorical order is the colorblind-safety mechanism - don't reorder). */
:root {
  color-scheme: light;
  --bg: #f9f9f7;
  --sidebar: #f1f1ee;
  --surface: #fcfcfb;
  --hover: rgba(11, 11, 11, 0.05);
  --active: rgba(11, 11, 11, 0.08);
  --text: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.18);
  --accent: #2a78d6;
  --accent-hover: #256abf;
  --accent-soft: rgba(42, 120, 214, 0.10);
  --accent-text: #ffffff;
  --user-bubble: #ecebe6;
  --user-text: #0b0b0b;
  --code-bg: #f0efec;
  --critical: #d03b3b;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 4px 16px rgba(11, 11, 11, 0.06);
  --chart-grid: #e1e0d9;
  --chart-axis: #c3c2b7;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0d0d0d;
    --sidebar: #141413;
    --surface: #1a1a19;
    --hover: rgba(255, 255, 255, 0.05);
    --active: rgba(255, 255, 255, 0.09);
    --text: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --border: rgba(255, 255, 255, 0.10);
    --border-strong: rgba(255, 255, 255, 0.18);
    --accent: #3987e5;
    --accent-hover: #5598e7;
    --accent-soft: rgba(57, 135, 229, 0.16);
    --accent-text: #ffffff;
    --user-bubble: #262624;
    --user-text: #f0efec;
    --code-bg: #262624;
    --critical: #e66767;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.35);
    --chart-grid: #2c2c2a;
    --chart-axis: #383835;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--text-secondary); }
svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app { display: grid; grid-template-columns: 264px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); height: 100%; overflow: hidden; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.mobile-only { display: none !important; }

.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; flex: none;
  border: 0; border-radius: 8px; background: transparent; color: var(--text-secondary);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }

/* ---------- sidebar ---------- */
.sidebar {
  display: flex; flex-direction: column; gap: 4px; min-height: 0;
  padding: 14px 12px 12px; background: var(--sidebar); border-right: 1px solid var(--border);
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 4px 10px; }
.logo { display: flex; align-items: center; gap: 10px; }
.logo-mark {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
  background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 16px;
}
.logo-name { font-weight: 650; line-height: 1.2; }
.logo-sub { font-size: 12px; color: var(--text-muted); line-height: 1.2; }

.new-chat {
  display: flex; align-items: center; gap: 8px; width: 100%; margin-bottom: 6px;
  padding: 9px 12px; border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface); font-weight: 550; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.new-chat:hover { border-color: var(--accent); color: var(--accent); }

.side-nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item, .recent-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; border: 0; border-radius: 8px; background: transparent; text-align: left;
  color: var(--text-secondary);
}
.nav-item:hover, .recent-item:hover { background: var(--hover); color: var(--text); }
.nav-item[aria-current="page"], .recent-item[aria-current="true"] { background: var(--active); color: var(--text); font-weight: 550; }

.side-label { margin: 16px 10px 4px; font-size: 12px; font-weight: 600; color: var(--text-muted); }
.recent { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.recent-item { display: block; font-size: 14px; }
.recent-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-time { display: block; font-size: 12px; color: var(--text-muted); font-weight: 400; }
.recent-empty { padding: 6px 10px; font-size: 13px; color: var(--text-muted); }

.side-foot {
  display: flex; align-items: center; gap: 10px; margin-top: 8px; padding: 10px 4px 0;
  border-top: 1px solid var(--border);
}
.avatar {
  display: grid; place-items: center; width: 32px; height: 32px; flex: none; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-weight: 650; font-size: 14px;
}
.who { min-width: 0; flex: 1; }
.who-name { font-size: 14px; font-weight: 550; line-height: 1.25; }
.who-email { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- chat ---------- */
.chat { flex: 1; min-height: 0; overflow-y: auto; padding: 32px 20px 16px; scroll-behavior: smooth; }
.column { max-width: 820px; margin: 0 auto; }

.welcome { padding-top: 7vh; }
.hello { font-size: 15px; font-weight: 600; color: var(--accent); }
.welcome h1 { font-size: 28px; line-height: 1.25; letter-spacing: -0.01em; margin: 6px 0 8px; }
.welcome > p { margin: 0; }
.examples { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 28px; }
.example {
  display: grid; grid-template-columns: auto 1fr; column-gap: 12px; row-gap: 2px; align-items: start;
  padding: 14px 16px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
  text-align: left; transition: border-color 0.15s, box-shadow 0.15s, transform 0.15s;
}
.example:hover { border-color: var(--accent); box-shadow: var(--shadow); transform: translateY(-1px); }
.example svg { grid-row: span 2; width: 20px; height: 20px; margin-top: 2px; color: var(--accent); }
.ex-title { font-weight: 600; }
.ex-text { font-size: 13px; color: var(--text-secondary); }

.msg { margin: 22px 0; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  max-width: 78%; padding: 10px 16px; border-radius: 18px 18px 4px 18px;
  background: var(--user-bubble); color: var(--user-text); white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant { display: grid; grid-template-columns: 32px minmax(0, 1fr); gap: 14px; }
.msg.assistant .logo-mark { width: 30px; height: 30px; font-size: 14px; border-radius: 8px; margin-top: 2px; }
.answer { min-width: 0; }
.answer > :first-child { margin-top: 0; }
.answer p { margin: 0.5em 0; }
.answer ul, .answer ol { padding-left: 1.3em; margin: 0.5em 0; }
.answer li { margin: 0.25em 0; }
.answer h1, .answer h2, .answer h3 { font-size: 16px; margin: 1em 0 0.4em; }
.answer code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 0.92em; }
.answer pre { background: var(--code-bg); padding: 12px; border-radius: 10px; overflow-x: auto; }
.answer .table-wrap { overflow-x: auto; margin: 10px 0; border: 1px solid var(--border); border-radius: 10px; }
.answer table, .chart-table table { border-collapse: collapse; width: 100%; font-size: 14px; }
.answer th, .answer td, .chart-table th, .chart-table td { padding: 7px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.answer tr:last-child td { border-bottom: 0; }
.answer th, .chart-table th { font-weight: 600; color: var(--text-secondary); background: var(--code-bg); }
.answer td.num, .answer th.num, .chart-table td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.answer .chart-card { margin: 14px 0 6px; }

.msg-actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0.75; }
.msg.assistant:hover .msg-actions { opacity: 1; }
.action {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 8px; border: 0; border-radius: 7px;
  background: transparent; color: var(--text-muted); font-size: 13px;
}
.action svg { width: 15px; height: 15px; }
.action:hover { background: var(--hover); color: var(--text); }

.msg.error .answer { color: var(--critical); }

.working { display: flex; align-items: center; gap: 10px; color: var(--text-secondary); padding-top: 4px; }
.pulse { display: inline-flex; gap: 4px; }
.pulse i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite ease-in-out; }
.pulse i:nth-child(2) { animation-delay: 0.15s; }
.pulse i:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1); } }
.working-time { font-variant-numeric: tabular-nums; color: var(--text-muted); }
.working-hint { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

/* ---------- composer ---------- */
.composer { padding: 8px 20px 14px; }
.composer-box {
  display: flex; align-items: flex-end; gap: 8px; max-width: 820px; margin: 0 auto;
  padding: 8px 8px 8px 16px; background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 16px; box-shadow: var(--shadow);
}
.composer-box:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; resize: none; max-height: 200px; padding: 7px 0; border: 0; outline: 0;
  background: transparent; color: var(--text); font: inherit;
}
.send {
  display: grid; place-items: center; width: 36px; height: 36px; flex: none; border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-text);
}
.send:hover:not(:disabled) { background: var(--accent-hover); }
.send:disabled { opacity: 0.35; cursor: default; }
.composer-hint { max-width: 820px; margin: 6px auto 0; font-size: 12px; color: var(--text-muted); text-align: center; }

/* ---------- chart card ---------- */
.chart-card {
  margin: 0; padding: 14px 16px 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; min-width: 0;
}
.chart-card figcaption { display: grid; grid-template-columns: 1fr auto; column-gap: 12px; margin-bottom: 10px; }
.chart-card figcaption .ghost { grid-column: 2; grid-row: 1 / span 2; align-self: start; }
.ghost {
  background: transparent; color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 12px;
}
.ghost:hover { border-color: var(--accent); }
.ghost.small { padding: 2px 10px; font-size: 13px; }
.chart-title { font-weight: 600; }
.chart-subtitle { font-size: 13px; color: var(--text-secondary); }
.chart-canvas { position: relative; height: 280px; }
.chart-canvas[class*="tall-"] { height: calc(60px + var(--rows, 6) * 32px); }
.tall-1 { --rows: 1; } .tall-2 { --rows: 2; } .tall-3 { --rows: 3; } .tall-4 { --rows: 4; }
.tall-5 { --rows: 5; } .tall-6 { --rows: 6; } .tall-7 { --rows: 7; } .tall-8 { --rows: 8; }
.tall-9 { --rows: 9; } .tall-10 { --rows: 10; } .tall-11 { --rows: 11; } .tall-12 { --rows: 12; }
.chart-table { overflow-x: auto; max-height: 360px; overflow-y: auto; }

/* ---------- dashboard ---------- */
.dashboard { flex: 1; min-height: 0; overflow-y: auto; padding: 32px 24px 40px; }
.dash-inner { max-width: 1200px; margin: 0 auto; }
.dash-head h1 { font-size: 24px; letter-spacing: -0.01em; margin: 0; }
.dash-head p { margin: 4px 0 20px; font-size: 13px; }
.dash-inner.loading .kpis, .dash-inner.loading .dash-grid { opacity: 0.5; }

.kpis { display: grid; grid-template-columns: 1.4fr repeat(3, 1fr); gap: 12px; margin-bottom: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px;
  display: flex; flex-direction: column; min-width: 0;
}
.kpi-label { font-size: 13px; color: var(--text-secondary); }
.kpi-value { font-size: 28px; font-weight: 600; margin-top: 4px; }
.kpi.hero .kpi-value { font-size: 48px; line-height: 1.1; }
.kpi-note { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.dash-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.dash-grid .wide { grid-column: 1 / -1; }

/* ---------- small screens ---------- */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi.hero { grid-column: 1 / -1; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .mobile-only { display: inline-grid !important; }
  .mobile-bar.mobile-only { display: flex !important; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(300px, 86vw);
    transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0.35); }
  .mobile-bar {
    align-items: center; gap: 8px; padding: 8px 12px; font-weight: 600;
    border-bottom: 1px solid var(--border); background: var(--surface);
  }
  .dash-grid { grid-template-columns: minmax(0, 1fr); }
  .chat { padding: 20px 16px 12px; }
  .composer { padding: 6px 16px 12px; }
  .dashboard { padding: 20px 16px 32px; }
}
@media (max-width: 600px) {
  .examples { grid-template-columns: minmax(0, 1fr); }
  .welcome h1 { font-size: 23px; }
  .msg.user .bubble { max-width: 90%; }
  .msg.assistant { grid-template-columns: minmax(0, 1fr); }
  .msg.assistant > .logo-mark { display: none; }
  .kpi.hero .kpi-value { font-size: 40px; }
  .composer-hint { display: none; }
}
