/* ==========================================================================
   Kildaris metrics dashboard
   Inherits the site's "instrument panel" art direction: dark-committed,
   mono labels, cyan/violet accents. Dark only, deliberately — this is an
   internal tool that matches the product it measures.

   Chart colors are the validated categorical order (see charts in dash.js).
   Slots were chosen by running the palette validator against this surface
   (#0c1120), not by eye: worst adjacent CVD delta-E 16.0, normal-vision 25.0.
   ========================================================================== */

:root {
  --void:        #04060b;
  --bg:          #070a12;
  --surface:     #0c1120;
  --surface-2:   #121828;
  --surface-3:   #1a2235;
  --line:        #1f2942;
  --line-soft:   #161d31;
  --line-bright: #2d3a5c;

  --ink:    #e8edf8;
  --ink-2:  #a2b0cc;
  --ink-3:  #6a7996;
  --ink-4:  #46536e;

  --ember:     #22d3ee;
  --ember-hi:  #67e8f9;
  --ember-glow: rgba(34, 211, 238, 0.22);
  --signal:    #a855f7;

  /* validated categorical slots, in fixed order — never cycled */
  --s1: #0ea5c4;  --s2: #8b46e8;  --s3: #bd7f1c;  --s4: #4d8de8;
  --s5: #d9534f;  --s6: #7b7ae0;  --s7: #3fa46a;
  --s-other: #46536e;          /* the 8th+ series folds to this gray */

  /* status — reserved meaning, never used as a series color */
  --good: #34d399;
  --warn: #fbbf24;
  --bad:  #fb7185;

  --grid:     #161d31;
  --axis:     #1f2942;

  --sans: "Segoe UI Variable Display", "SF Pro Display", -apple-system,
          BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, "Cascadia Mono",
          "JetBrains Mono", Menlo, Consolas, monospace;
  --r: 4px;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 0.95rem;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--ember); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible { outline: 2px solid var(--ember-hi); outline-offset: 2px; }

.mono { font-family: var(--mono); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* --- login ---------------------------------------------------------------- */

.login-wrap {
  min-height: 100dvh;
  display: grid; place-items: center;
  padding: 2rem 1.25rem;
  background:
    radial-gradient(900px 500px at 50% -10%, rgba(34,211,238,0.09), transparent 60%),
    var(--bg);
}

.login-card {
  width: 100%; max-width: 25rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 2rem 1.75rem;
  box-shadow: 0 24px 70px -20px rgba(0,0,0,0.8);
}

.login-mark {
  font-family: var(--mono); font-size: 0.72rem; letter-spacing: 0.2em;
  text-transform: uppercase; color: var(--ember-hi);
  display: flex; align-items: center; gap: 0.7em; margin-bottom: 1.4rem;
}
.login-mark::before {
  content: ""; width: 1.6em; height: 1px; flex: none;
  background: linear-gradient(90deg, var(--ember), var(--signal));
}

.login-card h1 { margin: 0 0 0.4rem; font-size: 1.4rem; letter-spacing: -0.02em; }
.login-card .sub { margin: 0 0 1.6rem; color: var(--ink-3); font-size: 0.88rem; }

.field { margin-bottom: 1rem; }
.field label {
  display: block; font-family: var(--mono); font-size: 0.68rem;
  letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--ink-3); margin-bottom: 0.4rem;
}
.field input {
  width: 100%; padding: 0.7em 0.85em;
  background: var(--void); color: var(--ink);
  border: 1px solid var(--line-bright); border-radius: var(--r);
  font-family: var(--sans); font-size: 0.95rem;
}
.field input:focus { outline: none; border-color: var(--ember); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.55em;
  padding: 0.7em 1.3em;
  background: var(--ember); color: #04121a;
  border: 1px solid var(--ember); border-radius: var(--r);
  font-family: var(--mono); font-size: 0.74rem; font-weight: 500;
  letter-spacing: 0.08em; text-transform: uppercase; cursor: pointer;
  transition: background 0.16s, transform 0.16s;
}
.btn:hover { background: var(--ember-hi); transform: translateY(-1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn.ghost {
  background: transparent; color: var(--ink); border-color: var(--line-bright);
}
.btn.ghost:hover { background: var(--surface-2); }
.btn.full { width: 100%; }

.msg {
  margin-top: 1rem; padding: 0.65em 0.85em; border-radius: var(--r);
  font-size: 0.85rem; display: none;
}
.msg.show { display: block; }
.msg.err { background: rgba(251,113,133,0.1); border: 1px solid rgba(251,113,133,0.35); color: #ffc2cb; }

/* --- app shell ------------------------------------------------------------ */

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: 0.7rem clamp(1rem, 3vw, 1.8rem);
  background: rgba(7,10,18,0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  font-family: var(--mono); font-size: 0.72rem; letter-spacing: 0.2em;
  text-transform: uppercase; color: var(--ember-hi); white-space: nowrap;
}

.controls { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; margin-left: auto; }

select, .ctl {
  padding: 0.45em 0.7em;
  background: var(--surface-2); color: var(--ink);
  border: 1px solid var(--line-bright); border-radius: var(--r);
  font-family: var(--mono); font-size: 0.72rem; letter-spacing: 0.04em;
  cursor: pointer;
}
select:focus { outline: none; border-color: var(--ember); }

.toggle {
  display: inline-flex; align-items: center; gap: 0.45em;
  padding: 0.45em 0.7em;
  background: var(--surface-2); border: 1px solid var(--line-bright);
  border-radius: var(--r);
  font-family: var(--mono); font-size: 0.7rem; letter-spacing: 0.04em;
  color: var(--ink-2); cursor: pointer; user-select: none;
}
.toggle input { accent-color: var(--ember); margin: 0; }
.toggle.on { border-color: var(--ember); color: var(--ink); }

/* --- tabs ----------------------------------------------------------------- */

.tabs {
  display: flex; gap: 0.15rem; overflow-x: auto;
  padding: 0 clamp(1rem, 3vw, 1.8rem);
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 3.1rem; z-index: 30;
  scrollbar-width: thin;
}
.tab {
  flex: none;
  padding: 0.75em 1em; background: none; border: 0;
  border-bottom: 2px solid transparent;
  color: var(--ink-3); cursor: pointer;
  font-family: var(--mono); font-size: 0.72rem;
  letter-spacing: 0.1em; text-transform: uppercase;
  white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ember); border-bottom-color: var(--ember); }

main { padding: clamp(1rem, 3vw, 1.8rem); max-width: 1600px; margin-inline: auto; }

.panel { display: none; }
.panel.active { display: block; }

/* --- cards & grid --------------------------------------------------------- */

.grid { display: grid; gap: 1rem; }
.grid.k2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 24rem), 1fr)); }
.grid.k3 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 19rem), 1fr)); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 1.1rem 1.2rem 1.25rem;
  min-width: 0;
}
.card > h2 {
  margin: 0 0 0.15rem; font-size: 0.95rem; font-weight: 600; letter-spacing: -0.01em;
}
.card > .hint {
  margin: 0 0 1rem; color: var(--ink-3); font-size: 0.78rem;
}
.card > h2 + .table-wrap, .card > h2 + .chart { margin-top: 1rem; }

/* --- stat tiles ----------------------------------------------------------- */

.tiles {
  display: grid; gap: 0.75rem;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 10.5rem), 1fr));
  margin-bottom: 1rem;
}
.tile {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r); padding: 0.9rem 1rem;
}
.tile .k {
  font-family: var(--mono); font-size: 0.65rem; letter-spacing: 0.13em;
  text-transform: uppercase; color: var(--ink-3); margin-bottom: 0.3rem;
}
.tile .v { font-size: 1.75rem; font-weight: 600; letter-spacing: -0.03em; line-height: 1.1; }
.tile .d {
  font-family: var(--mono); font-size: 0.7rem; margin-top: 0.25rem;
  display: flex; align-items: center; gap: 0.3em; color: var(--ink-3);
}
/* Delta cues pair an arrow glyph with the number, so direction never rests
   on color alone. */
.tile .d.up   { color: var(--good); }
.tile .d.down { color: var(--bad); }
.tile .d.flat { color: var(--ink-3); }

/* --- charts --------------------------------------------------------------- */

.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }

.axis-line { stroke: var(--axis); stroke-width: 1; }
.grid-line { stroke: var(--grid); stroke-width: 1; }
.tick-label {
  fill: var(--ink-3); font-family: var(--mono); font-size: 10px;
}
.series-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.series-dot  { stroke: var(--surface); stroke-width: 2; }

.legend {
  display: flex; flex-wrap: wrap; gap: 0.9rem;
  margin-top: 0.7rem;
  font-family: var(--mono); font-size: 0.7rem; color: var(--ink-2);
}
.legend .sw {
  display: inline-block; width: 10px; height: 10px; border-radius: 2px;
  margin-right: 0.45em; vertical-align: -1px;
}

/* crosshair + tooltip */
.xhair { stroke: var(--line-bright); stroke-width: 1; stroke-dasharray: 3 3; }
.tip {
  position: absolute; pointer-events: none; z-index: 20;
  background: var(--surface-3); border: 1px solid var(--line-bright);
  border-radius: var(--r); padding: 0.5em 0.7em;
  font-family: var(--mono); font-size: 0.7rem; color: var(--ink);
  white-space: nowrap; opacity: 0;
  transition: opacity 0.1s; box-shadow: 0 10px 30px -10px rgba(0,0,0,0.9);
}
.tip.show { opacity: 1; }
.tip .tk { color: var(--ink-3); }

/* --- ranked bar rows ------------------------------------------------------ */
/* A ranked list is nominal, so every bar takes the same slot-1 hue: bar length
   already encodes the value and spending the identity channel on it again
   would be noise. */
.bars { display: grid; gap: 0.5rem; }
.bar-row {
  display: grid; grid-template-columns: minmax(6rem, 12rem) 1fr auto;
  gap: 0.7rem; align-items: center;
  font-size: 0.82rem;
}
.bar-row .nm {
  color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.bar-row .tr { background: var(--surface-2); border-radius: 2px; height: 0.7rem; position: relative; }
.bar-row .fl {
  position: absolute; inset: 0 auto 0 0;
  background: var(--s1);
  border-radius: 2px 4px 4px 2px;   /* rounded data-end, square at the baseline */
  min-width: 2px;
}
.bar-row .vl {
  font-family: var(--mono); font-size: 0.76rem; color: var(--ink);
  font-variant-numeric: tabular-nums; text-align: right; min-width: 3.5rem;
}
.bar-row:hover .nm { color: var(--ink); }

/* --- tables --------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
th, td {
  padding: 0.5em 0.7em; text-align: left;
  border-bottom: 1px solid var(--line-soft);
  white-space: nowrap;
}
th {
  font-family: var(--mono); font-size: 0.65rem; letter-spacing: 0.11em;
  text-transform: uppercase; color: var(--ink-3); font-weight: 500;
  position: sticky; top: 0; background: var(--surface); z-index: 1;
  cursor: pointer; user-select: none;
}
th:hover { color: var(--ink); }
th.sorted::after { content: " ▾"; color: var(--ember); }
th.sorted.asc::after { content: " ▴"; }
td.num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-2); }
td.wide { white-space: normal; min-width: 14rem; }

.pill {
  display: inline-block; padding: 0.12em 0.55em; border-radius: 999px;
  font-family: var(--mono); font-size: 0.65rem; letter-spacing: 0.05em;
  border: 1px solid var(--line-bright); color: var(--ink-2);
}
.pill.biz { border-color: rgba(52,211,153,0.4); color: var(--good); }
.pill.isp { border-color: var(--line); color: var(--ink-4); }

.empty {
  padding: 2.5rem 1rem; text-align: center; color: var(--ink-3);
  font-family: var(--mono); font-size: 0.78rem; letter-spacing: 0.06em;
}

.live-dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--good); margin-right: 0.5em;
  animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.25; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important; animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

.loading { opacity: 0.45; pointer-events: none; }

footer.foot {
  padding: 1.5rem clamp(1rem, 3vw, 1.8rem) 3rem;
  color: var(--ink-4); font-family: var(--mono); font-size: 0.68rem;
  letter-spacing: 0.06em;
}

/* ==========================================================================
   Modals
   ========================================================================== */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(4, 6, 11, 0.78);
  backdrop-filter: blur(6px);
  display: grid; place-items: center;
  padding: clamp(1rem, 4vw, 3rem);
  opacity: 0; pointer-events: none;
  transition: opacity 0.18s ease;
}
.modal-backdrop.open { opacity: 1; pointer-events: auto; }

.modal {
  width: 100%; max-width: 60rem; max-height: 88dvh;
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line-bright);
  border-radius: var(--r);
  box-shadow: 0 40px 90px -24px rgba(0,0,0,0.9);
  transform: translateY(10px) scale(0.99);
  transition: transform 0.2s var(--ease-out, cubic-bezier(0.16,1,0.3,1));
  overflow: hidden;
}
.modal-backdrop.open .modal { transform: none; }
.modal.wide { max-width: 78rem; }
.modal.narrow { max-width: 30rem; }

.modal-head {
  display: flex; align-items: flex-start; gap: 1rem;
  padding: 1.1rem 1.25rem 0.9rem;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.modal-head h3 { margin: 0; font-size: 1.05rem; letter-spacing: -0.015em; }
.modal-head .sub {
  margin: 0.2rem 0 0; color: var(--ink-3); font-size: 0.8rem;
  font-family: var(--mono); letter-spacing: 0.04em;
}
.modal-x {
  margin-left: auto; flex: none;
  background: none; border: 1px solid var(--line-bright); border-radius: var(--r);
  color: var(--ink-2); cursor: pointer;
  width: 2rem; height: 2rem; line-height: 1;
  font-size: 1.05rem; display: grid; place-items: center;
}
.modal-x:hover { background: var(--surface-2); color: var(--ink); }

.modal-body { padding: 1.25rem; overflow-y: auto; flex: 1 1 auto; }
.modal-foot {
  display: flex; gap: 0.6rem; justify-content: flex-end; flex-wrap: wrap;
  padding: 0.9rem 1.25rem;
  border-top: 1px solid var(--line);
  background: var(--void); flex: none;
}

@media (prefers-reduced-motion: reduce) {
  .modal-backdrop, .modal { transition: none; }
}

/* --- key/value grid inside modals ---------------------------------------- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 0.4rem 1rem; font-size: 0.85rem; }
.kv dt {
  font-family: var(--mono); font-size: 0.66rem; letter-spacing: 0.11em;
  text-transform: uppercase; color: var(--ink-3); padding-top: 0.15rem;
}
.kv dd { margin: 0; color: var(--ink); overflow-wrap: anywhere; }

.modal-section { margin-top: 1.4rem; }
.modal-section:first-child { margin-top: 0; }
.modal-section > h4 {
  margin: 0 0 0.7rem; font-size: 0.72rem; font-family: var(--mono);
  letter-spacing: 0.13em; text-transform: uppercase; color: var(--ink-3);
  font-weight: 500;
}

/* --- tiles become interactive -------------------------------------------- */
.tile.clickable {
  cursor: pointer; text-align: left; width: 100%;
  font: inherit; color: inherit;
  transition: border-color 0.16s, transform 0.16s, background 0.16s;
}
.tile.clickable:hover {
  border-color: var(--ember); transform: translateY(-2px);
  background: var(--surface-2);
}
.tile.clickable:focus-visible { border-color: var(--ember-hi); }
.tile .spark { margin-top: 0.5rem; height: 26px; }
.tile .spark svg { width: 100%; height: 26px; display: block; overflow: visible; }

tr.clickable { cursor: pointer; }
tr.clickable:hover td:first-child { color: var(--ember); }

/* --- toasts --------------------------------------------------------------- */
.toasts {
  position: fixed; right: 1rem; bottom: 1rem; z-index: 200;
  display: flex; flex-direction: column; gap: 0.5rem; max-width: 24rem;
}
.toast {
  background: var(--surface-3); border: 1px solid var(--line-bright);
  border-left: 3px solid var(--ember);
  border-radius: var(--r); padding: 0.7em 0.9em;
  font-size: 0.84rem; color: var(--ink);
  box-shadow: 0 16px 40px -14px rgba(0,0,0,0.9);
  animation: toast-in 0.22s ease;
}
.toast.good { border-left-color: var(--good); }
.toast.bad  { border-left-color: var(--bad); }
@keyframes toast-in { from { opacity: 0; transform: translateX(16px); } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

/* --- insights ------------------------------------------------------------- */
.insight {
  border: 1px solid var(--line); border-left-width: 3px;
  border-radius: var(--r); background: var(--surface);
  padding: 0.95rem 1.1rem; margin-bottom: 0.75rem;
}
.insight.critical { border-left-color: var(--bad); }
.insight.warning  { border-left-color: var(--warn); }
.insight.info     { border-left-color: var(--s4); }
.insight.good     { border-left-color: var(--good); }

.insight-head { display: flex; align-items: baseline; gap: 0.7rem; flex-wrap: wrap; }
.insight-head h3 { margin: 0; font-size: 0.95rem; font-weight: 600; }
.sev {
  font-family: var(--mono); font-size: 0.6rem; letter-spacing: 0.12em;
  text-transform: uppercase; padding: 0.16em 0.5em; border-radius: 2px;
  border: 1px solid currentColor;
}
.sev.critical { color: var(--bad); }
.sev.warning  { color: var(--warn); }
.sev.info     { color: var(--s4); }
.sev.good     { color: var(--good); }
.insight-cat {
  font-family: var(--mono); font-size: 0.62rem; letter-spacing: 0.11em;
  text-transform: uppercase; color: var(--ink-4); margin-left: auto;
}
.insight p { margin: 0.5rem 0 0; font-size: 0.86rem; color: var(--ink-2); max-width: 80ch; }
.insight .fix {
  margin-top: 0.6rem; padding: 0.6rem 0.75rem;
  background: var(--void); border-radius: var(--r);
  font-size: 0.83rem; color: var(--ink);
}
.insight .fix b {
  display: block; font-family: var(--mono); font-size: 0.62rem;
  letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-3);
  margin-bottom: 0.3rem; font-weight: 500;
}
.insight .ev {
  margin-top: 0.5rem; font-family: var(--mono); font-size: 0.72rem; color: var(--ink-3);
}

.score-ring { display: flex; align-items: center; gap: 1.2rem; flex-wrap: wrap; }
.score-ring svg { flex: none; }
.score-ring .n { font-size: 2.2rem; font-weight: 600; letter-spacing: -0.03em; }

/* --- user admin ----------------------------------------------------------- */
.pw-meter { height: 4px; border-radius: 2px; background: var(--surface-3); margin-top: 0.45rem; overflow: hidden; }
.pw-meter i { display: block; height: 100%; width: 0; transition: width 0.2s, background 0.2s; }
.field .hint { font-size: 0.74rem; color: var(--ink-3); margin-top: 0.3rem; }
.danger { --btn-bg: var(--bad); border-color: var(--bad); color: #2a0710; }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #2a0710; }
.btn.danger:hover { background: #ff8fa0; }
