/*
 * Foliant design system. See UIUX_PATTERNS.md.
 * Type ramp: 4 sizes. Spacing scale: 6 values. Severity-only color.
 */

:root {
  /* Type */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --fs-title: 18px;
  --fs-body: 14px;
  --fs-small: 12px;
  --fs-chip: 11px;

  /* Spacing */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;

  /* Color: near-monochrome chrome */
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-subtle: #94a3b8;

  /* Color: severity (Carbon / Primer / Pajamas converged palette) */
  --error: #dc2626;
  --error-bg: #fef2f2;
  --error-border: #fecaca;
  --warning: #d97706;
  --warning-bg: #fffbeb;
  --warning-border: #fde68a;
  --info: #2563eb;
  --info-bg: #eff6ff;
  --success: #16a34a;
  --success-bg: #f0fdf4;

  /* Layout */
  --header-h: 44px;
  --rail-w: 220px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-body);
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}

code, .mono { font-family: var(--font-mono); font-size: 0.92em; }

/* ---------- Header ---------- */
header.app {
  position: sticky; top: 0; z-index: 10;
  height: var(--header-h);
  display: flex; align-items: center; gap: var(--s-3);
  padding: 0 var(--s-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
header.app .name {
  font-size: var(--fs-body); font-weight: 700; letter-spacing: 0.01em;
  margin-right: var(--s-3);
}
header.app .name .tagline { color: var(--text-muted); font-weight: 400; }
header.app .chips { display: flex; gap: var(--s-2); flex: 1; min-width: 0; }
header.app .spacer { flex: 1; }
.chip {
  display: inline-flex; align-items: center; gap: var(--s-1);
  padding: 2px var(--s-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  font-size: var(--fs-chip); color: var(--text-muted);
  max-width: 280px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip .label { color: var(--text-subtle); text-transform: uppercase; letter-spacing: 0.04em; }
.chip .value { color: var(--text); font-family: var(--font-mono); }
.chip.empty .value { font-style: italic; color: var(--text-subtle); font-family: var(--font-sans); }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  height: calc(100vh - var(--header-h));
  grid-template-columns: 1fr;
  overflow: hidden;
}
.layout.has-deck {
  grid-template-columns: minmax(420px, 1fr) minmax(420px, 1fr);
}
.pane {
  overflow: auto;
  padding: var(--s-5) var(--s-5);
  border-right: 1px solid var(--border);
}
.pane:last-child { border-right: 0; }

.section + .section { margin-top: var(--s-5); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1.section-title { font-size: var(--fs-title); margin-bottom: var(--s-3); }
h2.section-title { font-size: var(--fs-body); text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin-bottom: var(--s-2); }

/* ---------- Buttons ---------- */
button, .btn {
  font-family: inherit; font-size: var(--fs-body);
  padding: 6px var(--s-3);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  background: var(--surface); color: var(--text);
  cursor: pointer;
  display: inline-flex; align-items: center; gap: var(--s-2);
  transition: background-color 100ms ease, opacity 100ms ease;
}
button:hover, .btn:hover { background: var(--bg); }
button.primary { background: var(--text); color: var(--surface); border-color: var(--text); }
button.primary:hover { background: #1e293b; }
button.danger { background: var(--surface); color: var(--error); border-color: var(--error-border); }
button.danger:hover { background: var(--error-bg); }

/* Loading state: applies to the requesting element when htmx is in flight,
 * and to anything natively disabled. Shared across every button. */
button[disabled], button.htmx-request {
  cursor: wait;
  opacity: 0.7;
  pointer-events: none;
}
button .spinner {
  display: none;
  width: 12px; height: 12px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}
button.htmx-request .spinner { display: inline-block; }
button.htmx-request .label-default { display: none; }
button .label-loading { display: none; }
button.htmx-request .label-loading { display: inline; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Upload ---------- */
.upload {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: var(--s-4);
}
.upload form { display: flex; flex-wrap: wrap; align-items: end; gap: var(--s-3); }
.upload label { display: flex; flex-direction: column; gap: var(--s-1); font-size: var(--fs-small); color: var(--text-muted); flex: 1; min-width: 220px; }
.upload input[type="file"] { font-size: var(--fs-small); }
.upload .current { color: var(--text); font-family: var(--font-mono); font-size: var(--fs-small); }
.upload .help { color: var(--text-muted); font-size: var(--fs-small); margin: 0 0 var(--s-3); }

/* ---------- Findings ---------- */
.summary {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: var(--s-3) 0;
  margin: 0 0 var(--s-3);
  border-bottom: 1px solid var(--border);
}
.summary .counts { display: flex; gap: var(--s-3); align-items: center; margin-bottom: var(--s-2); }
.count {
  display: inline-flex; align-items: center; gap: var(--s-1);
  font-size: var(--fs-body); font-weight: 600;
}
.count.errors { color: var(--error); }
.count.warnings { color: var(--warning); }
.count.ok { color: var(--success); }
.count .num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

.chips-row { display: flex; gap: var(--s-1); flex-wrap: wrap; }
.filter-chip {
  font-size: var(--fs-chip);
  padding: 2px var(--s-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  cursor: pointer;
  color: var(--text-muted);
  user-select: none;
}
.filter-chip[aria-pressed="true"] { background: var(--text); color: var(--surface); border-color: var(--text); }
.filter-chip.error[aria-pressed="true"] { background: var(--error); border-color: var(--error); }
.filter-chip.warning[aria-pressed="true"] { background: var(--warning); border-color: var(--warning); }

/* findings table */
.slide-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-bottom: var(--s-2);
}
.slide-group > summary {
  list-style: none;
  cursor: pointer;
  padding: var(--s-2) var(--s-3);
  display: flex; align-items: center; gap: var(--s-2);
  font-size: var(--fs-small); font-weight: 600;
  border-bottom: 1px solid transparent;
}
.slide-group > summary::-webkit-details-marker { display: none; }
.slide-group > summary::before {
  content: "▸"; color: var(--text-muted); width: 12px; display: inline-block;
}
.slide-group[open] > summary::before { content: "▾"; }
.slide-group[open] > summary { border-bottom-color: var(--border); }
.slide-group .pill {
  font-family: var(--font-mono); font-size: var(--fs-chip);
  padding: 1px 6px; border-radius: 3px;
  background: var(--bg); color: var(--text-muted);
}
.slide-group .pill.has-error { background: var(--error-bg); color: var(--error); }

.findings-table { width: 100%; border-collapse: collapse; }
.findings-table tr.row { border-top: 1px solid var(--border); }
.findings-table tr.row:first-child { border-top: 0; }
.findings-table td { padding: var(--s-2) var(--s-3); vertical-align: top; font-size: var(--fs-small); }
.findings-table td.gutter { width: 18px; padding-right: 0; }
.findings-table td.ref { font-family: var(--font-mono); white-space: nowrap; color: var(--text); }
.findings-table td.reason { color: var(--text-muted); }

/* severity icon dots */
.icon {
  display: inline-block; width: 14px; height: 14px; border-radius: 50%;
  text-align: center; line-height: 14px;
  font-size: 10px; font-weight: 700; color: var(--surface);
  font-family: var(--font-sans);
}
.icon.error { background: var(--error); }
.icon.warning { background: var(--warning); }
.icon.info { background: var(--info); }
.icon.error::before { content: "!"; }
.icon.warning::before { content: "!"; }
.icon.info::before { content: "i"; }

/* row tinting by kind */
.findings-table tr.kind-BROKEN_REF td.gutter,
.findings-table tr.kind-TYPO_LIKELY td.gutter { box-shadow: inset 3px 0 0 var(--error); }
.findings-table tr.kind-MISSING_VALUE td.gutter,
.findings-table tr.kind-SOURCE_ERROR td.gutter { box-shadow: inset 3px 0 0 var(--warning); }

/* filter visibility: when a chip is OFF, its rows hide */
.findings[data-show-errors="off"] tr.kind-BROKEN_REF,
.findings[data-show-errors="off"] tr.kind-TYPO_LIKELY { display: none; }
.findings[data-show-warnings="off"] tr.kind-MISSING_VALUE,
.findings[data-show-warnings="off"] tr.kind-SOURCE_ERROR { display: none; }

/* hide groups whose visible rows count drops to zero. CSS :has() handles this on modern browsers. */
.findings .slide-group:not(:has(tr.row:not([hidden]))):has(tr.row) { /* no-op fallback */ }

/* ---------- Preview pane ---------- */
.preview-shell { display: flex; flex-direction: column; height: 100%; }
.preview-toolbar {
  display: flex; align-items: center; gap: var(--s-3);
  padding: var(--s-2) 0 var(--s-3);
  font-size: var(--fs-small);
}
.preview-toolbar a { color: var(--info); text-decoration: none; }
.preview-toolbar a:hover { text-decoration: underline; }
.preview-frame {
  flex: 1; min-height: 0;
  border: 1px solid var(--border); border-radius: 4px; overflow: hidden;
  background: #1f2937;
}
.preview-frame iframe { width: 100%; height: 100%; border: 0; display: block; }
.preview-empty { color: var(--text-muted); }

/* ---------- Phase progress ---------- */
.progress {
  display: none; /* shown only when htmx-request */
  flex-direction: column; gap: var(--s-2);
  padding: var(--s-2) 0;
  font-size: var(--fs-small);
  color: var(--text-muted);
}
.htmx-request .progress { display: flex; }
.progress .bar {
  position: relative; height: 4px; background: var(--border); border-radius: 2px; overflow: hidden;
}
.progress .bar::after {
  content: ""; position: absolute; left: 0; top: 0; height: 100%; width: 0%;
  background: var(--info);
  animation: phase-fill 12s ease-out forwards;
}
.progress .phases { display: flex; gap: var(--s-3); }
.progress .phase { opacity: 0.4; }
.progress .phase.active { opacity: 1; color: var(--text); font-weight: 600; }
.progress .phase-1 { animation: phase-1 12s steps(1, end) forwards; }
.progress .phase-2 { animation: phase-2 12s steps(1, end) forwards; }
.progress .phase-3 { animation: phase-3 12s steps(1, end) forwards; }

@keyframes phase-fill { from { width: 0; } to { width: 100%; } }
@keyframes phase-1 { 0% { opacity: 1; font-weight: 600; color: var(--text); } 35%, 100% { opacity: 0.4; font-weight: 400; color: inherit; } }
@keyframes phase-2 {
  0%, 35% { opacity: 0.4; font-weight: 400; color: inherit; }
  36%, 75% { opacity: 1; font-weight: 600; color: var(--text); }
  76%, 100% { opacity: 0.4; font-weight: 400; color: inherit; }
}
@keyframes phase-3 { 0%, 75% { opacity: 0.4; font-weight: 400; color: inherit; } 76%, 100% { opacity: 1; font-weight: 600; color: var(--text); } }

/* ---------- htmx indicators ---------- */
.indicator { display: none; color: var(--text-muted); margin-left: var(--s-2); font-size: var(--fs-small); }
.htmx-request .indicator { display: inline; }

/* ---------- Empty / OK states ---------- */
.empty {
  text-align: center; padding: var(--s-6) var(--s-4);
  color: var(--text-muted);
}
.ok-state { color: var(--success); font-weight: 600; padding: var(--s-3); background: var(--success-bg); border: 1px solid var(--success); border-radius: 4px; }
.error-banner { color: var(--error); padding: var(--s-3); background: var(--error-bg); border: 1px solid var(--error-border); border-radius: 4px; }

/* small util */
.kbd { font-family: var(--font-mono); font-size: var(--fs-chip); padding: 1px 4px; border: 1px solid var(--border-strong); border-radius: 3px; background: var(--bg); }

/* ---------- Top nav links in header ---------- */
header.app .name { text-decoration: none; color: inherit; }
header.app .topnav { display: flex; gap: var(--s-3); margin-right: var(--s-3); }
header.app .navlink {
  font-size: var(--fs-small); color: var(--text-muted); text-decoration: none;
  padding: 2px var(--s-2); border-radius: 3px;
}
header.app .navlink:hover { color: var(--text); background: var(--bg); }

/* ---------- History page ---------- */
details.project-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: var(--s-3);
}
details.project-card > summary {
  list-style: none;
  cursor: pointer;
  padding: var(--s-3) var(--s-4);
  display: flex; align-items: center; gap: var(--s-2);
  font-size: var(--fs-body);
}
details.project-card > summary::-webkit-details-marker { display: none; }
details.project-card > summary::before {
  content: "▸"; color: var(--text-muted); width: 12px;
}
details.project-card[open] > summary::before { content: "▾"; }
details.project-card > summary strong { font-weight: 600; }

.generations-table {
  width: 100%; border-collapse: collapse; font-size: var(--fs-small);
  border-top: 1px solid var(--border);
}
.generations-table th, .generations-table td {
  padding: var(--s-2) var(--s-3); text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.generations-table th { font-weight: 600; color: var(--text-muted); font-size: var(--fs-chip); text-transform: uppercase; letter-spacing: 0.04em; }
.generations-table td.actions { text-align: right; }
.generations-table tr.status-superseded td:not(.actions) { color: var(--text-muted); }
.generations-table tr.status-superseded td.mono { text-decoration: line-through; }
.generations-table .btn { padding: 2px 8px; font-size: var(--fs-chip); }

details.project-card > p { padding: var(--s-3) var(--s-4); margin: 0; }

/* ---------- Status badges ---------- */
.badge {
  display: inline-block; padding: 1px 8px;
  font-size: var(--fs-chip); font-weight: 600;
  border-radius: 999px;
  text-transform: lowercase;
  letter-spacing: 0.02em;
}
.badge.active { background: var(--success-bg); color: var(--success); }
.badge.superseded { background: var(--bg); color: var(--text-muted); border: 1px solid var(--border); }
.badge.failed { background: var(--error-bg); color: var(--error); }

.muted { color: var(--text-muted); }
.list-plain { list-style: none; padding: 0; margin: 0; }
.list-plain li { padding: var(--s-1) 0; border-bottom: 1px dotted var(--border); }
.list-plain li:last-child { border-bottom: 0; }

.section.migration {
  background: var(--info-bg);
  border: 1px solid var(--info);
  border-radius: 6px;
  padding: var(--s-3) var(--s-4);
}
.section.migration p { margin: 0; }
.count.migration-count { color: var(--info); margin-right: var(--s-1); }

/* ---------- Banner: building-type detection ---------- */
.section.banner {
  border-radius: 6px;
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--border);
}
.section.banner-auto {
  background: var(--info-bg, #eef);
  border-color: var(--info, #99c);
}
.section.banner-override {
  background: var(--warning-bg, #fff5e6);
  border-color: var(--warning, #d80);
}
.section.banner-unknown {
  background: var(--error-bg, #fee);
  border-color: var(--error, #c33);
}
.section.banner p { margin: 0 0 var(--s-2) 0; }
.section.banner .pill {
  display: inline-block; padding: 1px 8px;
  font-size: var(--fs-chip); font-weight: 600;
  border-radius: 999px; text-transform: lowercase;
  background: var(--bg); border: 1px solid var(--border);
}
.section.banner .banner-actions { display: flex; gap: var(--s-2); flex-wrap: wrap; }
.section.banner .banner-actions form { margin: 0; }
.section.banner .ref { font-family: var(--font-mono); color: var(--text-muted); }

.flash {
  display: inline-block;
  padding: var(--s-1) var(--s-3);
  background: var(--success-bg, #e6f7e6);
  color: var(--success, #196b19);
  border: 1px solid var(--success, #196b19);
  border-radius: 4px;
  font-size: var(--fs-small);
}

/* ---------- Regenerate confirm: combined radio groups ---------- */
.section.regen-confirm fieldset.radio-group {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: var(--s-2) var(--s-3);
  margin: 0 0 var(--s-3) 0;
}
.section.regen-confirm fieldset.radio-group legend {
  padding: 0 var(--s-2);
  font-weight: 600;
  font-size: var(--fs-small);
  color: var(--text-muted);
}
.section.regen-confirm label.radio {
  display: flex;
  gap: var(--s-2);
  padding: var(--s-2) 0;
  align-items: flex-start;
}
.section.regen-confirm label.radio input[type="radio"] { margin-top: 4px; }
.section.regen-confirm label.radio .help { font-size: var(--fs-small); color: var(--text-muted); }

/* ---------- Update report ---------- */
.section.update-report code {
  background: var(--surface);
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 0.95em;
}
.row.kind-DIVERGED .reason { color: var(--warning, #b06800); }

/* ---------- Drive picker: input + loading state ---------- */
.picker-input-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--s-2);
}
.picker-input-row input[type="search"] {
  flex: 1;
  padding: var(--s-2) var(--s-3);
  font-size: var(--fs-base);
  box-sizing: border-box;
}
.picker-spinner {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  font-size: var(--fs-small);
  color: var(--text-muted);
  opacity: 0;
  transition: opacity 150ms ease-in;
  pointer-events: none;
  white-space: nowrap;
}
.picker-spinner .spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}
/* htmx adds .htmx-request to the element pointed to by hx-indicator
   for the lifetime of the in-flight request. */
.picker-spinner.htmx-request { opacity: 1; }

/* ---------- Login form ---------- */
.login-card {
  max-width: 420px;
  margin: var(--s-5) auto;
}
.login-card label {
  display: flex;
  flex-direction: column;
  gap: var(--s-1);
  margin-bottom: var(--s-3);
  font-size: var(--fs-small);
  color: var(--text-muted);
}
.login-card input[type="text"],
.login-card input[type="password"] {
  padding: var(--s-2) var(--s-3);
  font-size: var(--fs-base);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: var(--font-sans);
  color: var(--text);
}
.login-card button {
  width: 100%;
}
