:root {
  --bg: #edf0f3;
  --panel: #ffffff;
  --ink: #1a2430;
  --muted: #5a6573;
  --line: #d4dae1;
  --line-soft: #e7ebef;
  --accent: #0e5a6b;
  --accent-soft: #e4eff1;
  --best: #d8eee0;
  --best-ink: #1d6a3b;
  --weak: #f9e2d4;
  --weak-ink: #8f3e12;
  --error: #a3271b;

  font-family: "Public Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; }
button, input, select { font: inherit; color: inherit; }
h1, h2 { margin: 0; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.page {
  max-width: 1280px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 4rem;
  display: grid;
  gap: 1.75rem;
}

/* ---------- Header ---------- */

.masthead {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1rem 2rem;
}
.intro { max-width: 40rem; }
.intro h1 { font-size: 2.1rem; font-weight: 700; letter-spacing: -0.015em; line-height: 1.1; }
.intro p { margin: 0.5rem 0 0; color: var(--muted); }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }
.save-status { font-size: 0.85rem; color: var(--muted); margin-right: 0.5rem; }
.save-status[data-state="ok"] { color: var(--best-ink); }
.save-status[data-state="error"] { color: var(--error); }

.btn {
  display: inline-flex; align-items: center;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn-quiet { background: transparent; border-color: transparent; color: var(--muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #0b4a58; color: #fff; }
.btn-danger { color: var(--error); border-color: transparent; background: transparent; }
.btn-danger:hover { border-color: var(--error); color: var(--error); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn-small { padding: 0.3rem 0.7rem; font-size: 0.875rem; }

.hint { margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--muted); }

/* ---------- Standings ---------- */

.summary {
  display: grid;
  grid-template-columns: minmax(0, 2.2fr) minmax(16rem, 1fr);
  gap: 1.25rem;
}
.standings, .weights-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1.25rem 1.5rem;
}

.tabs { display: flex; flex-wrap: wrap; gap: 0.25rem; border-bottom: 1px solid var(--line-soft); margin-bottom: 1rem; }
.tabs button {
  border: 0; background: none; cursor: pointer;
  padding: 0.5rem 0.75rem;
  color: var(--muted);
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }

.standings-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.25rem; }
.standings-list li {
  display: grid;
  grid-template-columns: minmax(8rem, 13rem) 1fr 4.5rem;
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 0;
}
.st-name { display: flex; flex-direction: column; font-weight: 600; min-width: 0; }
.st-name > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-domain { font-weight: 400; font-size: 0.8rem; color: var(--muted); }
.st-bar { height: 12px; background: var(--line-soft); border-radius: 2px; overflow: hidden; }
.st-bar > span { display: block; height: 100%; background: #8aa6ae; transition: width 0.35s ease; }
.is-you .st-bar > span { background: var(--accent); }
.st-score {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  text-align: right;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.you-tag {
  display: inline-block;
  padding: 0.05rem 0.4rem;
  border-radius: 3px;
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  vertical-align: 0.1em;
}

.weights-panel h2 { font-size: 1rem; font-weight: 600; margin-bottom: 0.75rem; }
.weights { display: grid; gap: 0.5rem; }
.weight { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.weight-input { display: inline-flex; align-items: center; gap: 0.25rem; color: var(--muted); }
.weight-input input {
  width: 4.25rem; padding: 0.3rem 0.4rem; text-align: right;
  border: 1px solid var(--line); border-radius: 5px; background: var(--panel);
  font-variant-numeric: tabular-nums;
}

/* ---------- Metric table ---------- */

.grid-wrap {
  overflow-x: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.grid { width: 100%; border-collapse: separate; border-spacing: 0; min-width: 640px; }
.grid th, .grid td { border-bottom: 1px solid var(--line-soft); padding: 0.45rem 0.6rem; text-align: left; vertical-align: middle; }

.metric-col {
  position: sticky; left: 0; z-index: 1;
  background: var(--panel);
  min-width: 15rem; max-width: 19rem;
  font-weight: 400;
  border-right: 1px solid var(--line-soft);
}
thead .metric-col { font-weight: 600; color: var(--muted); vertical-align: bottom; }

.site-col { min-width: 10rem; vertical-align: bottom; }
.site-col.is-you, .grid td.is-you { background: var(--accent-soft); }
.site-head { position: relative; display: grid; gap: 0.15rem; padding-right: 1.25rem; }
.site-head .you-tag { justify-self: start; margin-bottom: 0.15rem; }
.site-head { padding-right: 0; gap: 0.2rem; }
.site-name { font-weight: 600; font-size: 1rem; }
.site-domain { font-size: 0.82rem; color: var(--accent); text-decoration: none; overflow-wrap: anywhere; }
a.site-domain:hover { text-decoration: underline; }
.site-domain.is-missing { color: var(--muted); }
.crawl-status { font-size: 0.78rem; font-weight: 400; color: var(--muted); min-height: 1.1em; }
.crawl-line { display: block; }
.crawl-line.is-busy { color: var(--accent); }
.crawl-line.is-busy::before {
  content: ""; display: inline-block; width: 0.55rem; height: 0.55rem; margin-right: 0.35rem;
  border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent;
  animation: spin 0.8s linear infinite; vertical-align: -0.05em;
}
.crawl-line.is-error { color: var(--error); }
.crawl-notes summary { cursor: pointer; color: var(--accent); }
.crawl-notes ul { margin: 0.25rem 0 0; padding-left: 1rem; max-width: 16rem; }
.site-actions { display: flex; flex-wrap: wrap; gap: 0.3rem; margin-top: 0.35rem; }
@keyframes spin { to { transform: rotate(360deg); } }

.auto-val {
  display: block; padding: 0.35rem 0.45rem;
  font-variant-numeric: tabular-nums;
  border: 1px solid transparent;
}
.grid td.best .auto-val { color: var(--best-ink); font-weight: 600; }
.grid td.weakest .auto-val { color: var(--weak-ink); }

.src {
  display: inline-block; padding: 0 0.3rem; margin-right: 0.2rem;
  border-radius: 3px; font-size: 0.7rem; font-weight: 600; line-height: 1.5;
}
.src-crawl { background: var(--accent-soft); color: var(--accent); }
.src-speed { background: #e7e9f6; color: #3b4486; }
.src-manual { background: #f1ede4; color: #6b5a2e; }

.remove {
  border: 0; background: none; cursor: pointer;
  color: var(--muted); font-size: 1.1rem; line-height: 1;
  padding: 0.1rem 0.3rem; border-radius: 4px;
}
.remove:hover { color: var(--error); background: var(--weak); }
.remove-site { position: absolute; right: -0.25rem; top: 0; }

.cat-head th {
  background: var(--bg);
  padding-top: 0.9rem;
  border-bottom-color: var(--line);
}
.cat-head .metric-col, .cat-head th { position: static; }
.cat-name { font-weight: 700; font-size: 1rem; margin-right: 0.75rem; }
.cat-tools { color: var(--muted); font-size: 0.85rem; }

.metric-label { display: flex; align-items: baseline; gap: 0.4rem; font-weight: 500; }
.metric-label .remove { margin-left: auto; font-size: 1rem; }
.dir { color: var(--accent); font-size: 0.8rem; font-weight: 600; white-space: nowrap; cursor: help; }
.metric-source { font-size: 0.8rem; color: var(--muted); }

.grid td.val input, .grid td.val select {
  width: 100%;
  padding: 0.35rem 0.45rem;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel);
  font-variant-numeric: tabular-nums;
}
.grid td.val input.invalid { border-color: var(--error); }
.grid td.best { background: var(--best); }
.grid td.best input, .grid td.best select { border-color: #a9d6b8; color: var(--best-ink); font-weight: 600; }
.grid td.weakest { background: var(--weak); }
.grid td.weakest input, .grid td.weakest select { border-color: #ecc0a5; color: var(--weak-ink); }

.cat-score th, .cat-score td { font-weight: 600; font-variant-numeric: tabular-nums; border-bottom: 0; }
.cat-score td { font-size: 1.05rem; padding-left: 1rem; }

.add-metric-row td { padding: 0.4rem 0.6rem 1rem; border-bottom: 0; }
.add-metric { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; }
.add-metric input, .add-metric select {
  padding: 0.3rem 0.45rem; border: 1px dashed var(--line); border-radius: 5px;
  background: var(--panel); font-size: 0.875rem;
}
.add-metric input[name="label"] { min-width: 14rem; }

.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem 1rem; margin: 0.6rem 0 0; font-size: 0.85rem; color: var(--muted); }
.swatch { display: inline-block; width: 0.9rem; height: 0.9rem; border-radius: 3px; vertical-align: -0.1em; margin-right: -0.6rem; }
.swatch-best { background: var(--best); border: 1px solid #a9d6b8; }
.swatch-weak { background: var(--weak); border: 1px solid #ecc0a5; }
.legend-dir { margin-left: auto; }

/* ---------- Insights ---------- */

.insights { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }
.insights > div { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 1.25rem 1.5rem; }
.insights h2 { font-size: 1.15rem; font-weight: 700; }
.insight-list { list-style: none; margin: 0.9rem 0 0; padding: 0; }
.insight-list li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.1rem 1rem;
  padding: 0.6rem 0;
  border-top: 1px solid var(--line-soft);
}
.insight-list li.empty { display: block; color: var(--muted); font-size: 0.9rem; }
.ins-main { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem; }
.ins-cat { font-size: 0.8rem; color: var(--muted); }
.ins-detail { grid-column: 1; font-size: 0.875rem; color: var(--muted); }
.gap {
  grid-column: 2; grid-row: 1 / span 2; align-self: center;
  font-weight: 700; font-variant-numeric: tabular-nums;
  color: var(--weak-ink);
}
.gap-lead { color: var(--best-ink); }
.gap-tie { color: var(--muted); font-weight: 600; }

/* ---------- Site form ---------- */

.site-dialog {
  width: min(36rem, calc(100vw - 2rem));
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 1.5rem;
  color: var(--ink);
  background: var(--panel);
}
.site-dialog::backdrop { background: rgba(26, 36, 48, 0.45); }
.site-dialog h2 { font-size: 1.3rem; font-weight: 700; margin-bottom: 1rem; }
.field { display: grid; gap: 0.25rem; margin-bottom: 0.9rem; }
.field label { font-weight: 600; font-size: 0.92rem; }
.optional { font-weight: 400; color: var(--muted); font-size: 0.82rem; }
.field input, .field textarea {
  width: 100%; padding: 0.5rem 0.6rem;
  border: 1px solid var(--line); border-radius: 6px; background: var(--panel);
}
.field textarea { resize: vertical; font-size: 0.9rem; }
.field [aria-invalid="true"] { border-color: var(--error); }
.field-help { margin: 0; font-size: 0.8rem; color: var(--muted); }
.field-error { margin: 0; font-size: 0.82rem; color: var(--error); }
.field-error:empty { display: none; }
.field-row { display: grid; grid-template-columns: 1fr 9rem; gap: 1rem; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-top: 1.25rem; }
.dialog-actions .spacer { flex: 1; }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .summary, .insights { grid-template-columns: 1fr; }
  .metric-col { min-width: 11rem; }
}
@media (max-width: 560px) {
  .page { padding: 1.5rem 0.9rem 3rem; }
  .intro h1 { font-size: 1.7rem; }
  .standings-list li { grid-template-columns: 1fr 3.5rem; }
  .st-bar { grid-column: 1 / -1; grid-row: 2; }
  .st-score { font-size: 1.6rem; grid-row: 1; grid-column: 2; }
  .legend-dir { margin-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .st-bar > span { transition: none; }
  .crawl-line.is-busy::before { animation: none; }
}
@media (max-width: 560px) {
  .field-row { grid-template-columns: 1fr; }
}

/* ---------- Print ---------- */

@media print {
  :root { background: #fff; font-size: 11px; }
  .page { padding: 0; max-width: none; gap: 1rem; }
  .toolbar, .add-metric-row, .remove, .tabs, .hint, #weight-note, .site-actions, .crawl-notes, .src { display: none !important; }
  .standings, .weights-panel, .grid-wrap, .insights > div { border-color: #bbb; break-inside: avoid; }
  .grid-wrap { overflow: visible; }
  .metric-col { position: static; }
  .grid td.val input, .grid td.val select, .site-name, .site-domain, .weight-input input {
    border: 0; padding: 0; background: transparent; appearance: none; -webkit-appearance: none;
  }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
