:root {
  --paper: #f6f4ef;
  --panel: #fbfaf7;
  --stage: #ebe8e1;
  --ink: #1c1b19;
  --ink-2: #3d3a35;
  --muted: #6b675f;
  --hair: #e3dfd6;
  --hair-strong: #d2ccc0;
  --accent: #3b5b9a;          /* drawn from the worksheet guide lines */
  --accent-soft: #e4e9f3;
  --danger: #a8372c;
  --serif: "Libre Caslon Display", "Iowan Old Style", Georgia, serif;
  --sans: "Hanken Grotesk", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --radius: 10px;
  --topbar: 68px;
  font-family: var(--sans);
  color: var(--ink);
  color-scheme: light;
}

* { box-sizing: border-box; }

html { -webkit-font-smoothing: antialiased; }

body {
  margin: 0;
  background: var(--paper);
  font-size: 14px;
  line-height: 1.5;
  caret-color: var(--accent);
}

::selection { background: var(--accent-soft); color: var(--ink); }

.icon { width: 18px; height: 18px; flex: none; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--topbar);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 28px;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--hair);
}
.brand { display: flex; align-items: center; gap: 16px; min-width: 0; }
.wordmark {
  display: block;
  height: 36px;
  width: calc(var(--w, 210) * 1px);
  overflow: visible;
  flex: none;
}
.wordmark text {
  font-family: var(--serif);
  font-size: 28px;
  letter-spacing: -0.01em;
  fill: var(--ink);
}
.wordmark .dot { fill: var(--accent); }
/* Each letter of "page" steps lighter, echoing a worksheet row. */
.wordmark .f1 { fill: rgb(28 27 25 / 0.82); }
.wordmark .f2 { fill: rgb(28 27 25 / 0.58); }
.wordmark .f3 { fill: rgb(28 27 25 / 0.38); }
.wordmark .f4 { fill: rgb(28 27 25 / 0.22); }
.wordmark.draw text { animation: ink-word 2.2s cubic-bezier(.5, .1, .25, 1) .15s both; }
@keyframes ink-word {
  0%   { stroke: var(--ink); stroke-width: .7; stroke-dasharray: 90; stroke-dashoffset: 90; fill-opacity: 0; }
  62%  { stroke: var(--ink); stroke-width: .7; stroke-dasharray: 90; stroke-dashoffset: 0; fill-opacity: 0; }
  100% { stroke: var(--ink); stroke-width: 0; stroke-dasharray: 90; stroke-dashoffset: 0; fill-opacity: 1; }
}
.tagline { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions { display: flex; gap: 8px; flex: none; }

/* ---------- Buttons ---------- */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px 0 14px;
  font: inherit;
  font-weight: 550;
  border-radius: 999px;
  border: 1px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .2s var(--ease-out), border-color .2s var(--ease-out),
              box-shadow .25s var(--ease-out), transform .12s var(--ease-out);
}
.btn:active { transform: scale(0.97); }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--hair-strong); }
.btn.ghost:hover { background: #fff; border-color: var(--ink-2); }
.btn.ink {
  background: var(--ink);
  color: #fff;
  box-shadow: 0 1px 1px rgb(0 0 0 / .12), 0 4px 14px -4px rgb(28 27 25 / .35);
  overflow: hidden;
}
.btn.ink:hover { background: #000; box-shadow: 0 1px 1px rgb(0 0 0 / .12), 0 8px 22px -6px rgb(28 27 25 / .45); }
.btn:disabled { cursor: progress; }
.btn.ink.busy { background: var(--ink-2); }
.btn.ink.failed { background: var(--danger); }
.btn:disabled:not(.busy) { opacity: .45; cursor: default; box-shadow: none; }
/* Progress fill while the PDF renders */
.btn.ink::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgb(255 255 255 / .14);
  transform-origin: left;
  transform: scaleX(var(--progress, 0));
  transition: transform .35s var(--ease-out);
  pointer-events: none;
}
.btn-label { font-variant-numeric: tabular-nums; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 372px minmax(0, 1fr);
  min-height: calc(100vh - var(--topbar));
}
.panel {
  position: sticky;
  top: var(--topbar);
  max-height: calc(100vh - var(--topbar));
  overflow-y: auto;
  padding: 8px 28px 48px;
  background: var(--panel);
  border-right: 1px solid var(--hair);
  scrollbar-width: thin;
  scrollbar-color: var(--hair-strong) transparent;
}
.panel::-webkit-scrollbar { width: 10px; }
.panel::-webkit-scrollbar-thumb { background: var(--hair-strong); border-radius: 10px; border: 3px solid var(--panel); }

.group { padding: 26px 0 24px; border-bottom: 1px solid var(--hair); }
.group:last-child { border-bottom: 0; }
.group h2 {
  margin: 0 0 14px;
  font-family: var(--serif);
  font-weight: 400;
  font-size: 22px;
  line-height: 1.1;
  letter-spacing: -0.005em;
}

/* ---------- Font field ---------- */
.font-field {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px;
  background: #fff;
  border: 1px solid var(--hair-strong);
  border-radius: 14px;
  transition: border-color .2s var(--ease-out), box-shadow .25s var(--ease-out);
}
.font-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.specimen {
  flex: none;
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 10px;
  background: var(--paper);
  font-size: 24px;
  line-height: 1;
  overflow: hidden;
}
#specimen-glyph { display: block; transition: opacity .35s var(--ease-out), filter .35s var(--ease-out), transform .45s var(--ease-out); }
#specimen-glyph.swapping { opacity: 0; filter: blur(6px); transform: translateY(4px) scale(.92); transition-duration: .16s; }
.font-field input {
  flex: 1;
  min-width: 0;
  height: 46px;
  padding: 0 8px;
  font: inherit;
  font-size: 15px;
  border: 0;
  background: transparent;
  color: var(--ink);
}
.font-field input:focus { outline: none; }
.font-field input::placeholder { color: #8d887e; }
.font-go {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  margin-right: 2px;
  border: 0;
  border-radius: 10px;
  background: var(--ink);
  color: #fff;
  cursor: pointer;
  transition: transform .2s var(--ease-out), background-color .2s;
}
.font-go:hover { background: #000; }
.font-go:hover .icon { transform: translateX(2px); }
.font-go .icon { transition: transform .25s var(--ease-out); }
.font-go:active { transform: scale(.94); }
.font-go:disabled { background: var(--ink-2); cursor: progress; }
.font-go:disabled .icon { animation: nudge-r 1s ease-in-out infinite; }
@keyframes nudge-r { 50% { transform: translateX(3px); } }

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0 0;
  min-height: 20px;
  font-size: 13px;
  color: var(--muted);
}
.status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
  opacity: .9;
}
.status:empty::before { display: none; }
.status.loading::before { animation: pulse 1s ease-in-out infinite; }
.status.ok { color: var(--ink-2); }
.status.ok::before { background: var(--accent); }
.status.err { color: var(--danger); }
.status.err { animation: nudge .4s var(--ease-out); }
@keyframes pulse { 50% { transform: scale(.55); opacity: .35; } }
@keyframes nudge { 20% { transform: translateX(-3px); } 50% { transform: translateX(3px); } 80% { transform: translateX(-1px); } }

.hint { margin: 10px 0 0; font-size: 12.5px; line-height: 1.55; color: var(--muted); }
.hint a, .link {
  color: var(--ink-2);
  text-decoration: underline;
  text-decoration-color: var(--hair-strong);
  text-underline-offset: 3px;
  transition: text-decoration-color .2s;
}
.hint a:hover, .link:hover { text-decoration-color: var(--accent); color: var(--ink); }
.link { font: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

/* ---------- Content ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--hair-strong);
  background: #fff;
  color: var(--ink-2);
  cursor: pointer;
  transition: border-color .2s var(--ease-out), background-color .2s var(--ease-out), color .2s, transform .12s var(--ease-out);
}
.chip:hover { border-color: var(--ink-2); color: var(--ink); }
.chip:active { transform: scale(.95); }
.chip.quiet { background: transparent; border-color: transparent; color: var(--muted); }
.chip.quiet:hover { color: var(--danger); }

textarea {
  display: block;
  width: 100%;
  min-height: 132px;
  padding: 12px 14px;
  font: inherit;
  font-size: 14px;
  line-height: 1.6;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--hair-strong);
  border-radius: 12px;
  resize: vertical;
  transition: border-color .2s var(--ease-out), box-shadow .25s var(--ease-out);
}
textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
textarea.flash { animation: flash .6s var(--ease-out); }
@keyframes flash { from { background: var(--accent-soft); } }

/* ---------- Fields ---------- */
.field { display: block; margin-bottom: 18px; }
.field-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
  font-weight: 500;
  color: var(--ink-2);
}
.field output { color: var(--muted); font-weight: 450; font-variant-numeric: tabular-nums; }

input[type="range"] {
  --p: 50%;
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 22px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 4px;
  background: linear-gradient(to right, var(--ink) var(--p), var(--hair) var(--p));
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--hair); }
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--ink); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -7px;
  border-radius: 50%;
  background: #fff;
  border: 1.5px solid var(--ink);
  box-shadow: 0 1px 3px rgb(0 0 0 / .15);
  transition: transform .2s var(--ease-out), box-shadow .2s var(--ease-out);
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff; border: 1.5px solid var(--ink); box-shadow: 0 1px 3px rgb(0 0 0 / .15);
}
input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.12); }
input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.2); box-shadow: 0 0 0 6px var(--accent-soft); }
input[type="range"]:focus-visible { outline: none; }
input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px var(--accent-soft), 0 0 0 5.5px var(--accent); }

.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.stepper {
  display: flex;
  align-items: center;
  height: 40px;
  background: #fff;
  border: 1px solid var(--hair-strong);
  border-radius: 999px;
  padding: 3px;
  transition: border-color .2s, box-shadow .25s var(--ease-out);
}
.stepper:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.stepper button {
  flex: none;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
  transition: background-color .15s, transform .12s var(--ease-out);
}
.stepper button:hover { background: var(--paper); color: var(--ink); }
.stepper button:active { transform: scale(.88); }
.stepper button:disabled { color: var(--hair-strong); background: transparent; cursor: default; }
.stepper input {
  flex: 1;
  min-width: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  font: inherit;
  font-size: 15px;
  font-weight: 550;
  font-variant-numeric: tabular-nums;
  border: 0;
  background: transparent;
  color: var(--ink);
  -moz-appearance: textfield;
}
.stepper input:focus { outline: none; }
.stepper input::-webkit-inner-spin-button, .stepper input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

.switch { display: flex; align-items: center; gap: 12px; margin-top: 4px; cursor: pointer; color: var(--ink-2); font-weight: 500; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track {
  position: relative;
  flex: none;
  width: 36px;
  height: 22px;
  border-radius: 999px;
  background: var(--hair-strong);
  transition: background-color .25s var(--ease-out);
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgb(0 0 0 / .2);
  transition: transform .3s var(--ease-out);
}
.switch input:checked + .track { background: var(--ink); }
.switch input:checked + .track::after { transform: translateX(14px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Live preview of the fade, set in the chosen font */
.fade-strip {
  height: 72px;
  margin: 0 0 18px;
  padding: 4px 0;
  border-radius: 12px;
  background: #fff;
  border: 1px solid var(--hair);
  overflow: hidden;
}

/* ---------- Segmented control with sliding thumb ---------- */
.seg {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-top: 10px;
  padding: 3px;
  background: var(--stage);
  border-radius: 999px;
  isolation: isolate;
}
.seg::before {
  content: "";
  position: absolute;
  z-index: -1;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: calc(50% - 3px);
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 1px 2px rgb(0 0 0 / .08), 0 2px 8px -2px rgb(0 0 0 / .12);
  transition: transform .4s var(--ease-out);
}
.seg:has(label:nth-child(2) input:checked)::before { transform: translateX(100%); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block;
  padding: 7px 8px;
  text-align: center;
  border-radius: 999px;
  color: var(--muted);
  font-weight: 500;
  cursor: pointer;
  transition: color .25s var(--ease-out);
}
.seg span:hover { color: var(--ink-2); }
.seg input:checked + span { color: var(--ink); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- Stage ---------- */
.stage {
  min-width: 0;
  padding: 28px 40px 72px;
  background: var(--stage);
}
.summary {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px 14px;
  margin: 0 auto 24px;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.summary .font-name { color: var(--ink); font-size: 16px; line-height: 1; }
.summary span + span::before { content: "·"; margin-right: 14px; color: var(--hair-strong); }

.pages { display: flex; flex-direction: column; align-items: center; gap: 36px; }
.page { margin: 0; width: 100%; max-width: 760px; }
.page.landscape { max-width: 1000px; }
.page figcaption {
  margin-top: 10px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.sheet {
  display: block;
  width: 100%;
  height: auto;
  background: #fff;
  border-radius: 2px;
  box-shadow:
    0 0 0 1px rgb(28 27 25 / .04),
    0 1px 2px rgb(28 27 25 / .06),
    0 12px 32px -8px rgb(28 27 25 / .14),
    0 40px 80px -24px rgb(28 27 25 / .12);
}
.pages.busy { opacity: .55; filter: saturate(.6); transition: opacity .25s, filter .25s; }

/* Focal moment: when a new font arrives, a pen traces every glyph's outline in full ink,
   then the ink floods in and fades back to that row's shade. */
.writing .t.ink {
  animation: ink 1.25s cubic-bezier(.45, .05, .3, 1) both;
  animation-delay: calc(min(var(--i), 12) * 85ms + min(var(--j), 6) * 60ms);
}
@keyframes ink {
  0%   { stroke: var(--ink); stroke-width: var(--sw); stroke-dasharray: var(--d); stroke-dashoffset: var(--d); fill-opacity: 0; }
  58%  { stroke: var(--ink); stroke-width: var(--sw); stroke-dasharray: var(--d); stroke-dashoffset: 0; fill-opacity: 0; }
  72%  { stroke: var(--ink); stroke-width: var(--sw); stroke-dasharray: var(--d); stroke-dashoffset: 0; fill-opacity: .9; }
  100% { stroke: var(--ink); stroke-width: 0; stroke-dasharray: var(--d); stroke-dashoffset: 0; fill-opacity: 1; }
}
/* Dotted outlines already use a dash pattern, so they keep the left-to-right wipe. */
.writing .t.wipe {
  animation: write .75s var(--ease-out) both;
  animation-delay: calc(min(var(--i), 14) * 45ms);
}
@keyframes write {
  from { clip-path: inset(-20% 100% -40% -2%); opacity: .2; }
  to   { clip-path: inset(-20% -2% -40% -2%); opacity: 1; }
}
.settle .sheet { animation: settle .45s var(--ease-out) both; }
@keyframes settle { from { opacity: 0; transform: translateY(10px) scale(.985); } }

@media (prefers-reduced-motion: reduce) {
  .writing .t.ink, .writing .t.wipe, .wordmark.draw text { animation: fadein .3s ease-out both; animation-delay: 0s; }
  .settle .sheet { animation: fadein .2s ease-out both; }
  @keyframes fadein { from { opacity: 0; } }
  .seg::before, .switch .track::after, #specimen-glyph { transition-duration: .01s; }
  .status.err, .font-go:disabled .icon { animation: none; }
}

/* ---------- Small screens ---------- */
@media (max-width: 860px) {
  :root { --topbar: 60px; }
  .topbar { padding: 0 16px; }
  .wordmark { height: 28px; width: calc(var(--w, 210) * 28px / 36); }
  .tagline { display: none; }
  .btn { padding: 0 12px; height: 38px; }
  .btn.ghost span { display: none; }
  .btn.ghost { padding: 0; width: 38px; justify-content: center; }
  .layout { grid-template-columns: 1fr; }
  .panel { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--hair); padding: 0 16px 8px; }
  .stage { padding: 24px 16px 48px; }
  .pages { gap: 24px; }
  .chip { height: 36px; padding: 0 14px; }
  .stepper { height: 44px; }
  .stepper button { width: 36px; height: 36px; }
}
@media (max-width: 420px) {
  .topbar { gap: 10px; }
  .wordmark { height: 24px; width: calc(var(--w, 210) * 24px / 36); }
  .btn.ink { padding: 0 14px; }
  .btn.ink .icon { display: none; }
  .btn.ink .btn-label { font-size: 13px; }
}

/* ---------- Print ---------- */
@media print {
  body { background: #fff; }
  .topbar, .panel, .summary, .page figcaption { display: none !important; }
  .layout { display: block; }
  .stage { padding: 0; background: #fff; }
  .pages { display: block; }
  .page { max-width: none; break-after: page; }
  .page:last-child { break-after: auto; }
  .sheet {
    box-shadow: none;
    border-radius: 0;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .writing .t, .settle .sheet { animation: none !important; }
}
