/* PRL Ops — shared styles. Dark by default; light when the system asks for it. */
:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --surface-3: #2c2c2a;
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --series-1: #3987e5;
  --series-2: #d95926;
  --accent: #3987e5;
  --accent-ink: #ffffff;
  --good: #0ca30c;
  --good-text: #3bd13b;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --critical-text: #ff7a7a;
  --good-wash: rgba(12, 163, 12, 0.14);
  --warning-wash: rgba(250, 178, 25, 0.14);
  --critical-wash: rgba(208, 59, 59, 0.16);
  --info-wash: rgba(57, 135, 229, 0.14);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --page: #f9f9f7;
    --surface: #fcfcfb;
    --surface-2: #f3f2ee;
    --surface-3: #e9e8e3;
    --border: rgba(11, 11, 11, 0.10);
    --border-strong: rgba(11, 11, 11, 0.20);
    --text: #0b0b0b;
    --text-2: #52514e;
    --muted: #898781;
    --grid: #e1e0d9;
    --axis: #c3c2b7;
    --series-1: #2a78d6;
    --series-2: #eb6834;
    --accent: #2a78d6;
    --good-text: #006300;
    --critical-text: #b42828;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; color: var(--text-2); font-weight: 500; }
.muted { color: var(--muted); }
.text-2 { color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; }
.pos { color: var(--good-text); }
.neg { color: var(--critical-text); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 12px; }
.hidden { display: none !important; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 20px;
  padding: 0 24px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.brand span { color: var(--muted); font-weight: 500; }
.nav { display: flex; gap: 4px; height: 100%; }
.nav-link {
  display: flex; align-items: center; padding: 0 14px; color: var(--text-2); text-decoration: none;
  border-bottom: 2px solid transparent; font-weight: 500;
}
.nav-link:hover { color: var(--text); }
.nav-link.is-active { color: var(--text); border-bottom-color: var(--accent); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.price-pill { font-weight: 600; white-space: nowrap; }
.price-pill small { color: var(--muted); font-weight: 400; }

.mode-badge {
  font-size: 11px; font-weight: 700; letter-spacing: 0.04em; padding: 4px 8px; border-radius: 6px;
  background: var(--surface-3); color: var(--text-2); white-space: nowrap;
}
.mode-badge.live { background: var(--critical); color: #fff; }
.mode-badge.paper { background: var(--warning-wash); color: var(--warning); }
.mode-badge.demo { background: var(--info-wash); color: var(--accent); }

.sources { display: flex; gap: 10px; }
.source { position: relative; display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-2); cursor: default; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--good); }
.dot.stale { background: var(--warning); }
.dot.bad { background: var(--critical); }
.source .tip {
  display: none; position: absolute; top: 26px; right: 0; width: 260px; z-index: 30;
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 10px; color: var(--text); box-shadow: 0 8px 24px rgba(0,0,0,.35);
}
.source:hover .tip, .source:focus-within .tip { display: block; }

/* ---------------------------------------------------------------- layout */
.page { max-width: 1440px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--text-2); max-width: 760px; }
.head-tools { display: flex; align-items: center; gap: 12px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-12 { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); }
@media (max-width: 1100px) { .grid-2, .grid-3, .grid-12 { grid-template-columns: 1fr; } }
.stack { display: flex; flex-direction: column; gap: 16px; }
.grid > *, .stack > * { min-width: 0; }
.section { margin-top: 16px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 0; flex-wrap: wrap; }
.card-head p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.card-body { padding: 14px 16px 16px; }

/* ---------------------------------------------------------------- KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-width: 0; }
.kpi-label { font-size: 12px; color: var(--text-2); }
.kpi-value { font-size: 24px; font-weight: 600; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.is-critical { border-color: var(--critical); background: linear-gradient(0deg, var(--critical-wash), var(--critical-wash)), var(--surface); }
.kpi.is-warning { border-color: var(--warning); }

/* ---------------------------------------------------------------- controls */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; }
.seg button {
  border: 0; background: transparent; color: var(--text-2); padding: 6px 12px; border-radius: 6px;
  font: inherit; font-weight: 500; cursor: pointer;
}
.seg button.is-active { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--border-strong); }
.seg.sell-buy button.is-active[data-value="sell"] { background: var(--critical); color: #fff; box-shadow: none; }
.seg.sell-buy button.is-active[data-value="buy"] { background: var(--good); color: #fff; box-shadow: none; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-3); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.1); background: var(--accent); }
.btn-sell { background: var(--critical); border-color: var(--critical); color: #fff; }
.btn-sell:hover { filter: brightness(1.1); background: var(--critical); }
.btn-buy { background: var(--good); border-color: var(--good); color: #fff; }
.btn-buy:hover { filter: brightness(1.1); background: var(--good); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); padding: 6px 8px; }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-block { width: 100%; padding: 11px 14px; }

label.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--text-2); }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border-strong);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.input-group { display: flex; gap: 6px; }
.input-group input { flex: 1; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; }
.quick button { flex: 1; }
.hint { font-size: 12px; color: var(--muted); }
.preview {
  background: var(--surface-2); border: 1px dashed var(--border-strong); border-radius: 8px; padding: 10px 12px;
  color: var(--text-2); font-size: 13px;
}
.preview strong { color: var(--text); }

.switch { position: relative; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track { width: 36px; height: 20px; border-radius: 999px; background: var(--surface-3); border: 1px solid var(--border-strong); position: relative; transition: background .15s; flex: none; }
.switch .track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--text-2); transition: transform .15s, background .15s; }
.switch input:checked + .track { background: var(--good); border-color: var(--good); }
.switch input:checked + .track::after { transform: translateX(16px); background: #fff; }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- chips and alerts */
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; white-space: nowrap; background: var(--surface-3); color: var(--text-2); }
.chip.good { background: var(--good-wash); color: var(--good-text); }
.chip.warning { background: var(--warning-wash); color: var(--warning); }
.chip.critical { background: var(--critical-wash); color: var(--critical-text); }
.chip.info { background: var(--info-wash); color: var(--accent); }
.chip.sell { background: var(--critical-wash); color: var(--critical-text); }
.chip.buy { background: var(--good-wash); color: var(--good-text); }

.alert { display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid; }
.alert.critical { background: var(--critical-wash); border-color: var(--critical); }
.alert.warning { background: var(--warning-wash); border-color: var(--warning); }
.alert.info { background: var(--info-wash); border-color: var(--accent); }
.alert .icon { font-weight: 700; }

/* ---------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 500; font-size: 12px; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sorted::after { content: " ▾"; }
th.sorted.asc::after { content: " ▴"; }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--surface-2); }
tr.row-critical td { background: var(--critical-wash); }
tr.row-muted td { color: var(--muted); }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.cell-main { font-weight: 500; }
.cell-sub { font-size: 12px; color: var(--muted); }

/* ---------------------------------------------------------------- charts */
.chart-box { position: relative; height: 240px; }
.chart-box.tall { height: 300px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }

/* ---------------------------------------------------------------- pipeline (mining) */
.pipeline { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 0; }
.stage { position: relative; padding: 14px 18px 14px 16px; border-right: 1px solid var(--border); min-width: 0; }
.stage:last-child { border-right: 0; }
.stage-label { font-size: 12px; color: var(--text-2); }
.stage-value { font-size: 22px; font-weight: 600; margin-top: 4px; white-space: nowrap; }
.stage-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stage-lag { display: inline-block; margin-top: 10px; font-size: 11px; color: var(--text-2); background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px; }
.stage::after {
  content: ""; position: absolute; right: -7px; top: 50%; width: 12px; height: 12px; background: var(--surface);
  border-top: 1px solid var(--border); border-right: 1px solid var(--border); transform: translateY(-50%) rotate(45deg); z-index: 1;
}
.stage:last-child::after { display: none; }
.stage.done .stage-value { color: var(--good-text); }
.pipe-bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; gap: 2px; margin: 0 16px 14px; }
.pipe-bar span { display: block; height: 100%; }
@media (max-width: 1100px) { .pipeline { grid-template-columns: 1fr 1fr; } .stage::after { display: none; } }

/* ---------------------------------------------------------------- orders */
.ticker { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.ticker > div { padding: 12px 16px; border-right: 1px solid var(--border); }
.ticker > div:last-child { border-right: 0; }
.big { font-size: 26px; font-weight: 600; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); padding: 0 16px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; color: var(--text-2); padding: 12px 10px; font: inherit; font-weight: 600; cursor: pointer; }
.tabs button.is-active { color: var(--text); border-bottom-color: var(--accent); }
.progress { height: 8px; background: var(--surface-3); border-radius: 4px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.rule { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 14px; align-items: center; padding: 14px 16px; border-top: 1px solid var(--border); }
.rule:first-child { border-top: 0; }
.rule-title { font-weight: 600; }
.rule-desc { color: var(--text-2); font-size: 13px; margin-top: 2px; }
.rule-status { font-size: 12px; margin-top: 6px; }
.rule.is-off .rule-title, .rule.is-off .rule-desc { color: var(--muted); }
.rule-form { background: var(--surface-2); border-top: 1px solid var(--border); padding: 16px; }
.sentence { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; line-height: 2.2; }
.sentence select, .sentence input { width: auto; }
.sentence input[type=number] { width: 110px; }
.events { max-height: 360px; overflow-y: auto; }
.event { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 10px; padding: 7px 16px; border-top: 1px solid var(--border); font-size: 13px; }
.event:first-child { border-top: 0; }
.event.error { background: var(--critical-wash); }
.event.warn { background: var(--warning-wash); }

/* ---------------------------------------------------------------- modal and toasts */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border: 1px solid var(--border-strong); border-radius: 12px; width: min(460px, 100%); box-shadow: 0 20px 60px rgba(0,0,0,.5); }
.modal h2 { padding: 18px 20px 0; font-size: 17px; }
.modal .body { padding: 12px 20px; color: var(--text-2); }
.modal .body strong { color: var(--text); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px 18px; }
.toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--surface-2); border: 1px solid var(--border-strong); border-left: 4px solid var(--good); padding: 10px 14px; border-radius: 8px; max-width: 380px; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
.toast.error { border-left-color: var(--critical); }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 10px 16px; gap: 10px; }
  .nav { order: 3; width: 100%; height: 40px; overflow-x: auto; }
  .sources { display: none; }
  .page { padding: 16px; }
  .ticker { grid-template-columns: 1fr 1fr; }
  .ticker > div:nth-child(2) { border-right: 0; }
  .ticker > div:nth-child(-n+2) { border-bottom: 1px solid var(--border); }
  .big { font-size: 22px; }
  .kpi-value { font-size: 21px; }
  .page-head { align-items: flex-start; }
}

/* ---------------------------------------------------------------- rolling windows (mining) */
.section-title { font-size: 15px; font-weight: 600; margin: 24px 0 10px; }
table.audit td:first-child { white-space: normal; min-width: 180px; }
table.audit td.r { min-width: 110px; }
table.audit tr.group td { padding-top: 14px; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; border-bottom: none; }
table.audit tr.strong td { font-weight: 600; }
table.audit .cell-sub.warn { color: var(--warning); }
table.audit tr.strong td .cell-sub { font-weight: 400; }
table.audit .group-hint { text-transform: none; letter-spacing: 0; font-size: 12px; }
table.audit tr.warn-row td { color: var(--warning); }

/* ---------------------------------------------------------------- login */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(340px, 100%); padding: 24px; display: flex; flex-direction: column; gap: 16px; }
.login-card .brand { font-size: 18px; }
.login-error { margin: 0; color: var(--critical); font-size: 13px; }
