/* Components. Colours, spacing, radius and type come from tokens.css only. */

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  font-kerning: normal;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: var(--weight-semibold);
  text-wrap: balance;
}
h1 { font-size: var(--text-2xl); line-height: var(--leading-tight); letter-spacing: var(--tracking-display); }
h2 { font-size: var(--text-xl); line-height: var(--leading-snug); letter-spacing: var(--tracking-heading); }
h3 { font-size: var(--text-lg); line-height: var(--leading-snug); letter-spacing: var(--tracking-heading); }
p { margin: 0; text-wrap: pretty; }
a { color: var(--color-accent); text-decoration-thickness: 1px; text-underline-offset: 0.22em; }
a:hover { color: var(--color-accent-hover); text-decoration-thickness: 2px; }
code { font-family: var(--font-mono); font-size: 0.9em; }

:focus-visible {
  outline: var(--focus-ring-width) solid var(--color-focus);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
}

[hidden] { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.text-muted { color: var(--color-text-muted); }
.text-sm { font-size: var(--text-sm); }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---- layout helpers ---- */
.stack { display: flex; flex-direction: column; gap: var(--space-4); }
.stack-lg { display: flex; flex-direction: column; gap: var(--space-12); }
.cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
.container { width: 100%; max-width: var(--content-width); margin-inline: auto; padding-inline: var(--space-6); }

/* ---- app shell ---- */
.skip-link {
  position: absolute;
  left: var(--space-4);
  top: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  transform: translateY(-300%);
}
.skip-link:focus { transform: translateY(0); }

.app-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}
.app-header-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-8);
  min-height: 3.75rem;
}
.brand {
  font-family: var(--font-display);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-heading);
  color: var(--color-text);
  text-decoration: none;
}

.nav { display: flex; flex-wrap: wrap; gap: var(--space-1); }
.nav-link {
  display: inline-flex;
  align-items: center;
  min-height: 2.25rem;
  padding: 0 var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  letter-spacing: var(--tracking-label);
  text-decoration: none;
}
.nav-link:hover { color: var(--color-text); background: var(--color-surface-sunken); }
.nav-link[aria-current="page"] { color: var(--color-text); background: var(--color-surface-sunken); }

.app-main { padding-block: var(--space-12) var(--space-20); }

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3) var(--space-6);
  margin-bottom: var(--space-2);
}
.page-header p { color: var(--color-text-muted); margin-top: var(--space-2); max-width: 46ch; }
.section-lede { color: var(--color-text-muted); font-size: var(--text-sm); max-width: 56ch; margin-top: calc(var(--space-2) * -1); }

/* ---- buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-height);
  padding: 0 var(--space-5);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font: inherit;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  letter-spacing: var(--tracking-label);
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
}
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--color-primary); color: var(--color-on-primary); box-shadow: var(--shadow-press); }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); }
.btn-secondary { background: var(--color-surface); color: var(--color-text); border-color: var(--color-border-strong); }
.btn-secondary:hover:not(:disabled) { background: var(--color-surface-sunken); }
.btn-ghost { background: transparent; color: var(--color-text-muted); }
.btn-ghost:hover:not(:disabled) { background: var(--color-surface-sunken); color: var(--color-text); }
.btn-danger { background: var(--color-danger); color: var(--color-on-danger); box-shadow: var(--shadow-press); }
.btn-danger:hover:not(:disabled) { background: var(--color-danger-hover); }

/* ---- form controls ---- */
.input, .select, .textarea {
  width: 100%;
  min-height: var(--control-height);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  font: inherit;
  font-size: var(--text-base);
}
.textarea { min-height: 6rem; resize: vertical; }
.input::placeholder, .textarea::placeholder { color: var(--color-text-muted); opacity: 0.85; }
.input:hover:not(:disabled), .select:hover:not(:disabled), .textarea:hover:not(:disabled) { border-color: var(--color-text-muted); }
.input[aria-invalid="true"], .select[aria-invalid="true"], .textarea[aria-invalid="true"] {
  border-color: var(--color-danger);
}
.input:disabled, .select:disabled, .textarea:disabled { background: var(--color-surface-sunken); cursor: not-allowed; }

.checkbox { display: inline-flex; align-items: center; gap: var(--space-3); cursor: pointer; font-size: var(--text-sm); }
.checkbox input {
  width: 1.125rem;
  height: 1.125rem;
  margin: 0;
  accent-color: var(--color-accent);
}

.field { display: flex; flex-direction: column; gap: var(--space-2); }
.field-label { font-size: var(--text-sm); font-weight: var(--weight-medium); letter-spacing: var(--tracking-label); }
.field-hint { font-size: var(--text-sm); color: var(--color-text-muted); }
.field-error { font-size: var(--text-sm); color: var(--color-danger); }

/* ---- card (never nest a card inside a card) ---- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
  padding: var(--space-8);
}
.card-title { margin-bottom: var(--space-4); }

/* ---- table ---- */
.table-wrap {
  overflow-x: auto;
  padding: var(--space-2);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
}
.table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.table caption { text-align: left; padding: var(--space-4); font-weight: var(--weight-semibold); font-size: var(--text-base); }
.table th, .table td { padding: var(--space-4); text-align: left; vertical-align: middle; font-variant-numeric: tabular-nums; }
.table th {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
  border-bottom: 1px solid var(--color-border);
}
.table th.num, .table td.num { text-align: right; }
.table tbody tr + tr td { border-top: 1px solid var(--color-border); }

/* Stacked table: below 40rem each row becomes a block of label/value lines, so no column is ever cut off on a phone.
   Markup: <table class="table table-stack"> and data-label="Column name" on every <td>. Every table with 4 or more
   columns uses it (the starter's tests enforce this). The header stays in the DOM for screen readers. */
@media (max-width: 40rem) {
  .table-stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .table-stack, .table-stack tbody, .table-stack tr, .table-stack td { display: block; width: 100%; }
  .table-stack tbody tr { padding: var(--space-3) 0; }
  .table-stack tbody tr + tr { border-top: 1px solid var(--color-border); }
  .table-stack tbody tr + tr td { border-top: 0; }
  .table-stack td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--space-4);
    padding: var(--space-2) var(--space-4);
    text-align: right;
    overflow-wrap: anywhere;
  }
  .table-stack td::before {
    content: attr(data-label);
    flex-shrink: 0;
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    letter-spacing: 0.04em;
    text-transform: uppercase;
    text-align: left;
  }
}

/* ---- stat: one focal figure (a total, a KPI) with its label above it, left-aligned. Never label and value in
   opposite corners. ---- */
.stat { display: flex; flex-direction: column; gap: var(--space-2); }
.stat-label { color: var(--color-text-muted); font-size: var(--text-sm); font-weight: var(--weight-medium); }
.stat-value {
  font-family: var(--font-display);
  font-size: var(--text-3xl);
  font-weight: var(--weight-semibold);
  line-height: var(--leading-tight);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.stat-note { color: var(--color-text-muted); font-size: var(--text-sm); }

/* ---- badge ---- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0 var(--space-3);
  min-height: 1.5rem;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  letter-spacing: var(--tracking-label);
  background: var(--color-surface-sunken);
  color: var(--color-text-muted);
}
.badge-accent { background: var(--color-accent-subtle); color: var(--color-accent); }
.badge-success { background: var(--color-success-subtle); color: var(--color-success); }
.badge-warn { background: var(--color-warn-subtle); color: var(--color-warn); }
.badge-danger { background: var(--color-danger-subtle); color: var(--color-danger); }

/* ---- alert (inline error) ---- */
.alert {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-6);
  border-radius: var(--radius-lg);
  background: var(--color-danger-subtle);
  color: var(--color-danger);
}

/* ---- toast ---- */
.toast-region {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  left: var(--space-4);
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-2);
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  max-width: 26rem;
  padding: var(--space-3) var(--space-5);
  background: var(--color-inverse-bg);
  color: var(--color-inverse-text);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  font-size: var(--text-sm);
  pointer-events: auto;
}
.toast-dismiss {
  background: transparent;
  color: inherit;
  border: 0;
  border-radius: var(--radius-sm);
  padding: var(--space-1) var(--space-2);
  font: inherit;
  font-weight: var(--weight-medium);
  text-decoration: underline;
  text-underline-offset: 0.22em;
  cursor: pointer;
}

/* ---- empty state ---- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-12) var(--space-6);
  text-align: center;
  background: var(--color-surface-sunken);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
}
.empty p { color: var(--color-text-muted); max-width: 32rem; }
.empty .btn { margin-top: var(--space-4); }

/* ---- skeleton (static placeholder, no shimmer) ---- */
.skeleton {
  height: 1rem;
  border-radius: var(--radius-sm);
  background: var(--color-surface-sunken);
}
.skeleton + .skeleton { margin-top: var(--space-3); }

/* ---- dialog ---- */
.dialog {
  width: min(30rem, calc(100vw - var(--space-8)));
  padding: var(--space-8);
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
}
.dialog::backdrop { background: var(--color-scrim); }
.dialog h2 { margin-bottom: var(--space-2); }
.dialog-actions { display: flex; justify-content: flex-end; gap: var(--space-2); margin-top: var(--space-8); }
