:root {
  color-scheme: light;
  --paper: #f7f5ed;
  --surface: #fffef9;
  --ink: #23362e;
  --muted: #4e6258;
  --line: #d5ded0;
  --soft: #e9efe0;
  --active: #244e3d;
  --share: #f5cb74;
  font-family: system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}
* { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
[hidden] { display: none !important; }
main {
  display: grid;
  grid-template-areas: "theme" "play" "records";
  gap: 1rem;
  margin: auto;
  max-width: 34rem;
  padding: 1rem clamp(0.65rem, 4vw, 1.5rem) 1.5rem;
}
.clues { display: contents; }
.theme {
  grid-area: theme;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.4rem;
}
.theme h1 { margin: 0; font-size: clamp(1.4rem, 5.2vw, 2rem); }
.puzzle-tools { display: flex; gap: 0.15rem; }
.puzzle-tools button {
  display: grid;
  place-items: center;
  min-width: 44px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}
#help { font-weight: 750; }
.play { grid-area: play; min-width: 0; }
.records { grid-area: records; min-width: 0; }
h1, h2, p { margin: 0; }
.edition { text-transform: uppercase; letter-spacing: 0.15em; font-weight: 750; font-size: 0.75rem; color: var(--active); }
h1 { font: 500 clamp(2.1rem, 7vw, 3.5rem)/1.02 ui-serif, Georgia, serif; margin: 0.25rem 0 0.35rem; overflow-wrap: anywhere; }
h2 { font-size: 0.875rem; margin: 0.65rem 0 0.35rem; }
#status, #shared { color: var(--muted); font-size: 0.875rem; line-height: 1.35; }
.board {
  padding: clamp(0.5rem, 3vw, 1rem);
  border: 1px solid var(--line);
  border-radius: 1.125rem;
  background: var(--surface);
}
.metrics {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.35rem;
  margin: 0 0 0.55rem;
  padding-bottom: 0.45rem;
  border-bottom: 1px solid var(--line);
}
.metrics dt { color: var(--muted); font-size: 0.72rem; line-height: 1.15; }
.metrics dd { margin: 0.12rem 0 0; font-weight: 750; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
#score { color: var(--active); }
#remaining[data-expired] { color: #963816; }
.board-top, .reading { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.board-top { font-weight: 750; color: var(--muted); font-size: 0.78rem; padding: 0 0 0.32rem; }
#grid { display: grid; gap: 0.23rem; touch-action: none; user-select: none; -webkit-user-select: none; }
button {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  min-height: 44px;
  background: var(--surface);
  padding: 0.35rem 0.65rem;
  cursor: pointer;
}
button:focus-visible { outline: 3px solid #9b590e; outline-offset: 2px; }
button:disabled { opacity: 0.55; cursor: default; }
.cell {
  display: grid;
  place-items: center;
  position: relative;
  padding: 0;
  aspect-ratio: 1.12;
  max-height: 3.55rem;
  font: 650 clamp(1rem, 5vw, 1.5rem)/1 ui-serif, Georgia, serif;
  background: var(--soft);
}
.cell[data-window] {
  background: var(--active);
  color: white;
  border-color: var(--active);
}
.cell[data-window]::before {
  content: attr(data-window);
  position: absolute;
  left: 0.24em;
  top: 0.2em;
  font: 700 0.55em/1 system-ui, sans-serif;
}
.cell[data-shared] {
  border: 2px solid #80510b;
  background: var(--share);
  color: #3b2606;
}
.cell[data-shared]::after {
  content: "◆";
  position: absolute;
  bottom: 0.12em;
  right: 0.18em;
  font: 0.48em/1 system-ui, sans-serif;
}
.reading { margin-top: 0.6rem; flex-wrap: wrap; }
#letters { display: flex; gap: 0.3rem; }
#letters span {
  background: var(--soft);
  padding: 0.25rem 0.5rem;
  border-radius: 0.4rem;
  font-weight: 750;
}
#status:not(:empty) { margin: 0.4rem 0; }
#status:empty + .moves { margin-top: 0.45rem; }
.moves { display: grid; grid-template-columns: auto 1fr; gap: 0.5rem; }
.result-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.result-actions button { margin-top: 0.5rem; font-weight: 750; }
#commit { color: white; font-weight: 750; background: var(--active); border-color: var(--active); }
.history-scroll {
  overflow: auto;
  max-height: 11rem;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  background: var(--surface);
}
#history, #top-scores { margin: 0; padding: 0; list-style: none; }
#history li, #top-scores li { padding: 0.45rem 0.6rem; }
#history li + li, #top-scores li + li { border-top: 1px solid var(--line); }
#history small { display: block; color: var(--muted); }
.history-score { float: right; padding-left: 0.35rem; font-variant-numeric: tabular-nums; }
.history-undo .history-score { color: #963816; }
.archive { margin: 0.65rem 0; }
.archive summary { cursor: pointer; font-weight: 700; padding: 0.35rem 0; }
#archive-form { display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem; }
#archive-date { min-height: 44px; border: 1px solid var(--line); border-radius: 0.7rem; padding: 0.35rem; font: inherit; color: var(--ink); background: var(--surface); }
#rankings { font-size: 0.85rem; }
#rankings p { margin-block: 0.3rem; }
#top-scores { max-height: 11rem; overflow: auto; }
#your-rank { font-weight: 750; }
.boards { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.65rem; }
#loading { margin: 1rem; }

html[data-layout="wide"] #app {
  grid-template-areas: "play clues";
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  max-width: 65rem;
  gap: clamp(1rem, 4vw, 3rem);
  align-items: start;
}
html[data-layout="wide"] .clues { display: block; grid-area: clues; }
@media (min-width: 620px) {
  html:not([data-layout]) #app {
    grid-template-areas: "play clues";
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    max-width: 65rem;
    gap: clamp(1rem, 4vw, 3rem);
    align-items: start;
  }
  html:not([data-layout]) .clues { display: block; grid-area: clues; }
}
html[data-layout="single"] main {
  position: absolute;
  left: var(--region-x);
  top: var(--region-y);
  width: var(--region-width);
  height: var(--region-height);
  margin: 0;
  overflow: auto;
}
html[data-layout="book"] #app,
html[data-layout="tabletop"] #app {
  max-width: none;
  margin: 0;
  gap: 0;
  padding: 0;
  align-items: stretch;
  height: 100dvh;
}
html[data-layout="book"] #app {
  grid-template-areas: "play fold clues";
  grid-template-columns: minmax(0, var(--region-first)) minmax(0, var(--region-gap)) minmax(0, 1fr);
}
html[data-layout="tabletop"] #app {
  grid-template-areas: "clues" "fold" "play";
  grid-template-rows: var(--region-first) var(--region-gap) minmax(0, 1fr);
}
html:is([data-layout="book"], [data-layout="tabletop"]) .play,
html:is([data-layout="book"], [data-layout="tabletop"]) .clues {
  padding: 0.65rem clamp(0.5rem, 2vw, 1rem);
  overflow: auto;
}
html:is([data-layout="book"], [data-layout="tabletop"]) .clues { display: block; grid-area: clues; }
html[data-layout="tabletop"] .play { width: min(32rem, 100%); justify-self: center; }
html[data-layout="tabletop"] .cell { max-height: 44px; }
html[data-layout="tabletop"] .board { padding: 0.45rem; }
html[data-layout="tabletop"] .moves { margin-top: 0.25rem; }
html[data-layout="tabletop"] .clues { display: grid; grid-template-areas: "theme records"; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-content: center; gap: 0.75rem; }
html[data-layout="tabletop"] .history-scroll { max-height: 7rem; }

#replay-note { margin-top: 0.45rem; color: var(--muted); font-size: 0.875rem; }
dialog {
  max-width: min(24rem, 90vw);
  border: 1px solid var(--line);
  border-radius: 1rem;
  padding: 1rem;
  background: var(--surface);
  color: var(--ink);
}
dialog h2 { margin-top: 0; }
dialog .edition, #tutorial-hint { margin: 0.5rem 0 0.25rem; }
dialog p { line-height: 1.4; }
dialog::backdrop { background: rgb(35 54 46 / 48%); }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: end; gap: 0.45rem; margin-top: 0.9rem; }

#theme:focus, #home-title:focus { outline: none; }
#home {
  grid-template-areas: "home-play" "home-scores";
  align-content: start;
}
.home-play { grid-area: home-play; min-width: 0; }
.home-play header p, #recent-status { color: var(--muted); line-height: 1.4; }
.practice-notice { display: none; }
html[data-practice-preview] .practice-notice { display: block; margin-top: 0.4rem; }
html[data-practice-preview] .home-scores { display: none; }
html[data-practice-preview] #home { display: block; max-width: 34rem; }
.home-feature {
  display: grid;
  gap: 0.6rem;
  margin-top: 1.25rem;
  padding: clamp(1rem, 4vw, 1.45rem);
  border-radius: 1.125rem;
  background: var(--active);
  color: var(--surface);
}
.home-feature .edition { color: var(--share); }
.home-feature h2 { margin: 0; font: 500 clamp(1.8rem, 5vw, 2.45rem)/1.1 ui-serif, Georgia, serif; }
#today { justify-self: start; font-weight: 750; color: var(--ink); }
.home-scores {
  grid-area: home-scores;
  min-width: 0;
  align-self: start;
  border: 1px solid var(--line);
  border-radius: 1.125rem;
  background: var(--surface);
  padding: 1rem;
}
.home-scores h2 { margin: 0 0 0.45rem; font-size: 1.05rem; }
#recent-status { font-size: 0.875rem; }
#recent-scores { margin: 0; padding: 0; list-style: none; }
#recent-scores li + li { border-top: 1px solid var(--line); }
#recent-scores button {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.2rem 0.6rem;
  align-items: center;
  width: 100%;
  border: 0;
  text-align: left;
}
#recent-scores button:hover { background: var(--soft); }
#recent-scores small { color: var(--muted); }
.home-score { text-align: right; font-variant-numeric: tabular-nums; }
#refresh-recent { margin-top: 0.5rem; }

@media (min-width: 620px) {
  html:not([data-layout]) #home {
    grid-template-areas: "home-play home-scores";
    grid-template-columns: minmax(0, 1fr) minmax(0, 0.85fr);
    max-width: 58rem;
    gap: 1.5rem;
  }
}
html[data-layout="wide"] #home {
  grid-template-areas: "home-play home-scores";
  grid-template-columns: minmax(0, 1fr) minmax(0, 0.85fr);
  max-width: 58rem;
  gap: 1.5rem;
}
html:is([data-layout="book"], [data-layout="tabletop"]) #home {
  max-width: none;
  margin: 0;
  padding: 0;
  gap: 0;
  height: 100dvh;
  align-items: stretch;
}
html[data-layout="book"] #home {
  grid-template-areas: "home-play fold home-scores";
  grid-template-columns: minmax(0, var(--region-first)) minmax(0, var(--region-gap)) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}
html[data-layout="tabletop"] #home {
  grid-template-areas: "home-scores" "fold" "home-play";
  grid-template-rows: var(--region-first) var(--region-gap) minmax(0, 1fr);
}
html:is([data-layout="book"], [data-layout="tabletop"]) .home-play,
html:is([data-layout="book"], [data-layout="tabletop"]) .home-scores {
  padding: 0.65rem clamp(0.65rem, 3vw, 1.5rem);
  min-height: 0;
  overflow: auto;
}
html:is([data-layout="book"], [data-layout="tabletop"]) .home-scores {
  margin: 0.65rem;
  align-self: stretch;
}

#progress { display: flex; flex-wrap: wrap; justify-content: end; gap: 0.22rem; }
#progress span {
  display: inline-grid;
  place-items: center;
  width: 1.2rem;
  height: 1.2rem;
  border: 1px solid #788a7b;
  border-radius: 0.28rem;
  background: var(--soft);
}
#progress [data-checked] {
  border-color: #2d6a4b;
  background: #2d6a4b;
  color: white;
}
#progress [data-checked]::before { content: "✓"; font-size: 0.9rem; line-height: 1; }

.cell {
  --badge-0: #77531d;
  --badge-1: #285847;
  --badge-2: #753b58;
  --badge-3: #345579;
  --trail-0: #926827;
  --trail-1: #39765c;
  --trail-2: #9b5276;
  --trail-3: #4b7199;
}
#grid:not([data-rejected]) .cell[data-window]:not([data-shared]) {
  --trail-0: #f5cb74;
  --trail-1: #a0dbaf;
  --trail-2: #f3aeca;
  --trail-3: #a8d4fc;
}
.link-order {
  position: absolute;
  bottom: 0.18rem;
  left: 0.18rem;
  display: grid;
  place-items: center;
  min-width: 0.95rem;
  min-height: 0.95rem;
  padding: 0 0.12rem;
  border: 1px solid var(--surface);
  border-radius: 1rem;
  color: white;
  font: 750 0.62rem/1 system-ui, sans-serif;
  pointer-events: none;
}

#celebration { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 2; }
#celebration span {
  position: absolute;
  top: -1rem;
  width: 0.45rem;
  height: 0.7rem;
  border-radius: 0.08rem;
  animation: confetti-fall linear both;
}
#celebration span:nth-child(3n) { width: 0.38rem; height: 0.38rem; border-radius: 50%; }
@keyframes confetti-fall {
  0% { transform: translate3d(0, 0, 0) rotate(0); }
  78% { opacity: 1; }
  100% { transform: translate3d(var(--drift), 106dvh, 0) rotate(570deg); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  #celebration { display: none; }
}

#grid[data-rejected] .cell[data-window] {
  color: #61251d;
  background: #f8dcd5;
  border: 2px solid #9e3b32;
}
#status[data-rejected] {
  border: 1px solid #bd786e;
  border-radius: 0.55rem;
  background: #fae9e4;
  color: #61251d;
  padding: 0.45rem 0.6rem;
  font-weight: 650;
}
.history-rejected .history-score { color: #9e3b32; }
#penalty-rule { margin-top: 0.5rem; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
