:root {
  --bg: #f3efe6;
  --paper: #fffcf7;
  --ink: #1c1915;
  --muted: #5e584f;
  --line: #ddd6c8;
  --night: #1b1630;
  --plum: #3c2f66;
  --gold: #a08b62;
  --ok: #1f6b45;
  --bad: #8d2f2f;
  --serif: "Palatino Linotype", Palatino, Georgia, serif;
  --sans: "Segoe UI", sans-serif;
  --mono: Consolas, "Courier New", monospace;
  --shadow: 0 12px 32px rgba(27, 22, 48, 0.06);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 18px;
  line-height: 1.6;
}
a { color: inherit; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.15; }
h1 { font-size: clamp(2rem, 4vw, 3.4rem); margin: 0 0 0.6rem; }
h2 { font-size: 1.45rem; margin: 2rem 0 0.7rem; }
p { margin: 0 0 1rem; }

.eyebrow {
  margin: 0 0 0.4rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 0.78rem;
  color: var(--gold);
}
.lede { color: var(--muted); max-width: 38rem; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.8rem 1.2rem;
  background: var(--night);
  color: #f6f3ec;
}
.brand { font-family: var(--serif); text-decoration: none; font-size: 1.15rem; }
.topbar-title {
  margin: 0;
  flex: 1;
  color: #d9d2c5;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-nav { display: flex; gap: 0.8rem; }
.topbar-nav a { color: #f6f3ec; text-decoration: none; font-size: 0.95rem; }

.shell { width: min(1280px, calc(100% - 3rem)); margin: 0 auto; padding: 1.6rem 0 3rem; }
.page-toc { min-height: 100vh; }
.page-section, .page-lab { height: 100vh; overflow: hidden; display: flex; flex-direction: column; }
.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 1rem 1.25rem;
  padding: 1rem 1.25rem 1rem;
}
.stage-main { grid-column: 1; grid-row: 1; overflow: auto; padding-right: 0.4rem; }
.stage-video {
  grid-column: 2;
  grid-row: 1;
  align-self: stretch;
  justify-self: end;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-height: 0;
  height: 100%;
  animation: slide-in 0.75s cubic-bezier(.16, 1, .3, 1) both;
}
.video-label {
  margin: 0 0 0.45rem;
  font-size: 0.75rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.page-head { margin-bottom: 1.4rem; }

.cover { min-height: 100vh; display: grid; grid-template-columns: 0.8fr 1.2fr; }
.cover-night {
  background:
    radial-gradient(rgba(255, 255, 255, 0.14) 1px, transparent 1px),
    var(--night);
  background-size: 22px 22px, auto;
  color: #f6f3ec;
  padding: 1.5rem 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-height: 100vh;
  gap: 1rem;
}
.cover-stage {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
}
.cover-night .video-frame {
  flex: none;
  height: auto;
  width: min(100%, 26rem);
  max-width: 100%;
  aspect-ratio: 16 / 9;
}
.cover-night .video-pending { text-align: center; padding: 1rem; }
.cover-kicker { font-family: var(--serif); font-size: clamp(1.6rem, 3vw, 2.4rem); margin: 0; }
.cover-day { padding: clamp(2rem, 6vw, 5rem); display: flex; flex-direction: column; justify-content: center; }
.cover-day h2 { font-family: var(--sans); font-size: 1.25rem; font-weight: 500; color: var(--plum); }
.cover-day p { max-width: 38rem; font-size: 1.15rem; }
.cover-actions { display: flex; flex-wrap: wrap; gap: 0.8rem; margin-top: 0.6rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  padding: 0.75rem 1.15rem;
  text-decoration: none;
  border: 1px solid transparent;
  font: inherit;
  cursor: pointer;
}
.btn-primary { background: var(--night); color: #f6f3ec; }
.btn-primary:hover { background: var(--plum); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-quiet { background: transparent; color: var(--plum); }
.btn-danger { background: transparent; color: var(--bad); border-color: #e3c7c7; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.toc { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 0.7rem; }
.toc-item {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 0.9rem;
  align-items: center;
  padding: 0.9rem 1rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  text-decoration: none;
  box-shadow: var(--shadow);
}
.toc-item:hover { border-color: var(--plum); }
.toc-num {
  font-family: var(--mono);
  width: 2.6rem;
  height: 2.6rem;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--night);
  color: #f6f3ec;
}
.toc-title { font-size: 1.05rem; }
.toc-meta, .toc-now { color: var(--muted); font-size: 0.85rem; }
.toc-item.is-current { outline: 2px solid var(--plum); }
.toc-lab { margin-top: 2rem; }
.toc-lab h2 { margin-top: 0; }

.progress-wrap { margin-bottom: 1.2rem; }
.progress-label {
  margin: 0 0 0.35rem;
  font-family: var(--mono);
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.progress { height: 8px; background: #e4ded2; border-radius: 99px; overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--plum); }

.lesson p { font-size: 1.2rem; max-width: none; }
.video-frame {
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  width: auto;
  aspect-ratio: 9 / 16;
  max-width: min(42vw, 520px);
  background: #221c2e;
  border-radius: 18px;
  overflow: hidden;
  display: grid;
  place-items: center;
  box-shadow: 0 18px 40px rgba(27, 22, 48, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.has-video .video-frame { box-shadow: 0 0 0 1px rgba(160, 139, 98, 0.45), 0 18px 40px rgba(27, 22, 48, 0.22); }
.video-frame video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.video-pending { margin: 0; color: #d9d2c5; font-size: 1rem; }

.matrix-card, .code, .note, .calc, .eq, .pipeline, .chain, .tree, .data, .file-card, .stats article, .login-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.matrix-card { display: inline-block; padding: 1rem 1.2rem 1.2rem; margin: 0 0 1rem; }
.matrix-card figcaption, .matrix-name { font-family: var(--serif); font-size: 1.3rem; margin-bottom: 0.45rem; }
.pair { display: flex; flex-wrap: wrap; gap: 1rem; }
.brackets { display: inline-block; border-left: 2px solid var(--ink); border-right: 2px solid var(--ink); padding: 0.35rem 0.7rem; }
.matrix { border-collapse: collapse; font-family: var(--mono); font-size: 1.25rem; }
.matrix td { padding: 0.35rem 0.75rem; text-align: center; }
.matrix td.hot { background: #efe4c4; border-radius: 8px; }
.row-label { font-family: var(--sans); font-weight: 500; font-size: 0.95rem; color: var(--muted); padding-left: 0.8rem; border: 0; }
.col-caption { margin: 0 0 0.2rem 4.2rem; color: var(--muted); font-size: 0.9rem; }
.annotated-layout { display: flex; gap: 0.7rem; align-items: center; }
.arrows { display: grid; grid-auto-flow: column; justify-content: space-around; color: var(--muted); margin-bottom: 0.2rem; }

.code { margin: 0 0 1rem; overflow: hidden; }
.code-top { padding: 0.45rem 0.9rem; color: #b7ad9d; background: #241e33; font-family: var(--mono); font-size: 0.8rem; }
.code pre { margin: 0; padding: 1rem; background: #1c1830; color: #f4efe4; overflow: auto; }
.code code { font-family: var(--mono); font-size: 0.98rem; }
.tok-k { color: #d7c4f5; }
.tok-c { color: #9a917f; }

.pipeline, .chain { list-style: none; margin: 0 0 1rem; padding: 0.4rem; }
.pipeline li, .chain li {
  background: var(--bg);
  border-radius: 12px;
  padding: 0.75rem 1rem;
  margin: 0.45rem;
}
.pipeline li + li::before, .chain li + li { }
.pipeline li:not(:last-child)::after, .chain li:not(:last-child)::after {
  content: "↓";
  display: block;
  text-align: center;
  color: var(--plum);
  margin-top: 0.45rem;
}
.chain li { display: grid; gap: 0.15rem; }
.chain strong { letter-spacing: 0.04em; }
.chain span { color: var(--muted); }

.dims { display: flex; gap: 0.6rem; margin-bottom: 0.8rem; }
.dims span {
  font-family: var(--mono);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.7rem 0.9rem;
}
.dims-ok span { border-color: #b7d7c4; }
.dims-bad span { border-color: #e3c7c7; }
.eq, .calc { display: grid; gap: 0.2rem; padding: 0.9rem 1rem; margin: 0 0 1rem; font-family: var(--mono); }
.result {
  font-family: var(--serif);
  font-size: 2rem;
  margin: 0.4rem 0 1rem;
}
.note { padding: 0.2rem 1rem; border-left: 4px solid var(--plum); margin-bottom: 1rem; }
.closing { margin: 1.4rem 0; padding: 1.2rem 1.3rem; border: 0; background: var(--night); color: #f6f3ec; border-radius: 18px; }
.closing p { margin: 0; font-family: var(--serif); font-size: 1.45rem; max-width: none; }
.table-scroll { overflow-x: auto; margin-bottom: 1rem; }
.data { width: 100%; border-collapse: collapse; background: var(--paper); }
.data th, .data td { padding: 0.7rem 0.9rem; border-bottom: 1px solid var(--line); text-align: right; }
.data th:first-child, .data td:first-child { text-align: left; }
.tree { padding: 1rem 1.2rem; margin-bottom: 1rem; }
.tree-root { font-family: var(--serif); font-size: 1.4rem; margin: 0 0 0.4rem; }
.tree ul { margin: 0; padding-left: 1.2rem; }

.pager {
  grid-column: 1 / -1;
  grid-row: 2;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.6rem;
  align-items: center;
  padding: 0.7rem;
  background: rgba(255, 252, 247, 0.92);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.pager .btn-primary { justify-self: end; }

.lab-tabs, .lab-presets, .lab-actions, .inline-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.8rem 0; }
.lab-tabs button, .lab-presets button {
  font: inherit;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 999px;
  padding: 0.45rem 0.8rem;
  cursor: pointer;
}
.lab-tabs button.is-active { background: var(--night); color: #f6f3ec; border-color: var(--night); }
.builders { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.builder {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1rem;
  box-shadow: var(--shadow);
}
.builder h3 { margin: 0 0 0.7rem; }
.dims-control { display: flex; flex-wrap: wrap; gap: 0.8rem; margin-bottom: 0.8rem; }
.stepper { display: flex; align-items: center; gap: 0.35rem; font-size: 0.95rem; }
.stepper button {
  width: 2rem;
  height: 2rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  font: inherit;
  cursor: pointer;
}
.stepper output { min-width: 1.4rem; text-align: center; font-family: var(--mono); }
.sheet { display: grid; gap: 0.4rem; }
.sheet input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.55rem;
  text-align: center;
  font: 1.05rem var(--mono);
  background: #fff;
}
.sheet input.is-diff { border-color: var(--bad); background: #fbeeee; }
.lab-hint { color: var(--muted); font-size: 0.92rem; }
.lab-status {
  white-space: pre-wrap;
  border-radius: 14px;
  padding: 0.8rem 1rem;
  margin: 0.6rem 0 1rem;
}
.lab-status:empty { display: none; }
.lab-status.is-ok { background: #e7f3ec; color: var(--ok); }
.lab-status.is-bad { background: #fbeeee; color: var(--bad); }
.procedure { margin: 0 0 1rem; padding: 1rem; background: var(--paper); border: 1px solid var(--line); border-radius: 16px; }
.procedure-label { color: var(--muted); font-size: 0.9rem; margin-bottom: 0.3rem; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.8rem; margin-bottom: 1.2rem; }
.stats article { padding: 1rem; }
.stats span { display: block; color: var(--muted); font-size: 0.9rem; }
.stats strong { font-family: var(--serif); font-size: 2rem; }
.admin-table { width: 100%; border-collapse: collapse; background: var(--paper); border-radius: 16px; overflow: hidden; }
.admin-table th, .admin-table td { text-align: left; padding: 0.8rem; border-bottom: 1px solid var(--line); }
.admin-table a { color: var(--plum); }
.file-card { padding: 1rem; margin-bottom: 1rem; }
.file-card video { width: min(100%, 420px); border-radius: 12px; background: #000; }
.file-card dl { display: grid; gap: 0.4rem; margin: 0.8rem 0; }
.file-card div { display: flex; gap: 0.8rem; }
.file-card dt { color: var(--muted); min-width: 7rem; }
.file-card dd { margin: 0; }
.stack-form, .upload-form { display: grid; gap: 0.8rem; max-width: 32rem; }
.stack-form label { display: grid; gap: 0.3rem; }
.stack-form input {
  font: inherit;
  padding: 0.7rem 0.8rem;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
}
.flash { padding: 0.7rem 0.9rem; border-radius: 12px; }
.flash-ok { background: #e7f3ec; color: var(--ok); }
.flash-bad { background: #fbeeee; color: var(--bad); }
.back-link a { color: var(--plum); }
.login-card { width: min(420px, calc(100% - 2rem)); margin: 8vh auto; padding: 1.5rem; }
.login-back { margin-top: 1rem; font-size: 0.95rem; }

.progress span { transform-origin: left center; animation: fill-bar 0.9s cubic-bezier(.16, 1, .3, 1) both; }
.lesson > * { animation: rise 0.7s cubic-bezier(.16, 1, .3, 1) both; }
.lesson > *:nth-child(2) { animation-delay: 0.06s; }
.lesson > *:nth-child(3) { animation-delay: 0.12s; }
.lesson > *:nth-child(4) { animation-delay: 0.18s; }
.lesson > *:nth-child(5) { animation-delay: 0.24s; }
.lesson > *:nth-child(n + 6) { animation-delay: 0.3s; }
.cover-day > * { animation: rise 0.8s cubic-bezier(.16, 1, .3, 1) both; }
.cover-day h2 { animation-delay: 0.1s; }
.cover-day p { animation-delay: 0.18s; }
.cover-actions { animation-delay: 0.28s; }
.cover-night { animation: drift 22s linear infinite; }
.toc-item { animation: rise 0.55s cubic-bezier(.16, 1, .3, 1) both; transition: transform 0.25s ease, border-color 0.25s ease; }
.toc li:nth-child(2) .toc-item { animation-delay: 0.04s; }
.toc li:nth-child(3) .toc-item { animation-delay: 0.08s; }
.toc li:nth-child(4) .toc-item { animation-delay: 0.12s; }
.toc li:nth-child(5) .toc-item { animation-delay: 0.16s; }
.toc li:nth-child(n + 6) .toc-item { animation-delay: 0.2s; }
.toc-item:hover { transform: translateY(-3px); }
.btn { transition: transform 0.2s ease, background 0.2s ease; }
.btn:hover { transform: translateY(-1px); }
.matrix-card, .code, .note, .calc, .eq { transition: transform 0.25s ease; }
.matrix-card:hover, .code:hover { transform: translateY(-2px); }

@keyframes rise {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: none; }
}
@keyframes slide-in {
  from { opacity: 0; transform: translateX(28px); }
  to { opacity: 1; transform: none; }
}
@keyframes fill-bar {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}
@keyframes drift {
  from { background-position: 0 0, 0 0; }
  to { background-position: 22px 22px, 0 0; }
}

@media (max-width: 860px) {
  .cover { grid-template-columns: 1fr; }
  .cover-night { min-height: auto; }
  .page-section, .page-lab { height: auto; overflow: auto; display: block; }
  .stage { display: flex; flex-direction: column; padding: 1rem; }
  .stage-video { height: auto; align-items: center; }
  .stage-video .video-frame { height: min(78vh, 720px); width: auto; max-width: 100%; aspect-ratio: 9 / 16; }
  .cover-night .video-frame { height: auto; width: min(100%, 26rem); max-width: 100%; aspect-ratio: 16 / 9; }
  .stage-main { overflow: visible; }
  .builders, .stats, .pager, .toc { grid-template-columns: 1fr; }
  .pager .btn-primary { justify-self: stretch; }
  .toc-item { grid-template-columns: auto 1fr; }
  .toc-meta, .toc-now { grid-column: 2; }
  .topbar { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
