/* Layout: a stained ledger page is the hero; a narrow side column holds the letter-frequency table, with a keypad beneath the page for phones. */
  :root {
    --ground: #e6e0d6;
    --paper: #efe4c8;
    --paper-edge: #d8c79c;
    --stain: rgba(139, 94, 38, 0.2);
    --ink: #2a1c14;
    --pencil: #5b6470;
    --bad: #a3231b;
    --good: #3f6b3a;
    --seal: #8e1d17;
    --panel: #f6f2ea;
    --line: #cbbfa9;
    --muted: #6a5b50;
    --accent: #7a5619;
    --on-accent: #fffaf0;
    --sel: rgba(122, 86, 25, 0.22);
    --font-display: "IM Fell English", "Iowan Old Style", Georgia, serif;
    --font-body: "Karla", system-ui, -apple-system, "Segoe UI", sans-serif;
    --font-hand: "Reenie Beanie", "Segoe Script", "Bradley Hand", cursive;
  }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --ground: #17100d; --paper: #d9cba6; --paper-edge: #b9a778; --stain: rgba(110, 70, 25, 0.28);
      --ink: #25180f; --pencil: #4d5560; --bad: #9c1f18; --good: #3a6335; --seal: #a3231b;
      --panel: #221813; --line: #43332a; --muted: #ab9a8b; --accent: #d3a557; --on-accent: #1a1210;
      --sel: rgba(120, 80, 20, 0.3); color-scheme: dark;
    }
  }
  :root[data-theme="dark"] {
    --ground: #17100d; --paper: #d9cba6; --paper-edge: #b9a778; --stain: rgba(110, 70, 25, 0.28);
    --ink: #25180f; --pencil: #4d5560; --bad: #9c1f18; --good: #3a6335; --seal: #a3231b;
    --panel: #221813; --line: #43332a; --muted: #ab9a8b; --accent: #d3a557; --on-accent: #1a1210;
    --sel: rgba(120, 80, 20, 0.3); color-scheme: dark;
  }

  * { box-sizing: border-box; }
  body {
    background: var(--ground); color: var(--ink);
    font-family: var(--font-body); font-size: 16px; line-height: 1.5;
    padding-block: 28px 44px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
  }
  body, .side, .keys, .actions { color: var(--ink); }
  @media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) body { color: #efe5d2; } :root:not([data-theme="light"]) .ledger { color: var(--ink); } }
  :root[data-theme="dark"] body { color: #efe5d2; }
  :root[data-theme="dark"] .ledger { color: var(--ink); }

  .wrap { max-width: 1080px; margin: 0 auto; }
  header { margin-bottom: 20px; }
  .eyebrow { font-size: 12px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--accent); font-weight: 700; margin: 0 0 6px; }
  h1 { font-family: var(--font-display); font-weight: 400; font-size: clamp(34px, 6.4vw, 56px); line-height: 1.04; margin: 0 0 8px; text-wrap: balance; }
  .lede { margin: 0; color: var(--muted); max-width: 56ch; }

  .layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 22px; align-items: start; }
  @media (min-width: 920px) { .layout { grid-template-columns: minmax(0, 1fr) 268px; gap: 30px; } }

  /* The page */
  .ledger {
    position: relative; padding: clamp(18px, 3.4vw, 34px); min-width: 0;
    background:
      radial-gradient(ellipse 34% 30% at 82% 16%, var(--stain), transparent 70%),
      radial-gradient(ellipse 22% 26% at 14% 84%, var(--stain), transparent 72%),
      radial-gradient(ellipse 60% 45% at 50% 108%, rgba(120, 80, 30, 0.22), transparent 70%),
      var(--paper);
    border: 1px solid var(--paper-edge);
    box-shadow: 0 12px 34px rgba(30, 15, 5, 0.3), inset 0 0 60px rgba(120, 85, 30, 0.2);
    border-radius: 2px;
  }
  .ledger::before { /* torn-out strip along the left, where the page was cut from the binding */
    content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 10px;
    background: repeating-linear-gradient(to bottom, rgba(90, 60, 25, 0.28) 0 7px, transparent 7px 14px);
    opacity: 0.55;
  }
  .ledger-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 16px; border-bottom: 2px double rgba(42, 28, 20, 0.5); padding-bottom: 8px; margin-bottom: 14px; }
  .ledger-head b { font-family: var(--font-display); font-weight: 400; font-size: clamp(18px, 3.2vw, 24px); font-style: italic; }
  .ledger-head span { font-family: var(--font-display); font-size: 15px; opacity: 0.8; }

  .text { display: flex; flex-wrap: wrap; gap: 20px 18px; padding-block: 4px 6px; }
  .word { display: flex; flex-wrap: nowrap; }
  .cell {
    position: relative; width: clamp(22px, 3.6vw, 34px); padding: 0; margin: 0; border: 0; background: transparent;
    display: flex; flex-direction: column; align-items: center; cursor: pointer; font: inherit; color: inherit;
    border-radius: 2px;
  }
  .cell:hover { background: rgba(122, 86, 25, 0.1); }
  .cell.same { background: var(--sel); }
  .cell.sel { background: var(--sel); box-shadow: inset 0 -3px 0 var(--accent); }
  .cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
  .c {
    font-family: var(--font-display); font-size: clamp(17px, 2.8vw, 24px); line-height: 1.5; letter-spacing: 0.02em;
  }
  .g {
    font-family: var(--font-hand); font-size: clamp(24px, 3.8vw, 34px); line-height: 1; height: 1.15em;
    color: var(--pencil); border-bottom: 1px solid rgba(42, 28, 20, 0.45); width: 86%; text-align: center; text-transform: lowercase;
  }
  .cell.wrong .g { color: var(--bad); text-decoration: underline wavy var(--bad); text-underline-offset: 3px; }
  .cell.given .g { color: var(--ink); }
  .punct {
    font-family: var(--font-display); font-size: clamp(17px, 2.8vw, 24px); line-height: 1.5; width: 0.5em; text-align: center; align-self: flex-start;
  }

  .ledger.solved .g { color: var(--ink); font-family: var(--font-display); font-size: clamp(17px, 2.8vw, 24px); border-bottom-color: transparent; text-transform: uppercase; }
  .ledger.solved .cell { cursor: default; background: transparent; box-shadow: none; }
  .ledger.solved .c { opacity: 0.35; }

  .reveal { margin-top: 20px; padding-top: 16px; border-top: 1px solid rgba(42, 28, 20, 0.3); display: flex; flex-wrap: wrap; align-items: center; gap: 14px 22px; }
  .reveal[hidden] { display: none; }
  .seal {
    width: 84px; height: 84px; flex: none; border-radius: 50%; display: grid; place-items: center; text-align: center;
    background: radial-gradient(circle at 35% 30%, #c6372d, var(--seal) 60%, #5e100c);
    color: #f6dcc4; font-family: var(--font-display); font-size: 15px; line-height: 1.1;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.4), inset 0 0 0 4px rgba(0, 0, 0, 0.12), inset 0 0 0 6px rgba(255, 220, 190, 0.18);
    transform: rotate(-8deg);
    animation: stamp 0.7s cubic-bezier(.2, 1.4, .4, 1) 1;
  }
  .seal b { display: block; font-size: 26px; font-weight: 400; }
  .reveal p { margin: 0; flex: 1 1 240px; min-width: 0; font-family: var(--font-display); font-size: clamp(17px, 2.6vw, 21px); font-style: italic; line-height: 1.4; }
  @keyframes stamp { from { transform: rotate(-8deg) scale(1.9); opacity: 0; } to { transform: rotate(-8deg) scale(1); opacity: 1; } }

  /* Controls */
  .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
  .btn { font: 700 14px var(--font-body); padding: 9px 14px; border: 1px solid var(--accent); background: transparent; color: inherit; cursor: pointer; }
  .btn.primary { background: var(--accent); color: var(--on-accent); }
  .btn:focus-visible, .key:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
  .status { margin: 12px 0 0; min-height: 1.5em; font-size: 14px; color: var(--muted); }

  .keys { display: grid; grid-template-columns: repeat(auto-fill, minmax(34px, 1fr)); gap: 5px; margin-top: 16px; }
  .key {
    font: 700 15px var(--font-body); padding: 9px 0; text-transform: uppercase; cursor: pointer; text-align: center;
    background: var(--panel); border: 1px solid var(--line); color: inherit;
  }
  .key.used { opacity: 0.4; }
  .key.erase { grid-column: span 2; }

  /* Side column */
  .side { display: flex; flex-direction: column; gap: 18px; min-width: 0; }
  .panel { background: var(--panel); border: 1px solid var(--line); padding: 14px 14px 12px; }
  .panel h2 { font-family: var(--font-display); font-weight: 400; font-size: 20px; margin: 0 0 2px; }
  .panel .sub { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
  .freq { display: grid; grid-template-columns: 1.6em 1fr 2.2em 1.4em; gap: 4px 8px; align-items: center; font-size: 14px; font-variant-numeric: tabular-nums; }
  .freq .L { font-family: var(--font-display); font-size: 17px; }
  .freq .bar { height: 8px; background: var(--line); position: relative; }
  .freq .bar i { position: absolute; inset: 0 auto 0 0; background: var(--accent); }
  .freq .n { color: var(--muted); text-align: right; }
  .freq .a { font-family: var(--font-hand); font-size: 22px; line-height: 1; color: var(--accent); text-transform: lowercase; }
  .freq .row-sel { display: contents; }
  .tips { margin: 0; padding-left: 1.1em; font-size: 13.5px; color: var(--muted); }
  .tips li + li { margin-top: 4px; }
  .tips b { color: inherit; }

  .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  @media (prefers-reduced-motion: reduce) { .seal { animation: none; } }
