:root {
  --bg: #E9EDEF; --panel: #F7F9FA; --ink: #17232B; --muted: #5B6B75; --line: #CBD4D9;
  --amber: #B98300; --amber-bg: #FFF4D6; --red: #B23324; --red-bg: #FBE4E0;
  --green: #2F8F5B; --green-bg: #E1F2E8; --blue: #1F5F8B; --off: #9AA7AE;
  --sans: "Barlow", system-ui, -apple-system, "Segoe UI", sans-serif;
  --cond: "Barlow Semi Condensed", "Barlow", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12191D; --panel: #1A2328; --ink: #E3E9EC; --muted: #93A3AC; --line: #2C3940;
    --amber: #E8B23A; --amber-bg: #3A2F12; --red: #F0806F; --red-bg: #3D1F1A;
    --green: #6CC895; --green-bg: #15301F; --blue: #7DB6DE; --off: #5A6970;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--sans); }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.app { display: grid; grid-template-rows: auto 1fr; height: 100%; }
header.top { display: flex; align-items: center; gap: 24px; padding: 10px 20px;
  border-bottom: 1px solid var(--line); background: var(--panel); }
nav.tabs { display: flex; gap: 4px; }
nav.tabs button { background: none; border: 0; padding: 6px 12px; border-radius: 6px; cursor: pointer; color: var(--muted); font-weight: 500; }
nav.tabs button[aria-selected="true"] { background: var(--bg); color: var(--ink); }
.linkbtn { margin-left: auto; background: none; border: 0; color: var(--blue); cursor: pointer; padding: 4px; font-size: 14px; }

.view { display: none; min-height: 0; }
.view.active { display: grid; }

/* ---------- Sites ---------- */
#view-sites { grid-template-columns: 340px 1fr; }
aside.list { border-right: 1px solid var(--line); display: grid; grid-template-rows: auto 1fr; min-height: 0; background: var(--panel); }
.filters { padding: 14px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.filters input, .filters select { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.filters input { font-size: 16px; }
.count { font-size: 13px; color: var(--muted); }
ul.sites { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
ul.sites li button { width: 100%; text-align: left; background: none; border: 0; border-bottom: 1px solid var(--line);
  padding: 9px 14px 9px 18px; display: grid; grid-template-columns: 52px 1fr; gap: 2px 10px; cursor: pointer; position: relative; }
ul.sites li button::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: transparent; }
ul.sites li button[data-worst="block"]::before { background: var(--red); }
ul.sites li button[data-worst="warn"]::before { background: var(--amber); }
ul.sites li button:hover { background: var(--bg); }
ul.sites li button[aria-current] { background: var(--bg); box-shadow: inset -3px 0 0 var(--blue); }
.sid { font: 700 17px var(--cond); grid-row: span 2; align-self: center; }
.sname { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sproj { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

main.detail { overflow-y: auto; padding: 28px 36px 60px; }
.empty { color: var(--muted); max-width: 46ch; margin: 12vh auto; text-align: center; }
.empty strong { display: block; color: var(--ink); font: 600 20px var(--cond); margin-bottom: 6px; }

.site-head { display: flex; align-items: flex-end; gap: 22px; margin-bottom: 18px; flex-wrap: wrap; }
.site-num { font: 700 64px/0.9 var(--cond); letter-spacing: -.01em; }
.site-title h1 { margin: 0; font: 600 24px/1.2 var(--cond); }
.site-title p { margin: 2px 0 0; color: var(--muted); }
.site-title a { color: var(--blue); }
.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 99px; margin-left: 6px; vertical-align: 2px; }
.pill.ok { background: var(--green-bg); color: var(--green); }
.pill.no { background: var(--bg); color: var(--muted); border: 1px solid var(--line); }

.alerts { display: grid; gap: 6px; margin-bottom: 26px; max-width: 1800px; }
.alert { padding: 8px 12px; border-radius: 6px; border-left: 4px solid; }
.alert.block { background: var(--red-bg); border-color: var(--red); }
.alert.warn { background: var(--amber-bg); border-color: var(--amber); }
.alert.info { background: var(--bg); border-color: var(--off); color: var(--muted); }

h2 { font: 600 17px var(--cond); margin: 30px 0 12px; }
.slabel { font-size: 12px; color: var(--muted); margin: 8px 0 3px; }
.secret { display: flex; align-items: stretch; border: 1px dashed var(--line); border-radius: 6px; margin-bottom: 6px; overflow: hidden; max-width: 420px; }
.secret span { flex: 1; min-width: 0; font: 13px/1.4 var(--mono); padding: 6px 9px; color: var(--muted); overflow-wrap: anywhere; }
.secret span.shown { color: var(--ink); }
.secret button { border: 0; border-left: 1px solid var(--line); background: var(--bg); padding: 0 10px; cursor: pointer; font-size: 13px; font-weight: 500; }
.secret button:hover { color: var(--blue); }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px 40px; max-width: 1800px; }
dl { margin: 0; display: grid; grid-template-columns: 150px 1fr; gap: 4px 12px; }
dl h3 { grid-column: 1 / -1; margin: 12px 0 4px; font: 600 15px var(--cond); color: var(--muted); border-bottom: 1px solid var(--line); padding-bottom: 3px; }
dt { color: var(--muted); font-size: 14px; }
dd { margin: 0; font: 13.5px/1.5 var(--mono); word-break: break-all; }
dd.txt { font-family: var(--sans); font-size: 14.5px; word-break: normal; }
dd.none { color: var(--off); font-family: var(--sans); }
.comments { max-width: 1800px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 2px 14px; }
.comments .comment { padding: 10px 0; border-bottom: 1px solid var(--line); white-space: pre-wrap; }
.comments .comment:last-child { border-bottom: 0; }
.comments cite { display: block; font-style: normal; font-size: 13px; color: var(--muted); margin-bottom: 2px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; max-width: 100%; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-weight: 600; color: var(--muted); font-size: 13px; background: var(--panel); position: sticky; top: 0; }

/* ---------- Data quality ---------- */
#view-quality { overflow-y: auto; padding: 28px 36px 60px; align-content: start; }
.q-wrap { max-width: 1800px; }
.q-lead { font: 600 26px/1.25 var(--cond); margin: 0 0 4px; max-width: 40ch; }
.q-sub { color: var(--muted); margin: 0 0 22px; max-width: 70ch; }
.bar { display: flex; height: 34px; border-radius: 6px; overflow: hidden; background: var(--line); }
.bar button { border: 0; padding: 0; cursor: pointer; min-width: 3px; }
.bar button:hover, .bar button:focus-visible { filter: brightness(1.12); }
.proj { display: grid; grid-template-columns: minmax(140px, 240px) 56px 1fr 90px; gap: 0 14px; align-items: center; }
.proj > div { padding: 7px 0; border-bottom: 1px solid var(--line); min-width: 0; }
.proj .hd { font-size: 13px; color: var(--muted); font-weight: 600; }
.proj .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proj .name button { background: none; border: 0; padding: 0; cursor: pointer; font: inherit; color: var(--ink); text-align: left; }
.proj .name button:hover { color: var(--blue); text-decoration: underline; }
.proj .num { text-align: right; font-variant-numeric: tabular-nums; }
.proj .nr { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.proj .nr.zero { color: var(--off); font-weight: 400; }
.proj .bar { height: 16px; border-radius: 3px; }
.more { margin-top: 12px; }
.fixes { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; align-items: stretch; }
.fix { display: flex; flex-direction: column; gap: 6px; background: var(--panel);
  border: 1px solid var(--line); border-left: 5px solid var(--amber); border-radius: 8px; padding: 14px 16px; }
.fix.block { border-left-color: var(--red); }
.fix b { font: 700 28px/1 var(--cond); }
.fix .what { font-weight: 600; }
.fix .how { font-size: 14px; color: var(--muted); flex: 1; }
.fix button { align-self: flex-start; margin-top: 2px; }
@media (max-width: 700px) { .proj { grid-template-columns: 1fr 44px 70px; } .proj > div:nth-child(4n+3) { display: none; } }

/* ---------- Admin ---------- */
#view-admin { overflow-y: auto; padding: 28px 36px 60px; align-content: start; }
.admin-wrap { max-width: 1800px; display: grid; gap: 10px; }
.admin-wrap .form { max-width: none; }
.admin-wrap fieldset { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

.btn { padding: 9px 16px; border-radius: 6px; border: 1px solid var(--blue); background: var(--blue); color: #fff; font-weight: 600; cursor: pointer; }
.btn.secondary { background: none; color: var(--ink); border-color: var(--line); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: wait; }
.add-btn { width: 100%; }
.head-actions { margin-left: auto; align-self: center; }
.pill.manual { background: var(--amber-bg); color: var(--amber); }

/* Login */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 10; }
.login[hidden], [hidden] { display: none !important; }
.login form { width: min(360px, 92vw); display: grid; gap: 12px; }
.login h1 { font: 700 30px var(--cond); margin: 0; }
.login p { margin: 0 0 8px; color: var(--muted); }
.login label, .form label { display: grid; gap: 4px; font-size: 14px; font-weight: 500; }
.login input, .form input, .form select, .form textarea { padding: 9px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); font-size: 15px; }
.login .error { color: var(--red); font-size: 14px; min-height: 1.4em; margin: 0; }
.login .btn-google { width: min(360px, 92vw); margin-top: 14px; text-align: center; text-decoration: none;
  background: #EEF1F2; color: var(--ink); border: 1px solid var(--line); }
@media (prefers-color-scheme: dark) {
  .login .btn-google { background: #2A343A; }
}

/* Form */
.form { max-width: 900px; }
.form h1 { font: 600 26px var(--cond); margin: 0 0 6px; }
.form fieldset { border: 0; border-top: 1px solid var(--line); margin: 22px 0 0; padding: 14px 0 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px 20px; }
.form legend { font: 600 17px var(--cond); padding-right: 10px; }
.form .wide { grid-column: 1 / -1; }
.form textarea { min-height: 90px; resize: vertical; font-family: var(--sans); }
.form .check { display: flex; align-items: center; gap: 8px; font-weight: 500; align-self: end; padding-bottom: 8px; }
.form .check input { width: 18px; height: 18px; }
.form .hint { font-size: 12px; font-weight: 400; color: var(--muted); }
.form .field-error { font-size: 13px; font-weight: 500; color: var(--red); }
.form input[aria-invalid="true"] { border-color: var(--red); }
.form .actions { display: flex; gap: 10px; margin-top: 26px; }
.form .notice { margin: 10px 0 0; }

/* Add-site connectivity check (D-025): Ping -> SSH -> Save */
.stepper { display: flex; margin: 26px 0 4px; }
.stepper .step { flex: 1; display: flex; flex-direction: column; align-items: center; position: relative; }
.stepper .step:not(:first-child)::before {
  content: ""; position: absolute; top: 11px; right: 50%; width: 100%; height: 2px; background: var(--line);
}
.stepper .step.done:not(:first-child)::before, .stepper .step.failed:not(:first-child)::before { background: var(--blue); }
.stepper .dot {
  width: 24px; height: 24px; border-radius: 50%; background: var(--panel); border: 2px solid var(--line);
  position: relative; z-index: 1;
}
.stepper .step.active .dot { border-color: var(--blue); }
.stepper .step.active .dot::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%; border: 2px solid transparent;
  border-top-color: var(--blue); animation: step-spin .8s linear infinite;
}
.stepper .step.done .dot { background: var(--blue); border-color: var(--blue); }
.stepper .step.failed .dot { background: var(--red); border-color: var(--red); }
.stepper .label { margin-top: 6px; font-size: 13px; color: var(--muted); }
.stepper .step.active .label, .stepper .step.done .label, .stepper .step.failed .label { color: var(--ink); font-weight: 600; }
@keyframes step-spin { to { transform: rotate(360deg); } }
#step-detail.alert { margin-top: 14px; }

/* Remote access: ping (D-027) and embedded terminal (D-023) */
.terminal-panel { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.status-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--line); flex-shrink: 0; }
.status-dot.ok { background: var(--green); }
.status-dot.fail { background: var(--red); }
#terminal-container { flex-basis: 100%; margin-top: 4px; height: 420px; padding: 8px; border-radius: 8px; background: #0d1117; }
#terminal-container .xterm { height: 100%; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 7px 14px; border-radius: 6px; font-size: 14px; opacity: 0; transition: opacity .15s; pointer-events: none; }
.toast.show { opacity: 1; }

@media (max-width: 900px) {
  #view-sites { grid-template-columns: 1fr; }
  aside.list { max-height: 40vh; border-right: 0; border-bottom: 1px solid var(--line); }
  main.detail, #view-quality, #view-admin { padding: 20px 16px 50px; }
  header.top { gap: 12px; padding: 8px 12px; flex-wrap: wrap; }
  nav.tabs button { padding: 6px 8px; white-space: nowrap; }
  .site-num { font-size: 48px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .stepper .step.active .dot::after { animation: none; border-top-color: var(--blue); border-right-color: var(--blue); }
}
