* { box-sizing: border-box; }
:root { font-family: "Nunito", "Segoe UI", "Leelawadee UI", sans-serif; color: #173a34; background: #f7f9ee; }
body { margin: 0; min-width: 290px; }
[hidden] { display: none !important; }
header, main { max-width: 650px; margin: auto; padding: 0 18px; }
header { display: flex; justify-content: space-between; align-items: center; min-height: 60px; }
.language { display: flex; align-items: center; gap: 9px; margin-left: auto; }
#language-flag { display: block; width: 32px; height: 22px; object-fit: contain; background: white; border: 1px solid #bdd0be; border-radius: 2px; }
button, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid #bdd0be; border-radius: 14px; background: white; padding: 8px 12px; }
button:disabled { cursor: default; opacity: .52; }
button:focus-visible, select:focus-visible, summary:focus-visible { outline: 3px solid #104a8a; outline-offset: 3px; }
button[aria-pressed="true"] { outline: 3px solid #107a63; outline-offset: -3px; background: #dbf5e5; }
select { border: 1px solid #bdd0be; border-radius: 8px; background: white; padding: 6px; }
h1 { margin: 6px 0 18px; font-size: clamp(1.45rem, 6vw, 2.8rem); line-height: 1.1; }
h2 { margin: 18px 0 9px; font-size: 1.05rem; }
#start { padding-bottom: 25px; }
.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modes button { min-height: 83px; }
.modes strong, .modes span { display: block; }
.modes strong { color: #3b2469; font-size: 1.4rem; margin-bottom: 4px; }
.number-level { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 14px; }
.number-level select { max-width: 100%; }
.level-label { margin: 0 0 8px; font-size: .9rem; }
.characters { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 7px; }
.character { display: grid; place-items: center; padding: 5px; min-height: 105px; }
.character img { width: 100%; height: 76px; object-fit: contain; border-radius: 9px; }
.character small { font-size: .67rem; line-height: 1.25; }
.pose-controls { display: grid; grid-template-columns: 44px minmax(0, 1fr) 44px; gap: 8px; align-items: center; margin-top: 8px; }
.pose-controls button { min-width: 44px; min-height: 44px; padding: 6px; font-size: 1.2rem; }
#pose-preview select { width: 100%; min-width: 0; min-height: 44px; margin: 0; }
#pose-preview figure { margin: 8px 0; text-align: center; }
#pose-image { display: block; width: 100%; height: clamp(220px, 45vh, 360px); object-fit: contain; background: #fffdf5; border-radius: 12px; }
#pose-caption { margin-top: 8px; }
.primary { background: #166b52; color: white; border: 0; padding: 11px 24px; font-weight: 700; margin: 14px 0; }
.quiet { background: transparent; }
#game { max-width: 440px; margin: auto; padding-bottom: 18px; }
.game-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.game-heading h1 { font-size: clamp(1.15rem, 5vw, 1.65rem); margin: 7px 0 10px; }
.score-heading { display: grid; grid-template-columns: minmax(0,1fr) auto minmax(0,1fr); align-items: center; min-height: 48px; margin-bottom: 6px; }
#score { grid-column: 2; font-size: clamp(1.9rem, 8vw, 2.6rem); font-weight: 800; line-height: 1.2; text-align: center; font-variant-numeric: tabular-nums; }
#score-label { font-size: .7em; }
.bgm-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin-bottom: 8px; font-size: .8rem; }
.bgm-controls button { min-height: 44px; padding: 6px 10px; }
.bgm-controls label { display: flex; align-items: center; gap: 5px; }
#bgm-volume { width: 92px; min-height: 44px; margin: 0; }
#bgm-message { flex-basis: 100%; }
#bgm-message:empty { display: none; }
#personal-best { grid-column: 3; margin-left: 6px; color: #c51f32; font-weight: 800; font-size: clamp(.7rem, 2.8vw, .95rem); line-height: 1.25; overflow-wrap: anywhere; }
.playfield { --sky-height: clamp(62px, 15vw, 100px); position: relative; border: 2px solid #a4c4ba; border-radius: 20px; overflow: hidden; background: #e5f4fb; }
.sky, .board { display: grid; grid-template-columns: repeat(5, minmax(0,1fr)); gap: 5px; padding-inline: 7px; }
.sky { position: relative; height: var(--sky-height); align-items: start; padding-top: 5px; background: linear-gradient(#a8daf7, #effaff); }
.board { position: relative; padding-block: 7px; background: #c9e5de; }
.cell { aspect-ratio: 1; min-width: 0; border: 0; background: var(--tile, #f3faf6); box-shadow: inset 0 -3px #173a3420; padding: 0; font-size: clamp(1.2rem, 6vw, 2rem); font-weight: 750; }
.cell.empty { background: #edf7f2; box-shadow: none; }
.cell[aria-pressed="true"] { background: var(--tile); outline-color: #164287; box-shadow: inset 0 0 0 5px #fff9; }
.cell.clear-preview { animation: clear-flash .2s ease-in-out 2; outline: 3px solid #a83272; outline-offset: -4px; opacity: 1; }
@keyframes clear-flash { 50% { background: #fff; } }
@media (prefers-reduced-motion: reduce) { .cell.clear-preview { animation: none; } }
.cell.help-target { outline: 4px solid #d71818; outline-offset: -4px; }
.cell.help.help-target { animation: none; }
.cell.help { animation: pulse .45s ease-in-out infinite alternate; }
@keyframes pulse { to { outline: 4px solid #a83272; outline-offset: -5px; } }
.drop { position: relative; z-index: 2; display: flex; justify-content: center; flex-wrap: wrap; gap: 2px; font-size: clamp(.75rem, 3vw, 1rem); font-weight: 750; }
.drop span { display: grid; place-items: center; min-width: 1.25em; border-radius: 4px; }
.drop.shake { animation: shake .16s linear infinite alternate; }
@keyframes shake { to { transform: translateX(2px) rotate(2deg); } }
.falling { position: absolute; z-index: 3; display: grid; place-items: center; pointer-events: none; width: calc(20% - 7px); aspect-ratio: 1; border-radius: 11px; font-size: 1.2rem; font-weight: 800; }
.playfield.animal-mode .cell:not(.falling) { font-size: clamp(1.5rem, 7.5vw, 2.5rem); line-height: 1; }
.falling.tappable-drop { pointer-events: auto; cursor: pointer; }
.exchange-selected .falling.tappable-drop { pointer-events: none; }
.tools { display: grid; grid-template-columns: 1fr 1.1fr 1fr; align-items: center; gap: 8px; margin-top: 6px; }
.helper { grid-column: 2; position: relative; text-align: center; }
#helper { padding: 0; border: 0; background: transparent; }
#portrait { width: 76px; height: 78px; object-fit: contain; border-radius: 12px; }
#help-count { position: absolute; right: 0; top: 0; background: #f5edb8; border-radius: 8px; padding: 2px 5px; font-size: .7rem; font-weight: 700; pointer-events: none; }
.tools:has(#help-count:not([hidden])) { grid-template-columns: minmax(0, 1fr) minmax(104px, 1.1fr) minmax(0, 1fr); }
#helper:has(#help-count:not([hidden])) { display: inline-flex; flex-direction: column; align-items: center; width: 120px; max-width: 100%; padding: 6px; border: 2px solid #bdd0be; border-radius: 16px; background: #fffdf5; }
#helper:has(#help-count:not([hidden])) #portrait { width: 96px; max-width: 100%; height: 98px; }
#helper #help-count:not([hidden]) { position: static; display: block; width: 100%; padding: 7px 4px; font-size: 1rem; line-height: 1.2; }
#equation, #status { min-height: 1.45em; margin: 4px 0; text-align: center; }
#equation { font-weight: 800; }
details { margin-top: 8px; padding: 9px 12px; border-radius: 12px; background: #e7f2e7; }
details p { margin: 7px 0 2px; }
summary { cursor: pointer; }
.result { position: absolute; inset: 0; z-index: 4; display: grid; place-items: center; padding: 16px; background: #173a3480; text-align: center; }
.result.game-over { align-items: start; }
.result-panel { background: #fffdf5; border-radius: 18px; padding: 20px 24px; box-shadow: 0 6px 24px #173a3440; }
.result-panel h2 { margin: 0 0 12px; font-size: clamp(1.25rem, 5vw, 1.75rem); }
.result-panel .primary { margin: 0; }
#effects { pointer-events: none; position: absolute; inset: 0; }
.petal { position: absolute; left: 50%; top: 55%; width: 13px; height: 8px; border-radius: 80% 20% 80% 20%; background: var(--color); animation: scatter .62s ease-out forwards; }
@keyframes scatter { to { transform: translate(var(--x), var(--y)) rotate(160deg); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .drop.shake, .petal { animation: none; } .cell.help { animation: none; outline: 4px solid #a83272; outline-offset: -5px; } }

/* Keep seven numeric rows and playable controls together on shorter phones.
   Never shrink a numeric cell below roughly 44 CSS pixels. Picture match is unchanged. */
@media (max-width: 500px) and (max-height: 740px) {
  body:has(#game.number-mode:not([hidden])) header { min-height: 44px; }
  body:has(#game.number-mode:not([hidden])) #home,
  body:has(#game.number-mode:not([hidden])) #language { min-height: 44px; }
  #game.number-mode .game-heading h1 { margin: 2px 0; font-size: 1.15rem; }
  #game.number-mode .level-label { margin: 0 0 2px; font-size: .8rem; }
  #game.number-mode .playfield { width: min(100%, clamp(258px, calc((100dvh - 270px) * .7142857), 340px)); margin-inline: auto; }
  #game.number-mode .sky { height: 32px; }
  #game.number-mode .cell { font-size: 1.2rem; }
  #game.number-mode .tools { grid-template-columns: 1fr; gap: 8px; margin-top: 4px; }
  #game.number-mode .helper { grid-column: 1; }
  #game.number-mode #helper { flex-direction: row; justify-content: center; gap: 4px; width: min(100%, 220px); min-height: 56px; padding: 3px; }
  #game.number-mode #helper #portrait { width: 44px; height: 44px; }
  #game.number-mode #helper #help-count { width: auto; padding: 4px; font-size: .9rem; white-space: nowrap; }
  #game.number-mode #equation, #game.number-mode #status { margin: 2px 0; min-height: 1.3em; font-size: .9rem; }
  #game.number-mode details { margin-top: 5px; }
}

#show-guide { min-height: 44px; margin-bottom: 4px; }
.start-guide { margin-top: 10px; padding: 12px 16px; border-radius: 14px; background: #e7f2e7; line-height: 1.6; overflow-wrap: anywhere; }
.start-guide h2 { margin-top: 0; }
.start-guide h3 { margin: 12px 0 4px; font-size: 1rem; }
.start-guide p { margin: 4px 0 10px; }

/* Both boards support touch dragging; the rest of the page still scrolls. */
#game .board { touch-action: pinch-zoom; user-select: none; -webkit-user-select: none; }
#game .cell { cursor: grab; }
#game .board.is-dragging .cell { cursor: grabbing; }
#game .cell.drag-source { opacity: .35; }
#game .cell.drag-outline { opacity: .85; box-shadow: none; outline: 3px dashed #164287; outline-offset: -4px; }
#game .refill-blocked-hint { margin: 5px 0 7px; padding: 5px 8px; color: #643100; background: #fff3d7; border-radius: 8px; text-align: center; font-size: .92rem; font-weight: 700; }
#game .refill-blocked-hint:empty { margin: 0; padding: 0; }
#game .board .cell.refill-blocker { position: relative; }
#game .board .cell.refill-blocker::after { content: ""; position: absolute; z-index: 2; left: 50%; bottom: calc(100% - 3px); width: 32px; height: 16px; transform: translateX(-50%); pointer-events: none; background: #a83200; clip-path: polygon(0 50%, 30% 0, 30% 31%, 70% 31%, 70% 0, 100% 50%, 70% 100%, 70% 69%, 30% 69%, 30% 100%); animation: refill-hint-pulse 1.1s ease-in-out infinite; }
#game .board .cell.refill-blocker[data-refill-direction="left"]::after { clip-path: polygon(0 50%, 37% 0, 37% 29%, 100% 29%, 100% 71%, 37% 71%, 37% 100%); }
#game .board .cell.refill-blocker[data-refill-direction="right"]::after { clip-path: polygon(100% 50%, 63% 0, 63% 29%, 0 29%, 0 71%, 63% 71%, 63% 100%); }
@keyframes refill-hint-pulse { 50% { opacity: .5; } }
@media (prefers-reduced-motion: reduce) { #game .board .cell.refill-blocker::after { animation: none; } }
#game .cell.drop-target { box-shadow: inset 0 0 0 4px #164287; }
.drag-ghost { position: fixed; z-index: 20; pointer-events: none; display: grid; place-items: center; transform: translate(-50%, -50%); border-radius: 14px; box-shadow: 0 6px 16px #173a3450; opacity: .9; }

.clock-label { display:flex; align-items:center; justify-content:space-between; gap:8px; margin:6px 0; font-weight:700; }
.clock-label button { min-height:44px; }
#time-bar { height:18px; background:#dbe3df; border-radius:12px; overflow:hidden; }
#time-fill { height:100%; background:#168bd0; }
#time-bar[data-color="yellow"] #time-fill { background:#e5b50a; }
#time-bar[data-color="red"] #time-fill { background:#d62735; }
#free-time { margin:4px 0 8px; font-size:.85rem; text-align:right; }
#clock-wait { position:absolute; top:24px; left:7px; right:7px; height:calc(var(--sky-height) - 24px); z-index:1; display:flex; align-items:center; justify-content:center; margin:0; padding:2px 6px; text-align:center; font-size:clamp(.75rem,2.6vw,.85rem); line-height:1.25; pointer-events:none; }
#pause-overlay { position:absolute; inset:0; z-index:8; background:#edf3e9; display:flex; flex-direction:column; align-items:center; justify-content:center; padding:20px; }
.playfield.paused > :not(#pause-overlay) { visibility:hidden; }
.playfield.paused * { animation-play-state:paused !important; }
#helper.urgent #portrait { animation:helper-hurry .18s ease-in-out infinite alternate; }
@keyframes helper-hurry { from { transform:translateX(-4px) rotate(-5deg); } to { transform:translateX(4px) rotate(5deg); } }
@media(prefers-reduced-motion:reduce) { #helper.urgent #portrait { animation:none; outline:3px solid #d62735; } }
#saved-animal { padding:12px; border-radius:14px; background:#e7f2e7; }
#saved-animal button { min-height:44px; margin:4px; }
#paid-guide { line-height:1.5; }
#save-warning { color:#9b2e19; font-size:.9rem; }
.celebration { position:fixed; inset:0; z-index:30; background:#fff8df; display:grid; grid-template-rows:auto minmax(0,1fr) auto; justify-items:center; gap:12px; padding:20px; padding-bottom:max(20px,env(safe-area-inset-bottom)); }
.celebration h2 { margin:0; font-size:clamp(1.8rem,6vw,3.5rem); }
.celebration img { width:100%; height:100%; min-height:0; object-fit:contain; }
.celebration button { min-height:44px; }

.voice-credit { text-align: center; padding: 16px; font-size: .8rem; }
#voice-message { flex-basis: 100%; }
#voice-message:empty { display:none; }
