:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #6b7280;
  --line: #e3e6ea;
  --accent: #7c3aed;
  --accent-ink: #ffffff;
  --ok: #16794a;
  --ok-bg: #e6f5ee;
  --bad: #b42318;
  --bad-bg: #fdeceb;
  --warn: #92500a;
  --warn-bg: #fdf3e4;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { max-width: 100%; overflow-x: hidden; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }

/* ── Layout ─────────────────────────────────────────────────────── */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 210px; flex: 0 0 210px; background: var(--panel);
  border-right: 1px solid var(--line); padding: 14px 10px;
  display: flex; flex-direction: column; gap: 2px;
}
.brand { font-weight: 650; padding: 6px 10px 14px; letter-spacing: -0.01em; }
.sidebar a {
  display: block; padding: 8px 10px; border-radius: 6px;
  color: var(--ink); text-decoration: none; font-size: 13.5px;
}
.sidebar a:hover { background: #f0f1f4; }
.sidebar a.active { background: var(--accent); color: var(--accent-ink); }
.sidebar-foot {
  margin-top: auto; padding: 10px; font-size: 12px; color: var(--muted);
  display: flex; align-items: center; gap: 6px;
}
.view { flex: 1; padding: 22px 26px; min-width: 0; }

.banner {
  background: var(--warn-bg); color: var(--warn); padding: 7px 14px;
  font-size: 12.5px; border-bottom: 1px solid #f0d9b5;
}

/* ── Headings & bars ────────────────────────────────────────────── */
.head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.head h1 { font-size: 19px; margin: 0; font-weight: 640; letter-spacing: -0.01em; }
.head .spacer { flex: 1; }
.sub { color: var(--muted); font-size: 13px; margin: 0 0 16px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }

/* ── Controls ───────────────────────────────────────────────────── */
input, select, textarea, button {
  font: inherit; color: inherit;
}
input[type=text], input[type=number], input[type=search], select, textarea {
  padding: 6px 9px; border: 1px solid var(--line); border-radius: 6px;
  background: #fff; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #ddd6fe; border-color: var(--accent); }
textarea { width: 100%; resize: vertical; min-height: 62px; }
button {
  padding: 6px 12px; border: 1px solid var(--line); border-radius: 6px;
  background: #fff; cursor: pointer;
}
button:hover { background: #f4f4f6; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); border-color: #f0c4c0; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.sm { padding: 3px 8px; font-size: 12.5px; }

/* ── Table ──────────────────────────────────────────────────────── */
.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden;
}
/* If a table is ever wider than its card, the card scrolls — never the page. */
.card { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbfc; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { text-align: right; white-space: nowrap; }
.thumb { width: 34px; height: 34px; object-fit: cover; border-radius: 5px; border: 1px solid var(--line); }
.thumb-empty { width: 34px; height: 34px; border-radius: 5px; background: #f0f1f4; }

/* ── Bits ───────────────────────────────────────────────────────── */
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 550; }
.pill-ok { background: var(--ok-bg); color: var(--ok); }
.pill-bad { background: var(--bad-bg); color: var(--bad); }
.pill-warn { background: var(--warn-bg); color: var(--warn); }
.pill-mute { background: #eef0f3; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-ok { background: #16a34a; } .dot-bad { background: #dc2626; } .dot-unknown { background: #b6bcc5; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.muted { color: var(--muted); }
.loading, .empty { padding: 34px; text-align: center; color: var(--muted); }
.err {
  background: var(--bad-bg); color: var(--bad); border: 1px solid #f5c6c2;
  padding: 10px 12px; border-radius: var(--radius); margin-bottom: 14px;
  white-space: pre-wrap; word-break: break-word;
}
.pager { display: flex; align-items: center; gap: 10px; padding: 10px 12px; color: var(--muted); font-size: 13px; }
.pager .spacer { flex: 1; }

/* ── Forms ──────────────────────────────────────────────────────── */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 12px; color: var(--muted); font-weight: 550; }
.field.wide { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 7px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 16px 0 0; }
legend { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 0 5px; }

/* ── Modal ──────────────────────────────────────────────────────── */
.backdrop {
  position: fixed; inset: 0; background: rgba(15, 17, 21, .45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow: auto; z-index: 50;
}
.modal {
  background: var(--panel); border-radius: 10px; width: min(760px, 100%);
  box-shadow: 0 18px 48px rgba(0, 0, 0, .22);
}
.modal header { padding: 14px 18px; border-bottom: 1px solid var(--line); font-weight: 620; }
.modal .body { padding: 18px; }
.modal footer { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }

/* ── Toast ──────────────────────────────────────────────────────── */
.toast-root { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast {
  background: #16181d; color: #fff; padding: 9px 14px; border-radius: 7px;
  font-size: 13px; box-shadow: 0 6px 20px rgba(0, 0, 0, .25); max-width: 380px;
}
.toast.bad { background: var(--bad); }

/* ── Health ─────────────────────────────────────────────────────── */
.health-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.health-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.health-card h3 { margin: 0 0 8px; font-size: 14.5px; display: flex; align-items: center; gap: 8px; }
.health-card .metric { font-size: 22px; font-variant-numeric: tabular-nums; }
.health-card .why { margin-top: 8px; font-size: 12px; color: var(--bad); word-break: break-word; white-space: pre-wrap; }

/* ── Mobile ─────────────────────────────────────────────────────── */
@media (max-width: 760px) {
  .shell { flex-direction: column; }
  .view { padding: 14px 16px 40px; }

  /* Sidebar becomes a scrolling top bar. Only the nav scrolls, not the page. */
  .sidebar {
    width: auto; flex: none; flex-direction: row; align-items: center;
    gap: 6px; padding: 8px 12px; overflow-x: auto; overflow-y: hidden;
    border-right: 0; border-bottom: 1px solid var(--line);
    position: sticky; top: 0; z-index: 20; -webkit-overflow-scrolling: touch;
  }
  .sidebar::-webkit-scrollbar { display: none; }
  .sidebar { scrollbar-width: none; }
  .brand { display: none; }
  .sidebar a { padding: 7px 12px; white-space: nowrap; font-size: 13px; }
  .sidebar-foot { display: none; }

  .head { flex-wrap: wrap; gap: 8px; }
  .head h1 { font-size: 17px; }
  .toolbar > * { flex: 1 1 auto; }
  .toolbar input[type=search] { min-width: 0 !important; width: 100%; }

  /* Tables stop being tables: each row is a stack, each value keeps its
     column heading as a prefix (data-label, set in views/_shared.js). */
  .card > table, .card thead, .card tbody, .card tr, .card th, .card td {
    display: block; white-space: normal; overflow: visible;
  }
  .card thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .card tr { border-bottom: 1px solid var(--line); padding: 12px 14px; }
  .card tbody tr:last-child { border-bottom: 0; }
  .card td {
    border: 0; padding: 3px 0; display: flex; gap: 10px;
    align-items: center; text-align: left;
  }
  .card td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 38%; min-width: 0;
    color: var(--muted); font-size: 11.5px; text-transform: uppercase;
    letter-spacing: .04em; font-weight: 600;
  }
  .card td.num { text-align: left; }
  .card td.actions { justify-content: flex-start; flex-wrap: wrap; gap: 8px; padding-top: 10px; }
  .card td.actions button { flex: 1 1 auto; padding: 8px 10px; }

  .pager { flex-wrap: wrap; gap: 8px; }
  .pager button { flex: 1 1 auto; }

  /* Forms and modals */
  .grid { grid-template-columns: 1fr; }
  /* Block, not flex: a stretched flex item is sized to the visible height, so
     a form taller than the screen painted its background for one screen only
     and the page showed through the rest. As a block it grows with content. */
  .backdrop { padding: 0; display: block; }
  .modal { width: 100% !important; border-radius: 0; min-height: 100%; }
  .modal footer { position: sticky; bottom: 0; background: var(--panel); }
  .modal footer button { flex: 1 1 auto; padding: 10px; }

  /* Touch targets */
  button { padding: 8px 12px; }
  button.sm { padding: 6px 10px; font-size: 13px; }
  input[type=text], input[type=number], input[type=search], input[type=date],
  select, textarea { font-size: 16px; padding: 9px 10px; }  /* 16px stops iOS zooming on focus */

  .health-grid { grid-template-columns: 1fr; }
  .toast-root { left: 12px; right: 12px; bottom: 12px; }
  .toast { max-width: none; }
}
