/* Matatalab Simulator — kid-friendly tangible-block look */
:root {
  --bg: #f4f8ff;
  --surface: #ffffff;
  --surface-2: #eef3fb;
  --ink: #1f2a44;
  --ink-soft: #4b5876;
  --accent: #ff9c2a;
  --accent-2: #ffd34a;
  --green: #5fc36a;
  --red: #ef5b5b;
  --blue: #5aa9ff;
  --purple: #b388ff;
  --teal: #2cc6c6;
  --shadow: 0 6px 18px rgba(28, 42, 80, 0.12);
  --radius: 14px;
  --grid-line: #c4d2ea;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  /* Disable browser-native touch behaviors that interfere with custom drag. */
  touch-action: manipulation;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
/* Stop iOS from zooming or scrolling while a drag is in progress. */
body.touch-dragging { touch-action: none; overflow: hidden; }
.block, .placed, .entity, .wall-slot, .slot, .cell {
  /* Don't fire scrolling on these; pointermove must stay with us. */
  touch-action: none;
}
/* Floating ghost element for touch drag. */
.touch-ghost { pointer-events: none; will-change: left, top, transform; }

/* When in fullscreen, drop the rounded body shadow and edge padding so the app
   uses the full screen real estate. */
body.is-fullscreen { background: var(--bg); }
body.is-fullscreen .topbar { padding-top: max(10px, env(safe-area-inset-top)); }
body.is-fullscreen .layout { padding-bottom: max(12px, env(safe-area-inset-bottom)); }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 22px;
  background: linear-gradient(90deg, #fff7e6, #fff);
  border-bottom: 1px solid #ecdcb8;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04);
}
.logo {
  display: flex; align-items: center; gap: 10px;
  font-weight: 800; font-size: 22px; letter-spacing: .3px;
}
.logo em { font-style: normal; color: var(--accent); margin-left: 6px; }
.logo-dot {
  width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #ffe69b, var(--accent));
  box-shadow: 0 2px 6px rgba(255, 156, 42, .45);
}
.hint-top { color: var(--ink-soft); font-size: 13px; }
.toolbar { display: flex; gap: 10px; align-items: center; }
.btn-group { display: flex; gap: 6px; }
.toolbar-divider {
  width: 1px; height: 28px;
  background: linear-gradient(180deg, transparent, #d6cda9, transparent);
  margin: 0 4px;
}

.btn {
  border: none; padding: 9px 14px; font-weight: 800; font-size: 14px;
  border-radius: 12px; cursor: pointer; box-shadow: var(--shadow);
  transition: transform .08s ease, filter .15s ease;
}
.btn:hover:not(:disabled) { filter: brightness(1.05); transform: translateY(-1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-play     { background: var(--green); color: #fff; }
.btn-continue { background: #34a853;     color: #fff; }
.btn-stop     { background: var(--red);   color: #fff; }
.btn-step     { background: #6c8cff;      color: #fff; }
.btn-stepback { background: #8aa0e8;      color: #fff; }
.btn-reset    { background: var(--blue);  color: #fff; }
.btn-ghost    { background: var(--surface-2); color: var(--ink-soft); box-shadow: none; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 290px 1fr auto;
  gap: 16px;
  padding: 16px;
  height: calc(100vh - 60px);
  height: calc(100dvh - 60px);
}

/* iPad Pro 12.9" landscape: 1366x1024 css px. Keep 3 columns; the map column
   is sized by the map itself (square aspect), not by a fraction. */
@media (max-width: 1400px) and (min-width: 1100px) {
  .layout {
    grid-template-columns: 240px 1fr auto;
    gap: 12px;
    padding: 12px;
  }
}

/* Smaller landscapes / portrait iPads (<1100): stack so the map gets full width.
   Two rows: row 1 = palette + program, row 2 = map. The map row has a fixed
   minimum height so the 640px square always fits without being clipped. */
@media (max-width: 1099px) {
  .layout {
    grid-template-columns: 240px 1fr;
    /* Row 2 size is driven by the map (auto-sizes), row 1 takes the rest. */
    grid-template-rows: minmax(220px, 1fr) auto;
    gap: 12px;
    padding: 12px;
  }
  .world { grid-column: 1 / -1; }
  /* On portrait iPads, give the map size a vertical cap so it always fits in
     the available row 2 space. */
  .map {
    --size: min(56vh, 60vw, 600px);
  }
}

/* ===========================================================================
 * iPhone breakpoints are defined at the END of this file so they reliably
 * override earlier media queries (e.g. the iPad < 1099px rules) for any
 * cascading rules with the same specificity. See bottom of file.
 * =========================================================================== */

/* Small fallback for narrow screens that aren't phones. */
@media (max-width: 720px) and (min-width: 501px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    height: auto;
    overflow-y: auto;
  }
  body { overflow: auto; }
}

/* Compact top bar on iPads. */
@media (max-width: 1400px) {
  .topbar { padding: 10px 16px; gap: 8px; flex-wrap: wrap; }
  .logo { font-size: 18px; }
  .logo-dot { width: 18px; height: 18px; }
  .hint-top { display: none; }
  .toolbar { gap: 6px; }
  .btn { padding: 8px 11px; font-size: 13px; }
}
@media (max-width: 1099px) {
  .hint-top { display: block; flex-basis: 100%; order: 3; text-align: center; font-size: 12px; }
}

/* Inventory */
.palette {
  background: #d5eef1; border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); overflow-y: auto;
}
.palette h2 { margin: 0 0 6px; font-size: 18px; }
.palette-section { margin-top: 12px; }
.palette-section h3 {
  margin: 0 0 8px; font-size: 13px; text-transform: uppercase;
  color: var(--ink-soft); letter-spacing: .8px;
}
.palette-section h3 small { font-size: 11px; text-transform: none; letter-spacing: 0; color: var(--ink-soft); opacity: .8; }
.palette-flow {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  align-items: start;
}
.palette-flow .block { width: 100%; min-height: 56px; padding: 4px; }
.palette-flow .block svg { width: 100%; height: auto; max-width: 56px; max-height: 56px; }
.palette-flow .block span:not(.num):not(.avail) { font-size: 10px; line-height: 1.1; }
.palette-flow .block .num { font-size: 22px; padding: 2px 0 0; }
.tip { color: var(--ink-soft); font-size: 12px; line-height: 1.5; margin: 4px 0 0; }

/* Block tiles in palette */
.block {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; padding: 10px 6px;
  border-radius: 12px;
  border: 2px solid transparent;
  background: var(--surface-2);
  color: var(--ink);
  font-weight: 700; font-size: 12px;
  cursor: grab; user-select: none;
  transition: transform .08s ease, box-shadow .15s ease, opacity .15s;
}
.block:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.block:active { cursor: grabbing; }
.block svg { width: 34px; height: 34px; fill: currentColor; }
.block.depleted { display: none !important; }
.avail { display: none; }
/* Inventory tiles whose count has been consumed - we keep the DOM slot in place
   (visibility:hidden) so the remaining tiles don't shuffle positions. */
.block.inv-depleted { visibility: hidden; pointer-events: none; }
.block .num { font-size: 26px; font-weight: 900; line-height: 1; padding: 6px 0 2px; }

.block-move { background: #fff3e0; color: #b85c00; border-color: #ffd9a8; }
.block-loop { background: #e8f6e9; color: #2f7a39; border-color: #b9e3bd; }
.block-num  { background: #ecdfff; color: #5b2bbf; border-color: #d4bdff; }
.block-wall { background: #fde2e2; color: #b8302f; border-color: #f5b9b9; }

/* "full-tile" blocks render their entire shape (with bottom dent) as one SVG.
   Strip the default panel chrome so the SVG carries the look. */
.block.full-tile,
.placed.full-tile {
  background: transparent;
  border: none;
  padding: 0;
  box-shadow: none;
}
.block.full-tile svg,
.placed.full-tile svg {
  width: 100%; height: 100%;
  display: block;
}
.palette-flow .block.full-tile { padding: 0; min-height: 0; display: flex; align-items: center; justify-content: center; }
.palette-flow .block.full-tile svg { width: min(100%, 64px); height: auto; aspect-ratio: 1 / 1; }
.placed.full-tile { padding: 4px; }
.placed.full-tile.executing { outline-offset: 0; border-radius: 14px; }
.placed.full-tile:hover { transform: translateY(-1px); filter: brightness(1.05); }

/* Wall stick tiles in palette: render thin red sticks. */
.palette-flow .block.full-tile.wall-tile { min-height: 48px; padding: 6px 4px; }
.palette-flow .block.full-tile.wall-tile svg { max-width: 100%; max-height: 56px; height: auto; }
/* Number block tiles in palette: render at the same width as the movement-block icon
   (56px) so they line up visually within the 4-column grid. */
.palette-flow .block.full-tile.num-tile {
  min-height: 0;
  padding: 0;
  display: flex; align-items: center; justify-content: center;
  aspect-ratio: auto;
}
.palette-flow .block.full-tile.num-tile svg {
  width: min(100%, 64px); height: auto;
  aspect-ratio: 64 / 24;
  max-width: none; max-height: none;
}

/* When a placed movement tile carries a number, the SVG is taller (combined). */
.placed.full-tile.with-num { padding: 2px; }
.placed.full-tile.with-num svg { height: 100%; width: 100%; }

/* ===== Program (6 cols × 3 rows) ===== */
/* Board / world panels keep their internal scrollers in check. */
.board, .world {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  min-height: 0; min-width: 0;
  overflow: hidden;
}
@media (max-width: 1400px) {
  .board, .world { padding: 10px; }
}
.board-header, .world-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.board-header h2, .world-header h2 { margin: 0; font-size: 18px; }
.board-info, .world-info { color: var(--ink-soft); font-size: 13px; }
.program-foot { margin: 8px 2px 0; color: var(--ink-soft); font-size: 12px; }

/* ===== Countdown timer ===== */
.timer-panel {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid #e3eaf6;
  border-radius: 12px;
  background: #fafcff;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px 14px;
  align-items: center;
}
.timer-panel.disabled { opacity: .55; }
.timer-toggle {
  display: flex; align-items: center; gap: 8px;
  font-weight: 700; color: var(--ink-soft); font-size: 13px;
  cursor: pointer; user-select: none;
}
.timer-toggle input { width: 16px; height: 16px; cursor: pointer; }
.timer-mode-switch {
  display: inline-flex;
  border: 1px solid #d8e1f3;
  border-radius: 999px;
  padding: 2px;
  background: #fff;
}
.timer-mode-btn {
  border: none; background: transparent;
  padding: 4px 10px; font-size: 12px; font-weight: 700;
  color: var(--ink-soft); cursor: pointer;
  border-radius: 999px;
  transition: background-color .12s, color .12s;
}
.timer-mode-btn.active { background: var(--accent); color: #fff; }
.timer-mode-btn:disabled { opacity: .5; cursor: not-allowed; }
.timer-display {
  font-family: "SFMono-Regular", "Consolas", "Menlo", monospace;
  font-size: 28px; font-weight: 800;
  color: var(--ink);
  letter-spacing: 1px;
  text-align: center;
  padding: 4px 14px;
  background: #fff;
  border: 1px solid #e3eaf6;
  border-radius: 10px;
  min-width: 110px;
}
.timer-display.warn   { color: #cf6a00; background: #fff5e1; border-color: #ffd6a8; }
.timer-display.danger { color: #b8302f; background: #ffe5e5; border-color: #f5b9b9; animation: timer-flash 1s ease-in-out infinite; }
.timer-display.done   { color: #b8302f; background: #ffe5e5; border-color: #f5b9b9; }
@keyframes timer-flash {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.06); }
}
.timer-controls {
  display: flex; align-items: center; gap: 6px;
  flex-wrap: wrap;
}
.timer-controls .btn { padding: 6px 10px; font-size: 12px; }
.timer-set {
  display: flex; align-items: center; gap: 4px;
  font-size: 12px; color: var(--ink-soft); font-weight: 700;
}
.timer-set input[type=number] {
  width: 56px; padding: 4px 6px; font-size: 13px;
  border: 1px solid #d8e1f3; border-radius: 6px;
}
.timer-set input[type=number]:disabled { background: #f3f6fc; color: #aab4c8; }
@media (max-width: 1099px) {
  .timer-panel {
    grid-template-columns: 1fr;
    gap: 6px;
    padding: 8px 10px;
  }
  .timer-display { font-size: 24px; padding: 2px 10px; min-width: 0; }
  .timer-controls { flex-wrap: wrap; }
  .timer-controls .btn { padding: 5px 10px; }
}

.program-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 8px;
  flex: 1 1 auto;
  background: repeating-linear-gradient(135deg, #fafcff 0 12px, #f3f6fc 12px 24px);
  border: 2px dashed #cdd7eb;
  border-radius: 12px;
  padding: 10px;
}
.slot {
  position: relative;
  background: rgba(255,255,255,.6);
  border: 2px dashed #d0dbef;
  border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  min-height: 56px;
  min-width: 0;
  transition: background-color .12s, border-color .12s;
}
/* When a block has been placed in the slot, tint the slot background to match
   the inventory color so it's visually clear the slot is occupied. */
.slot:has(.placed),
.slot.filled {
  background: #d5eef1;
  border-style: solid;
  border-color: #b6dde2;
}
.slot.drag-over { background: #fff3d8; border-color: var(--accent); }
.slot-index {
  position: absolute; top: 4px; left: 6px;
  font-size: 10px; color: var(--ink-soft); font-weight: 700;
  background: rgba(255,255,255,.7); border-radius: 4px; padding: 0 4px;
}
.placed {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  width: 100%;
  flex: 1 1 auto;
  padding: 6px 4px 4px;
  border-radius: 10px;
  border: 2px solid transparent;
  font-weight: 800; font-size: 11px;
  cursor: pointer; user-select: none;
  background: transparent;
}
.placed svg { width: 30px; height: 30px; fill: currentColor; }
.placed.placed-move { background: #fff3e0; color: #b85c00; border-color: #ffd9a8; }
.placed.placed-loop { background: #e8f6e9; color: #2f7a39; border-color: #b9e3bd; }
.placed.executing {
  outline: 4px solid var(--accent);
  outline-offset: 3px;
  border-radius: 12px;
  box-shadow: 0 0 0 8px rgba(255, 156, 42, .25), 0 6px 16px rgba(255, 156, 42, .35);
  animation: nudge .9s ease-in-out infinite;
  z-index: 2;
}
@keyframes nudge {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.08); }
}
/* Also light up the slot itself when its block is the current step. */
.slot.executing {
  background: rgba(255, 211, 74, .35);
  border-color: var(--accent);
}
.slot.executing .slot-index {
  background: var(--accent); color: #fff;
}
.num-mod {
  margin-top: 4px;
  background: #ecdfff; color: #5b2bbf;
  border: 2px solid #d4bdff; border-radius: 8px;
  padding: 1px 6px; font-size: 14px; font-weight: 900;
  display: flex; align-items: center; justify-content: center;
  min-width: 24px;
}
.slot.drop-num-ok { box-shadow: inset 0 0 0 3px var(--purple); }

/* ===== Map ===== */
.world { min-height: 0; }
.map-wrap {
  flex: 1 1 auto;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, #eaf6e7, #d6ecd0);
  border-radius: 12px;
  padding: 16px;
  position: relative;
  overflow: hidden;
}
/* Layer for free-floating movement blocks placed onto the map. Sits on top
   of the grid but doesn't cover it - children are individually positioned. */
.map-blocks {
  position: absolute; inset: 0;
  pointer-events: none;
  z-index: 4;
}
.floating-block {
  position: absolute;
  width: 64px; height: 64px;
  pointer-events: auto;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  touch-action: none;
  border-radius: 12px;
  box-shadow: 0 6px 14px rgba(0,0,0,.18);
  transform-origin: 50% 50%;
  transform: translate(-50%, -50%) rotate(0deg);
  transition: box-shadow .12s;
}
.floating-block svg { width: 100%; height: 100%; display: block; pointer-events: none; }
.floating-block:active { cursor: grabbing; }
.floating-block.dragging { z-index: 9999; box-shadow: 0 12px 28px rgba(0,0,0,.28); }
.floating-block .floating-rot {
  position: absolute; bottom: -8px; right: -8px;
  width: 22px; height: 22px;
  background: #fff;
  border: 1px solid #d8e1f3;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; line-height: 1;
  box-shadow: 0 2px 4px rgba(0,0,0,.15);
  cursor: grab;
}
.floating-block .floating-rot::before { content: "⟳"; color: var(--ink-soft); font-weight: 800; }
.map-wrap.drop-floating { background: linear-gradient(180deg, #d8eece, #c0debb); }
.map {
  --cells: 4;
  /* Keep the grid perfectly square. Sizing prefers vertical space (the column is
     auto-sized off this), capped at 640px so the program column has enough room
     on iPad Pro landscape (1366x1024). */
  --size: min(70vh, 640px);
  width: var(--size); height: var(--size);
  position: relative;
  background: #ffffffaa;
  border: 3px solid #c1d8b1;
  border-radius: 8px;
  /* light grid lines */
  background-image:
    linear-gradient(0deg,  var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: calc(100% / var(--cells)) calc(100% / var(--cells));
}
.cell {
  position: absolute;
  width: calc(100% / var(--cells));
  height: calc(100% / var(--cells));
}
.cell.drop-target { background: rgba(255, 156, 42, .22); }

/* Wall slots between cells. We render an invisible wide hit area so a finger /
   mouse can drop a wall onto a gridline easily; the actual visible wall is a
   thin stick child positioned in the center of the slot. */
.wall-slot {
  position: absolute;
  border-radius: 4px;
  pointer-events: auto;
  background: transparent;
  transition: background .12s;
}
.wall-slot.v {
  /* widened from 12px to ~30% of a cell so dropping is forgiving */
  width: calc(100% / var(--cells) * 0.30);
  height: calc(100% / var(--cells) * 0.86);
  transform: translate(-50%, -50%);
  /* Visualize the hit area subtly while the user drags. */
}
.wall-slot.h {
  width: calc(100% / var(--cells) * 0.86);
  height: calc(100% / var(--cells) * 0.30);
  transform: translate(-50%, -50%);
}
.wall-slot.drop-ok      { background: rgba(184, 48, 47, .18); outline: 2px dashed rgba(184, 48, 47, .5); }
/* Placed walls on the map: red sticks (rendered via a child .wall-stick element). */
.wall-slot.has-wall { background: transparent !important; }
.wall-slot.has-wall { cursor: pointer; }
.wall-stick {
  position: absolute;
  background: linear-gradient(180deg, #ee5454, #c92424);
  border-radius: 3px;
  box-shadow: 0 0 0 1px #b32525, 0 2px 4px rgba(0,0,0,.18);
  cursor: grab;
  touch-action: none;
}
.wall-stick:active { cursor: grabbing; }
.wall-slot.v .wall-stick {
  top: 7%; bottom: 7%;
  left: 50%; width: 8px;
  transform: translateX(-50%);
}
.wall-slot.h .wall-stick {
  left: 7%; right: 7%;
  top: 50%; height: 8px;
  transform: translateY(-50%);
}

/* Robot & goal on map */
.entity {
  position: absolute;
  width: calc(100% / var(--cells));
  height: calc(100% / var(--cells));
  display: flex; align-items: center; justify-content: center;
  cursor: grab; user-select: none;
  /* Smooth, slightly easeful motion - duration is set dynamically from JS. */
  transition: left 0.35s cubic-bezier(.4, 0, .2, 1),
              top  0.35s cubic-bezier(.4, 0, .2, 1);
}
.entity:active { cursor: grabbing; }
.entity.no-anim, .entity.no-anim .robot { transition: none !important; }
.flag {
  width: 70%; height: 78%;
  position: relative;
}
/* Red flag on a brown pole - replaces the old gold star. */
.flag::before {
  /* Pole */
  content: ""; position: absolute;
  left: 22%; top: 4%; bottom: 4%;
  width: 8%;
  background: linear-gradient(180deg, #6b3e1d, #3d2310);
  border-radius: 2px;
}
.flag::after {
  /* Triangular pennant */
  content: ""; position: absolute;
  left: 30%; top: 8%;
  width: 60%; height: 44%;
  background: linear-gradient(180deg, #ff5050, #cf2222);
  clip-path: polygon(0 0, 100% 25%, 0 50%);
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.18));
}
.robot {
  width: 70%; height: 70%; position: relative;
  border-radius: 30% 30% 26% 26%;
  background: linear-gradient(180deg, #ffd34a 0%, #ff9c2a 100%);
  box-shadow: 0 4px 10px rgba(0,0,0,.2), inset 0 -4px 0 rgba(0,0,0,.08);
  transform-origin: 50% 50%;
  transform: rotate(0deg);
  transition: transform 0.35s cubic-bezier(.4, 0, .2, 1);
}
.robot-arrow {
  position: absolute; top: -8%; left: 50%; transform: translateX(-50%);
  width: 0; height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 14px solid #d05b00;
}
.robot-eye {
  position: absolute; top: 28%; width: 16%; height: 16%;
  background: #1f2a44; border-radius: 50%;
}
.robot-eye.left  { left: 22%; }
.robot-eye.right { right: 22%; }
.robot-eye::after {
  content: ""; position: absolute; top: 15%; left: 15%;
  width: 35%; height: 35%; background: #fff; border-radius: 50%;
}
.entity.bumped > .robot { animation: shake .3s ease; }
@keyframes shake {
  0%, 100% { transform: rotate(var(--rot, 0deg)) translate(0,0); }
  25%      { transform: rotate(var(--rot, 0deg)) translate(-4px,0); }
  50%      { transform: rotate(var(--rot, 0deg)) translate(4px, 0); }
  75%      { transform: rotate(var(--rot, 0deg)) translate(-3px,0); }
}

/* Legend */
.legend {
  display: flex; gap: 14px; align-items: center; margin-top: 10px;
  color: var(--ink-soft); font-size: 13px; flex-wrap: wrap;
}
.legend-dot { display: inline-block; width: 14px; height: 14px; border-radius: 4px; margin-right: 6px; vertical-align: middle; }
.legend-dot.bot  { background: linear-gradient(180deg, #ffd34a, #ff9c2a); }
.legend-dot.flag { background: linear-gradient(180deg, #ff5050, #cf2222); }
.legend .hint { margin-left: auto; }

/* ===== Settings dialog ===== */
.settings-dialog {
  border: none; border-radius: 14px; padding: 0;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
  width: min(560px, 92vw);
}
.settings-dialog::backdrop { background: rgba(0,0,0,.35); }
.settings-dialog form {
  padding: 18px 20px 14px;
  font-family: inherit;
  color: var(--ink);
}
.settings-dialog h3 { margin: 0 0 6px; }
.settings-note { margin: 0 0 12px; color: var(--ink-soft); font-size: 13px; }
.settings-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 14px 18px;
}
.settings-group h4 {
  margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--ink-soft);
}
.settings-list { display: flex; flex-direction: column; gap: 6px; }
.settings-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  font-size: 13px;
}
.settings-extras {
  margin-top: 14px; padding-top: 12px; border-top: 1px solid #eef1f7;
}
.settings-row.settings-row-wide {
  font-size: 14px; font-weight: 700;
}
.settings-grid-display {
  font-family: "SFMono-Regular", "Consolas", "Menlo", monospace;
  color: var(--ink-soft); font-size: 12px;
  margin-left: 6px;
}
.settings-row .stepper {
  display: inline-flex; align-items: center; gap: 0;
  border: 1px solid #d8e1f3; border-radius: 8px; overflow: hidden;
  background: #fff;
}
.settings-row .stepper button {
  width: 26px; height: 28px;
  border: none;
  background: var(--surface-2);
  color: var(--ink-soft);
  font-weight: 800; font-size: 16px; line-height: 1;
  cursor: pointer;
  transition: background-color .12s;
}
.settings-row .stepper button:hover:not(:disabled) { background: #dde6f5; color: var(--ink); }
.settings-row .stepper button:disabled { opacity: .4; cursor: not-allowed; }
.settings-row .stepper input[type=number] {
  width: 40px;
  padding: 4px 4px;
  font-size: 14px;
  border: none;
  text-align: center;
  background: transparent;
  /* hide the browser's native spinner since we provide our own */
  -moz-appearance: textfield;
  appearance: textfield;
}
.settings-row .stepper input::-webkit-outer-spin-button,
.settings-row .stepper input::-webkit-inner-spin-button {
  -webkit-appearance: none; margin: 0;
}
.settings-speed {
  display: flex; align-items: center; gap: 10px;
  margin-top: 16px; padding-top: 12px; border-top: 1px solid #eef1f7;
  font-size: 13px;
  flex-wrap: wrap;
}
.settings-speed input[type=text] {
  width: 90px; padding: 6px 10px; font-size: 14px;
  border: 1px solid #d8e1f3; border-radius: 8px;
  font-family: "SFMono-Regular", "Consolas", "Menlo", monospace;
}
.settings-speed-hint { color: var(--ink-soft); font-size: 11px; }
.settings-speed-hint code {
  font-family: "SFMono-Regular", "Consolas", "Menlo", monospace;
  background: var(--surface-2); padding: 1px 4px; border-radius: 3px; font-size: 11px;
}
#settings-speed-val { min-width: 60px; text-align: right; color: var(--ink-soft); }
.settings-dialog menu {
  display: flex; gap: 8px; justify-content: flex-end;
  margin: 16px 0 0; padding: 0;
}
.settings-dialog menu button {
  border: none; padding: 8px 14px; border-radius: 8px; font-weight: 700; cursor: pointer;
  background: var(--surface-2); color: var(--ink-soft);
}
.settings-dialog menu .btn-primary { background: var(--accent); color: #fff; }


/* ===========================================================================
 * PHONE LAYOUTS — defined at the end so they reliably override earlier rules.
 * Only iPhones (and very small windows) hit these breakpoints. iPad and
 * desktop layouts above are unaffected.
 * =========================================================================== */

/* iPhone landscape (≤ 950 × ≤ 500 px, e.g. 932 × 430).
   Layout:
   - The MAP is anchored to the right half of the screen (full height) via
     position: fixed.
   - The remaining left half holds: toolbar buttons (top), program board
     (middle), inventory strip (bottom).
   All section titles hidden so the small viewport gets every available pixel. */
@media (max-width: 950px) and (max-height: 500px) and (orientation: landscape) {
  body { overflow: hidden; }

  /* Pin the map to the right half of the screen. */
  .world {
    position: fixed;
    top: 0; right: 0;
    width: 50vw; height: 100dvh;
    margin: 0;
    padding: 6px;
    border-radius: 0;
    box-shadow: none;
    background: var(--surface);
    display: flex; align-items: center; justify-content: center;
    overflow: hidden;
    z-index: 10;
  }

  /* Map: square, sized to fit the half-screen tile. */
  .map-wrap {
    flex: 0 0 auto;
    width: var(--size); height: var(--size);
    padding: 0;
    background: transparent;
    border-radius: 8px;
    overflow: hidden;
  }
  .map {
    --size: min(calc(100dvh - 12px), calc(50vw - 12px));
  }

  /* Top bar lives across the LEFT half (above the program). It's at body level
     in the DOM, so we just shrink it to the left half visually. */
  .topbar {
    width: 50vw;
    padding: 4px 6px;
    gap: 4px;
    flex-wrap: nowrap;
    overflow-x: auto;
    background: linear-gradient(90deg, #fff7e6, #fff);
  }
  .topbar .logo { display: none; }
  .topbar .hint-top { display: none; }
  .topbar .btn { padding: 4px 7px; font-size: 10px; }
  .topbar .btn-group { gap: 2px; }
  .topbar .toolbar-divider { display: none; }
  /* Shorten less-essential labels to icons-only on phones. */
  .topbar #btn-fullscreen { font-size: 0; padding: 4px 7px; }
  .topbar #btn-fullscreen::before { content: "⛶"; font-size: 12px; }
  .topbar #btn-clear { font-size: 0; padding: 4px 7px; }
  .topbar #btn-clear::before { content: "⌫"; font-size: 12px; }
  .topbar #btn-settings { font-size: 0; padding: 4px 7px; }
  .topbar #btn-settings::before { content: "⚙"; font-size: 12px; }

  /* Layout fills the LEFT half below the topbar. The .world inside .layout is
     pulled out of normal flow via position: fixed (above), so it doesn't take
     space in the left-half grid. */
  .layout {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 6px;
    padding: 6px;
    width: 50vw;
    height: calc(100dvh - 38px); /* topbar takes ~38px */
    overflow: hidden;
  }
  .layout > .board   { grid-column: 1; grid-row: 1; min-width: 0; padding: 6px; }
  /* Palette grows to ~110px to fit 2 rows of 32px tiles + padding/gaps.
     Allow vertical scrolling so the user can reach all blocks if there are
     more sections than fit in the visible area. */
  .layout > .palette { grid-column: 1; grid-row: 2;
                       padding: 6px;
                       overflow-x: hidden; overflow-y: auto;
                       height: 112px; max-height: 112px;
                       white-space: normal;
                       /* Sections sit side-by-side and wrap to a new line when
                          they don't fit; user can then scroll vertically. */
                       display: flex; flex-wrap: wrap; align-content: flex-start;
                       gap: 6px; }
  .layout > .palette > .palette-section { margin: 0; }

  /* Hide all chrome on phones - titles, status, hint, legend, footer, timer. */
  .world-header,
  .board-header,
  .program-foot,
  .timer-panel,
  .legend           { display: none !important; }

  /* Compact program board. */
  .program-grid { gap: 3px; padding: 4px; }
  .slot { min-height: 0; border-width: 1px; }
  .slot-index { font-size: 8px; padding: 0 2px; }

  /* Inventory: two-row strip with small icon-only tiles. Each section becomes
     its own 2-row column-flowing mini-grid; sections sit side-by-side and the
     whole strip scrolls horizontally if it overflows. */
  .palette h2 { display: none; }
  .palette-section h3 { display: none; }
  .palette-section {
    display: inline-grid;
    grid-auto-flow: column;
    grid-template-rows: repeat(2, 32px);
    gap: 3px;
    vertical-align: top;
    margin: 0 8px 0 0;
  }
  .palette-flow { display: contents; }
  .palette-flow .block { width: 32px; min-height: 32px; padding: 0; }
  .palette-flow .block.full-tile svg { width: 100%; height: auto; }
  .palette-flow .block.full-tile.num-tile { width: 32px; }
  .palette-flow .block.full-tile.num-tile svg { width: 100%; height: auto; }
  .palette-flow .block.inv-depleted { display: none !important; }
  .palette .palette-section:last-of-type { display: none; } /* hide "Tips" */
}

/* iPhone portrait (≤ 500 px wide).
   Layout: map on top, program in middle, inventory strip at bottom. */
@media (max-width: 500px) {
  .layout {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    gap: 8px;
    padding: 8px;
    height: calc(100dvh - 50px);
    overflow-y: auto;
  }
  .world   { grid-column: 1; grid-row: 1; padding: 6px; min-width: 0;
             display: flex; align-items: center; justify-content: center; }
  .board   { grid-column: 1; grid-row: 2; padding: 6px; }
  .palette { grid-column: 1; grid-row: 3;
             padding: 4px 6px;
             overflow-x: auto; overflow-y: hidden;
             max-height: 80px;
             white-space: nowrap;
  }

  .map-wrap {
    flex: 0 0 auto;
    width: var(--size); height: var(--size);
    margin: 0 auto;
    padding: 0;
    background: transparent;
    border-radius: 8px;
    overflow: hidden;
  }
  .map {
    --size: min(46vh, 88vw, 380px);
  }

  /* Hide all chrome on phones. */
  .topbar .hint-top  { display: none; }
  .world-header,
  .board-header,
  .program-foot,
  .timer-panel,
  .legend           { display: none !important; }

  /* Inventory: strip with small tiles, no section labels. */
  .palette h2 { display: none; }
  .palette-section h3 { display: none; }
  .palette-section { display: inline-block; vertical-align: middle; margin: 0 6px 0 0; }
  .palette-flow { display: inline-flex; gap: 3px; flex-wrap: nowrap; }
  .palette-flow .block { width: 40px; min-height: 40px; padding: 0; flex: 0 0 40px; }
  .palette-flow .block.full-tile svg { width: 100%; height: auto; }
  .palette-flow .block.full-tile.num-tile { width: 40px; }
  .palette-flow .block.full-tile.num-tile svg { width: 100%; height: auto; }
  .palette-flow .block.inv-depleted { display: none !important; }
  .palette .palette-section:last-of-type { display: none; } /* hide "Tips" */

  /* Compact program board. */
  .program-grid { gap: 4px; padding: 6px; }
  .slot { min-height: 44px; border-width: 1px; }
  .slot-index { font-size: 9px; padding: 0 2px; }

  /* Compact top bar. */
  .topbar { padding: 6px 10px; gap: 4px; flex-wrap: wrap; }
  .logo { font-size: 14px; }
  .logo em { display: none; }
  .logo-dot { width: 14px; height: 14px; }
  .btn { padding: 6px 8px; font-size: 11px; }
  .btn-group { gap: 3px; flex-wrap: wrap; }
  .toolbar-divider { display: none; }
  body { overflow: auto; touch-action: manipulation; }
  body.touch-dragging { overflow: hidden; }
}


/* Rotate-prompt overlay - hidden by default, shown on iPhone-sized portrait viewports. */
.rotate-prompt {
  display: none;
  position: fixed; inset: 0;
  background: linear-gradient(180deg, #1f2a44, #0e1426);
  color: #fff;
  align-items: center; justify-content: center;
  z-index: 100000;
  padding: 24px;
  text-align: center;
}
.rotate-prompt-card {
  max-width: 320px;
}
.rotate-prompt h2 { margin: 0 0 8px; font-size: 22px; font-weight: 800; }
.rotate-prompt p  { margin: 0; opacity: .85; font-size: 15px; line-height: 1.4; }
.rotate-icon {
  width: 96px; height: 96px;
  margin: 0 auto 18px;
  animation: rotateNudge 2.4s ease-in-out infinite;
}
.rotate-icon svg { width: 100%; height: 100%; }
@keyframes rotateNudge {
  0%, 30%       { transform: rotate(0deg); }
  50%, 80%      { transform: rotate(-90deg); }
  100%          { transform: rotate(0deg); }
}

/* Show the overlay on iPhone-sized PORTRAIT viewports (narrow + tall enough
   to be a phone, not a tablet). */
@media (max-width: 500px) and (orientation: portrait) {
  .rotate-prompt { display: flex; }
  /* While the overlay is up, hide the rest of the UI to avoid scroll/touch
     fighting underneath. */
  body > .topbar,
  body > .layout,
  body > dialog { display: none !important; }
  body { overflow: hidden; }
}
