/* WireMirage UI stylesheet (slice 21).
 *
 * Implements the tokens + components from `web-ui-design.md`. Light
 * mode is the default; dark mode kicks in via prefers-color-scheme
 * with no manual toggle. Density-leaning (14px base) per spec.
 */

:root {
  /* Colour tokens — light */
  --bg: #fdfdfc;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #6a6a6a;
  --border: #e5e5e3;
  --border-strong: #c9c9c5;
  --accent: #1d4ed8;
  --accent-bg: #eff4ff;
  --accent-text: #ffffff;

  /* Status colours */
  --status-2xx: #15803d;
  --status-3xx: #1d4ed8;
  --status-4xx: #a16207;
  --status-5xx: #b91c1c;

  /* Typography */
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  --text-xs: 12px;
  --text-sm: 13px;
  --text-base: 14px;
  --text-lg: 16px;
  --text-xl: 20px;
  --text-2xl: 24px;
  --line: 1.5;

  /* Spacing — 4px scale */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;

  /* Radii + focus */
  --radius-sm: 3px;
  --radius: 5px;
  --radius-lg: 8px;
  --focus: 0 0 0 2px var(--accent-bg), 0 0 0 4px var(--accent);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161617;
    --surface: #1f1f22;
    --text: #f3f3f1;
    --text-muted: #9a9a96;
    --border: #2f2f34;
    --border-strong: #46464c;
    --accent: #7aa2ff;
    --accent-bg: #1f2740;
    --accent-text: #0e1730;
    --status-2xx: #86efac;
    --status-3xx: #7aa2ff;
    --status-4xx: #fbbf24;
    --status-5xx: #fca5a5;
  }
}

/* -- Reset / base -------------------------------------------------- */

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

html {
  color-scheme: light dark;
  background: var(--bg);
}

body {
  margin: 0;
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: var(--line);
  color: var(--text);
  background: var(--bg);
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover { text-decoration: underline; }
a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--radius-sm); }

code, pre {
  font-family: var(--font-mono);
  font-size: 0.95em;
}

pre.code-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-3) var(--space-4);
  overflow-x: auto;
  margin: var(--space-3) 0;
}

h1, h2, h3 { margin: 0 0 var(--space-3); font-weight: 600; line-height: 1.3; }
h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }

p { margin: 0 0 var(--space-3); }
.text-muted { color: var(--text-muted); }

/* -- Layout -------------------------------------------------------- */

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-header__inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-3) var(--space-5);
  display: flex;
  align-items: center;
  gap: var(--space-6);
}
.app-brand {
  font-weight: 700;
  color: var(--text);
  font-size: var(--text-lg);
}
.app-brand:hover { text-decoration: none; }

.app-nav {
  display: flex;
  gap: var(--space-4);
  margin-right: auto;
}
.app-nav a {
  color: var(--text);
  font-size: var(--text-sm);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
}
.app-nav a:hover {
  background: var(--accent-bg);
  text-decoration: none;
}

.app-user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-size: var(--text-sm);
}
.app-user__name { color: var(--text); font-weight: 500; }

.app-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--space-6) var(--space-5);
}

/* -- Common pieces ------------------------------------------------ */

.page-header {
  margin-bottom: var(--space-6);
}
.page-header__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  margin-bottom: var(--space-5);
}
.card__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}
.card__header h2 { margin: 0; }

.empty-state {
  color: var(--text-muted);
  padding: var(--space-3) 0;
}

.action-list {
  margin: 0;
  padding-left: var(--space-5);
}
.action-list li { margin-bottom: var(--space-1); }

/* -- Tables ------------------------------------------------------- */

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}
.data-table th,
.data-table td {
  padding: var(--space-2) var(--space-3);
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.data-table th {
  font-weight: 600;
  color: var(--text-muted);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.data-table tbody tr:hover {
  background: var(--accent-bg);
}

.status-2xx { color: var(--status-2xx); font-weight: 600; }
.status-3xx { color: var(--status-3xx); font-weight: 600; }
.status-4xx { color: var(--status-4xx); font-weight: 600; }
.status-5xx { color: var(--status-5xx); font-weight: 600; }

/* -- Buttons + links --------------------------------------------- */

.btn {
  display: inline-block;
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  font-size: var(--text-base);
  font-family: inherit;
  cursor: pointer;
  transition: background 80ms ease-out;
}
.btn:hover { background: var(--accent-bg); }
.btn:focus-visible { outline: none; box-shadow: var(--focus); }

.btn--primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}
.btn--primary:hover { background: var(--accent); filter: brightness(1.05); }

.btn--ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}
.btn--ghost:hover { background: var(--surface-2); color: var(--text); }

.btn--danger {
  background: transparent;
  border-color: var(--status-5xx);
  color: var(--status-5xx);
}
.btn--danger:hover {
  background: var(--status-5xx);
  color: var(--accent-text);
}

.btn--disabled,
.btn--ghost.btn--disabled {
  opacity: 0.45;
  pointer-events: none;
  cursor: not-allowed;
}

.link {
  color: var(--accent);
  font-size: var(--text-sm);
}

.link-button {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
}
.link-button:hover { text-decoration: underline; }

.logout-form { display: inline; margin: 0; }

/* -- Badges ------------------------------------------------------- */

.badge {
  display: inline-block;
  padding: 1px var(--space-2);
  border-radius: 999px;
  font-size: var(--text-xs);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.badge--admin {
  background: var(--accent-bg);
  color: var(--accent);
}

/* -- Auth screens ------------------------------------------------- */

.auth-body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  background: var(--bg);
}
.auth-card {
  width: min(28rem, 92vw);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-8) var(--space-6);
}
.auth-title {
  margin-bottom: var(--space-5);
  text-align: center;
}
.auth-form { display: grid; gap: var(--space-3); }
.auth-field { display: grid; gap: var(--space-1); }
.auth-field__label {
  font-size: var(--text-sm);
  color: var(--text-muted);
  font-weight: 500;
}
.auth-field input {
  font: inherit;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
}
.auth-field input:focus { outline: none; box-shadow: var(--focus); border-color: var(--accent); }
.auth-error {
  color: var(--status-5xx);
  font-size: var(--text-sm);
  margin: 0;
}
.auth-hint {
  margin-top: var(--space-4);
  color: var(--text-muted);
  font-size: var(--text-xs);
  text-align: center;
}

/* -- Filter forms (list pages) ----------------------------------- */

.filter-form {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3) var(--space-4);
  align-items: flex-end;
}

.filter-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 9rem;
}
.filter-field__label {
  font-size: var(--text-xs);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.filter-field input,
.filter-field select {
  font: inherit;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
}
.filter-field input:focus,
.filter-field select:focus { outline: none; box-shadow: var(--focus); border-color: var(--accent); }

/* -- Pagination ---------------------------------------------------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: var(--space-5);
}

/* -- Detail pages -------------------------------------------------- */

.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-sm);
  margin-bottom: var(--space-3);
}
.breadcrumb__sep {
  color: var(--text-muted);
}

.meta-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: var(--space-5);
  row-gap: var(--space-2);
  margin: 0;
}
.meta-grid dt {
  color: var(--text-muted);
  font-size: var(--text-sm);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.meta-grid dd {
  margin: 0;
}

.card--success {
  border-left: 3px solid var(--status-2xx);
}
.card--error {
  border-left: 3px solid var(--status-5xx);
}
.inline-form { display: inline; margin: 0; }
.token-actions {
  display: flex;
  gap: var(--space-2);
}

.action-row {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-3);
}

.edit-disclosure {
  margin-top: var(--space-3);
}
.edit-disclosure summary {
  cursor: pointer;
  color: var(--accent);
  font-size: var(--text-sm);
  padding: var(--space-1) 0;
}
.edit-disclosure[open] summary {
  margin-bottom: var(--space-3);
}

.filter-checkbox {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-muted);
}
.filter-checkbox input[type="checkbox"] {
  margin: 0;
}

/* status pill (used in journal tables on the route detail page) */
.status {
  display: inline-block;
  padding: 1px var(--space-2);
  border-radius: var(--radius);
  font-variant-numeric: tabular-nums;
}

/* -- Form grid (route creation, etc.) ----------------------------- */

.form-grid {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: var(--space-5);
  row-gap: var(--space-3);
  align-items: center;
  margin-bottom: var(--space-4);
}
.form-grid > label {
  color: var(--text-muted);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.form-grid input,
.form-grid select {
  font: inherit;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
.form-grid input:focus,
.form-grid select:focus {
  outline: none;
  box-shadow: var(--focus);
  border-color: var(--accent);
}

.source-editor {
  width: 100%;
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  padding: var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  resize: vertical;
}
.source-editor:focus {
  outline: none;
  box-shadow: var(--focus);
  border-color: var(--accent);
}

.source-block {
  margin: 0;
  padding: var(--space-3);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre;
  max-height: 32rem;
}
.source-block code {
  background: none;
  padding: 0;
  font-size: inherit;
}

/* Ace editor host (slice 41). Ace renders into the div via absolute-
   positioned children, so the host needs an explicit height. We give
   the editor variant a bit more room than the read-only viewer since
   it's the working surface; both can scroll past their box. */
.ace-host {
  width: 100%;
  height: 24rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  /* Ace draws its own background, but if the script fails to load
     (network blip, ad-blocker, etc.) the textContent fallback should
     still be readable. */
  font-family: var(--font-mono);
  font-size: var(--text-sm);
}
.ace-host--viewer {
  height: 20rem;
}

/* -- Detail-page footer (secondary nav row) ------------------------ */

.page-footer {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

/* Action row sitting inside a page-header card carries less margin
   than the standalone action-row used in lower sections. */
.page-header .action-row {
  margin-top: var(--space-4);
  margin-bottom: 0;
}
