/* Space Is Not Enough: design tokens and a few components.
   Dark is the default. Light follows the system or data-theme="light".
   High contrast follows prefers-contrast or data-contrast="high". */

:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0e17;
  --surface: #131827;
  --surface-2: #1c2336;
  --border: #2c3550;
  --text: #e6e9f2;
  --muted: #a9b3ca;
  --accent: #ffb000;
  --on-accent: #141827;
  --link: #6fd0ff;
  --ok: #5cf08a;
  --warn: #ffb000;
  --danger: #ff8a80;
  --violet: #c4b5fd;
  --code-bg: #07090f;
  --code-bar: #1c2336;
  --code-text: #e6e9f2;
  --code-green: #5cf08a;
  --code-muted: #a9b3ca;
}

@media not all {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f7f5ef;
    --surface: #ffffff;
    --surface-2: #ece8dd;
    --border: #cfc8b8;
    --text: #141827;
    --muted: #4a5370;
    --accent: #8a5300;
    --on-accent: #ffffff;
    --link: #0b5f86;
    --ok: #0f6b34;
    --warn: #8a5300;
    --danger: #a82015;
    --violet: #5b34b5;
  }
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f7f5ef;
  --surface: #ffffff;
  --surface-2: #ece8dd;
  --border: #cfc8b8;
  --text: #141827;
  --muted: #4a5370;
  --accent: #8a5300;
  --on-accent: #ffffff;
  --link: #0b5f86;
  --ok: #0f6b34;
  --warn: #8a5300;
  --danger: #a82015;
  --violet: #5b34b5;
}

:root {
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
  --space-8: 4rem;

  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.25rem;
  --text-xl: 1.563rem;
  --text-2xl: 1.953rem;
  --text-3xl: clamp(2rem, 1.2rem + 3.5vw, 3.052rem);
  --leading-tight: 1.2;
  --leading-body: 1.6;
  --measure: 68ch;

  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 14px;
  --radius-pill: 999px;

  --focus: 3px solid var(--link);
  --duration: 160ms;
}

/* High contrast: after the themes so it wins over them. */
@media (prefers-contrast: more) {
  :root {
    --bg: #000000;
    --surface: #000000;
    --surface-2: #101010;
    --border: #ffffff;
    --text: #ffffff;
    --muted: #e8e8e8;
    --accent: #ffc933;
    --on-accent: #000000;
    --link: #8fdcff;
    --ok: #7dffa6;
    --warn: #ffc933;
    --danger: #ffaaa2;
    --violet: #dcd2ff;
    --code-bg: #000000;
  }
}

:root[data-contrast="high"] {
  color-scheme: dark;
  --bg: #000000;
  --surface: #000000;
  --surface-2: #101010;
  --border: #ffffff;
  --text: #ffffff;
  --muted: #e8e8e8;
  --accent: #ffc933;
  --on-accent: #000000;
  --link: #8fdcff;
  --ok: #7dffa6;
  --warn: #ffc933;
  --danger: #ffaaa2;
  --violet: #dcd2ff;
  --code-bg: #000000;
}

/* Base */
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-body);
}
h1, h2, h3 { line-height: var(--leading-tight); margin: var(--space-6) 0 var(--space-3); }
h1 { font-size: var(--text-3xl); }
h2 { font-size: var(--text-2xl); }
h3 { font-size: var(--text-xl); }
a { color: var(--link); text-underline-offset: 0.15em; }
img, svg { max-width: 100%; height: auto; }
:focus-visible { outline: var(--focus); outline-offset: 2px; }

/* Buttons */
.btn {
  display: inline-block;
  padding: var(--space-2) var(--space-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: filter var(--duration), transform var(--duration);
}
.btn:hover { filter: brightness(1.12); transform: translateY(-1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

/* Cards */
.card {
  padding: var(--space-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
}
.card > :first-child { margin-top: 0; }
.card > :last-child { margin-bottom: 0; }
.card-grid {
  display: grid;
  gap: var(--space-5);
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
}

/* Callouts */
.callout {
  margin: var(--space-5) 0;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-left: 4px solid var(--link);
  border-radius: var(--radius-md);
  background: var(--surface);
}
.callout-ok { border-left-color: var(--ok); }
.callout-warn { border-left-color: var(--warn); }
.callout-danger { border-left-color: var(--danger); }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
th, td { padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); text-align: left; }
th { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

/* Code blocks stay dark in every theme. */
code, pre, kbd { font-family: var(--font-mono); font-size: 0.925em; }
:not(pre) > code {
  padding: 0.1em 0.35em;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
pre {
  overflow-x: auto;
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--code-bg);
  color: var(--code-green);
}

/* Terminal window: .terminal > .terminal-bar + pre */
.terminal {
  margin: var(--space-5) 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--code-bg);
  color: var(--code-text);
}
.terminal-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--code-bar);
  color: var(--code-muted);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
}
.terminal-bar::before {
  content: "";
  width: 0.6rem;
  height: 0.6rem;
  margin-right: 2.2rem;
  border-radius: var(--radius-pill);
  background: #ff8a80;
  box-shadow: 1rem 0 #ffb000, 2rem 0 #5cf08a;
}
.terminal pre { margin: 0; border: 0; border-radius: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .btn:hover { transform: none; }
}
