:root {
  --bg: #dbeefc;
  --paper: #fbfdfc;
  --ink: #1f3029;
  --muted: #4c5f6c;
  --line: #b4d3e8;
  --river: #17708a;
  --river-soft: #b4e0dc;
  --river-ink: #ffffff;
  --tile: #deedc9;
  --tile-ink: #2c5a22;
  --tile-line: #a8ca89;
  --heading: #1e4f33;
  --sun-ink: #3a2208;
  --sun-faint: #fbe6b4;
  --sun-deep: #7a3c00;
  --sun-mid: #b0640a;
  --sun: #dc8a14;
  --sun-soft: #ffc04a;
  --cell-bg: #fff4dc;
  --cell-line: #d6b97c;
  --word: #f8e0ab;
  --blank: #c6e1f5;
  --bad: #b23a30;
  --ok: #2b7043;
  --hl: #fdeb9e;
  --shadow: 0 1px 0 rgba(30, 60, 85, .14);

  --serif: Charter, 'Bitstream Charter', 'Iowan Old Style', 'Palatino Linotype', Georgia, Cambria, 'Times New Roman', serif;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', 'DejaVu Sans', sans-serif;

  --gap: 3px;
  --maxw: 560px;
  --boardw: min(100vw, 960px);
  --cols: 12;
  --edge: 10px;
  /* buňka nesmí být menší než pohodlný dotykový cíl; co se nevejde, posouvá se */
  --cell: clamp(32px, calc((var(--boardw) - 32px - 2 * var(--edge) - (var(--cols) + 1) * var(--gap)) / var(--cols)), 40px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10253a;
    --paper: #182f45;
    --ink: #e9ebe5;
    --muted: #9fb3c0;
    --line: #2e4b62;
    --river: #5ab8c9;
    --river-soft: #1f4a55;
    --river-ink: #0f1a19;
    --tile: #2b4a2a;
    --tile-ink: #d3ebc0;
    --tile-line: #3f6638;
    --heading: #cfe6b8;
    --sun-ink: #1f1408;
    --sun-faint: #4a3a18;
    --sun-deep: #f8c877;
    --sun-mid: #f0b050;
    --sun: #f0b050;
    --sun-soft: #8a6219;
    --cell-bg: #38332a;
    --cell-line: #52492f;
    --word: #574720;
    --blank: #0a1826;
    --bad: #ec7a6e;
    --ok: #8bc87f;
    --hl: #4f4518;
    --shadow: 0 1px 0 rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.45;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

/* ---- hlavička ---- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 4px;
  max-width: var(--maxw);
  width: 100%;
  margin: 0 auto;
}
.brand { text-align: center; }
.brand h1 {
  font-family: var(--serif);
  font-weight: 700;
  font-style: italic;
  font-size: 1.7rem;
  line-height: 1.1;
  margin: 0;
  letter-spacing: .01em;
  color: var(--heading);
}
.brand h1::after {
  content: "";
  display: block;
  width: 4.5em;
  margin: 6px auto 0;
  border-top: 2px dashed var(--river);
  opacity: .6;
}
.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-family: var(--serif);
  font-size: .9rem;
}
.icon-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  flex: none;
}
.icon-btn svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn svg .fill { fill: currentColor; }
.icon-btn:active { background: var(--river-soft); }

/* ---- hlavní plocha ---- */
main {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 16px 24px;
}

.crumb {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 6px;
}
.crumb-text { flex: 1; text-align: center; min-width: 0; }
.section-title {
  font-family: var(--serif);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--heading);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.meta {
  color: var(--muted);
  font-size: .85rem;
  font-variant-numeric: tabular-nums;
}
.nav-btn {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 1.8rem;
  line-height: 1;
  color: var(--river);
  flex: none;
}
.nav-btn:disabled { color: var(--line); cursor: default; }
.nav-btn:not(:disabled):active { background: var(--river-soft); }

.progress {
  height: 3px;
  background: var(--line);
  border-radius: 2px;
  margin: 6px 0 14px;
  overflow: hidden;
}
.progress div {
  height: 100%;
  width: 0;
  background: var(--sun);
  transition: width .4s;
}

main > :not(.board) { max-width: var(--maxw); margin-inline: auto; }

/* ---- hrací deska: když se nevejde, posouvá se do stran ---- */
.board {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  margin: 0 -16px;
  padding: 4px 16px 8px;
  user-select: none;
  -webkit-user-select: none;
  scrollbar-width: thin;
  /* stíny na okrajích naznačují, že je kam posouvat */
  background:
    linear-gradient(to right, var(--bg) 40%, transparent) left / 32px 100% no-repeat local,
    linear-gradient(to left, var(--bg) 40%, transparent) right / 32px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, .16), transparent) left / 12px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, .16), transparent) right / 12px 100% no-repeat scroll;
}
.board > * { width: max-content; margin-inline: auto; }
.pool { margin-bottom: calc(var(--cell) / 2); }

.pool, .grid {
  display: grid;
  grid-template-columns: var(--edge) repeat(var(--cols), var(--cell)) var(--edge);
  gap: var(--gap);
}

.pool {
  align-items: stretch;
  padding-bottom: calc(var(--cell) / 2);
  border-bottom: 2px dashed var(--line);
}
.pool-col {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: var(--gap);
  min-height: calc(var(--depth, 1) * (var(--cell) + var(--gap)) - var(--gap));
  border-radius: 6px;
  transition: background .15s;
}
.pool-col.active { background: var(--sun-faint); box-shadow: 0 0 0 3px var(--sun-faint); }

.tile, .cell {
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * .58);
  font-weight: 600;
  line-height: 1;
  border-radius: 5px;
  padding: 0;
}

.tile {
  border: 1px solid var(--tile-line);
  background: var(--tile);
  color: var(--tile-ink);
  box-shadow: var(--shadow);
  transition: transform .08s, background-color .15s, color .15s;
}
.pool-col.active .tile { color: var(--sun-mid); }
.pool-col.active-sel .tile { color: var(--sun-deep); }
.tile:active { transform: scale(.9); }
.tile.flying { position: relative; z-index: 2; box-shadow: 0 3px 8px rgba(0, 0, 0, .18); }

.grid { position: relative; }
.cell {
  background: var(--cell-bg);
  border: 1px solid var(--cell-line);
  border-bottom-width: 2px;
  color: var(--ink);
}
.cell.blank, .cell.void { background: var(--blank); border-color: transparent; }
.edge {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: calc(var(--cell) * .55);
  font-weight: 700;
  line-height: 1;
}
.cell.word { background: var(--word); }
.cell.sel { border-color: var(--sun); background: var(--sun-soft); box-shadow: inset 0 0 0 1px var(--sun); }
.cell.locked { color: var(--river); }
.cell .ch {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: 4px;
}
.cell .ch.flying {
  position: relative;
  z-index: 2;
  background: var(--tile);
  color: var(--tile-ink);
  box-shadow: 0 0 0 1px var(--tile-line), 0 3px 8px rgba(0, 0, 0, .18);
}
.cell.pop, .tile.pop { animation: pop .18s ease-out; }

.board.solved .cell:not(.blank):not(.void) {
  background: var(--word);
  border-color: transparent;
  color: var(--ink);
  animation: wave .5s ease-out both;
  animation-delay: calc(var(--i, 0) * 12ms);
}
.board.gaveup .cell:not(.blank):not(.void) { color: var(--muted); }

.grid.shake { animation: shake .4s; }

@keyframes pop { 50% { transform: scale(1.12); } }
@keyframes wave { 40% { transform: translateY(-5px); } }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

.status {
  min-height: 1.5em;
  text-align: center;
  color: var(--muted);
  margin: 10px 0 0;
  font-size: .95rem;
}
.status.bad { color: var(--bad); }
.status.ok { color: var(--ok); font-weight: 600; }

/* ---- odkrytý text ---- */
.prose {
  font-family: var(--serif);
  font-size: 1.08rem;
  line-height: 1.6;
}
.prose p { margin: 0 0 .8em; text-indent: 1.2em; hyphens: auto; }
.prose p.verse { text-indent: 0; padding-left: 2em; font-style: italic; margin-bottom: .2em; }
.prose mark {
  background: var(--hl);
  color: inherit;
  border-radius: 3px;
  padding: 0 1px;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.prose ul { padding-left: 1.2em; }
.prose li { margin-bottom: .4em; }
.prose .small { font-size: .85rem; color: var(--muted); text-indent: 0; }

.reveal {
  margin-top: 18px;
  padding: 18px 18px 6px;
  background: var(--paper);
  border-radius: 12px;
  border: 1px solid var(--line);
  animation: rise .5s ease-out;
}
.reveal .result {
  font-family: var(--sans);
  font-size: .9rem;
  color: var(--muted);
  text-indent: 0;
  text-align: center;
  border-top: 1px solid var(--line);
  padding-top: 10px;
  margin-top: 14px;
}
.reveal .the-end { text-align: center; text-indent: 0; font-style: italic; }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }

/* ---- akční lišta ---- */
.actions {
  position: sticky;
  bottom: 0;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-top: 1px solid var(--line);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}
.play-actions, .done-actions {
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  gap: 6px;
}
.play-actions button {
  flex: 1;
  min-height: 52px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 2px;
  font-size: .78rem;
  line-height: 1.15;
  text-align: center;
  color: var(--ink);
}
.play-actions button span { font-size: 1.25rem; line-height: 1; color: var(--river); }
.play-actions button:active { background: var(--river-soft); }
.play-actions button:disabled { opacity: .4; }

.done-actions button {
  min-height: 48px;
  border-radius: 10px;
  padding: 0 14px;
  font-weight: 600;
}
.done-actions .primary {
  flex: 1;
  border: 0;
  background: var(--sun);
  color: #2a1a08;
}
.done-actions .secondary {
  border: 1px solid var(--line);
  background: var(--paper);
}
.done-actions button:disabled { opacity: .45; }

/* ---- dialogy ---- */
dialog.sheet {
  border: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  width: min(560px, 100vw);
  max-width: 100vw;
  max-height: min(85dvh, 760px);
  border-radius: 14px;
  display: none;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog.sheet[open] { display: flex; }
dialog.sheet::backdrop { background: rgba(20, 25, 24, .45); }
dialog.popup {
  border: 0;
  padding: 8px;
  margin: auto auto calc(76px + env(safe-area-inset-bottom));
  width: min(420px, calc(100vw - 24px));
  background: var(--paper);
  color: var(--ink);
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
dialog.popup[open] { animation: rise .2s ease-out; }
dialog.popup::backdrop { background: rgba(20, 25, 24, .3); }
dialog.popup form { display: flex; flex-direction: column; gap: 4px; margin: 0; }
dialog.popup button {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font: inherit;
}
dialog.popup button strong { font-size: 1rem; }
dialog.popup button span { font-size: .82rem; color: var(--muted); }
dialog.popup button:active, dialog.popup button:focus-visible { background: var(--river-soft); outline: none; }
dialog.popup button[value="solve"] strong { color: var(--bad); }
dialog.popup .cancel {
  align-items: center;
  margin-top: 2px;
  border-top: 1px solid var(--line);
  border-radius: 0 0 10px 10px;
  color: var(--muted);
}
@media (hover: hover) {
  dialog.popup button:hover { background: var(--river-soft); }
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 8px 20px;
  border-bottom: 1px solid var(--line);
  margin: 0;
}
.sheet-head h2 { font-family: var(--serif); margin: 0; font-size: 1.3rem; }
.sheet-body { padding: 16px 20px; overflow-y: auto; overscroll-behavior: contain; }
.sheet-body > p:first-child { text-indent: 0; }
.sheet-foot { padding: 8px 20px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); text-align: right; }
.sheet-actions { display: flex; gap: 6px; padding-inline: 8px; }
.sheet-actions button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 52px;
  padding: 2px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  font-size: .78rem;
  line-height: 1.15;
}
.sheet-actions button span { font-size: 1.25rem; line-height: 1; color: var(--river); }
.sheet-actions button:active { background: var(--river-soft); }
.sheet-actions .danger, .sheet-actions .danger span { color: var(--bad); }

.book h3 {
  font-size: 1.05rem;
  margin: 1.4em 0 .5em;
  text-align: center;
  font-style: italic;
}
.book h3:first-child { margin-top: 0; }
.sub {
  font-size: .95rem;
  font-weight: 400;
  font-style: italic;
  color: var(--muted);
  text-align: center;
  margin: 1em 0 .4em;
}
.book .chunk {
  display: block;
  position: relative;
  padding: 2px 8px;
  margin-inline: -8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color .15s, box-shadow .15s;
}
.book .chunk:active { background: var(--river-soft); }
.book .chunk { margin-bottom: .8em; }
.book .chunk p:last-of-type { margin-bottom: 0; }
.book .chunk .meta {
  margin-top: .1em;
  line-height: 1.3;
  text-align: right;
  text-indent: 0;
  font-family: var(--sans);
  font-size: .75rem;
  color: var(--muted);
}
@media (hover: hover) {
  .book .chunk:hover {
    background: var(--river-soft);
    box-shadow: inset 3px 0 0 var(--river);
  }
  .book .chunk:hover .meta { color: var(--river); }
}
.book .empty { color: var(--muted); text-indent: 0; text-align: center; }

@media (max-width: 600px) {
  dialog.sheet {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    padding-top: env(safe-area-inset-top);
  }
}

/* ---- toast ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: .9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (hover: hover) {
  .tile:hover { filter: brightness(1.08); }
  .cell:not(.blank):not(.void):hover { border-color: var(--sun); }
  .play-actions button:hover { background: var(--river-soft); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
