/* Design tokens. Light by default, dark via prefers-color-scheme. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --border-strong: #c4cad2;
  --text: #14181d;
  --text-dim: #5c6672;
  --accent: #1f5fd0;
  --accent-soft: #e8f0fd;
  --ok: #157347;
  --ok-soft: #e6f4ec;
  --warn: #9a6400;
  --warn-soft: #fdf3e0;
  --err: #b42318;
  --err-soft: #fdeceb;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #181c22;
    --surface-2: #21262e;
    --border: #2c333c;
    --border-strong: #3d454f;
    --text: #e8ebef;
    --text-dim: #98a2b0;
    --accent: #7aa7f5;
    --accent-soft: #1a2436;
    --ok: #56c98a;
    --ok-soft: #14261d;
    --warn: #e0aa4a;
    --warn-soft: #2a2113;
    --err: #f18b82;
    --err-soft: #2d1815;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 1px 3px rgb(0 0 0 / 25%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 17px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
p { margin: 0; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; flex-direction: column; margin-right: auto; }
.brand small { color: var(--text-dim); font-size: 12px; }

main {
  max-width: 1120px;
  margin: 0 auto;
  padding: 24px 20px 80px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.card-head > :first-child { margin-right: auto; }

/* ---------- Controls ---------- */
button {
  font: inherit;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.ghost { border-color: transparent; background: transparent; color: var(--text-dim); }
button.ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
button.tiny { padding: 3px 8px; font-size: 12px; }

input, select {
  font: inherit;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--text-dim); }

/* ---------- Status pill ---------- */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-dim);
  white-space: nowrap;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.pill.ok { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.pill.warn { color: var(--warn); background: var(--warn-soft); border-color: transparent; }
.pill.err { color: var(--err); background: var(--err-soft); border-color: transparent; }
.pill.info { color: var(--accent); background: var(--accent-soft); border-color: transparent; }

/* ---------- Dropzone ---------- */
.dropzone {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 44px 20px;
  text-align: center;
  cursor: pointer;
  background: var(--surface);
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.drag {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone strong { display: block; margin-bottom: 4px; }
.dropzone span { color: var(--text-dim); font-size: 13px; }

/* ---------- Progress ---------- */
.spinner {
  width: 16px; height: 16px; flex: none;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.5s; } }

.progress-row { display: flex; align-items: center; gap: 10px; }
.meter { height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; margin-top: 14px; }
.meter i { display: block; height: 100%; background: var(--accent); transition: width 0.4s ease; }

/* ---------- Header fields ---------- */
.fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
table { border-collapse: collapse; width: 100%; min-width: 780px; font-size: 14px; }
th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  padding: 0 8px 8px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 5px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
th.num, td.num { text-align: right; }
td input {
  width: 100%;
  padding: 5px 7px;
  border-color: transparent;
  background: transparent;
}
td input:hover { border-color: var(--border); }
td input:focus { border-color: var(--accent); background: var(--surface); }
td input.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.flagged td { background: var(--warn-soft); }
tr.flagged td input:focus { background: var(--surface); }
.rownote {
  display: block;
  font-size: 11.5px;
  color: var(--warn);
  padding: 0 8px 6px;
}
tfoot td { border-top: 1px solid var(--border-strong); border-bottom: none; padding-top: 10px; font-weight: 600; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- Notices ---------- */
.notice {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 14px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  line-height: 1.45;
}
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.err { background: var(--err-soft); color: var(--err); }
.notice.info { background: var(--accent-soft); color: var(--accent); }
.notice ul { margin: 4px 0 0; padding-left: 18px; }
.notice b { display: block; }

/* ---------- Misc ---------- */
.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 18px; }
.actions .spacer { margin-left: auto; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.dim { color: var(--text-dim); }
.hint { font-size: 12.5px; color: var(--text-dim); line-height: 1.5; }
[hidden] { display: none !important; }

details.raw { margin-top: 18px; }
details.raw summary { cursor: pointer; font-size: 13px; color: var(--text-dim); }
details.raw pre {
  margin: 10px 0 0;
  padding: 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
}

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 22px;
  width: min(440px, calc(100vw - 32px));
  box-shadow: 0 12px 32px rgb(16 24 40 / 18%);
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
dialog form { display: flex; flex-direction: column; gap: 14px; }

.steps { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--text-dim); flex-wrap: wrap; }
.steps b { color: var(--text); font-weight: 600; }
.steps span[aria-current="step"] { color: var(--accent); font-weight: 600; }

/* ---------- Narrow screens ---------- */
@media (max-width: 600px) {
  .topbar { padding: 10px 14px; gap: 8px; }
  .brand small { display: none; }
  .brand { min-width: 0; }
  .brand h1 { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  /* The status pill absorbs the squeeze so the key button never gets pushed off-screen. */
  .topbar .pill { flex: 0 1 auto; min-width: 0; overflow: hidden; }
  .topbar > button { flex: none; }
  #healthText { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  main { padding: 16px 14px 60px; gap: 16px; }
  .card { padding: 16px; }
  /* Keep the horizontal scroller flush with the card edge. */
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .actions .spacer { display: none; }
  .actions button { flex: 1 1 auto; }
}
