:root {
  --bg: #ffffff;
  --bg-sunken: #f6f7f9;
  --bg-raised: #ffffff;
  --text: #1c2126;
  --text-muted: #5b6470;
  --border: #e2e5ea;
  --accent: #2d5c8a;
  --accent-soft: #eaf1f8;
  --note-bg: #fff8e6;
  --note-border: #e8d38a;
  --fillin-bg: #fdeeee;
  --fillin-text: #a13c3c;
  --code-bg: #f0f2f5;
  --shadow: 0 1px 2px rgba(20, 24, 30, 0.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171b;
    --bg-sunken: #1b1f24;
    --bg-raised: #1e2227;
    --text: #e6e9ed;
    --text-muted: #9aa4b0;
    --border: #2c313a;
    --accent: #6ea8dc;
    --accent-soft: #1d2b3a;
    --note-bg: #2b2513;
    --note-border: #5c4e21;
    --fillin-bg: #2e1c1c;
    --fillin-text: #e08a8a;
    --code-bg: #23272e;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  }
}

:root[data-theme="dark"] {
  --bg: #14171b;
  --bg-sunken: #1b1f24;
  --bg-raised: #1e2227;
  --text: #e6e9ed;
  --text-muted: #9aa4b0;
  --border: #2c313a;
  --accent: #6ea8dc;
  --accent-soft: #1d2b3a;
  --note-bg: #2b2513;
  --note-border: #5c4e21;
  --fillin-bg: #2e1c1c;
  --fillin-text: #e08a8a;
  --code-bg: #23272e;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* ==========================================================================
   Base / mobile-first styles (smallest screens)
   ========================================================================== */

* { box-sizing: border-box; }

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  row-gap: 6px;
  gap: 10px;
}

.brand-link {
  display: contents;
  color: inherit;
  text-decoration: none;
}

.brand-link:hover .brand {
  text-decoration: underline;
}

.brand {
  order: 1;
  font-weight: 700;
  font-size: 1.05rem;
}

.brand-sub {
  order: 3;
  flex-basis: 100%;
  display: block;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: -4px;
  margin-bottom: 6px;
}

.theme-toggle {
  order: 4;
  align-self: center;
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  padding: 2px;
  border-radius: 999px;
}

.theme-toggle-option {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-family: var(--font);
  padding: 4px 9px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}

.theme-toggle-option:hover {
  color: var(--text);
}

.theme-toggle-option.active {
  background: var(--accent);
  color: var(--bg);
  font-weight: 600;
}

.nav-toggle {
  order: 2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  width: 34px;
  height: 34px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  cursor: pointer;
}

.nav-toggle:hover {
  border-color: var(--accent);
}

.nav-toggle-icon {
  position: relative;
  display: block;
  width: 16px;
  height: 12px;
}

.nav-toggle-icon span {
  position: absolute;
  left: 0;
  width: 100%;
  height: 2px;
  border-radius: 1px;
  background: var(--text);
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.nav-toggle-icon span:nth-child(1) { top: 0; }
.nav-toggle-icon span:nth-child(2) { top: 5px; }
.nav-toggle-icon span:nth-child(3) { top: 10px; }

.nav-toggle[aria-expanded="true"] .nav-toggle-icon span:nth-child(1) {
  top: 5px;
  transform: rotate(45deg);
}

.nav-toggle[aria-expanded="true"] .nav-toggle-icon span:nth-child(2) {
  opacity: 0;
}

.nav-toggle[aria-expanded="true"] .nav-toggle-icon span:nth-child(3) {
  top: 5px;
  transform: rotate(-45deg);
}

.layout {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr;
  gap: 40px;
  padding: 16px 14px 48px;
}

.sidenav {
  display: none;
  flex-direction: column;
  gap: 2px;
}

.sidenav.open {
  display: flex;
  position: fixed;
  top: var(--topbar-h, 64px);
  left: 0;
  right: 0;
  z-index: 40;
  background: var(--bg-raised);
  box-shadow: var(--shadow);
  padding: 12px 18px 16px;
  max-height: calc(100vh - var(--topbar-h, 64px));
  overflow-y: auto;
  border-bottom: 1px solid var(--border);
}

.sidenav a {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.85rem;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid transparent;
}

.sidenav a:hover {
  background: var(--bg-sunken);
  color: var(--text);
}

.sidenav a.active {
  color: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}

.content {
  min-width: 0;
}

section {
  padding-bottom: 44px;
  margin-bottom: 44px;
  border-bottom: 1px solid var(--border);
  scroll-margin-top: calc(var(--topbar-h, 64px) + 16px);
}

section:last-child {
  border-bottom: none;
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 16px;
}

h2 {
  font-size: 1.2rem;
  margin: 0 0 16px;
}

h3 {
  font-size: 1.05rem;
  margin: 28px 0 10px;
  color: var(--text);
}

p { margin: 0 0 14px; }

.hint {
  color: var(--text-muted);
  font-size: 0.92rem;
}

code {
  font-family: var(--mono);
  background: var(--code-bg);
  padding: 0.1em 0.4em;
  border-radius: 4px;
  font-size: 0.88em;
}

a { color: var(--accent); }

.fillin {
  color: var(--fillin-text);
  background: var(--fillin-bg);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.92em;
}

li.fillin {
  display: list-item;
  background: none;
  padding: 0;
  color: var(--fillin-text);
}

.table-scroll {
  overflow-x: auto;
  margin-bottom: 8px;
  -webkit-overflow-scrolling: touch;
}

.ref-table {
  width: 100%;
  min-width: 480px;
  border-collapse: collapse;
  margin-bottom: 0;
}

.ref-table th, .ref-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.ref-table th {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  font-weight: 600;
}

.ref-table td {
  font-size: 0.9rem;
}

.ref-table td:first-child {
  white-space: nowrap;
  font-weight: 600;
}

.rule-list {
  padding-left: 22px;
}

.rule-list li {
  margin-bottom: 14px;
}

.plain-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.plain-list li {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.plain-list li:last-child { border-bottom: none; }

.plain-list.numbered {
  list-style: decimal;
  padding-left: 22px;
}

.plain-list.numbered li {
  border-bottom: none;
  padding: 4px 0;
}

.gotcha-list dt {
  font-weight: 700;
  margin-top: 18px;
}

.gotcha-list dt:first-child { margin-top: 0; }

.gotcha-list dd {
  margin: 4px 0 0;
  color: var(--text-muted);
}

.note {
  background: var(--note-bg);
  border: 1px solid var(--note-border);
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 0.92rem;
}

.checklist {
  list-style: none;
  padding: 0;
  margin: 0 0 8px;
}

.checklist li {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.checklist li:last-child { border-bottom: none; }

.checklist label {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.95rem;
  cursor: pointer;
}

.checklist input[type="checkbox"] {
  margin-top: 3px;
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  accent-color: var(--accent);
}

.checklist li.checked label {
  color: var(--text-muted);
  text-decoration: line-through;
}


/* ==========================================================================
   ~560px and up — topbar no longer needs to wrap
   ========================================================================== */
@media (min-width: 561px) {
  .topbar-inner {
    padding: 14px 24px;
    flex-wrap: nowrap;
  }
  .brand {
    order: 0;
  }
  .brand-sub {
    order: 0;
    flex-basis: auto;
    display: inline;
    font-size: 0.9rem;
    margin-top: 0;
    margin-bottom: 0;
  }
  .theme-toggle {
    order: 0;
    margin-left: auto;
    padding: 3px;
  }
  .theme-toggle-option {
    font-size: 0.85rem;
    padding: 5px 12px;
  }
  .nav-toggle {
    order: 0;
  }
  .layout {
    padding: 20px 18px 60px;
  }
  h1 {
    font-size: 1.9rem;
  }
  h2 {
    font-size: 1.4rem;
  }
  .sidenav a {
    font-size: 0.9rem;
    padding: 7px 10px;
  }
  .checklist label {
    gap: 10px;
    font-size: 1rem;
  }
  .ref-table th, .ref-table td {
    padding: 10px 12px;
  }
  .ref-table th {
    font-size: 0.82rem;
  }
  .ref-table td {
    font-size: 1rem;
  }
}

/* ==========================================================================
   Desktop (1024px and up) — two-column layout, sidebar always visible
   ========================================================================== */
@media (min-width: 1024px) {
  .nav-toggle {
    display: none;
  }
  .layout {
    grid-template-columns: 230px 1fr;
    padding: 32px 24px 80px;
  }
  .sidenav, .sidenav.open {
    display: flex;
    position: sticky;
    top: 70px;
    left: auto;
    right: auto;
    z-index: auto;
    background: none;
    box-shadow: none;
    padding: 0;
    align-self: start;
    max-height: calc(100vh - 100px);
    overflow-y: auto;
    border-bottom: none;
  }
  .sidenav a {
    border: none;
    border-left: 2px solid transparent;
  }
  .sidenav a.active {
    border-left-color: var(--accent);
    background: var(--accent-soft);
  }
}
