/* ---- Design Tokens */

:root {
  /* Typography */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", "Segoe UI Mono", Menlo, monospace;
  --line-height: 1.5;

  /* Colors */
  --bg: #fff;
  --bg-surface: #f8f9fa;
  --text: #1a1a2e;
  --text-muted: #6c757d;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-text: #fff;
  --border: #dee2e6;

  /* Shape */
  --radius: 0.375rem;
  --spacing: 1rem;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --heading-weight: 700;

  /* Semantic */
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;

}

/* ---- Backward Compat Aliases (--pico-* → new vars) */

:root {
  --pico-block-spacing-vertical: var(--spacing);
  --pico-muted-border-color: var(--border);
  --pico-muted-color: var(--text-muted);
  --pico-border-radius: var(--radius);
  --pico-card-background-color: var(--bg-surface);
  --pico-primary: var(--accent);
  --pico-primary-background: var(--accent);
  --pico-primary-inverse: var(--accent-text);
  --pico-color: var(--text);
  --pico-font-family-monospace: var(--font-mono);
  --pico-ins-color: var(--success);
  --pico-del-color: var(--warning);
  --pico-color-red-500: var(--danger);
}

/* ---- Normalize */

*, *::before, *::after { box-sizing: border-box; }

:where(html) {
  font-family: var(--font);
  font-size: 100%;
  line-height: var(--line-height);
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
}

:where(body) {
  margin: 0;
  min-height: 100vh;
}

/* ---- Typography */

:where(h1, h2, h3, h4, h5, h6) {
  margin: 0 0 0.5em;
  line-height: 1.2;
  font-weight: var(--heading-weight);
  text-wrap: balance;
}
:where(h1) { font-size: 2rem; }
:where(h2) { font-size: 1.6rem; }
:where(h3) { font-size: 1.35rem; }
:where(h4) { font-size: 1.15rem; }
:where(h5) { font-size: 1rem; }
:where(h6) { font-size: 0.875rem; }

:where(hgroup) { margin: 0 0 var(--spacing); }
:where(hgroup > *:last-child) { color: var(--text-muted); font-weight: 400; }

:where(p) { margin: 0 0 var(--spacing); }

:where(a) {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}
:where(a:hover) { color: var(--accent-hover); }
:where(a:visited) { color: var(--accent); }

:where(small) { font-size: 0.875em; }
:where(mark) { background: #fef08a; padding: 0.1em 0.2em; border-radius: 2px; }
:where(del) { color: var(--danger); }
:where(ins) { color: var(--success); text-decoration: none; }

:where(blockquote) {
  margin: 0 0 var(--spacing);
  padding: 0.5em 0 0.5em 1.25em;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
}

:where(hr) {
  border: none;
  border-top: 1px solid var(--border);
  margin: calc(var(--spacing) * 1.5) 0;
}

/* ---- Code */

:where(code, kbd, samp) {
  font-family: var(--font-mono);
  font-size: 0.875em;
}
:where(:not(pre) > code) {
  background: var(--bg-surface);
  padding: 0.15em 0.35em;
  border-radius: var(--radius);
}
:where(pre) {
  margin: 0 0 var(--spacing);
  padding: var(--spacing);
  background: var(--bg-surface);
  border-radius: var(--radius);
  overflow-x: auto;
}
:where(pre > code) { background: none; padding: 0; font-size: 0.875em; }
:where(kbd) {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.1em 0.35em;
  font-size: 0.8em;
}

/* ---- Cards (article) */

:where(article) {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--spacing);
  margin: 0 0 var(--spacing);
}
:where(article > header:first-child) {
  margin: calc(var(--spacing) * -1) calc(var(--spacing) * -1) var(--spacing);
  padding: var(--spacing);
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  font-weight: 700;
}
:where(article > footer:last-child) {
  margin: var(--spacing) calc(var(--spacing) * -1) calc(var(--spacing) * -1);
  padding: var(--spacing);
  border-top: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
}

/* ---- Buttons */

:where(button, [role="button"], input[type="submit"], input[type="reset"], input[type="button"]) {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5em 1em;
  font: inherit;
  font-size: 0.9375rem;
  line-height: 1.5;
  color: var(--accent-text);
  background: var(--accent);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  text-decoration: none;
}
:where(button, [role="button"], input[type="submit"], input[type="reset"], input[type="button"]):hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
:where(button, [role="button"], input[type="submit"], input[type="reset"], input[type="button"]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
:where(button, [role="button"]):disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

:where(.outline) {
  color: var(--accent);
  background: transparent;
}
:where(.outline:hover) { background: var(--accent); color: var(--accent-text); }

:where(.secondary) {
  color: var(--text-muted);
  background: var(--bg-surface);
  border-color: var(--border);
}
:where(.secondary:hover) { background: var(--border); color: var(--text); }

:where(.contrast) {
  color: var(--bg);
  background: var(--text);
  border-color: var(--text);
}
:where(.contrast:hover) { opacity: 0.85; }

/* ---- Forms */

:where(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select, textarea) {
  display: block;
  width: 100%;
  padding: 0.5em 0.75em;
  font: inherit;
  font-size: 0.9375rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.15s;
}
:where(input, select, textarea):focus {
  border-color: var(--accent);
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
:where(input, select, textarea):disabled { opacity: 0.5; cursor: not-allowed; }

:where(label) { display: block; margin-bottom: 0.25em; font-weight: 500; }
:where(fieldset) { border: 1px solid var(--border); border-radius: var(--radius); padding: var(--spacing); margin: 0 0 var(--spacing); }
:where(legend) { font-weight: 700; padding: 0 0.25em; }

:where([aria-invalid="true"]) { border-color: var(--danger) !important; }
:where([aria-invalid="false"]) { border-color: var(--success) !important; }

:where(input[role="switch"]) {
  appearance: none;
  width: 2.5em;
  height: 1.25em;
  background: var(--border);
  border-radius: 1em;
  position: relative;
  cursor: pointer;
  display: inline-block;
  vertical-align: middle;
  border: none;
  outline-offset: 2px;
}
:where(input[role="switch"])::before {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: calc(1.25em - 4px);
  height: calc(1.25em - 4px);
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s;
}
:where(input[role="switch"]:checked) { background: var(--accent); }
:where(input[role="switch"]:checked)::before { transform: translateX(1.25em); }

/* ---- Tables */

:where(table) {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 var(--spacing);
}
:where(th, td) {
  padding: 0.5em 0.75em;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
:where(th) { font-weight: 600; }
:where(tbody tr:hover) { background: var(--bg-surface); }

/* ---- Layout */

:where(.container) {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--spacing);
}

:where(.grid) {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  gap: var(--spacing);
}

:where([role="group"]) {
  display: inline-flex;
  border-radius: var(--radius);
}
:where(fieldset[role="group"]) {
  display: flex;
  width: 100%;
  padding: 0;
  margin: 0;
  border: none;
}
:where(fieldset[role="group"] > input,
  fieldset[role="group"] > select,
  fieldset[role="group"] > textarea) { flex: 1; min-width: 0; }
:where([role="group"] > *) { border-radius: 0; }
:where([role="group"] > :first-child) { border-radius: var(--radius) 0 0 var(--radius); }
:where([role="group"] > :last-child) { border-radius: 0 var(--radius) var(--radius) 0; }
:where([role="group"] > * + *) { border-left: 0; }

/* ---- Navigation */

:where(nav) {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

:where(nav ul) {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  gap: 0.5em;
  align-items: center;
}
:where(nav a) { text-decoration: none; }

/* ---- Interactive */

:where(details) {
  margin: 0 0 var(--spacing);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--spacing);
}
:where(summary) { cursor: pointer; font-weight: 600; }
:where(details[open] > summary) { margin-bottom: 0.5em; }

:where(dialog) {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  max-width: min(90vw, 600px);
}
:where(dialog::backdrop) { background: rgba(0, 0, 0, 0.5); }
:where(dialog > article) { margin: 0; border: none; }

/* ---- Progress / Loading */

:where(progress) {
  appearance: none;
  height: 0.5em;
  border: none;
  border-radius: var(--radius);
  background: var(--bg-surface);
}
:where(progress)::-webkit-progress-bar { background: var(--bg-surface); border-radius: var(--radius); }
:where(progress)::-webkit-progress-value { background: var(--accent); border-radius: var(--radius); }
:where(progress)::-moz-progress-bar { background: var(--accent); border-radius: var(--radius); }

:where([aria-busy="true"]) {
  position: relative;
  pointer-events: none;
  opacity: 0.7;
}
:where([aria-busy="true"])::after {
  content: "";
  display: inline-block;
  width: 1em; height: 1em;
  margin-left: 0.5em;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: anvil-spin 0.6s linear infinite;
  vertical-align: middle;
}
@keyframes anvil-spin { to { transform: rotate(360deg); } }

/* ---- Media */

:where(img, svg, video, canvas) { max-width: 100%; height: auto; display: block; }
:where(figure) { margin: 0 0 var(--spacing); }
:where(figcaption) { color: var(--text-muted); font-size: 0.875em; margin-top: 0.5em; }
