/* Stavan site. The metaphor: a desk with notebook pages on it.
   Handwriting only ever appears on paper; everything practical sits on the desk in the system UI face. */

:root {
  --desk: #e9ecf1;
  --paper: #ffffff;
  --text: #1d2026;
  --muted: #5e6570;
  --rule: #c5d3e8;
  --grid: #dde5f1;
  --dot: #b4c1d4;
  --ink: #000000;           /* changed by the ink picker */
  --F: 36px;                /* writing size, changed by the slider */
  --L: calc(var(--F) * 1.7);
  --sheet-w: 880px;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { background: var(--desk); }
body { margin: 0; font: 16px/1.55 var(--ui); color: var(--text); -webkit-font-smoothing: antialiased; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.hand { font-family: "Stavan", cursive; font-feature-settings: "calt" 1, "kern" 1; font-weight: 400; }
button, .btn { font: inherit; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ---------- toolbar: a floating pen bar, like the one in a notes app ---------- */
.toolbar {
  position: sticky; top: 12px; z-index: 10;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 18px;
  width: min(var(--sheet-w), calc(100% - 24px)); margin: 12px auto 0; padding: 8px 14px;
  background: rgba(255, 255, 255, 0.86); backdrop-filter: blur(14px) saturate(1.4); -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border: 1px solid #d7dce4; border-radius: 14px; font-size: 14px;
}
.tool-group { display: flex; align-items: center; gap: 6px; }
.tool-label { color: var(--muted); margin-right: 2px; }
.swatch {
  width: 22px; height: 22px; border-radius: 50%; border: 0; padding: 0; cursor: pointer;
  background: var(--c); box-shadow: inset 0 0 0 2px #fff, 0 0 0 1px #c9cfd8;
}
.swatch[aria-checked="true"] { box-shadow: inset 0 0 0 3px #fff, 0 0 0 2px var(--c); }
input[type="range"] { width: 110px; accent-color: var(--ink); }
.seg { background: #eef0f4; border-radius: 9px; padding: 2px; gap: 2px; }
.seg button {
  border: 0; background: transparent; color: var(--muted); border-radius: 7px; padding: 4px 9px; cursor: pointer;
  display: inline-flex; align-items: center; min-height: 28px;
}
.seg button[aria-checked="true"] { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(29, 32, 38, 0.12); }
.seg svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.4; }
.seg svg .dot { stroke-width: 2.4; stroke-linecap: round; }
.actions button, .btn, .ghost, .copy {
  border: 1px solid #cfd5de; background: #fff; color: var(--text); border-radius: 9px; padding: 5px 11px;
  cursor: pointer; text-decoration: none; display: inline-block; min-height: 32px; line-height: 20px;
}
.actions button:hover, .btn:hover, .ghost:hover, .copy:hover { border-color: #9aa3b1; }
.primary, .actions .primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.primary:hover, .actions .primary:hover { border-color: var(--ink); filter: brightness(1.15); }

/* ---------- sheets of paper ---------- */
main { padding: 22px 12px 40px; }
.sheet {
  position: relative; width: min(var(--sheet-w), 100%); margin: 0 auto 28px; background: var(--paper);
  border-radius: 3px; box-shadow: 0 1px 2px rgba(29, 32, 38, 0.06), 0 8px 28px -12px rgba(29, 32, 38, 0.22);
}
.sheet-head { padding: 34px 56px 6px; min-height: 160px; display: grid; grid-template-columns: 1fr auto; align-items: start; gap: 0 24px; }
.sheet-head .subtitle { grid-column: 1 / -1; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; flex: 0 0 auto; max-width: 240px; padding-top: 6px; font-size: 14px; }
.title { color: var(--ink); height: 92px; margin: 0; font-size: inherit; }
.title svg { height: 100%; width: auto; display: block; overflow: visible; }
.subtitle { margin: 2px 0 0; font-size: 30px; line-height: 1.2; color: #636363; }

/* the writing area: paper templates aligned to the font's baseline.
   baseline of line n = pad-top + 1.05 F + n L   (line box 1.7em, ascent .95em, descent .55em) */
.sheet-body { position: relative; --pt: 10px; --pl: 56px; }
.sheet-body textarea, .replay-layer {
  font-size: var(--F); line-height: 1.7; color: var(--ink); padding: var(--pt) var(--pl) calc(var(--L) * 1.2);
}
.sheet-body textarea {
  display: block; width: 100%; min-height: calc(var(--L) * 9 + var(--pt)); border: 0; outline: none; resize: none;
  overflow: hidden; background-color: transparent; caret-color: var(--ink);
}
.sheet-body textarea::selection { background: color-mix(in srgb, var(--ink) 16%, transparent); }
.sheet-body textarea:focus-visible { outline: none; }
.sheet-body:has(textarea:focus-visible) { box-shadow: inset 3px 0 0 var(--ink); }
.replay-layer { position: absolute; inset: 0; pointer-events: auto; cursor: text; }
.replay-layer svg { display: block; overflow: visible; }
.lined { background-repeat: repeat; }

body[data-paper="ruled"] .lined, [data-paper-fixed="ruled"] {
  background-image: linear-gradient(to bottom, transparent calc(var(--L) - 1.25px), var(--rule) calc(var(--L) - 1.25px), var(--rule) var(--L));
  background-size: 100% var(--L);
  background-position: 0 calc(var(--pt) + var(--F) * 1.05 + 1px - var(--L));
}
body[data-paper="grid"] .lined {
  background-image:
    linear-gradient(to bottom, transparent calc(var(--L) / 2 - 1px), var(--grid) calc(var(--L) / 2 - 1px), var(--grid) calc(var(--L) / 2)),
    linear-gradient(to right, transparent calc(var(--L) / 2 - 1px), var(--grid) calc(var(--L) / 2 - 1px), var(--grid) calc(var(--L) / 2));
  background-size: calc(var(--L) / 2) calc(var(--L) / 2);
  background-position: 0 calc(var(--pt) + var(--F) * 1.05 + 1px - var(--L) / 2), calc(var(--pl) - var(--L) / 2) 0;
}
body[data-paper="dots"] .lined {
  background-image: radial-gradient(circle, var(--dot) 1.3px, transparent 1.8px);
  background-size: calc(var(--L) / 2) calc(var(--L) / 2);
  background-position: calc(var(--pl) - var(--L) / 4) calc(var(--pt) + var(--F) * 1.05 - var(--L) / 4);
}
[data-paper-fixed="grid"] {
  --g: 22px;
  background-image:
    linear-gradient(to bottom, transparent calc(var(--g) - 1px), var(--grid) calc(var(--g) - 1px), var(--grid) var(--g)),
    linear-gradient(to right, transparent calc(var(--g) - 1px), var(--grid) calc(var(--g) - 1px), var(--grid) var(--g));
  background-size: var(--g) var(--g);
}
[data-paper-fixed="dots"] {
  background-image: radial-gradient(circle, var(--dot) 1.3px, transparent 1.8px);
  background-size: 24px 24px;
}

.status { width: min(var(--sheet-w), 100%); margin: -18px auto 18px; min-height: 22px; color: var(--muted); font-size: 14px; text-align: right; }

/* ---------- desk copy ---------- */
.desk-copy { width: min(640px, 100%); margin: 0 auto 40px; padding: 8px 4px; }
.desk-copy h2 { font-size: 22px; line-height: 1.25; margin: 0 0 10px; font-weight: 650; letter-spacing: -0.01em; }
.desk-copy h3 { font-size: 16px; margin: 26px 0 6px; font-weight: 650; }
.desk-copy p { margin: 0 0 10px; color: #3a3f48; }
.row { display: flex; flex-wrap: wrap; gap: 8px; }
code { font: 0.92em ui-monospace, "SF Mono", Menlo, monospace; background: #dfe3ea; padding: 1px 5px; border-radius: 4px; }
.snippet { position: relative; margin: 8px 0 12px; }
.snippet pre {
  margin: 0; padding: 14px 16px; padding-right: 76px; background: #fff; border: 1px solid #d5dbe3; border-radius: 10px;
  font: 13px/1.55 ui-monospace, "SF Mono", Menlo, monospace; white-space: pre-wrap; overflow-wrap: anywhere; color: #2a2f37;
}
.snippet .copy { position: absolute; top: 8px; right: 8px; }
.bookmarklet { font-family: "Stavan", cursive; font-size: 24px; line-height: 1; padding: 8px 16px; cursor: grab; }

/* ---------- inner pages ---------- */
.sheet-pad { padding: 34px 56px 40px; }
.page-title { font-size: 46px; line-height: 1.2; margin: 0 0 4px; color: var(--ink); font-weight: 400; }
.page-note { margin: 0 0 20px; color: var(--muted); max-width: 60ch; background: rgba(255, 255, 255, 0.85); }

.picker { display: flex; flex-wrap: wrap; gap: 2px; margin-bottom: 18px; font-feature-settings: "calt" 0, "kern" 0; }
.picker button {
  font: inherit; font-size: 28px; line-height: 1; width: 44px; height: 46px; border: 0; border-radius: 8px; padding: 0 0 6px;
  background: transparent; color: var(--text); cursor: pointer;
}
.picker button:hover { background: rgba(29, 32, 38, 0.06); }
.picker button[aria-selected="true"] { background: var(--ink); color: #fff; }
.origins { display: grid; background: #fff; border: 1px solid #dfe5ee; border-radius: 12px; overflow: hidden; }
.origins:empty { display: none; }
.origin { display: grid; grid-template-columns: 92px 1fr; align-items: center; gap: 18px; padding: 10px 16px 10px 8px; }
.origin + .origin { border-top: 1px solid #edf0f5; }
.origin .glyph { height: 92px; display: flex; align-items: center; justify-content: center; color: var(--ink); }
.origin .glyph svg { height: 100%; width: auto; max-width: 100%; }
.origin .word { min-width: 0; }
.origin .word svg { max-width: 100%; height: 84px; width: auto; display: block; color: var(--ink); }
.origin .cap { font-size: 14px; color: var(--muted); margin: 2px 0 0; }
.origin svg { overflow: visible; }
.built { font-size: 15px; color: var(--muted); padding: 12px 4px; background: rgba(255, 255, 255, 0.9); }

.game { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.choice {
  height: 170px; border: 1px solid #d5dbe3; border-radius: 12px; background: rgba(255, 255, 255, 0.92); cursor: pointer; padding: 18px;
  display: flex; align-items: center; justify-content: center; color: var(--ink); position: relative;
}
.choice:hover:not([disabled]) { border-color: var(--ink); }
.choice svg { max-width: 100%; max-height: 100%; overflow: visible; }
.choice[disabled] { cursor: default; }
.choice .tag {
  position: absolute; left: 12px; top: 10px; font: 13px var(--ui); color: var(--muted);
}
.choice.is-real .tag { color: #1a7f37; }
.choice.picked { border-color: var(--ink); border-width: 2px; padding: 17px; }
.game-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; min-height: 36px; }
.game-result { margin: 0; color: #3a3f48; background: rgba(255, 255, 255, 0.9); }

.foot { text-align: center; color: var(--muted); font-size: 14px; padding: 10px 16px 40px; }
.foot a, .desk-copy a:not(.btn) { color: inherit; }

@media (max-width: 720px) {
  .toolbar { position: static; gap: 8px 12px; }
  .sheet-head { padding: 24px 22px 4px; min-height: 0; grid-template-columns: 1fr; }
  .actions { justify-content: flex-start; max-width: none; padding-top: 0; order: 3; margin-top: 10px; }
  .title { height: 64px; }
  .subtitle { font-size: 24px; }
  .sheet-body { --pl: 22px; }
  .sheet-pad { padding: 24px 20px 30px; }
  .page-title { font-size: 36px; }
  .origin { grid-template-columns: 64px 1fr; gap: 10px; }
  .origin .glyph { height: 70px; }
  .origin .word svg { height: 48px; }
  .choice { height: 120px; padding: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}

/* highlighted letter inside its source word */
.origin .word .faint { stroke: #b9c1cc; }

/* ---------- live ink: the textarea keeps the caret and selection, the pen draws the letters ---------- */
.ink-layer { position: absolute; inset: 0; pointer-events: none; padding: var(--pt) var(--pl); color: var(--ink); }
.ink-layer svg { display: block; overflow: visible; }
.ink-layer .s { stroke-dasharray: 1 2; stroke-dashoffset: 1; opacity: 0; animation: w var(--d) linear var(--t) forwards; }
@keyframes w { 0% { opacity: 1; stroke-dashoffset: 1; } 100% { opacity: 1; stroke-dashoffset: 0; } }
@media (prefers-reduced-motion: reduce) { .ink-layer .s { animation: none; opacity: 1; stroke-dashoffset: 0; } }
.sheet-body.live textarea { color: transparent; }
.sheet-body:not(.live) .ink-layer { display: none; }

.toggle {
  border: 1px solid #cfd5de; background: #fff; color: var(--muted); border-radius: 9px; padding: 4px 11px; min-height: 32px;
  cursor: pointer; display: inline-flex; align-items: center; gap: 7px;
}
.toggle::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #c9cfd8; }
.toggle[aria-pressed="true"] { color: var(--text); border-color: #9aa3b1; }
.toggle[aria-pressed="true"]::before { background: var(--ink); }

/* save menu */
.menu { position: relative; }
.menu summary {
  list-style: none; border: 1px solid #cfd5de; background: #fff; border-radius: 9px; padding: 5px 11px; min-height: 32px;
  line-height: 20px; cursor: pointer; user-select: none;
}
.menu summary::-webkit-details-marker { display: none; }
.menu summary::after { content: ""; display: inline-block; margin-left: 7px; width: 6px; height: 6px; border: solid currentColor; border-width: 0 1.5px 1.5px 0; transform: translateY(-3px) rotate(45deg); }
.menu[open] summary { border-color: #9aa3b1; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 5; min-width: 190px; padding: 5px; background: #fff;
  border: 1px solid #d7dce4; border-radius: 11px; box-shadow: 0 10px 30px -12px rgba(29, 32, 38, 0.3); display: grid;
}
.menu-list button { border: 0; background: transparent; text-align: left; padding: 7px 10px; border-radius: 7px; cursor: pointer; font-size: 14px; color: var(--text); }
.menu-list button:hover, .menu-list button:focus-visible { background: #eef0f4; }
.actions button[disabled] { opacity: 0.6; cursor: progress; }

/* code page */
@font-face { font-family: "Stavan Mono"; src: url("dist/StavanMono-Regular.woff2") format("woff2"); font-display: swap; }
.mono-sample {
  margin: 4px 0 18px; padding: 18px 22px; background: rgba(255, 255, 255, 0.94); border: 1px solid #dfe5ee; border-radius: 12px;
  font: 24px/1.6 "Stavan Mono", monospace; font-feature-settings: "calt" 1; color: #000; overflow-x: auto; white-space: pre;
}
.mono-sample .k { color: #d10000; }
.mono-sample .n { color: #007aff; }
.mono-sample .c { color: #636363; }
.mono-dl { margin: 0 0 8px; }
.page-note.small { font-size: 14px; }

.url-preview { margin: 4px 0 12px; }
.url-preview img { max-width: 100%; height: auto; display: block; }

@media (max-width: 720px) {
  .mono-sample { font-size: 16px; padding: 12px 14px; }
  .menu-list { right: auto; left: 0; }
}
