:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #475569;
  --line: #cbd5e1;
  --accent: #2563eb;
  --storage: #16a34a;
  --network: #0891b2;
  --security: #d97706;
  --th-bg: #fffbeb;
  --zh-bg: #eff6ff;
  --nav-w: 280px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Thai", sans-serif;
  line-height: 1.65;
  font-size: 17px;
}
a { color: var(--accent); text-underline-offset: 3px; }
a:focus-visible, button:focus-visible { outline: 3px solid var(--security); outline-offset: 2px; }
.skip-link {
  position: absolute; left: -9999px; top: 0; background: var(--ink); color: #fff;
  padding: .6rem 1rem; z-index: 20;
}
.skip-link:focus { left: 1rem; top: 1rem; }

.site-header {
  display: flex; gap: 1rem; align-items: baseline; flex-wrap: wrap;
  padding: 1rem 1.5rem; border-bottom: 1px solid var(--line); background: var(--panel);
}
.site-header .code { margin: 0; font-weight: 700; letter-spacing: .04em; color: var(--muted); }
.site-header .title { margin: 0; font-weight: 650; }

.layout {
  position: relative;
  display: grid;
  grid-template-columns: var(--nav-w, 280px) minmax(0, 1fr);
  min-height: calc(100vh - 140px);
  transition: grid-template-columns .15s ease;
}
.layout.is-resizing { transition: none; user-select: none; }
.sidebar {
  border-right: 1px solid var(--line); background: var(--panel);
  padding: 1.25rem 1rem; position: sticky; top: 0; align-self: start;
  max-height: 100vh; overflow: auto;
}
.nav-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: .5rem; margin-bottom: .75rem;
}
.nav-toggle {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; min-width: 44px; padding: .45rem .7rem;
  font: inherit; font-size: .9rem; line-height: 1; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  cursor: pointer;
}
.nav-toggle:hover { background: #eff6ff; }
.nav-toggle-icon { display: inline-block; font-size: 1rem; transition: transform .15s ease; }
.nav-code {
  margin: 0; color: var(--muted); font-size: .72rem; font-weight: 700;
  letter-spacing: .04em; overflow-wrap: anywhere;
}
.nav-resizer {
  position: absolute; top: 0; bottom: 0;
  left: calc(var(--nav-w, 280px) - 4px);
  width: 24px; z-index: 6; padding: 0; border: 0;
  background: transparent; cursor: col-resize; touch-action: none;
}
.nav-resizer::before {
  content: ""; position: absolute; top: 0; bottom: 0; left: 3px;
  width: 2px; border-radius: 2px; background: var(--line);
  transition: background-color .15s ease, width .15s ease;
}
.nav-resizer:hover::before, .nav-resizer:focus-visible::before,
.layout.is-resizing .nav-resizer::before { background: var(--accent); width: 3px; }
.nav-resizer:focus-visible { outline: 3px solid var(--security); outline-offset: -3px; }
.sidebar .home, .sidebar .master, .sidebar .start { display: block; font-weight: 600; margin: .25rem 0 .75rem; }
.sidebar ol { list-style: none; margin: 0; padding: 0; }
.sidebar li { margin: .15rem 0; }
.sidebar a { display: block; padding: .4rem .5rem; border-radius: 6px; color: var(--ink); text-decoration: none; }
.sidebar a:hover { background: #eff6ff; }
.sidebar a[aria-current="page"] { background: #dbeafe; color: #1e3a8a; font-weight: 650; }
.sidebar .num { color: var(--muted); font-variant-numeric: tabular-nums; margin-right: .35rem; }

/* Collapsed navigation is a 56px rail: the toggle and the course code stay,
   the session list is hidden. The rail width only applies to the side-by-side
   layout; under the 820px breakpoint the stacked layout ignores it. */
html[data-nav-collapsed="true"] .nav-body { display: none; }
@media (min-width: 821px) {
  html[data-nav-collapsed="true"] { --nav-w: 56px; }
  html[data-nav-collapsed="true"] .sidebar { padding: .5rem .25rem; }
  html[data-nav-collapsed="true"] .nav-head {
    flex-direction: column; justify-content: flex-start; gap: .5rem; margin-bottom: 0;
  }
  html[data-nav-collapsed="true"] .nav-toggle { width: 100%; padding: .45rem 0; }
  html[data-nav-collapsed="true"] .nav-toggle-text { display: none; }
  html[data-nav-collapsed="true"] .nav-toggle-icon { transform: rotate(180deg); }
  html[data-nav-collapsed="true"] .nav-code {
    writing-mode: vertical-rl; text-orientation: mixed; align-self: center;
  }
}

main { padding: 2rem clamp(1rem, 4vw, 3rem) 4rem; max-width: 960px; width: 100%; }
h1 { font-size: clamp(1.7rem, 3vw, 2.3rem); line-height: 1.2; margin: .2rem 0 .6rem; }
h2 { margin-top: 2.4rem; font-size: 1.35rem; }
h3 { margin-top: 1.8rem; font-size: 1.1rem; }
.lede { color: var(--muted); font-size: 1.05rem; }
.start-here {
  border-left: 4px solid var(--accent); background: var(--panel);
  padding: .75rem 1rem; border-radius: 0 8px 8px 0;
}
.breadcrumb { color: var(--muted); font-size: .9rem; }
.question { font-weight: 600; border-left: 4px solid var(--accent); padding-left: 1rem; }
.prereq {
  border-left: 4px solid var(--accent); background: var(--panel);
  padding: .6rem .9rem; border-radius: 0 8px 8px 0;
}
.downloads dt { font-weight: 600; }
.downloads dd { margin: 0 0 .6rem 1.2rem; }
.downloads .download { font-size: .9rem; font-weight: 600; margin-left: .5rem; }
.document-bar {
  display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between;
  gap: .4rem 1rem; margin: 1rem 0 1.5rem; padding: .6rem .9rem;
  background: var(--panel); border-left: 4px solid var(--accent);
  border-radius: 0 8px 8px 0;
}
.document-bar p { margin: 0; }
.document-bar .download { font-weight: 600; }

table { border-collapse: collapse; width: 100%; margin: 1.2rem 0; font-size: .95rem; }
.table-wrap { overflow-x: auto; margin: 1.2rem 0; }
.table-wrap table { margin: 0; }
th, td { border: 1px solid var(--line); padding: .5rem .6rem; text-align: left; vertical-align: top; }
th { background: #f1f5f9; }
table.weeks td:nth-child(1), table.weeks td:nth-child(5) { font-variant-numeric: tabular-nums; }
img { max-width: 100%; height: auto; }
figure { margin: 1.5rem 0; max-width: 100%; }
figure figcaption { color: var(--muted); font-size: .9rem; margin-top: .4rem; }

code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
p, li, td, th, code { overflow-wrap: anywhere; }
code { background: #f1f5f9; padding: .1rem .3rem; border-radius: 4px; font-size: .9em; }
pre { background: #0f172a; color: #e2e8f0; padding: 1rem; border-radius: 8px; overflow: auto; max-width: 100%; }
pre code { background: none; padding: 0; color: inherit; }

blockquote { margin: 1.2rem 0; padding: .4rem 1rem; border-left: 4px solid var(--line); color: var(--muted); }
details.th { margin: 1.5rem 0; padding: .6rem 1rem; background: var(--th-bg); border: 1px solid #fde68a; border-radius: 8px; }
details.th summary { cursor: pointer; font-weight: 600; }
details.zh { margin: 1.5rem 0; padding: .6rem 1rem; background: var(--zh-bg); border: 1px solid #bfdbfe; border-radius: 8px; }
details.zh summary { cursor: pointer; font-weight: 600; }
[lang="th"], [lang="zh-Hans"] { line-height: 1.8; }

.scene-wrap {
  position: relative; width: 100%; aspect-ratio: 16 / 9; margin: 1rem 0 .4rem;
  background: #eef1f4; border: 1px solid var(--line); border-radius: 12px; overflow: hidden;
}
.scene-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.scene-wrap #labels { position: absolute; inset: 0; pointer-events: none; }
.scene-wrap #labels .label {
  position: absolute; transform: translate(-50%, -50%); white-space: nowrap;
  font-size: 12px; color: var(--ink); background: rgba(255,255,255,.82);
  padding: 2px 6px; border-radius: 999px; border: 1px solid var(--line);
}
.scene-wrap #fallback { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #fff; }
.scene-info { color: var(--muted); font-size: .9rem; margin: .2rem 0 .6rem; }
.scene-controls { display: flex; gap: .5rem; flex-wrap: wrap; }
.diagram { margin: 1.5rem 0; }
.diagram img { max-width: 100%; height: auto; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.diagram figcaption { color: var(--muted); font-size: .9rem; margin-top: .4rem; }
figure { margin: 1.5rem 0; }
figure img { max-width: 100%; height: auto; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
figure figcaption { color: var(--muted); font-size: .9rem; margin-top: .4rem; }
.scene-controls button, .copy {
  font: inherit; font-size: .9rem; padding: .4rem .8rem; border-radius: 6px;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; color: var(--ink);
}
.scene-controls button:hover, .copy:hover { background: #eff6ff; }

.pager { display: flex; justify-content: space-between; gap: 1rem; margin-top: 3rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.pager a { text-decoration: none; font-weight: 600; }
.site-footer { border-top: 1px solid var(--line); padding: 1rem 1.5rem; color: var(--muted); font-size: .85rem; }

@media (max-width: 820px) {
  .layout { display: block; grid-template-columns: none; }
  .nav-resizer { display: none; }
  .sidebar { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .sidebar ol { display: block; }
  .site-header { padding: .75rem 1rem; }
  .site-header .title { overflow-wrap: anywhere; }
  main { padding: 1.25rem; }
}

@media print {
  .nav-toggle, .nav-resizer { display: none !important; }
  .layout { display: block; grid-template-columns: minmax(0, 1fr); }
  .sidebar, .site-header, .site-footer, .pager, .scene-controls, .skip-link { display: none; }
  body { background: #fff; font-size: 11pt; }
  main { max-width: none; padding: 0; }
  a { color: inherit; text-decoration: underline; }
  pre { background: #f1f5f9; color: #0f172a; border: 1px solid var(--line); }
  details.th, details.zh { background: transparent; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .layout, .nav-toggle-icon, .nav-resizer::before { transition: none; }
}
