/* Browser controls are separate from the source game's desktop presentation. */
button { color: var(--ink); }
.web-game-controls,
.web-editor-controls { display: none; }
.editor-mobile-tool-toggle { display: none; }

@media (max-width: 760px), (pointer: coarse) {
  #game-screen #grid .moving-character,
  #editor-screen #editor-grid .moving-character,
  #game-screen #grid .selection-outline,
  #editor-screen #editor-grid .selection-outline { will-change: transform; }
  html, body { overflow-x: hidden; }
  #app.parchment {
    width: 100%;
    height: auto;
    min-height: 100dvh;
    overflow: visible;
    display: block;
    padding: max(12px, env(safe-area-inset-top)) 10px max(30px, env(safe-area-inset-bottom));
  }
  .screen.active { width: 100%; min-width: 0; align-items: flex-start; }
  .panel { min-width: 0; padding: 20px 14px; }
  #start-screen.screen { width: calc(100% + 20px); margin-inline: -10px; }
  .home-gallery { display: flex; flex-direction: column; min-height: 0; }
  .home-gallery-board { order: 0; width: 100%; height: min(430px, max(calc(100vw - 8px), 47dvh)); min-height: 0; max-height: none; padding: 4px; border-radius: calc(var(--radius-xl) - 3px) calc(var(--radius-xl) - 3px) 0 0; }
  .home-gallery-board iframe { height: 100%; }
  .home-gallery-menu { order: 1; padding: 18px 18px 22px; }
  .home-gallery-menu h1 { margin-bottom: 16px; font-size: clamp(40px, 11vw, 58px); }
  .home-gallery-menu .menu-actions { width: 100%; gap: 8px; padding-bottom: 0; }
  .home-gallery-menu .menu-actions button { width: 100%; max-width: none; min-height: 46px; }

  #chapter-screen { --menu-square-size: min(90px, calc((100vw - 76px) / 3)); }
  #chapter-screen:has(.has-chapter-ten) { --menu-square-size: min(78px, calc((100vw - 80px) / 4)); }
  #chapter-screen .panel { padding: 20px 10px; }
  #chapter-screen .chapter-layout { display: flex; flex-direction: column; align-items: center; width: 100%; }
  #chapter-screen .chapter-card-section,
  #chapter-screen .chapter-more-section { width: 100%; align-items: center; }
  #chapter-screen .chapter-more-section { order: 0; }
  #chapter-screen .chapter-card-section { order: 1; }
  #chapter-screen .chapter-list { grid-template-rows: none; grid-auto-rows: var(--menu-square-size); max-width: 100%; padding: 9px; gap: 5px; }
  #chapter-screen .chapter-btn { width: var(--menu-square-size); height: var(--menu-square-size); aspect-ratio: 1; overflow: hidden; gap: 2px; padding: 2px; }
  #chapter-screen .chapter-number { font-size: clamp(25px, 8vw, 40px); }
  #chapter-screen .chapter-name { font-size: clamp(11px, 3vw, 14px); }
  #chapter-screen .chapter-sub { font-size: 10px; line-height: 1; }
  #chapter-screen .chapter-more-list { width: min(100%, 340px); height: auto; grid-template-rows: none; grid-auto-rows: minmax(44px, auto); gap: 8px; }
  #chapter-screen .chapter-more-btn { font-size: 14px; }

  #editor-screen { --tool-cell-size: 38px; }
  #editor-screen .editor-panel { padding: 16px 10px; }
  #editor-screen .editor-workspace { padding: 9px; gap: 8px; }
  #editor-screen .editor-workspace-heading h3 { font-size: 1.2rem; }
  #editor-screen .editor-workspace-heading p { font-size: .8rem; }
  #editor-screen .editor-build-row { flex-direction: column; align-items: stretch; gap: 10px; }
  #editor-screen .tool-row { flex-direction: column; width: 100%; padding: 8px; }
  #editor-screen .tool-palette { grid-template-columns: repeat(5, var(--tool-cell-size)); gap: 5px; justify-content: center; }
  #editor-screen .editor-top-row { width: 100%; min-width: 0; }
  #editor-screen .editor-edge-layout { max-width: 100%; }
  #editor-screen #editor-grid,
  #game-screen #grid { padding: 6px; touch-action: none; }
  #editor-screen .editor-action-bar { flex-wrap: wrap; }
  #editor-screen .editor-action-bar button { flex: 1 1 30%; min-width: 0; padding-inline: 5px; font-size: .85rem; }
  .web-editor-controls { display: flex; justify-content: center; gap: 5px; width: 100%; }
  .web-editor-controls button { min-width: 0; padding: 7px 6px; flex: 1; font-size: .8rem; }
  .web-editor-controls .web-switch { flex: 2; }
  #solver-card { max-width: calc(100vw - 20px); min-width: 0; }

  #app.parchment:has(#game-screen.active) { height: 100dvh; min-height: 0; overflow: hidden; padding: max(8px, env(safe-area-inset-top)) 10px max(10dvh, env(safe-area-inset-bottom)); }
  #game-screen.active { height: 100%; align-items: stretch; }
  #game-screen .game-layout { width: 100%; height: 100%; gap: 6px; }
  #game-screen .challenge-hud { order: 0; }
  #game-screen .level-meta { order: 1; margin: 0 auto; }
  #game-screen .level-instructions.show { order: 2; position: static; width: 100%; max-width: 100%; margin: 0; font-size: .78rem; line-height: 1.1; }
  #game-screen .action-bar { order: 3; margin-top: auto; }
  #game-screen .web-game-controls { order: 4; }
  #game-screen .play-area { order: 5; flex: 0 1 auto; min-height: 0; }
  #game-screen .grid-shell { display: flex; flex-direction: column; align-items: center; max-width: 100%; }
  #game-screen #win-banner { pointer-events: none; }
  #game-screen .grid-shell,
  #game-screen .grid-wrapper { max-width: 100%; }
  #game-screen .action-bar,
  #game-screen.endless-game .action-bar { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); width: 100%; max-width: 100%; gap: 6px; }
  #game-screen .action-bar .route-back { width: 100%; min-width: 0; }
  #game-screen .action-bar .route-back-main { flex: 1 1 auto; width: auto; min-width: 0; padding-inline: 4px; font-size: .85rem; }
  #game-screen .action-bar .route-back:has(> .route-back-arrow + .route-back-arrow) > .route-back-arrow:first-child { display: none; }
  #game-screen .action-bar > button,
  #game-screen.endless-game .action-bar > button { width: 100%; min-width: 0; min-height: 44px; height: auto; padding: 7px 5px; font-size: .88rem; white-space: normal; line-height: 1.15; }
  #game-screen #back-to-levels,
  #game-screen #reset-level { background: #fffdf6; }
  .web-game-controls { display: flex; justify-content: center; width: 100%; }
  .web-game-controls .web-switch { width: 100%; min-height: 44px; padding: 7px; }
  .challenge-hud { flex-wrap: wrap; justify-content: center; font-size: .8rem; }
  .level-instructions { max-width: calc(100vw - 28px); }
}

@media (max-width: 360px) {
  #editor-screen { --tool-cell-size: 32px; }
  #editor-screen .tool-palette { gap: 3px; }
}

@media (max-height: 720px) and (max-width: 760px) {
  #game-screen .level-instructions.show { display: none; }
}

@media (max-height: 640px) and (max-width: 760px) {
  #app.parchment:has(#start-screen.active) { padding-block: 8px; }
  .home-gallery-menu { padding: 12px 16px 14px; }
  .home-gallery-menu h1 { margin-bottom: 10px; }
  .home-gallery-menu .menu-actions { gap: 6px; }
  .home-gallery-menu .menu-actions button { min-height: 42px; }
  #chapter-screen { --menu-square-size: min(70px, calc((100dvh - 375px) / 3)); }
  #chapter-screen .panel { padding-block: 12px; }
  #chapter-screen .chapter-more-list { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-auto-rows: minmax(40px, auto); gap: 6px; }
  #chapter-screen .chapter-more-list > :last-child { grid-column: 1 / -1; }
  #chapter-screen .chapter-more-btn { font-size: 12px; white-space: normal; line-height: 1.1; }
  #chapter-screen .chapter-layout h2 { font-size: 30px; }
}

@media (max-width: 760px), (pointer: coarse) {
  #app.parchment:has(#settings-screen.active),
  #app.parchment:has(#editor-screen.active) {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  }
  #settings-screen.active,
  #editor-screen.active { height: 100%; align-items: stretch; }

  #settings-screen .panel { height: 100%; min-height: 0; overflow: hidden; gap: 6px; padding: 10px; }
  #settings-screen .panel-header { align-items: center; }
  #settings-screen .panel-header h2 { margin: 0; font-size: clamp(26px, 7vw, 34px); line-height: 1; }
  #settings-screen .panel-header .subtitle { display: none; }
  #settings-screen .settings-grid { flex: 1 1 auto; min-height: 0; gap: 0; }
  #settings-screen .settings-group { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-content: start; gap: 6px; width: 100%; }
  #settings-screen .settings-group > * { min-width: 0; grid-row: auto; }
  #settings-screen #fullscreen-toggle { display: none; }
  #settings-screen .volume-control { grid-column: 1 / -1; grid-row: auto; gap: 3px; padding: 6px 10px; }
  #settings-screen .volume-header { font-size: .85rem; line-height: 1.1; }
  #settings-screen .volume-row { display: grid; grid-template-columns: 105px minmax(0, 1fr); align-items: center; gap: 6px; min-height: 31px; }
  #settings-screen .volume-label-row { gap: 4px; }
  #settings-screen .volume-label-small,
  #settings-screen .volume-value { font-size: .76rem; line-height: 1.1; }
  #settings-screen .movement-speed-control { grid-column: 1 / -1; grid-row: auto; padding: 5px 2px 4px; }
  #settings-screen .movement-speed-control label { display: flex; justify-content: space-between; gap: 8px; }
  #settings-screen .movement-speed-current { color: var(--muted); font-weight: 600; }
  #settings-screen .movement-speed-control .volume-slider { margin-top: 7px; padding-inline: 6px; }
  #settings-screen .movement-speed-labels { display: none; }
  #settings-screen #advanced-settings-btn,
  #settings-screen #controls-btn,
  #settings-screen #colors-btn,
  #settings-screen #credits-btn { grid-column: auto; grid-row: auto; }
  #settings-screen #advanced-settings-btn,
  #settings-screen #controls-btn,
  #settings-screen #colors-btn { grid-row: auto; }
  #settings-screen #reset-progress,
  #settings-screen .settings-hint { grid-column: 1 / -1; grid-row: auto; }
  #settings-screen .settings-group > button { min-height: 42px; height: auto; padding: 5px; font-size: .79rem; line-height: 1.1; white-space: normal; }
  #settings-screen .settings-hint { margin: 0; font-size: .7rem; line-height: 1.1; }
  #settings-screen .editor-footer { margin-top: auto; }
  #settings-screen .editor-footer button { min-height: 42px; padding-block: 7px; }

  #editor-screen { --tool-cell-size: clamp(32px, 9vw, 38px); }
  #editor-screen .editor-panel { height: 100%; min-height: 0; overflow: hidden; gap: 5px; padding: 8px; }
  #editor-screen .editor-title-row { flex: 0 0 auto; align-items: center; min-height: 36px; }
  #editor-screen .editor-eyebrow { display: none; }
  #editor-screen .editor-title-inline { font-size: clamp(25px, 7vw, 32px); line-height: 1; }
  #editor-screen .editor-status { max-width: 44%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .7rem; }
  #editor-screen .editor-controls { flex: 1 1 auto; min-height: 0; gap: 5px; }
  #editor-screen .editor-workspace { position: relative; flex: 1 1 auto; min-height: 0; padding: 5px; gap: 5px; }
  #editor-screen .editor-workspace-heading { display: none; }
  #editor-screen .editor-mobile-tool-toggle { display: flex; align-items: center; justify-content: space-between; gap: 5px; width: 100%; min-height: 40px; padding: 5px 8px; background: #fffdf6; font-size: .78rem; }
  #editor-screen .editor-mobile-tool-toggle strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .84rem; }
  #editor-screen .editor-mobile-tool-toggle[aria-expanded="true"] { background: #e9ddc8; }
  #editor-screen .editor-build-row { flex: 1 1 auto; min-height: 0; gap: 0; }
  #editor-screen .tool-row { display: none; position: absolute; z-index: 20; top: 49px; left: 5px; right: 5px; width: auto; padding: 8px; border: 2px solid var(--ink); box-shadow: 0 12px 28px rgba(0,0,0,.25); }
  #editor-screen .tool-row.mobile-tools-open { display: flex; }
  #editor-screen .tool-palette { grid-template-columns: repeat(5, var(--tool-cell-size)); gap: 5px; }
  #editor-screen .editor-top-row { flex: 1 1 auto; min-height: 0; align-items: center; }
  #editor-screen .editor-action-bar { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; width: 100%; }
  #editor-screen .editor-action-bar button { width: 100%; min-height: 42px; padding: 5px 2px; font-size: .76rem; line-height: 1.05; white-space: normal; }
  #editor-screen .web-editor-controls { width: 100%; }
  #editor-screen .web-editor-controls .web-switch { flex: 1; min-height: 40px; padding: 5px; }
  #editor-screen .editor-back-row { width: 100%; }
  #editor-screen .editor-back-row .route-back,
  #editor-screen .editor-back-row > button { width: 100%; min-height: 40px; }
}

/* Keep the playable UI above a thumb-safe bottom strip on phones. */
@media (max-width: 760px), (pointer: coarse) {
  html, body { height: 100%; overflow: hidden; }
  #app.parchment {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding: max(8px, env(safe-area-inset-top)) 10px max(10dvh, env(safe-area-inset-bottom));
  }
  #app.parchment:has(#start-screen.active) {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(10dvh, env(safe-area-inset-bottom));
    padding-inline: 14px;
  }
  .screen.active { width: 100%; max-height: 100%; min-height: 0; align-items: flex-end; }
  #start-screen.active,
  #chapter-screen.active,
  #endless-screen.active,
  #colors-screen.active,
  #level-select.active { height: 100%; align-items: stretch; }

  #start-screen.screen { width: 100%; height: 100%; margin-inline: 0; }
  #start-screen .home-gallery { height: 100%; min-height: 0; }
  #start-screen .home-gallery-board { flex: 1 1 auto; height: auto; min-height: 0; max-height: none; }
  #start-screen .home-gallery-menu { flex: 0 0 auto; }

  #chapter-screen {
    --menu-square-size: min(110px, calc((100vw - 58px) / 3), calc((90dvh - 295px) / 4));
  }
  #chapter-screen:has(.has-chapter-ten) { --menu-square-size: min(110px, calc((100vw - 58px) / 3), calc((90dvh - 295px) / 4)); }
  #chapter-screen .panel { height: 100%; min-height: 0; padding: 12px 8px; overflow: hidden; }
  #chapter-screen .chapter-layout { height: 100%; min-height: 0; justify-content: space-between; gap: 8px; }
  #chapter-screen .chapter-card-section,
  #chapter-screen .chapter-more-section { gap: 6px; }
  #chapter-screen .chapter-more-list {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: repeat(3, minmax(40px, auto));
    grid-auto-rows: minmax(40px, auto);
    width: 100%;
    gap: 5px;
  }
  #chapter-screen .chapter-more-list > :last-child { grid-column: auto; }
  #chapter-screen .chapter-more-btn { min-height: 40px; padding: 5px 8px; font-size: 14px; white-space: normal; line-height: 1.05; }
  #chapter-screen .chapter-more-list .route-back { min-height: 40px; }
  #chapter-screen .chapter-more-list .route-back-main { min-width: 0; padding: 4px 8px; font-size: 14px; white-space: normal; }
  #chapter-screen .chapter-list {
    grid-template-columns: repeat(3, var(--menu-square-size));
    grid-template-rows: repeat(4, var(--menu-square-size));
    grid-auto-rows: var(--menu-square-size);
  }

  #level-select.grid-level-select { --cell-size: min(72px, calc((100vw - 90px) / 5), calc((90dvh - 200px) / 5)); }
  #level-select.grid-level-select.grid-size-4x3 { --cell-size: min(85px, calc((100vw - 94px) / 4), calc((90dvh - 190px) / 3)); }
  #level-select.grid-level-select.grid-size-3x3 { --cell-size: min(100px, calc((100vw - 90px) / 3), calc((90dvh - 190px) / 3)); }
  #level-select .panel { max-height: 100%; margin-top: auto; }
  #level-select.grid-level-select .panel { justify-content: flex-end; }
  #level-select.grid-level-select .panel-header { margin-bottom: 4px; }
  #level-select.grid-level-select .panel-header h2 { font-size: clamp(26px, 7vw, 42px); letter-spacing: 0; }

  #endless-screen .endless-panel {
    width: 100%;
    height: 100%;
    min-height: 0;
    max-width: none;
    padding: 12px;
    gap: 6px;
    justify-content: space-between;
    overflow: hidden;
  }
  #endless-screen .panel-header { justify-content: center; }
  #endless-screen .panel-header h2 { margin: 0; font-size: clamp(28px, 7vw, 38px); }
  #endless-screen .endless-setup-layout { grid-template-columns: minmax(0, 1fr); width: 100%; min-height: 0; gap: 10px; }
  #endless-screen .endless-setup-section { gap: 5px; }
  #endless-screen .endless-setup-section h3 { font-size: .9rem; }
  #endless-screen { --endless-choice-size: min(140px, calc((100vw - 48px) / 2), calc((90dvh - 370px) / 2)); }
  #endless-screen .endless-tile-list { width: max-content; max-width: 100%; margin-inline: auto; grid-template-columns: repeat(2, var(--endless-choice-size)); grid-template-rows: repeat(2, var(--endless-choice-size)); gap: 6px; }
  #endless-screen .endless-tile-choice { width: var(--endless-choice-size); height: var(--endless-choice-size); gap: 3px; padding: 3px; font-size: .76rem; }
  #endless-screen .endless-tile-choice .cell { --cell-size: clamp(26px, 5dvh, 44px); --tile-size: var(--cell-size); width: var(--cell-size); height: var(--cell-size); flex-basis: var(--cell-size); }
  #endless-screen .endless-difficulty-list { width: min(100%, calc(var(--endless-choice-size) * 2 + 6px)); height: auto; margin-inline: auto; grid-template-columns: minmax(0, 1fr); grid-template-rows: repeat(4, minmax(40px, auto)); gap: 5px; }
  #endless-screen .endless-difficulty-list button { min-height: 40px; padding: 2px; font-size: .8rem; }
  #endless-screen .mode-record { font-size: .64rem; }
  #endless-screen .mode-status { min-height: 1em; font-size: .72rem; text-align: center; }
  #endless-screen .endless-actions { width: 100%; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  #endless-screen .endless-actions > button,
  #endless-screen .endless-actions > .route-back { min-width: 0; min-height: 44px; }
  #endless-screen .endless-actions .route-back-main { min-width: 0; padding-inline: 4px; }

  #colors-screen { --color-choice-size: min(72px, calc((100vw - 64px) / 3), calc((90dvh - 210px) / 6)); }
  #colors-screen .panel { width: 100%; height: 100%; min-height: 0; max-width: none; padding: 12px; gap: 6px; overflow: hidden; }
  #colors-screen .panel-header { display: block; text-align: center; }
  #colors-screen .panel-header h2 { font-size: clamp(24px, 6vw, 34px); line-height: 1.05; }
  #colors-screen .panel-header .subtitle { font-size: .8rem; }
  #colors-screen .settings-grid,
  #colors-screen .settings-group { flex: 1 1 auto; min-height: 0; gap: 0; }
  #colors-screen .character-color-pickers { flex: 1 1 auto; min-height: 0; grid-template-columns: 1fr; grid-template-rows: repeat(2, minmax(0, 1fr)); gap: 4px; }
  #colors-screen .character-color-group { align-content: center; justify-items: center; gap: 4px; min-height: 0; }
  #colors-screen .character-color-group + .character-color-group { padding: 3px 0 0; }
  #colors-screen .character-color-group h3 { margin: 0; font-size: .87rem; }
  #colors-screen .character-color-options { grid-template-columns: repeat(3, var(--color-choice-size)); gap: 4px; justify-content: center; }
  #colors-screen .character-color-btn { width: var(--color-choice-size); height: var(--color-choice-size); min-width: 0; padding: 2px; gap: 2px; font-size: clamp(10px, 2.8vw, 12px); line-height: 1; }
  #colors-screen .character-color-swatch { width: clamp(22px, 4dvh, 30px); height: clamp(22px, 4dvh, 30px); }
  #colors-screen .editor-footer { width: 100%; margin-top: auto; }
  #colors-screen .editor-footer .route-back { width: 100%; }
}

@media (max-width: 360px) and (max-height: 640px) {
  #chapter-screen .chapter-layout h2 { font-size: 27px; }
  #chapter-screen .chapter-number { font-size: 20px; line-height: 1; }
  #chapter-screen .chapter-name { font-size: 9px; line-height: 1; }
  #chapter-screen .chapter-sub { font-size: 8px; line-height: 1; }
  #colors-screen .panel-header .subtitle { display: none; }
}
