/*
 * Car Protect console.
 *
 * One palette, defined once as variables and flipped by [data-theme]. The dark theme
 * is the default signal (this gets read in a driveway at night as often as at a desk)
 * but the toggle wins in both directions.
 */

:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --ink: #16181d;
  --ink-dim: #5c6472;
  --line: #dfe3ea;
  --accent: #2f6df6;
  --accent-ink: #ffffff;
  --ok: #1f8a4c;
  --warn: #c2670a;
  --bad: #c2352c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --panel: #161b22;
    --panel-2: #1d232c;
    --ink: #e8ecf2;
    --ink-dim: #97a2b2;
    --line: #262d38;
    --accent: #5b8dff;
    --accent-ink: #08101f;
    --ok: #48c07c;
    --warn: #e0a33c;
    --bad: #f07067;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

:root[data-theme="light"] {
  --bg: #f6f7f9; --panel: #ffffff; --panel-2: #f0f2f5;
  --ink: #16181d; --ink-dim: #5c6472; --line: #dfe3ea;
  --accent: #2f6df6; --accent-ink: #ffffff;
  --ok: #1f8a4c; --warn: #c2670a; --bad: #c2352c;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0e1116; --panel: #161b22; --panel-2: #1d232c;
  --ink: #e8ecf2; --ink-dim: #97a2b2; --line: #262d38;
  --accent: #5b8dff; --accent-ink: #08101f;
  --ok: #48c07c; --warn: #e0a33c; --bad: #f07067;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0 0 .35em; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; letter-spacing: .01em; }
p { margin: 0 0 .8em; }
.muted { color: var(--ink-dim); font-size: .875rem; }
.error { color: var(--bad); font-size: .875rem; }
[hidden] { display: none !important; }

/* --------------------------------- brand -------------------------------- */

.brand {
  display: flex; align-items: center; gap: .55rem;
  font-weight: 650; letter-spacing: -.01em;
}
.brand-mark {
  display: grid; place-items: center;
  width: 22px; height: 22px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 55%, #12d6a0));
  color: var(--accent-ink);
  font-size: .6rem; font-weight: 800; letter-spacing: .02em;
}

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

.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }

.login-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex; flex-direction: column; gap: .35rem;
}
.login-card h1 { margin-top: 1rem; }
.login-card label { font-size: .8rem; color: var(--ink-dim); margin-top: .7rem; }

input[type="email"], input[type="password"], input[type="search"], input[type="text"], select {
  width: 100%;
  padding: .6rem .7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--ink);
  font: inherit;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Reveal toggle. Inside the field rather than beside it, so the form keeps one column.
   MUST come after the generic input rule above: `.password-field input` and
   `input[type="password"]` have identical specificity, so source order is the only
   thing deciding the padding — and losing it puts the text under the button. */
.password-field { position: relative; display: flex; }
.password-field input { padding-right: 2.7rem; }
.reveal {
  position: absolute; right: 5px; top: 50%; transform: translateY(-50%);
  display: grid; place-items: center;
  width: 30px; height: 30px; padding: 0;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--ink-dim);
}
.reveal:hover { color: var(--ink); background: var(--panel); border-color: transparent; }
.reveal svg { width: 18px; height: 18px; }
.reveal .eye-off { display: none; }
.reveal.on { color: var(--accent); }
.reveal.on .eye { display: none; }
.reveal.on .eye-off { display: block; }

button {
  font: inherit; cursor: pointer;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--ink);
  padding: .5rem .8rem;
}
button:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
button.primary {
  margin-top: 1.1rem;
  background: var(--accent); color: var(--accent-ink);
  border-color: transparent; font-weight: 600; padding: .65rem .9rem;
}
button.ghost { background: transparent; }
button:disabled { opacity: .55; cursor: default; }

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

.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .8rem clamp(12px, 3vw, 28px);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.spacer { flex: 1 1 auto; }
.device-picker { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.device-picker select { min-width: min(320px, 52vw); }

.tabs {
  display: flex; gap: .25rem; overflow-x: auto;
  padding: .4rem clamp(12px, 3vw, 28px);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 57px; z-index: 19;
}
.tabs button {
  border: 0; background: transparent; color: var(--ink-dim);
  padding: .45rem .8rem; border-radius: 999px; white-space: nowrap;
}
.tabs button.active { background: var(--panel-2); color: var(--ink); font-weight: 600; }

.content { padding: clamp(14px, 3vw, 28px); max-width: 1280px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}
.card-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap; margin-bottom: .6rem;
}
.filters { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.filters input[type="search"] { width: min(260px, 60vw); }
.check { display: flex; align-items: center; gap: .35rem; font-size: .85rem; color: var(--ink-dim); }

.grid-2 { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 860px) { .grid-2 { grid-template-columns: 1fr 1fr; } }

/* -------------------------------- stats --------------------------------- */

.stat-row {
  display: grid; gap: 12px; margin-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.stat {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px;
}
.stat-value { font-size: 1.7rem; font-weight: 680; letter-spacing: -.02em; }
.stat-label { color: var(--ink-dim); font-size: .8rem; }
.stat.ok .stat-value { color: var(--ok); }
.stat.warn .stat-value { color: var(--warn); }
.stat.bad .stat-value { color: var(--bad); }

/* ------------------------------ access code ------------------------------ */

.code-card { max-width: 720px; }
.code-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }

.code {
  font-family: var(--mono);
  font-size: clamp(2.6rem, 11vw, 4.4rem);
  font-weight: 700;
  letter-spacing: .12em;
  text-align: center;
  padding: .35em 0 .2em;
  color: var(--accent);
  word-break: break-all;
}

.countdown { width: 48px; height: 48px; flex: none; }
.countdown-track { fill: none; stroke: var(--line); stroke-width: 12; }
.countdown-arc {
  fill: none; stroke: var(--accent); stroke-width: 12; stroke-linecap: round;
  transform: rotate(-90deg); transform-origin: 60px 60px;
  transition: stroke-dashoffset .9s linear;
}
/* Bigger in the viewBox than it sounds: the ring renders at 48px, so the stroke and
   the number have to grow in SVG units just to stay legible at that size. */
.countdown text { fill: var(--ink-dim); font-size: 40px; font-family: var(--mono); }

.drift { margin-top: .5rem; border-top: 1px solid var(--line); padding-top: .8rem; }

.lookup-card { max-width: 720px; }

/* ------------------------ invite a code-only user ------------------------ */

.invite-card { max-width: 640px; }

.invite-head { display: flex; align-items: flex-start; gap: .9rem; margin-bottom: 1rem; }
.invite-head h3 { margin-bottom: .15em; }
.invite-head p { margin: 0; }
.invite-icon {
  display: grid; place-items: center; flex: none;
  width: 42px; height: 42px; border-radius: 12px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 18%, transparent),
                                      color-mix(in srgb, var(--accent) 6%, transparent));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line));
  color: var(--accent);
}
.invite-icon svg { width: 22px; height: 22px; }

/* What the role is, before you hand it out — the "cannot" half matters as much. */
.capabilities { list-style: none; margin: 0 0 1.2rem; padding: 0; display: grid; gap: .32rem; }
.capabilities li {
  display: flex; align-items: baseline; gap: .55rem;
  font-size: .875rem; color: var(--ink-dim);
}
.capabilities li::before { font-weight: 700; flex: none; }
.capabilities .can::before { content: "✓"; color: var(--ok); }
.capabilities .cannot::before { content: "✕"; color: var(--bad); }

.field-label {
  display: block; margin: .9rem 0 .3rem;
  font-size: .8rem; font-weight: 600; color: var(--ink-dim);
}

/* Radio cards rather than a checkbox: both outcomes are then written down, so
   "what happens if I leave it unticked" is not a question anyone has to ask. */
.choices { display: grid; gap: .5rem; }
@media (min-width: 620px) { .choices { grid-template-columns: 1fr 1fr; } }

.choice {
  display: flex; gap: .55rem; cursor: pointer;
  padding: .7rem .8rem;
  border: 1px solid var(--line); border-radius: 11px;
  background: var(--panel-2);
  transition: border-color .12s ease, background .12s ease;
}
.choice:hover { border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.choice input { margin: .2rem 0 0; accent-color: var(--accent); flex: none; }
.choice:has(input:checked) {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--panel));
}
.choice:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
.choice-body { display: grid; gap: .15rem; }
.choice-title { font-size: .875rem; font-weight: 600; }
.choice-title em {
  font-style: normal; font-size: .68rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .04em;
  color: var(--accent); margin-left: .3rem;
}
.choice-desc { font-size: .8rem; color: var(--ink-dim); line-height: 1.35; }

#newUserPasswordRow { margin-top: .55rem; }
#addUserForm .primary { margin-top: 1rem; }

/* Shown once, so it has to be unmissable and easy to copy correctly. */
.credentials {
  margin-top: 1.2rem; padding: 16px;
  border: 1px solid color-mix(in srgb, var(--ok) 45%, var(--line));
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--ok) 8%, var(--panel));
}
.credentials-head { display: flex; align-items: flex-start; gap: .7rem; margin-bottom: .9rem; }
.credentials-head p { margin: 0; }
.credentials h4 { margin: 0 0 .2em; font-size: 1rem; }
.credentials .tick {
  display: grid; place-items: center; flex: none;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--ok); color: #fff; font-size: .85rem; font-weight: 700;
}
.credential-row {
  display: flex; align-items: center; gap: .6rem; flex-wrap: wrap;
  padding: .3rem 0;
}
.credential-row > span { color: var(--ink-dim); font-size: .78rem; min-width: 74px; }
.credential-row code {
  font-family: var(--mono); font-size: .95rem; letter-spacing: .04em;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: .28rem .5rem; word-break: break-all;
  flex: 1 1 auto;
}
.credential-row.password code {
  font-size: 1.15rem; font-weight: 700; letter-spacing: .12em; color: var(--ink);
}
.credential-row .copy { padding: .28rem .6rem; font-size: .78rem; flex: none; }
.credentials-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .9rem; }
.manual-row { display: flex; gap: .5rem; flex-wrap: wrap; margin: .5rem 0; }
.manual-row input {
  flex: 0 1 180px; width: auto;
  font-family: var(--mono); font-size: 1.15rem; letter-spacing: .14em; text-align: center;
}
.drift summary { cursor: pointer; color: var(--ink-dim); font-size: .875rem; }
.neighbours { display: grid; gap: 6px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); margin-top: .6rem; }
.neighbour {
  display: flex; justify-content: space-between; gap: .6rem;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 9px; padding: .4rem .6rem; font-family: var(--mono); font-size: .95rem;
}
.neighbour.now { border-color: var(--accent); }
.neighbour span { color: var(--ink-dim); font-size: .78rem; font-family: system-ui, sans-serif; }

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

.chart svg { width: 100%; height: auto; display: block; }

/* A donut is a key, not a picture. Sized to be read at a glance beside its legend
   rather than stretched to the width of the card — at full card width the ring was
   bigger than everything it was explaining. */
.donut { display: flex; align-items: center; gap: 1.2rem; flex-wrap: wrap; }
.donut svg { width: 120px; flex: none; }
.donut .legend { flex: 1 1 160px; margin-top: 0; }

.legend { display: grid; gap: .3rem; margin-top: .6rem; }
.legend div { display: flex; align-items: center; gap: .5rem; font-size: .85rem; }
.swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 34%) 1fr auto; gap: .6rem; align-items: center; margin-bottom: .35rem; font-size: .85rem; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-dim); }
.bar-track { background: var(--panel-2); border-radius: 999px; height: 10px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 999px; }
.chart-empty { color: var(--ink-dim); font-size: .875rem; padding: .5rem 0; }

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

.table-wrap { overflow-x: auto; max-height: 62vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: .875rem; }
th, td { text-align: left; padding: .5rem .7rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
th {
  position: sticky; top: 0; z-index: 1;
  background: var(--panel-2); color: var(--ink-dim);
  font-weight: 600; font-size: .78rem; letter-spacing: .03em; text-transform: uppercase;
  cursor: pointer; user-select: none;
}
th[data-sorted]::after { content: " ▲"; font-size: .7em; }
th[data-sorted="desc"]::after { content: " ▼"; }
tbody tr:hover { background: var(--panel-2); }
td.wrap { white-space: normal; min-width: 220px; }
td.mono, .mono { font-family: var(--mono); font-size: .82rem; color: var(--ink-dim); }
tr.group td {
  background: var(--panel-2); font-weight: 650; color: var(--ink);
  position: sticky; top: 28px; z-index: 1;
}

.pill {
  display: inline-block; padding: .1rem .45rem; border-radius: 999px;
  font-size: .74rem; font-weight: 600; border: 1px solid transparent;
}
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.pill.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.pill.dim { color: var(--ink-dim); border-color: var(--line); }

.row-actions button { padding: .25rem .55rem; font-size: .8rem; }

/* --------------------------------- log ----------------------------------- */

.log {
  font-family: var(--mono); font-size: .8rem; line-height: 1.45;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; max-height: 60vh; overflow: auto; white-space: pre-wrap; word-break: break-word;
  margin: 0;
}

/* -------------------------------- toast ---------------------------------- */

.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow);
  border-radius: 999px; padding: .55rem 1rem; font-size: .875rem; z-index: 50; margin: 0;
}
