/* Kairos dashboard.
 *
 * Light is the base and dark is applied over it, under two scopes: the media
 * query follows the OS, and the `data-theme` attribute follows the toggle. The
 * `:where(:not(...))` guard keeps the toggle winning both directions - without
 * it, choosing light on an OS set to dark would have no effect.
 *
 * Both step sets come from the same validated ramps (light surface #fcfcfb,
 * dark #1a1a19). Dark is a selected set of steps, not an inverted copy.
 * Charts here are single-series, so there is no categorical palette to keep
 * apart; the one place hues sit together is the status chips, and each carries
 * a dot and a word so colour is never the only signal.
 */

:root {
  color-scheme: light;

  --page:           #f2f1ee;
  --surface-1:      #fcfcfb;
  --surface-2:      #f0efec;
  --surface-3:      #e4e3de;
  --line:           #dedcd6;
  --line-strong:    #c6c4bc;

  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #77756e;

  /* Marks only - all body text stays near-black in this mode. A dark forest
     green rather than the ramp's brighter greens: 6.09:1 on the light surface
     as a mark, and 6.25:1 for white text sitting on it as a button fill.
     It measures ΔE 17.3 from the status "good" green, clear of the 15 floor,
     so a chart bar cannot be mistaken for a health indicator. It is close to
     status "critical" red under protanopia (ΔE 2.8), which is tolerable only
     because the two never share a chart and every status chip carries a dot
     and a word as well as its colour. */
  --series-1:       #0e6f3c;
  --series-1-dim:   #0d5f35;
  /* Sits behind near-black text in the score column, so it has to stay pale. */
  --series-1-wash:  #cfe9d9;

  --good:           #0ca30c;
  --warning:        #fab219;
  --serious:        #ec835a;
  --critical:       #d03b3b;

  /* Value text, not marks: these need text contrast, which the status steps
     do not have on a light surface. */
  --pos-text:       #006300;
  --neg-text:       #a82121;
  --chip-ok:        #056605;
  --chip-warn:      #8a5a00;
  --chip-bad:       #a82121;

  --console-bg:     #f7f7f4;
  --grid:           #e6e5e0;
  --row-line:       #efeeea;
  --row-selected:   #e2ecf9;
  --shadow:         rgba(20, 20, 18, .18);

  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* One block, applied under both scopes. */
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page:           #121211;
    --surface-1:      #1a1a19;
    --surface-2:      #212120;
    --surface-3:      #2a2a28;
    --line:           #34342f;
    --line-strong:    #45453e;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #8b8a80;
    --series-1:       #3987e5;
    --series-1-dim:   #1c5cab;
    --series-1-wash:  #1c5cab;
    --pos-text:       #7fe07f;
    --neg-text:       #ff9a9a;
    --chip-ok:        #7fe07f;
    --chip-warn:      #ffd479;
    --chip-bad:       #ff9a9a;
    --console-bg:     #101010;
    --grid:           #2b2b28;
    --row-line:       #232320;
    --row-selected:   #1e2a3a;
    --shadow:         rgba(0, 0, 0, .55);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page:           #121211;
  --surface-1:      #1a1a19;
  --surface-2:      #212120;
  --surface-3:      #2a2a28;
  --line:           #34342f;
  --line-strong:    #45453e;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #8b8a80;
  --series-1:       #3987e5;
  --series-1-dim:   #1c5cab;
  --series-1-wash:  #1c5cab;
  --pos-text:       #7fe07f;
  --neg-text:       #ff9a9a;
  --chip-ok:        #7fe07f;
  --chip-warn:      #ffd479;
  --chip-bad:       #ff9a9a;
  --console-bg:     #101010;
  --grid:           #2b2b28;
  --row-line:       #232320;
  --row-selected:   #1e2a3a;
  --shadow:         rgba(0, 0, 0, .55);
}

* { box-sizing: border-box; }

/* Grid and flex items default to min-width:auto, which means a long number or
   a wide table refuses to shrink and pushes the whole page sideways. Every
   container that holds one has to opt out explicitly. */
.cols > *, .tiles > *, header > *, .side > *, .panel > *, main > * { min-width: 0; }
.chips, .controls, .brand, main, header { min-width: 0; max-width: 100%; }

body {
  margin: 0;
  /* The wallet table scrolls inside its own panel; nothing should make the
     document itself scroll sideways. */
  overflow-x: hidden;
  background: var(--page);
  color: var(--text-primary);
  font: 13px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------------- header */

header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}

.brand { font-weight: 650; letter-spacing: .3px; display: flex; align-items: center; gap: 8px; }
.brand b { letter-spacing: 1.6px; font-weight: 700; }
.brand span { color: var(--text-muted); font-weight: 400; }
/* Inlined rather than an <img>: an external SVG cannot see the page's custom
   properties, so it would stay blue when the theme toggle says light-green. */
.logo { display: block; flex: none; overflow: visible; }
.logo path {
  fill: none;
  stroke: var(--series-1-dim);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.logo path.lead { stroke: var(--series-1); }
.logo .dot { fill: var(--series-1); }
.who { color: var(--text-muted); font: 11px var(--mono); margin-left: 6px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }

/* Status is never colour alone: every chip carries a glyph and a word. */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font: 11px/1 var(--mono);
  color: var(--text-secondary);
  white-space: nowrap;
}
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); }
.chip.ok    { color: var(--chip-ok); } .chip.ok .dot    { background: var(--good); }
.chip.warn  { color: var(--chip-warn); } .chip.warn .dot  { background: var(--warning); }
.chip.bad   { color: var(--chip-bad); } .chip.bad .dot   { background: var(--critical); }
.chip.idle  { color: var(--text-muted); }

.spacer { flex: 1; }

.controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.controls label { color: var(--text-muted); font: 11px/1 var(--mono); }

input[type="number"] {
  width: 62px;
  padding: 4px 6px;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  color: var(--text-primary);
  font: 12px var(--mono);
}
input[type="checkbox"] { accent-color: var(--series-1); }

button {
  padding: 5px 11px;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  color: var(--text-primary);
  font: 12px var(--sans);
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--surface-3); border-color: var(--text-muted); }
button:disabled { opacity: .4; cursor: not-allowed; }
button.primary { background: var(--series-1-dim); border-color: var(--series-1); }
button.primary:hover:not(:disabled) { background: var(--series-1); }
button.danger { border-color: color-mix(in srgb, var(--critical) 50%, var(--line-strong)); }
button.danger:hover:not(:disabled) { background: color-mix(in srgb, var(--critical) 12%, var(--surface-2)); }
button.tiny, .linkbtn { padding: 2px 7px; font-size: 11px; }

/* An anchor styled as a button: it is a real link, so middle-click and
   "copy link address" work, which they would not on a button with an onclick. */
.linkbtn {
  display: inline-flex;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: var(--sans);
  line-height: 1.5;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.linkbtn:hover { background: var(--surface-3); border-color: var(--series-1); }

/* Segmented control: theme and language pickers. The selected option is
   marked by fill *and* stays a button with a title, so the state is not
   carried by a colour difference alone. */
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 5px; overflow: hidden; }
.seg button {
  border: none;
  border-radius: 0;
  border-left: 1px solid var(--line-strong);
  padding: 3px 8px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1.6;
}
.seg button:first-child { border-left: none; }
.seg button.on { background: var(--series-1-dim); color: #fff; }
.seg button:hover:not(.on) { background: var(--surface-3); color: var(--text-primary); }

.loginprefs { display: flex; justify-content: center; gap: 8px; margin-top: 14px; }

a.ext { color: var(--series-1); text-decoration: none; }
a.ext:hover { text-decoration: underline; }

/* ------------------------------------------------------------------ grid */

main { padding: 14px 16px 28px; display: grid; gap: 14px; }
.side { display: grid; gap: 14px; align-content: start; }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.tile {
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
}
.tile .label {
  font: 10px/1.25 var(--mono);
  letter-spacing: .6px;
  text-transform: uppercase;
  color: var(--text-muted);
  /* Room for two lines whether or not this label needs them: Turkish wraps
     where English does not, and without the reservation the numbers in one
     row of tiles stop lining up. */
  min-height: 25px;
}
.tile .value {
  font: 600 22px/1.2 var(--mono);
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
}
.tile .sub { font: 11px var(--mono); color: var(--text-muted); margin-top: 2px; }
.tile .value.pos { color: var(--pos-text); }
.tile .value.neg { color: var(--neg-text); }

.cols { display: grid; grid-template-columns: 1.85fr 1fr; gap: 14px; }
@media (max-width: 1150px) { .cols { grid-template-columns: 1fr; } }

.panel {
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel > h2 {
  margin: 0;
  padding: 9px 12px;
  font: 600 12px/1 var(--sans);
  letter-spacing: .3px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  display: flex;
  align-items: center;
  gap: 8px;
}
.panel > h2 .count { color: var(--text-muted); font-weight: 400; font-family: var(--mono); }
.panel > h2 .spacer { flex: 1; }
.panel .body { padding: 12px; }
.panel .body.flush { padding: 0; }

.note {
  padding: 14px 12px;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.6;
}
.note strong { color: var(--text-secondary); font-weight: 600; }

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

table { width: 100%; border-collapse: collapse; font: 12px var(--mono); }
th {
  text-align: right;
  padding: 7px 10px;
  font: 10px/1 var(--mono);
  letter-spacing: .5px;
  text-transform: uppercase;
  color: var(--text-muted);
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  position: sticky;
  top: 0;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
th:first-child, td:first-child { text-align: left; }
th.sorted { color: var(--text-primary); }
th .arrow { opacity: .55; margin-left: 3px; }
td {
  padding: 6px 10px;
  text-align: right;
  border-bottom: 1px solid var(--row-line);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tbody tr.selected { background: var(--row-selected); }
/* Grows to whatever the row gives it rather than stopping at a fixed height:
 * a hard max-height leaves a half-drawn row above dead space whenever the
 * neighbouring column is taller. */
.scroll { overflow: auto; min-height: 0; flex: 1; }
.panel .body.flush.scroll { max-height: min(66vh, 720px); }

.addr { font-family: var(--mono); color: var(--text-secondary); }
.rank { color: var(--text-muted); width: 28px; }
.pos { color: var(--pos-text); }
.neg { color: var(--neg-text); }
.dim { color: var(--text-muted); }

/* Score cell: a bar behind the number, so magnitude reads without a chart. */
.scorecell { position: relative; }
.scorecell .bar {
  position: absolute;
  left: 0; top: 3px; bottom: 3px;
  background: var(--series-1-wash);
  border-radius: 2px;
  opacity: .55;
}
.scorecell .num { position: relative; font-weight: 600; }

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

.chart { width: 100%; display: block; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-text { fill: var(--text-muted); font: 10px var(--mono); }
.chart .bar-fill { fill: var(--series-1); }
.chart .bar-fill:hover { fill: var(--series-1-dim); }
.chart .value-text { fill: var(--text-secondary); font: 10px var(--mono); }
.chart .spark-line { fill: none; stroke: var(--series-1); stroke-width: 2; }
.chart .spark-dot { fill: var(--series-1); }

/* --------------------------------------------------------------- console */

.console {
  height: 230px;
  overflow: auto;
  background: var(--console-bg);
  font: 11.5px/1.55 var(--mono);
  padding: 8px 12px;
}
.console div { white-space: pre-wrap; word-break: break-all; }
.console .t { color: var(--text-muted); margin-right: 8px; }
.console .finder { color: var(--text-secondary); }
.console .system { color: var(--series-1); }
.console .error  { color: var(--neg-text); }

/* ---------------------------------------------------------------- drawer */

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(560px, 94vw);
  background: var(--surface-1);
  border-left: 1px solid var(--line-strong);
  box-shadow: -18px 0 40px var(--shadow);
  transform: translateX(100%);
  transition: transform .18s ease-out;
  z-index: 30;
  display: flex;
  flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer header {
  position: static;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  gap: 6px;
  flex-wrap: nowrap;
  padding: 8px 10px;
}
/* The title yields before the controls do - an address is already truncated,
   whereas a wrapped close button lands on its own line. */
.drawer header .brand {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
  font-size: 12px;
}
.drawer header .spacer { flex: 0 1 auto; min-width: 4px; }
.drawer .content { overflow: auto; padding: 14px; display: grid; gap: 14px; }

.kv { display: grid; grid-template-columns: 148px 1fr; gap: 4px 12px; font: 12px var(--mono); }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }

.reasons { display: flex; flex-wrap: wrap; gap: 6px; }
.reason {
  background: color-mix(in srgb, var(--critical) 14%, var(--surface-1));
  border: 1px solid color-mix(in srgb, var(--critical) 45%, var(--surface-1));
  color: var(--neg-text);
  border-radius: 4px;
  padding: 2px 7px;
  font: 11px var(--mono);
}

.bars { display: grid; gap: 7px; }
.barrow { display: grid; grid-template-columns: 90px 1fr 46px; align-items: center; gap: 8px; }
.barrow .name { font: 11px var(--mono); color: var(--text-secondary); }
.barrow .track { height: 9px; background: var(--surface-3); border-radius: 3px; position: relative; }
/* Zero sits at the centre: components are signed, so a bar to the left is
 * genuinely different from a short bar to the right. */
.barrow .fill { position: absolute; top: 0; bottom: 0; border-radius: 3px; background: var(--series-1); }
.barrow .fill.neg { background: var(--critical); }
/* Zero is the reference the signed bars are read against, so it has to be
 * visible against the track rather than merged into it. */
.barrow .zero { position: absolute; top: -3px; bottom: -3px; width: 1px; background: var(--text-muted); left: 50%; }
.barrow .track { overflow: visible; }
.barrow .val { font: 11px var(--mono); color: var(--text-secondary); text-align: right; }

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

/* Covers the app rather than living on its own route: there is no partly
   authenticated state to render, and no redirect to get wrong. */
.login {
  position: fixed;
  inset: 0;
  background: var(--page);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 60;
}
.login.show { display: flex; }

.loginbox {
  width: 320px;
  display: grid;
  gap: 8px;
  padding: 28px 26px 24px;
  background: var(--surface-1);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 22px 60px var(--shadow);
  justify-items: stretch;
}
.loginbox img { justify-self: center; }
.loginbox h1 {
  margin: 4px 0 0;
  font: 700 20px/1 var(--sans);
  letter-spacing: 2.4px;
  text-align: center;
}
.loginbox .tagline {
  margin: 0 0 12px;
  text-align: center;
  color: var(--text-muted);
  font: 11px var(--mono);
}
.loginbox label {
  font: 10px/1 var(--mono);
  letter-spacing: .6px;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-top: 4px;
}
.loginbox input {
  padding: 8px 10px;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  color: var(--text-primary);
  font: 13px var(--mono);
  width: 100%;
}
.loginbox input:focus {
  outline: none;
  border-color: var(--series-1);
}
.loginbox button { margin-top: 14px; padding: 9px; font-size: 13px; }
.loginerr {
  min-height: 16px;
  color: var(--neg-text);
  font: 11px var(--mono);
  margin-top: 6px;
}

.toast {
  position: fixed;
  bottom: 18px; left: 50%;
  transform: translateX(-50%);
  background: var(--surface-3);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 9px 16px;
  font: 12px var(--mono);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 40;
}
.toast.show { opacity: 1; }

/* ----------------------------------------------------------------- mobile */

/* Two breakpoints, both about what stops fitting rather than about devices.
 * At 1150px the two-column layout collapses; at 760px the header controls stop
 * sharing a line with the brand and the table sheds the columns that are
 * diagnostics rather than the answer. */

@media (max-width: 760px) {
  header {
    gap: 8px;
    padding: 8px 10px;
    align-items: flex-start;
  }
  header .spacer { display: none; }
  .brand { width: 100%; }
  .brand span { display: none; }
  .chips { width: 100%; gap: 5px; flex-wrap: wrap; }
  .chip { font-size: 10px; padding: 3px 7px; }
  .controls { width: 100%; gap: 5px; }
  .controls label[for] { display: none; }
  .hide-sm { display: none; }
  input[type="number"] { width: 54px; }
  button { padding: 6px 10px; }

  main { padding: 10px 10px 24px; gap: 10px; }
  .tiles { grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 7px; }
  .tile .value { overflow: hidden; text-overflow: ellipsis; }
  .tile { padding: 8px 9px; }
  .tile .value { font-size: 18px; }
  .tile .sub { display: none; }

  /* The columns that survive answer "who and how good"; raw score, effective
     count and last-seen are for reading a wallet closely, which is what the
     drawer is for. */
  #top-table th:nth-child(4), #top-table td:nth-child(4),
  #top-table th:nth-child(8), #top-table td:nth-child(8),
  #top-table th:nth-child(10), #top-table td:nth-child(10) { display: none; }
  th, td { padding: 6px 7px; font-size: 11px; }

  .console { height: 180px; font-size: 11px; }
  .panel .body.flush.scroll { max-height: 60vh; }

  .drawer { width: 100vw; border-left: none; }
  .drawer header { flex-wrap: wrap; }
  .kv { grid-template-columns: 116px 1fr; font-size: 11px; }
  .barrow { grid-template-columns: 72px 1fr 40px; }

  .loginbox { width: min(340px, calc(100vw - 32px)); }
}

/* Coarse pointers need a bigger target than a 6px-tall row gives them. */
@media (pointer: coarse) {
  tbody tr.clickable td { padding-top: 10px; padding-bottom: 10px; }
  button, .linkbtn { min-height: 32px; }
}

@media (prefers-reduced-motion: reduce) {
  .drawer { transition: none; }
  .toast { transition: none; }
}
