:root {
  --paper: #f7f1e6; --card: #fffaf1; --cell: #f1e8d8; --ink: #1f1a16; --muted: #7a6d60;
  --line: #e3d7c4; --sel: #f2c94c; --accent: #1f7a6e; --seal: #1f7a6e; --seal-ink: #fff;
  --switch: #1f4e9c; --shadow: rgba(90, 60, 20, .45); --dot: rgba(120, 90, 50, .10);
  --c1:#f4a6a0; --c2:#9fd3c7; --c3:#f6d186; --c4:#b9c7f0; --c5:#d7b4e8; --c6:#a8d8a0; --c7:#f7b98b; --c8:#9cc9e8;
  --c9:#f2a7c9; --c10:#b5e3a1; --c11:#ffd9a0; --c12:#a7d8f0; --c13:#d9c2a3; --c14:#c8b8f5; --c15:#9ee0d6;
  --serif: "Noto Serif", Georgia, serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
html[data-theme="dark"] {
  --paper: #17140f; --card: #221e18; --cell: #2c2620; --ink: #f3ece0; --muted: #a99c8b;
  --line: #3a332b; --sel: #b8942e; --accent: #4fb3a5; --seal: #4fb3a5; --seal-ink: #fff;
  --switch: #5b8ae0; --shadow: rgba(0, 0, 0, .8); --dot: rgba(255, 240, 220, .05);
  --c1:#8e4a45; --c2:#3f7a6e; --c3:#8a7432; --c4:#4b5c94; --c5:#6e4c85; --c6:#4b7a44; --c7:#93593a; --c8:#3d6a8a;
  --c9:#8a4766; --c10:#557a45; --c11:#8f6a35; --c12:#3f6f8a; --c13:#7a6448; --c14:#5d4f94; --c15:#3d7f75;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; }
body {
  background-color: var(--paper);
  background-image: radial-gradient(var(--dot) 1px, transparent 1.2px);
  background-size: 18px 18px;
  color: var(--ink); font-family: var(--ui);
  -webkit-user-select: none; user-select: none; overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }

#app { max-width: 620px; margin: 0 auto; padding: 10px 12px 28px; }

/* Header */
.head { display: flex; align-items: center; gap: 14px; margin: 6px 2px 12px; }
.seal {
  flex: none; width: 54px; height: 54px; border-radius: 9px; background: var(--seal);
  transform: rotate(-4deg); display: grid; place-items: center; position: relative;
}
.seal::after { content: ""; position: absolute; inset: 4px; border-radius: 6px; border: 1.5px solid rgba(255,255,255,.35); }
.seal span { font-family: var(--serif); font-weight: 700; font-size: 31px; color: var(--seal-ink); line-height: 1; }
.titles { min-width: 0; flex: 1; }
h1 { font-family: var(--serif); font-weight: 900; font-size: clamp(19px, 5.9vw, 30px); margin: 0; white-space: nowrap; line-height: 1.15; }
.affirm {
  margin: 3px 0 0; color: var(--muted); font-size: clamp(12px, 3.5vw, 14px); font-style: italic;
  line-height: 1.3; min-height: 2.6em; display: flex; align-items: flex-start;
  transition: opacity .5s ease;
}
.affirm.fade { opacity: 0; }

/* Bar */
.bar { display: flex; gap: 8px; margin-bottom: 10px; }
.pill {
  background: var(--card); border: 1px solid var(--line); border-radius: 999px;
  height: 40px; padding: 0 14px; display: flex; align-items: center; gap: 6px; font-size: 14px; white-space: nowrap;
}
.chip { flex: 1; min-width: 0; justify-content: space-between; font-weight: 600; }
#chipText { overflow: hidden; text-overflow: ellipsis; }
.caret { color: var(--muted); }
.clock { font-variant-numeric: tabular-nums; font-weight: 600; }
.gear { padding: 0 11px; font-size: 17px; }

/* Board */
.board-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 10px;
  box-shadow: 0 8px 24px -16px var(--shadow);
}
.grid-wrap { position: relative; }
.grid {
  display: grid; gap: 3px; touch-action: none; aspect-ratio: 1 / 1; width: 100%;
  grid-template-columns: repeat(var(--n), 1fr);
}
.grid.shake { animation: shake .3s; }
@keyframes shake { 20%{transform:translateX(-6px)} 40%{transform:translateX(5px)} 60%{transform:translateX(-4px)} 80%{transform:translateX(2px)} }
.cell {
  background: var(--cell); border-radius: 8px; display: grid; place-items: center;
  font-weight: 800; font-size: var(--fs, 18px); line-height: 1; color: var(--ink);
  transition: transform .08s, background-color .1s;
}
.cell.found { background: var(--fc); }
.cell.sel { background: var(--sel); transform: scale(1.07); position: relative; z-index: 1; }
.cell.hint { animation: hint .45s 3; }
@keyframes hint { 50% { background: var(--accent); color: #fff; transform: scale(1.15); } }

.pause-cover {
  position: absolute; inset: 0; border-radius: 12px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 16px;
  background: color-mix(in srgb, var(--card) 55%, transparent); backdrop-filter: blur(9px); -webkit-backdrop-filter: blur(9px);
}
.pause-word { font-family: var(--serif); font-weight: 900; font-size: 40px; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-top: 10px; min-width: 0; }
.count { font-weight: 700; font-size: clamp(12px, 3.5vw, 14px); white-space: nowrap; font-variant-numeric: tabular-nums; }
.tools { display: flex; gap: 5px; }
.tools .btn { padding: 8px clamp(7px, 2.4vw, 11px); font-size: clamp(13px, 3.6vw, 14px); }
.btn {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 11px;
  font-size: 14px; font-weight: 600; white-space: nowrap;
}
.btn:active { transform: scale(.96); }
.btn.primary { background: var(--seal); color: var(--seal-ink); border-color: transparent; }
.btn.wide { width: 100%; padding: 13px; font-size: 16px; }
.btn:disabled { opacity: .45; }

/* Word cards */
.words { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
@media (min-width: 640px) { .words { grid-template-columns: 1fr 1fr 1fr; } }
.wcard {
  position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 9px 12px; text-align: left; min-width: 0;
}
.wcard .w { font-family: var(--serif); font-weight: 700; font-size: 18px; letter-spacing: .5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.wcard .len { font-size: 12px; color: var(--muted); }
.wcard.found { border-color: transparent; }
.wcard.found::after { content: "✓"; position: absolute; top: 5px; right: 8px; font-weight: 800; font-size: 13px; }
.help { text-align: center; color: var(--muted); font-size: 12px; margin: 14px 0 0; }

/* Sheets */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.28); z-index: 20; }
.backdrop.dim { background: rgba(0,0,0,.4); }
.sheet { position: fixed; z-index: 21; background: var(--paper); color: var(--ink); display: flex; flex-direction: column; }
.sheet.bottom {
  left: 0; right: 0; bottom: 0; max-height: 86vh; border-radius: 20px 20px 0 0; max-width: 620px; margin: 0 auto;
  box-shadow: 0 -10px 30px -10px rgba(0,0,0,.35); animation: up .22s ease-out;
}
.sheet.full { inset: 0; z-index: 30; animation: fadein .18s; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fadein { from { opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; max-width: 620px; width: 100%; margin: 0 auto; }
.sheet-head h2 { margin: 0; font-family: var(--serif); font-size: 22px; font-weight: 900; }
.sheet-body { overflow-y: auto; padding: 0 16px 28px; max-width: 620px; width: 100%; margin: 0 auto; }
.sheet-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 18px 2px 8px; }
.note { color: var(--muted); font-size: 13px; margin: 8px 2px 0; }
.note.pad { margin: 0; padding: 0 14px 12px; }
.note.center { text-align: center; }
.foot { margin-top: 26px; font-size: 12px; }

.seg { display: flex; background: var(--cell); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border-radius: 9px; padding: 9px 4px; font-size: 14px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 4px rgba(0,0,0,.12); }
.seg.sizes button { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.seg.sizes small { font-size: 11px; font-weight: 500; opacity: .8; }
.seg.small { flex: none; }
.seg.small button { padding: 6px 12px; font-size: 13px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.chips button {
  background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 8px 13px; font-size: 14px; font-weight: 600;
}
.chips button.on { background: var(--seal); color: var(--seal-ink); border-color: transparent; }
.chips button small { font-weight: 500; opacity: .8; }

.group { background: var(--card); border: 1px solid var(--line); border-radius: 12px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; min-height: 52px; font-size: 15px; }
.group > .row + .row, .group > .row + .note + .row { border-top: 1px solid var(--line); }

.switch { appearance: none; -webkit-appearance: none; width: 46px; height: 28px; border-radius: 999px; background: var(--line); position: relative; flex: none; cursor: pointer; transition: background .2s; margin: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s; }
.switch:checked { background: var(--switch); }
.switch:checked::after { transform: translateX(18px); }
input[type=range] { width: 52%; accent-color: var(--switch); }

/* Win card */
.win {
  position: fixed; z-index: 25; left: 50%; top: 50%; width: min(88vw, 360px); transform: translate(-50%, -50%);
  background: var(--card); border-radius: 20px; padding: 26px 22px 20px; text-align: center;
  box-shadow: 0 20px 50px -20px rgba(0,0,0,.5); animation: pop .35s cubic-bezier(.2,1.4,.4,1);
}
@keyframes pop { from { transform: translate(-50%, -50%) scale(.7); opacity: 0; } }
.party { font-size: 46px; }
.praise { font-family: var(--serif); font-weight: 900; font-size: 34px; color: var(--accent); margin-top: 4px; }
.praise-line { font-size: 15px; margin-top: 4px; }
.result { color: var(--muted); font-size: 14px; margin: 12px 0 18px; }

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 20px); z-index: 40;
  background: #2a241d; color: #f6efe4; border-radius: 999px; padding: 10px 18px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
