* { box-sizing: border-box; }
html { background: #faf7ef; color: #283d32; }
body { margin: 0; font-family: Arial, "Noto Sans TC", sans-serif; }
button, select, a, #difficulty-field label, #difficulty-field input { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
button, select { font: inherit; color: #283d32; background: #fffdf7; border: 2px solid #45654e; border-radius: 10px; min-height: 48px; }
button { padding: 8px 14px; cursor: pointer; }
button:disabled { cursor: default; color: #68726b; border-color: #acb5a9; }
button:focus-visible, a:focus-visible, select:focus-visible, summary:focus-visible { outline: 3px dashed #111; outline-offset: 3px; }
[hidden] { display: none !important; }
.game-page { max-width: 900px; margin: 0 auto; padding: 8px 16px; font-size: 20px; }
.game-header { display: flex; align-items: center; gap: 8px; }
h1 { font-size: 30px; margin: 0; line-height: 1.15; flex: 1; color: #254f39; }
.home-link { color: #315a47; display: inline-flex; align-items: center; justify-content: center; min-width: 48px; min-height: 48px; padding: 0 4px; font-size: 24px; font-weight: 700; white-space: nowrap; }
.game-page #language-toggle { font-size: 18px; }
#new-game, #confirm { color: #fffdf7; background: #315a47; border-color: #254532; }
.game-info { margin: 4px 0 6px; font-size: 20px; line-height: 1.3; }
#level-label { display: inline-block; margin-right: 8px; white-space: nowrap; font-size: 18px; line-height: 1.3; border: 1px solid #8a6b35; padding: 1px 6px; border-radius: 5px; color: #674c21; background: #f7e8ba; }
/* Wrapped hints can use the full next line instead of a narrow text column. */
#message { display: inline; margin: 0; font-size: 20px; line-height: 1.3; overflow-wrap: anywhere; }
.complete { color: #21633f; font-weight: bold; text-decoration: underline; text-decoration-thickness: 2px; }
.board { display: grid; width: 100%; border: 2px solid #45654e; background: #fffdf7; }
.cell { position: relative; min-width: 0; width: 100%; aspect-ratio: 1; min-height: 0; padding: 0; border: 1px solid #777; border-radius: 0; display: flex; align-items: center; justify-content: center; font-size: 32px; font-size: clamp(24px, 7vw, 56px); line-height: 1; color: #111; background: #fff; user-select: none; }
.cell:disabled { color: #111; border-color: #777; opacity: 1; }
.cell.selected { box-shadow: inset 0 0 0 4px #254532; background: #d8eadc; }
.cell.given { font-weight: 700; }
.cell.answer { color: #21633f; }
.cell.conflict, .cell.error { text-decoration: underline double; box-shadow: inset 0 -5px #111; }
.cell.box-right { border-right: 3px solid #111; }
.cell.box-bottom { border-bottom: 3px solid #111; }
.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 10px 0; }
.controls button { min-width: 56px; font-size: 23px; flex: 1 1 56px; max-width: 150px; background: #e7efdf; }
.controls button:disabled { background: #f1f1e9; }
.direction-pad { display: grid; grid-template-columns: repeat(3, minmax(56px, 76px)); grid-template-rows: repeat(2, minmax(48px, auto)); }
.controls.direction-pad button { width: 100%; max-width: none; min-height: 52px; padding: 6px; font-size: 28px; }
.direction-pad [data-action="up"] { grid-column: 2; grid-row: 1; }
.direction-pad [data-action="left"] { grid-column: 1; grid-row: 2; }
.direction-pad [data-action="down"] { grid-column: 2; grid-row: 2; }
.direction-pad [data-action="right"] { grid-column: 3; grid-row: 2; }
.secondary { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-start; }
.secondary > button { flex: 0 1 auto; }
.secondary details[open] { flex-basis: 100%; }
details { flex: 1; min-width: 0; font-size: 20px; }
summary { min-height: 48px; padding: 10px 4px; cursor: pointer; }
details ul { padding-left: 24px; line-height: 1.6; }
.status { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 18px; line-height: 1.4; margin: 6px 0 0; }
.status p { margin: 0; }
.modal { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(255,255,255,.96); z-index: 10; display: flex; align-items: center; justify-content: center; padding: 20px; overflow: auto; }
.dialog-card { width: 100%; max-width: 480px; max-height: 100%; overflow: auto; border: 3px solid #45654e; border-radius: 14px; background: #fffdf7; padding: 22px; }
.dialog-card h2 { font-size: 26px; margin: 0 0 12px; }
.dialog-card p { font-size: 20px; line-height: 1.5; }
/* Visible native radios avoid opening a browser-owned popup on the Kindle. */
#difficulty-field { border: 0; padding: 0; margin: 16px 0 0; min-width: 0; }
#difficulty-field legend { padding: 0; margin-bottom: 8px; font-size: 20px; font-weight: 700; }
#difficulty-field label { display: flex; align-items: center; gap: 12px; min-height: 52px; padding: 10px 12px; border: 2px solid #45654e; border-radius: 10px; margin: 6px 0; cursor: pointer; touch-action: manipulation; }
#difficulty-field input { width: 22px; height: 22px; flex-shrink: 0; margin: 0; accent-color: #315a47; }
#difficulty-field input:focus-visible { outline: 3px dashed #111; outline-offset: 3px; }
#difficulty-field span { font-size: 20px; font-weight: 700; }
#difficulty-description { margin: 12px 0; }
.dialog-actions { display: flex; gap: 12px; }
.dialog-actions button { flex: 1; }
/* Detailed illustrations remain static; only a changed cell updates its art. */
.game-art { display: block; width: 100%; height: 100%; pointer-events: none; }
.cell > .game-art { position: absolute; top: 0; right: 0; bottom: 0; left: 0; }
.game-memory { gap: 8px; border: 0; background: transparent; }
.game-memory .cell { border: 3px solid #537467; border-radius: 12px; background: #fff8e7; }
.game-memory .face-down { background: #e1eee3; }
.game-memory .face-up { border-color: #9b6837; }
.game-memory .matched { border: 3px dashed #367148; background: #e1efcf; }
.game-memory .matched::after { content: '✓'; position: absolute; right: 3px; bottom: 3px; border: 1px solid #315a47; border-radius: 50%; width: 22px; height: 22px; font: bold 16px/20px Arial, sans-serif; color: #fff; background: #315a47; }
.game-tictactoe { gap: 7px; border: 7px solid #826339; background: #cdb488; border-radius: 12px; }
.game-tictactoe .cell { border: 1px solid #b79662; border-radius: 7px; background: #fff5dc; font-size: clamp(50px, 15vw, 120px); }
.game-tictactoe .winner { background: #e1efc9; box-shadow: inset 0 0 0 4px #447142; }
.game-maze { border: 3px solid #3f644b; background: #f6edce; }
.game-maze .cell { font-size: clamp(15px, 3.5vw, 32px); border-color: #3f644b; background: #fff5db; }
.game-maze .maze-start { background: #e9efcb; }
.game-maze .maze-exit { background: #f4deb4; }
.game-maze .maze-player { background: #fff9e7; }
.game-pipes { border: 3px solid #416d75; background: #e6f0eb; }
.pipe { font-family: monospace; background: #f0f5eb; border-color: #a7bcb0; }
/* Retain legible basic pipes if the illustration script cannot load. */
.pipe svg:not(.game-art) { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; fill: #235c76; }
.pipe svg:not(.game-art) path { fill: none; stroke: #235c76; stroke-width: 10; }
.pipe-source::after { content: '水'; position: absolute; left: 3px; top: 3px; font: bold 14px Arial, sans-serif; color: #fff; background: #235c76; border: 1px solid #234b55; border-radius: 4px; padding: 2px; }
.pipe.error { outline: 3px dashed #8c3629; outline-offset: -7px; }
.game-sokoban { border: 3px solid #6c6246; background: #f7eacb; }
.game-sokoban .cell { background: #fbefcf; border-color: #e4d4af; }
.game-sokoban .wall { background: #ccd2a5; border-color: #727952; }
.game-sokoban .goal { background: #e8edbd; }
.game-sokoban .cell, .game-parking .cell { font-size: clamp(20px, 5vw, 44px); }
.game-parking { border: 3px solid #6c8079; background: #dbe5df; }
.game-parking .cell { border-color: #92a79b; background: #e8eee7; }
.game-parking .vehicle { border-color: #edf1e7; }
.game-parking .vehicle.selected { background: #fcf1ba; border-color: #254532; box-shadow: none; }
.game-parking .target { font-weight: bold; color: #854921; }
.game-parking .exit { font-weight: bold; background: #d4e8bc; }
.game-parking .exit::after { content: '→'; position: absolute; right: 1px; bottom: 1px; padding: 0 3px; border: 1px solid #315a47; border-radius: 3px; background: #fff8e9; color: #254532; font: bold 20px/24px Arial, sans-serif; pointer-events: none; }
.game-mini-sudoku { border: 3px solid #605443; }
.game-mini-sudoku .cell { background: #fff9ed; border-color: #b5a389; }
.game-mini-sudoku .cell:nth-child(3), .game-mini-sudoku .cell:nth-child(4),
.game-mini-sudoku .cell:nth-child(7), .game-mini-sudoku .cell:nth-child(8),
.game-mini-sudoku .cell:nth-child(9), .game-mini-sudoku .cell:nth-child(10),
.game-mini-sudoku .cell:nth-child(13), .game-mini-sudoku .cell:nth-child(14) { background: #f1e6ce; }
.game-mini-sudoku .box-right { border-right-color: #605443; }
.game-mini-sudoku .box-bottom { border-bottom-color: #605443; }
.game-mini-sudoku .cell.selected { background: #d8eadc; }
/* Keep long English navigation on two rows without reducing its type size. */
@media (max-width: 559px) {
  html[lang="en"] .game-header { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 4px 8px; }
  html[lang="en"] .game-header h1 { grid-column: 1 / 3; grid-row: 1; }
  html[lang="en"] .game-header #language-toggle { grid-column: 3; grid-row: 1; }
  html[lang="en"] .game-header .home-link { grid-column: 1; grid-row: 2; margin-right: 0; justify-self: start; }
  html[lang="en"] .game-header #new-game { grid-column: 2 / 4; grid-row: 2; justify-self: end; }
}
@media (max-width: 419px) {
  .game-page { padding: 8px; }
  h1 { font-size: 23px; }
  .game-header { gap: 8px; }
  .game-header button { padding: 6px 8px; font-size: 20px; }
  .game-page #language-toggle { padding: 6px 8px; }
  .game-info, #message { font-size: 18px; }
  .controls { gap: 6px; }
  .controls button { font-size: 20px; }
  .game-memory { gap: 6px; }
  .modal { padding: 12px; }
  .dialog-card { padding: 16px; }
}
/* Without flexbox gap (see interface.css), the game buttons and the difficulty choices get the same spacing from margins.
   The direction keys are a grid, whose gap such browsers support; a game without buttons keeps its empty row as it is. */
@supports not (aspect-ratio: 1 / 1) {
  html:not(.readerplay-kobo) .controls:not(.direction-pad):not(:empty) { gap: 0; margin: 2px -4px 10px; }
  html:not(.readerplay-kobo) .controls:not(.direction-pad):not(:empty) > * { margin: 8px 4px 0; }
  html:not(.readerplay-kobo) #difficulty-field label { gap: 0; }
  html:not(.readerplay-kobo) #difficulty-field input { margin-right: 12px; }
  @media (max-width: 419px) {
    html:not(.readerplay-kobo) .controls:not(.direction-pad):not(:empty) { margin: 4px -3px 10px; }
    html:not(.readerplay-kobo) .controls:not(.direction-pad):not(:empty) > * { margin: 6px 3px 0; }
  }
}
