:root {
  color-scheme: light dark;
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #eef1ee;
  --fg: #17201c;
  --muted: #5d6b64;
  --border: #dce3df;
  --accent: #0f766e;
  --accent-fg: #ffffff;
  --accent-soft: #d9f0eb;
  --success: #15803d;
  --warning: #b45309;
  --error: #b91c1c;
  --c1: #0d9488;
  --c2: #d97706;
  --c3: #4f46e5;
  --c4: #be123c;
  --c5: #7c3aed;
  --c6: #0369a1;
  --shadow: 0 1px 2px rgba(16, 24, 20, 0.06), 0 8px 24px rgba(16, 24, 20, 0.04);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101614;
    --surface: #17201c;
    --surface-2: #1f2b26;
    --fg: #e8efeb;
    --muted: #9aada4;
    --border: #2b3a33;
    --accent: #2dd4bf;
    --accent-fg: #06201c;
    --accent-soft: #133832;
    --success: #4ade80;
    --warning: #fbbf24;
    --error: #f87171;
    --c1: #2dd4bf;
    --c2: #fbbf24;
    --c3: #818cf8;
    --c4: #fb7185;
    --c5: #c084fc;
    --c6: #38bdf8;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 32px rgba(0, 0, 0, 0.25);
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.25; margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 1.65rem; }
h2 { font-size: 1.05rem; }
p { margin: 0; }
button, input, select { font: inherit; color: inherit; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--error); font-size: 0.9rem; }
.success { color: var(--success); }

/* ---------------------------------------------------------------- */
/* Top bar                                                          */
/* ---------------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 22px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.brand-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--c3));
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1;
}

.tabs button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 12px;
  border-radius: 9px;
  cursor: pointer;
  white-space: nowrap;
}

.tabs button:hover { background: var(--surface-2); color: var(--fg); }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.who { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.who-name { color: var(--muted); font-size: 0.9rem; }

/* ---------------------------------------------------------------- */
/* Layout                                                           */
/* ---------------------------------------------------------------- */

main {
  width: min(1180px, 100% - 32px);
  margin: 0 auto;
  padding: 26px 0 50px;
}

.view { display: flex; flex-direction: column; gap: 18px; }
.view-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
  margin-top: 4px;
}
.view-head p { max-width: 70ch; }
.view-head-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 18px;
  align-items: start;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 12px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 13px 15px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stat-label { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; }
.stat-value { font-size: 1.28rem; font-weight: 650; letter-spacing: -0.02em; }
.stat-note { color: var(--muted); font-size: 0.8rem; }

/* ---------------------------------------------------------------- */
/* Buttons, forms                                                   */
/* ---------------------------------------------------------------- */

button {
  cursor: pointer;
  border-radius: 9px;
  border: 1px solid transparent;
  padding: 8px 14px;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
}

button:disabled { opacity: 0.55; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}
button.primary:hover { filter: brightness(1.07); }

button.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--fg);
}
button.ghost:hover { background: var(--surface-2); }

button.small { padding: 5px 10px; font-size: 0.85rem; }

button.danger {
  background: transparent;
  border-color: color-mix(in srgb, var(--error) 45%, var(--border));
  color: var(--error);
}
button.danger:hover { background: color-mix(in srgb, var(--error) 10%, transparent); }

button.link {
  background: transparent;
  border: 0;
  color: var(--accent);
  padding: 4px 6px;
  font-size: 0.88rem;
}
button.link:hover { text-decoration: underline; }

.range { display: flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.range button {
  border: 0;
  border-radius: 0;
  background: var(--surface);
  color: var(--muted);
  padding: 7px 11px;
  font-size: 0.85rem;
}
.range button + button { border-left: 1px solid var(--border); }
.range button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.span-2 { grid-column: 1 / -1; }

label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 550;
}

input, select, textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  color: var(--fg);
  width: 100%;
  min-width: 0;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input[type="file"] { padding: 8px; background: var(--surface-2); }

.form-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------- */
/* Tables                                                           */
/* ---------------------------------------------------------------- */

.table-wrap { overflow-x: auto; margin: 0 -4px; }

table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
td.actions { text-align: right; white-space: nowrap; }
td.actions .link + .link { margin-left: 8px; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge.active { color: var(--success); border-color: color-mix(in srgb, var(--success) 40%, var(--border)); background: color-mix(in srgb, var(--success) 10%, transparent); }
.badge.pause, .badge.gap { color: var(--warning); border-color: color-mix(in srgb, var(--warning) 40%, var(--border)); background: color-mix(in srgb, var(--warning) 10%, transparent); }
.badge.unknown { color: var(--muted); }
.badge.planned { color: var(--c2); border-color: color-mix(in srgb, var(--c2) 45%, var(--border)); background: color-mix(in srgb, var(--c2) 10%, transparent); }
.badge.sev-1 { color: var(--success); border-color: color-mix(in srgb, var(--success) 45%, var(--border)); }
.badge.sev-2 { color: var(--c1); border-color: color-mix(in srgb, var(--c1) 45%, var(--border)); }
.badge.sev-3 { color: var(--warning); border-color: color-mix(in srgb, var(--warning) 45%, var(--border)); }
.badge.sev-4 { color: var(--c2); border-color: color-mix(in srgb, var(--c2) 45%, var(--border)); }
.badge.sev-5 { color: var(--error); border-color: color-mix(in srgb, var(--error) 45%, var(--border)); }
.badge.none { color: var(--muted); }

/* ---------------------------------------------------------------- */
/* Charts                                                           */
/* ---------------------------------------------------------------- */

.chart-card { padding-bottom: 12px; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; color: var(--muted); font-size: 0.8rem; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 16px; height: 3px; border-radius: 2px; display: inline-block; }
.swatch-actual { background: var(--c1); }
.swatch-planned { background: repeating-linear-gradient(90deg, var(--c2) 0 4px, transparent 4px 7px); }
.swatch-level { background: var(--c3); }

.chart-wrap { position: relative; width: 100%; }
.chart-wrap svg {
  display: block;
  width: 100%;
  height: auto;
  touch-action: pan-y;
}
.chart-wrap svg.level-chart {
  touch-action: none;
  cursor: crosshair;
  user-select: none;
}
.chart-wrap svg.level-chart:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.chart-wrap svg.level-chart.is-panning { cursor: grabbing; }

text.axis { fill: var(--muted); font-size: 11px; font-family: inherit; font-variant-numeric: tabular-nums; }
.axis-title { fill: var(--muted); font-size: 10px; font-family: inherit; letter-spacing: 0.04em; text-transform: uppercase; }
.gridline { stroke: var(--border); stroke-width: 1; }
.nowline { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 4 4; }
.nowlabel {
  fill: var(--fg);
  font-size: 10.5px;
  font-weight: 600;
  font-family: inherit;
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.dose-actual { stroke: var(--c1); stroke-width: 3; stroke-linecap: round; }
.dose-planned { stroke: var(--c2); stroke-width: 2; stroke-dasharray: 3 3; }
.dose-dot-actual { fill: var(--c1); }
.dose-dot-planned { fill: var(--surface); stroke: var(--c2); stroke-width: 2; }
.dose-label { fill: var(--fg); opacity: 0.78; font-size: 9.5px; font-family: inherit; font-variant-numeric: tabular-nums; }
.level-line { fill: none; stroke: var(--c3); stroke-width: 2.3; stroke-linejoin: round; stroke-linecap: round; }
.level-area { fill: var(--c3); opacity: 0.1; }
.level-projected { fill: none; stroke: var(--c3); stroke-width: 2.3; stroke-dasharray: 5 5; opacity: 0.9; }
.level-dot { fill: var(--c3); stroke: var(--surface); stroke-width: 2; }
.crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 2 3; pointer-events: none; }
.projection-zone { fill: var(--c2); opacity: 0.045; }
.zone-label { fill: var(--muted); font-size: 9.5px; font-family: inherit; letter-spacing: 0.06em; text-transform: uppercase; }
.rise-band { fill: var(--accent); opacity: 0.07; }
.latency-line { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 3 3; opacity: 0.8; }
.peak-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.peak-label {
  fill: var(--accent);
  font-size: 10px;
  font-weight: 650;
  font-family: inherit;
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 4px;
  stroke-linejoin: round;
}
.chart-empty { fill: var(--muted); font-size: 13px; font-family: inherit; }
.weight-line { fill: none; stroke: var(--c1); stroke-width: 2; }
.weight-dot { fill: var(--c1); stroke: var(--surface); stroke-width: 1.5; }

.chart-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 2px 2px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.76rem;
}
.chart-window { font-variant-numeric: tabular-nums; color: var(--fg); font-weight: 550; }
.chart-hint { flex: 1; min-width: 180px; }
.chart-zoom { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.chart-zoom button {
  border: 0;
  border-radius: 0;
  background: var(--surface-2);
  color: var(--muted);
  min-width: 30px;
  padding: 4px 8px;
  font-size: 0.78rem;
}
.chart-zoom button + button { border-left: 1px solid var(--border); }
.chart-zoom button:hover { background: var(--accent-soft); color: var(--accent); }

.tooltip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  box-shadow: var(--shadow);
  padding: 7px 10px;
  font-size: 0.8rem;
  line-height: 1.45;
  max-width: 230px;
  transform: translate(10px, -50%);
}
.tooltip strong { display: block; font-size: 0.85rem; }
.tooltip .tooltip-muted { color: var(--muted); font-size: 0.74rem; }

/* ---------------------------------------------------------------- */
/* Assistant                                                        */
/* ---------------------------------------------------------------- */

.assistant-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 18px;
  align-items: start;
}

.chat-card { min-height: 560px; }

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  max-height: 56vh;
  min-height: 260px;
  padding: 4px 2px;
}

.msg {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: 14px;
  line-height: 1.55;
}
.msg-body { overflow-wrap: anywhere; }
.msg-body .rt-heading { font-weight: 650; margin-top: 6px; }
.msg-body .rt-heading:first-child { margin-top: 0; }
.msg-body .rt-p { margin: 2px 0; }
.msg-body .rt-gap { height: 5px; }
.msg-body ul { margin: 4px 0; padding-left: 18px; display: flex; flex-direction: column; gap: 3px; }
.msg-body code { background: var(--surface); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; font-size: 0.9em; }
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-fg);
  border-bottom-right-radius: 4px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.thinking { opacity: 0.7; font-style: italic; }
.msg .meta { margin-top: 6px; font-size: 0.7rem; opacity: 0.65; }
.msg .msg-body.error { color: var(--error); }

.proposal {
  align-self: stretch;
  background: var(--surface);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, var(--border));
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.proposal ul { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 4px; }
.proposal li { overflow-wrap: anywhere; }
.proposal .form-actions { margin-top: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 5px 11px;
  font-size: 0.8rem;
  border-radius: 999px;
}
.chips button:hover { color: var(--fg); border-color: var(--accent); }

.chat-form { display: flex; gap: 10px; align-items: flex-end; }
.chat-form textarea {
  flex: 1;
  resize: vertical;
  min-height: 46px;
  max-height: 180px;
  padding: 10px 11px;
}

.assistant-side { position: sticky; top: 78px; }
.plain-list, .doc-list {
  margin: 0;
  padding-left: 18px;
  font-size: 0.86rem;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.doc-list li { overflow-wrap: anywhere; }
.doc-list .doc-path { color: var(--muted); font-size: 0.76rem; }
.doc-list .doc-delete { margin-left: 6px; }
.assistant-side h2:not(:first-child) { margin-top: 6px; }
.assistant-model { display: flex; flex-direction: column; gap: 4px; margin-bottom: 6px; font-size: 13px; color: var(--muted); }
.assistant-model select { width: 100%; }

/* ---------------------------------------------------------------- */
/* Notices, banner, login                                           */
/* ---------------------------------------------------------------- */

.banner {
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
  color: var(--fg);
  padding: 11px 14px;
  border-radius: 10px;
  margin-bottom: 16px;
}
.banner.error { background: color-mix(in srgb, var(--error) 12%, var(--surface)); border-color: color-mix(in srgb, var(--error) 40%, var(--border)); }

.notice {
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: 9px;
  padding: 11px 12px;
  font-size: 0.9rem;
  word-break: break-word;
}
.scope-notice {
  border-style: solid;
  border-color: color-mix(in srgb, var(--warning) 45%, var(--border));
  background: color-mix(in srgb, var(--warning) 9%, var(--surface));
}
.notice code { background: var(--surface); padding: 2px 7px; border-radius: 6px; border: 1px solid var(--border); font-size: 0.95rem; }

footer {
  margin-top: 34px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
}

.login-body {
  --bg: #fafafa;
  --fg: #222222;
  --muted: #555555;
  --border: #bbbbbb;
  --accent: #444444;
  --accent-fg: #ffffff;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(300px, 100%);
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 24px;
}
.login-card h1 { font-size: 1.25rem; font-weight: 500; }
.login-card input, .login-card button { border-radius: 3px; }

@media (prefers-color-scheme: dark) {
  .login-body {
    --bg: #181818;
    --fg: #eeeeee;
    --muted: #aaaaaa;
    --border: #666666;
    --accent: #cccccc;
    --accent-fg: #181818;
  }
}

/* ---------------------------------------------------------------- */
/* Responsive                                                       */
/* ---------------------------------------------------------------- */

@media (max-width: 900px) {
  .assistant-layout { grid-template-columns: minmax(0, 1fr); }
  .assistant-side { position: static; }
  .msg { max-width: 94%; }
}

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px 14px; padding: 10px 14px; }
  .tabs { order: 3; flex: 1 0 100%; width: 100%; flex-wrap: wrap; overflow: visible; }
  .tabs button { padding: 6px 9px; font-size: 0.88rem; }
  .who { margin-left: auto; }
  .who-name { display: none; }
  main { width: calc(100% - 24px); padding-top: 16px; }
  h1 { font-size: 1.35rem; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .card { padding: 14px; }
  .dose-label { font-size: 19px; }
  text.axis { font-size: 20px; }
  .axis-title { font-size: 18px; }
  .nowlabel { font-size: 19px; }
  .peak-label { font-size: 20px; }
  .zone-label { font-size: 18px; }
  .chart-empty { font-size: 22px; }
  .chart-hint { display: none; }
  .chart-foot { gap: 8px; }
}
