/* ============================================================
   Resting Signal — design tokens
   Light palette is the base; dark redefines tokens only.
   ============================================================ */
:root {
  color-scheme: light;

  --plane:        #f2f2ef;
  --surface:      #fbfbf9;
  --surface-2:    #f6f6f2;
  --surface-sunk: #ecece7;

  --ink:          #101211;
  --ink-2:        #53574e;
  --muted:        #83877d;

  --grid:         #e3e3db;
  --axis:         #c6c7be;
  --border:       rgba(16, 18, 17, 0.11);
  --border-firm:  rgba(16, 18, 17, 0.20);

  --accent:       #0f7d56;   /* text + fills that carry text */
  --accent-mark:  #1baf7a;   /* chart marks */
  --accent-wash:  rgba(27, 175, 122, 0.10);

  --good:         #0ca30c;
  --warning:      #fab219;
  --serious:      #ec835a;
  --critical:     #d03b3b;
  --good-ink:     #006300;
  --critical-ink: #ab2727;
  --warning-ink:  #8a6100;

  /* sleep stages — validated adjacent order (deep, rem, light, awake) */
  --s-deep:  #4a3aa7;
  --s-rem:   #1baf7a;
  --s-light: #2a78d6;
  --s-awake: #eb6834;
  /* general series */
  --s1: #2a78d6;
  --s2: #eb6834;

  --shadow-card: 0 1px 2px rgba(16,18,17,.05), 0 6px 16px -10px rgba(16,18,17,.18);

  --fs-xs:   0.6875rem;
  --fs-sm:   0.8125rem;
  --fs-base: 0.9375rem;
  --fs-md:   1.0625rem;
  --fs-lg:   1.3125rem;
  --fs-xl:   1.875rem;

  --ui:   "Archivo", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --prose:"Source Serif 4", ui-serif, Georgia, "Times New Roman", serif;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;

  --gutter: 16px;
  --tabbar-h: 58px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --plane:        #0d0e0f;
    --surface:      #16181a;
    --surface-2:    #1c1f21;
    --surface-sunk: #101213;
    --ink:          #f4f5f2;
    --ink-2:        #b8bcb2;
    --muted:        #83867d;
    --grid:         #262a2c;
    --axis:         #383c3e;
    --border:       rgba(255, 255, 255, 0.11);
    --border-firm:  rgba(255, 255, 255, 0.22);
    --accent:       #3fcb95;
    --accent-mark:  #199e70;
    --accent-wash:  rgba(63, 203, 149, 0.12);
    --good-ink:     #0ca30c;
    --critical-ink: #e06a6a;
    --warning-ink:  #e0a52f;
    --s-deep:  #9085e9;
    --s-rem:   #199e70;
    --s-light: #3987e5;
    --s-awake: #d95926;
    --s1: #3987e5;
    --s2: #d95926;
    --shadow-card: 0 1px 2px rgba(0,0,0,.4), 0 8px 20px -12px rgba(0,0,0,.7);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --plane:        #0d0e0f;
  --surface:      #16181a;
  --surface-2:    #1c1f21;
  --surface-sunk: #101213;
  --ink:          #f4f5f2;
  --ink-2:        #b8bcb2;
  --muted:        #83867d;
  --grid:         #262a2c;
  --axis:         #383c3e;
  --border:       rgba(255, 255, 255, 0.11);
  --border-firm:  rgba(255, 255, 255, 0.22);
  --accent:       #3fcb95;
  --accent-mark:  #199e70;
  --accent-wash:  rgba(63, 203, 149, 0.12);
  --good-ink:     #0ca30c;
  --critical-ink: #e06a6a;
  --warning-ink:  #e0a52f;
  --s-deep:  #9085e9;
  --s-rem:   #199e70;
  --s-light: #3987e5;
  --s-awake: #d95926;
  --s1: #3987e5;
  --s2: #d95926;
  --shadow-card: 0 1px 2px rgba(0,0,0,.4), 0 8px 20px -12px rgba(0,0,0,.7);
}

/* ============================================================
   Reset & base
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--plane);
  color: var(--ink);
  font-family: var(--ui);
  font-size: var(--fs-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
img { max-width: 100%; display: block; }
[hidden] { display: none !important; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; text-wrap: balance; letter-spacing: -0.012em; }
p { margin: 0; }
a { color: var(--accent); text-underline-offset: 2px; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

.num { font-variant-numeric: tabular-nums; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ============================================================
   Connect / setup screen
   ============================================================ */
.connect {
  min-height: 100svh;
  padding-block: calc(env(safe-area-inset-top, 0px) + 28px) calc(env(safe-area-inset-bottom, 0px) + 40px);
  padding-inline: var(--gutter);
  background: var(--plane);
}
.connect-inner { max-width: 34rem; margin: 0 auto; }
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 20px; }
.brand-mark { border-radius: 13px; width: 56px; height: 56px; }
.brand h1 { font-size: var(--fs-lg); letter-spacing: -0.02em; }
.brand-sub { color: var(--muted); font-size: var(--fs-sm); }
.lede {
  font-family: var(--prose); font-size: var(--fs-md); line-height: 1.55;
  color: var(--ink-2); margin-bottom: 28px; max-width: 62ch;
  padding: 14px 16px; background: var(--accent-wash);
  border-left: 2px solid var(--accent-mark); border-radius: 0 6px 6px 0;
}
.steps { list-style: none; counter-reset: step; margin: 0; padding: 0; display: grid; gap: 30px; }
.steps > li { counter-increment: step; position: relative; padding-left: 38px; }
.steps > li::before {
  content: counter(step);
  position: absolute; left: 0; top: 1px;
  width: 25px; height: 25px; border-radius: 50%;
  background: var(--ink); color: var(--surface);
  display: grid; place-items: center;
  font-family: var(--mono); font-size: var(--fs-xs); font-weight: 500;
}
.steps h2 { font-size: var(--fs-md); margin-bottom: 6px; }
.steps p { color: var(--ink-2); font-size: var(--fs-sm); margin-bottom: 10px; }
.fieldmap { margin: 14px 0 0; display: grid; gap: 12px; }
.fieldmap dt {
  font-family: var(--mono); font-size: var(--fs-xs); font-weight: 500;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
.fieldmap dd { margin: 3px 0 0; display: grid; gap: 4px; }
.fieldmap dd b { font-weight: 600; font-size: var(--fs-base); }
.why { color: var(--muted); font-size: var(--fs-xs); line-height: 1.45; }
.copyable {
  font-family: var(--mono); font-size: var(--fs-sm); word-break: break-all;
  background: var(--surface-sunk); border: 1px solid var(--border);
  padding: 7px 10px; border-radius: 6px; cursor: pointer; display: block;
  position: relative; padding-right: 58px;
}
.copyable::after {
  content: "Copy"; position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  font-family: var(--ui); font-size: var(--fs-xs); font-weight: 600; color: var(--accent);
}
.copyable.copied::after { content: "Copied"; }
.field { display: grid; gap: 6px; margin-bottom: 10px; }
.field label {
  font-family: var(--mono); font-size: var(--fs-xs); font-weight: 500;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
input[type="text"], input[type="number"], select {
  font-family: var(--mono); font-size: var(--fs-md);
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border-firm); border-radius: 8px;
  padding: 11px 12px; width: 100%;
}
input[type="text"]:focus, input[type="number"]:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.note { color: var(--muted); font-size: var(--fs-xs); line-height: 1.5; }
.err { color: var(--critical-ink); font-size: var(--fs-sm); margin-top: 10px; }
.disclosure { margin-top: 30px; border-top: 1px solid var(--border); padding-top: 16px; }
.disclosure summary { font-size: var(--fs-sm); font-weight: 600; cursor: pointer; }
.disclosure p { color: var(--ink-2); font-size: var(--fs-sm); margin-top: 8px; }

.btn {
  border: 1px solid var(--border-firm); background: var(--surface);
  border-radius: 8px; padding: 9px 14px; font-size: var(--fs-sm); font-weight: 600;
  cursor: pointer; display: inline-flex; align-items: center; gap: 7px;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] .btn-primary,
:root:not([data-theme="light"]) .btn-primary { color: #06120d; }
@media (prefers-color-scheme: light) { :root:not([data-theme="dark"]) .btn-primary { color: #fff; } }
.btn-lg { padding: 13px 20px; font-size: var(--fs-md); width: 100%; justify-content: center; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn-danger { color: var(--critical-ink); border-color: var(--border-firm); background: var(--surface); }

/* ============================================================
   App chrome
   ============================================================ */
#app { min-height: 100svh; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: color-mix(in srgb, var(--plane) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  -webkit-backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top, 0px);
}
.topbar-inner {
  display: flex; align-items: flex-end; justify-content: space-between; gap: 12px;
  padding: 11px var(--gutter) 10px;
}
.topbar-title h1 { font-size: var(--fs-lg); letter-spacing: -0.022em; }
.sync-line { font-family: var(--mono); font-size: var(--fs-xs); color: var(--muted); }
.topbar-actions { display: flex; gap: 4px; flex: none; }
.icon-btn {
  background: none; border: 0; padding: 8px; border-radius: 8px;
  color: var(--ink-2); cursor: pointer; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-sunk); color: var(--ink); }
.icon-btn.spinning svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.sync-bar { height: 2px; background: var(--surface-sunk); overflow: hidden; }
.sync-bar-fill { height: 100%; width: 30%; background: var(--accent-mark); animation: slide 1.1s ease-in-out infinite; }
@keyframes slide { 0% { margin-left: -32%; } 100% { margin-left: 102%; } }

main {
  flex: 1;
  padding-inline: var(--gutter);
  padding-block: 18px calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 28px);
  max-width: 44rem; width: 100%; margin: 0 auto;
}
main:focus { outline: none; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tabbar button {
  background: none; border: 0; cursor: pointer;
  display: grid; justify-items: center; gap: 2px;
  padding: 8px 2px 7px; color: var(--muted);
  font-size: 10px; font-weight: 600; letter-spacing: .01em;
  min-height: var(--tabbar-h);
  align-content: center;
}
.tabbar button[aria-current="true"] { color: var(--accent); }
.tabbar svg { width: 21px; height: 21px; }

/* ============================================================
   Panels & primitives
   ============================================================ */
.stack { display: grid; gap: 26px; }
.panel { display: grid; gap: 11px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.panel-head h2 { font-size: var(--fs-md); }
.panel-head .aside { font-family: var(--mono); font-size: var(--fs-xs); color: var(--muted); flex: none; }
.panel-note { color: var(--ink-2); font-size: var(--fs-sm); line-height: 1.5; }
.section-rule { border: 0; border-top: 1px solid var(--border); margin: 4px 0 0; }
.eyebrow {
  font-family: var(--mono); font-size: var(--fs-xs); font-weight: 500;
  text-transform: uppercase; letter-spacing: .07em; color: var(--muted);
}

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 15px;
}

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; }
@media (min-width: 560px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 11px; padding: 11px 12px 12px;
  display: grid; gap: 2px; align-content: start;
}
.tile-label {
  font-family: var(--mono); font-size: 10px; font-weight: 500;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tile-value { font-size: 1.5rem; font-weight: 600; letter-spacing: -0.03em; line-height: 1.12; }
.tile-value small { font-size: 0.75rem; font-weight: 500; color: var(--muted); letter-spacing: 0; margin-left: 1px; }
.tile-delta { font-family: var(--mono); font-size: var(--fs-xs); color: var(--muted); display: flex; align-items: center; gap: 4px; }
.tile-delta.up-good, .tile-delta.down-good { color: var(--good-ink); }
.tile-delta.up-bad, .tile-delta.down-bad { color: var(--critical-ink); }
.tile-spark { margin-top: 5px; }

/* readiness hero */
.hero {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 18px 16px 16px;
  display: grid; gap: 14px; justify-items: center; text-align: center;
  box-shadow: var(--shadow-card);
}
.hero-arc { position: relative; width: 190px; max-width: 62vw; }
.hero-readout {
  position: absolute; inset: 0; display: grid; place-content: center; gap: 0; text-align: center;
  padding-bottom: 12%;
}
.hero-score { font-size: 3.1rem; font-weight: 700; letter-spacing: -0.045em; line-height: 1; }
.hero-band { font-family: var(--mono); font-size: var(--fs-xs); font-weight: 500; text-transform: uppercase; letter-spacing: .08em; }
.hero-verdict { font-family: var(--prose); font-size: var(--fs-md); line-height: 1.5; color: var(--ink-2); max-width: 40ch; }
.hero-drivers { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.driver {
  font-family: var(--mono); font-size: var(--fs-xs);
  border: 1px solid var(--border); border-radius: 999px; padding: 3px 9px;
  color: var(--ink-2); display: inline-flex; align-items: center; gap: 5px;
}
.driver i { width: 6px; height: 6px; border-radius: 50%; flex: none; font-style: normal; }

/* recommendation cards */
.recs { display: grid; gap: 10px; }
.rec {
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--sev, var(--muted));
  border-radius: 10px; padding: 13px 14px; display: grid; gap: 7px;
}
.rec-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.rec-sev {
  font-family: var(--mono); font-size: 10px; font-weight: 500;
  text-transform: uppercase; letter-spacing: .07em;
  display: inline-flex; align-items: center; gap: 5px; color: var(--sev-ink, var(--ink-2));
}
.rec-sev svg { width: 13px; height: 13px; flex: none; }
.rec-domain { font-family: var(--mono); font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .07em; }
.rec h3 { font-size: var(--fs-base); line-height: 1.35; }
.rec-evidence {
  font-family: var(--mono); font-size: var(--fs-xs); color: var(--ink-2);
  background: var(--surface-sunk); border-radius: 6px; padding: 7px 9px; line-height: 1.5;
}
.rec-action { font-family: var(--prose); font-size: var(--fs-base); line-height: 1.55; color: var(--ink-2); }
.rec-action b { color: var(--ink); font-weight: 600; }

/* insight / correlation rows */
.corr { display: grid; gap: 9px; }
.corr-row {
  display: grid; gap: 7px; padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.corr-row:last-child { border-bottom: 0; }
.corr-claim { font-family: var(--prose); font-size: var(--fs-base); line-height: 1.5; }
.corr-claim b { font-weight: 600; }
.corr-meta { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.corr-strength { display: flex; align-items: center; gap: 3px; }
.corr-strength i { width: 13px; height: 4px; border-radius: 2px; background: var(--surface-sunk); font-style: normal; }
.corr-strength i.on { background: var(--s1); }
.corr-strength i.on.neg { background: var(--s2); }
.corr-stat { font-family: var(--mono); font-size: var(--fs-xs); color: var(--muted); }

/* charts */
.chart-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 10px 8px; }
.chart-wrap.flush { padding: 12px 6px 6px; }
svg.chart { display: block; width: 100%; height: auto; overflow: visible; touch-action: pan-y; }
.chart text { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 13px; padding: 9px 4px 2px; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; font-size: var(--fs-xs); color: var(--ink-2); }
.legend-swatch { width: 9px; height: 9px; border-radius: 2.5px; flex: none; }

.tooltip {
  position: fixed; z-index: 90; pointer-events: none;
  background: var(--ink); color: var(--plane);
  border-radius: 7px; padding: 7px 9px;
  font-family: var(--mono); font-size: var(--fs-xs); line-height: 1.5;
  box-shadow: 0 6px 20px -6px rgba(0,0,0,.45); max-width: 220px;
}
.tooltip b { font-weight: 500; }
.tooltip .tt-head { font-weight: 500; margin-bottom: 3px; opacity: .72; }
.tooltip .tt-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tooltip .tt-row i { width: 7px; height: 7px; border-radius: 2px; flex: none; font-style: normal; }
.tooltip .tt-row span:last-child { margin-left: auto; }

/* data table fallback (relief rule for low-contrast marks) */
.table-toggle {
  background: none; border: 0; color: var(--accent); cursor: pointer;
  font-size: var(--fs-xs); font-weight: 600; padding: 4px 0; text-align: left;
}
.data-table-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: var(--fs-xs); font-family: var(--mono); }
table.data th, table.data td { text-align: right; padding: 5px 7px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--muted); font-weight: 500; }
table.data td { font-variant-numeric: tabular-nums; }

/* empty / building states */
.building {
  border: 1px dashed var(--border-firm); border-radius: 11px;
  padding: 16px; display: grid; gap: 8px; background: var(--surface-2);
}
.building h3 { font-size: var(--fs-base); }
.building p { font-size: var(--fs-sm); color: var(--ink-2); line-height: 1.5; }
.progress-dots { display: flex; gap: 4px; align-items: center; margin-top: 2px; }
.progress-dots i { width: 9px; height: 9px; border-radius: 50%; background: var(--surface-sunk); border: 1px solid var(--border); font-style: normal; }
.progress-dots i.on { background: var(--accent-mark); border-color: var(--accent-mark); }

/* settings */
.setting-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 13px 0; border-bottom: 1px solid var(--border);
}
.setting-row:last-of-type { border-bottom: 0; }
.setting-row .label { display: grid; gap: 2px; }
.setting-row .label b { font-size: var(--fs-base); font-weight: 600; }
.setting-row .label span { font-size: var(--fs-xs); color: var(--muted); line-height: 1.45; }
.setting-row input[type="number"], .setting-row select { width: 112px; flex: none; padding: 8px 10px; font-size: var(--fs-sm); }
.scope-grid { display: flex; flex-wrap: wrap; gap: 5px; }
.scope-chip {
  font-family: var(--mono); font-size: var(--fs-xs); padding: 3px 8px;
  border-radius: 999px; border: 1px solid var(--border); color: var(--ink-2);
}
.scope-chip.on { border-color: var(--accent-mark); color: var(--accent); background: var(--accent-wash); }
.scope-chip.off { opacity: .55; text-decoration: line-through; }

.disclaimer {
  font-size: var(--fs-xs); color: var(--muted); line-height: 1.55;
  border-top: 1px solid var(--border); padding-top: 14px;
}

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + 14px);
  z-index: 95; background: var(--ink); color: var(--plane);
  font-size: var(--fs-sm); font-weight: 500;
  padding: 9px 15px; border-radius: 999px;
  box-shadow: 0 8px 24px -8px rgba(0,0,0,.5); max-width: calc(100vw - 32px);
}

/* ---- deployment configuration status (setup screen) ---- */
.status-strip {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 11px 13px; margin-bottom: 26px;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); font-size: var(--fs-sm);
}
.status-dot {
  width: 9px; height: 9px; border-radius: 50%; flex: none;
  background: var(--muted); margin-top: 6px;
}
.status-strip .status-text { color: var(--ink-2); line-height: 1.45; }
.status-strip.ok   { border-color: color-mix(in srgb, var(--good) 45%, var(--border)); }
.status-strip.ok   .status-dot { background: var(--good); }
.status-strip.wait { border-color: color-mix(in srgb, var(--warning) 45%, var(--border)); }
.status-strip.wait .status-dot { background: var(--warning); }
.status-strip.bad  { border-color: color-mix(in srgb, var(--critical) 45%, var(--border)); }
.status-strip.bad  .status-dot { background: var(--critical); }

.copyable.block { margin: 8px 0; line-height: 1.5; }
.why code {
  font-family: var(--mono); font-size: 10px;
  background: var(--surface-sunk); padding: 1px 4px; border-radius: 3px;
}

/* long URLs and scope names must never push the page sideways */
.steps p, .steps a, .steps dd, .why, .fieldmap dd b { overflow-wrap: anywhere; }
