:root {
  --cream: #fbf3e4;
  --paper: #fffaf0;
  --sage: #8fa98a;
  --sage-deep: #5a7a58;
  --coral: #e8735a;
  --coral-deep: #c9553d;
  --ink: #3b3a33;
  --muted: #6d6b5f;
  --line: rgba(90, 70, 40, 0.16);
  --shadow: 0 10px 30px rgba(90, 60, 30, 0.14);
  --radius: 18px;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  color: var(--ink);
  background: radial-gradient(circle at 15% 0%, #fff7e6 0, var(--cream) 45%, #f1e6cf 100%);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

.app { max-width: 1500px; margin: 0 auto; padding: 14px clamp(12px, 2vw, 28px) 28px; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 2px 12px; }
.brand h1 { margin: 0; font: 700 clamp(1.5rem, 3.2vw, 2.2rem)/1.05 var(--serif); letter-spacing: -0.01em; }
.brand h1 em { color: var(--coral); font-style: italic; }
.tagline { margin: 3px 0 0; color: var(--muted); font-size: 0.95rem; }

/* buttons */
.btn {
  font: 600 0.92rem/1 var(--sans);
  color: var(--ink);
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  padding: 11px 16px;
  min-height: 44px;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(90, 60, 30, 0.08);
  transition: transform 0.08s, background 0.15s, box-shadow 0.15s;
}
.btn:hover { background: #fff; }
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--coral); color: #fff; border-color: var(--coral-deep); }
.btn.primary:hover { background: var(--coral-deep); }
.btn.danger { background: #b4432f; color: #fff; border-color: #8f3322; }
.btn.ghost { background: transparent; box-shadow: none; }
.top-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
.btn.tog[aria-pressed="true"] { background: var(--sage-deep, #5b8a5b); color: #fff; }
/* top music bar (not sticky: it never covers the touch pad) */
.topbar { flex-wrap: wrap; }
.now-bar {
  display: flex; align-items: center; gap: 6px; flex: 1 1 340px; max-width: 560px; min-width: 0;
  background: var(--cream); border: 1.5px solid var(--line); border-radius: 999px; padding: 3px 6px;
}
.now-bar .now-skip { padding: 0 12px; min-width: 44px; font-size: 0.8rem; }
.now-bar .now-select { flex: 1 1 120px; min-width: 170px; margin: 0; padding: 8px 10px; border-radius: 999px; text-overflow: ellipsis; }
.now-status { flex: 0 1 190px; min-width: 0; font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.support { font-size: 0.78rem; color: var(--muted); opacity: 0.9; }
.support-link { color: var(--sage-deep); text-decoration: underline; text-underline-offset: 2px; }
.support-link:hover { color: var(--coral-deep); }
.win .support { margin: 14px 0 0; }
.tabpanel > .support { grid-column: 1 / -1; margin: 0; text-align: center; }
.audio-note { flex-basis: 100%; margin: 0; font-size: 0.85rem; opacity: 0.8; }
.btn.wide { grid-column: 1 / -1; }
button:focus-visible, input:focus-visible, textarea:focus-visible, canvas:focus-visible {
  outline: 3px solid #3f7fb0; outline-offset: 2px;
}

/* layout */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 350px; gap: clamp(12px, 2vw, 24px); align-items: start; }

.stage {
  position: relative;
  display: flex; flex-direction: column; gap: 10px;
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: 26px;
  padding: 12px;
  box-shadow: var(--shadow);
  height: calc(100vh - 124px);
  height: calc(100dvh - 124px);
  min-height: 460px;
}
.hud { display: flex; flex-wrap: wrap; gap: 8px; padding-right: 150px; }
.stat {
  display: flex; align-items: baseline; gap: 8px;
  background: var(--cream); border: 1px solid var(--line); border-radius: 12px; padding: 7px 12px;
}
.stat b { font: 700 1.1rem/1 var(--serif); font-variant-numeric: tabular-nums; }
.label { display: block; font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.stat.route[data-ok="1"] { background: #e3efdd; border-color: #a7c79e; }
.stat.route[data-ok="1"] b { color: var(--sage-deep); }
.stat.route[data-ok="0"] b { color: var(--coral-deep); }

.mode-switch {
  position: absolute; top: 12px; right: 12px; z-index: 2;
  display: flex; background: var(--cream); border: 1.5px solid var(--line); border-radius: 999px; padding: 3px;
}
.mode-switch button {
  font: 700 0.9rem/1 var(--sans); border: 0; background: transparent; color: var(--muted);
  padding: 10px 18px; min-height: 40px; border-radius: 999px; cursor: pointer;
}
.mode-switch button[aria-pressed="true"] { background: var(--sage-deep); color: #fff; }
body[data-mode="run"] .mode-switch button[aria-pressed="true"] { background: var(--coral); }

.canvas-wrap { position: relative; flex: 1; min-height: 0; border-radius: 18px; overflow: hidden; }
#field { display: block; width: 100%; height: 100%; touch-action: none; border-radius: 18px; cursor: crosshair; user-select: none; -webkit-user-select: none; }

.toast {
  position: absolute; left: 50%; bottom: 14px; transform: translate(-50%, 10px);
  background: rgba(59, 58, 51, 0.92); color: #fff; padding: 9px 16px; border-radius: 999px;
  font-size: 0.9rem; opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; max-width: 90%; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.win {
  position: absolute; inset: 0; margin: auto; width: min(420px, 90%); height: fit-content;
  background: var(--paper); border: 2px solid var(--coral); border-radius: 24px; padding: 22px;
  text-align: center; box-shadow: 0 20px 50px rgba(60, 40, 20, 0.3);
}
.win h2 { margin: 0 0 6px; font: 700 1.8rem/1.1 var(--serif); color: var(--coral-deep); }
.win p { margin: 0 0 14px; color: var(--muted); }
.win-stats { display: flex; justify-content: center; gap: 22px; margin: 0 0 16px; }
.win-stats dt { font-size: 0.7rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.win-stats dd { margin: 2px 0 0; font: 700 1.3rem/1 var(--serif); }
.win-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

/* touch pad */
.dpad {
  display: none; grid-template-columns: repeat(3, 62px); grid-template-rows: repeat(2, 56px); gap: 6px;
  justify-content: center; align-items: center; padding-top: 2px;
  grid-template-areas: ". up undo" "left down right";
}
.dpad-btn {
  min-width: 48px; min-height: 48px;
  font-size: 1.2rem; color: var(--sage-deep); background: var(--cream); border: 1.5px solid var(--line);
  border-radius: 16px; cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  box-shadow: 0 3px 0 rgba(90, 60, 30, 0.12);
}
.dpad-btn:active { transform: translateY(2px); box-shadow: none; background: #e3efdd; }
.dpad-btn.up { grid-area: up; } .dpad-btn.down { grid-area: down; }
.dpad-btn.left { grid-area: left; } .dpad-btn.right { grid-area: right; }
.dpad-undo { grid-area: undo; padding: 0; }

/* panel */
html { scrollbar-gutter: stable; } /* tab switches never change the stage width */
.panel { display: flex; flex-direction: column; gap: 12px; }
.tabpanel { display: flex; flex-direction: column; gap: 12px; }
.minimap-card h2 { margin-bottom: 8px; }
.minimap-card .note { margin-top: 8px; }
#minimap { display: block; width: 100%; max-width: 340px; height: auto; margin: 0 auto; border-radius: 10px; image-rendering: pixelated; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; background: var(--cream); border: 1.5px solid var(--line); border-radius: 999px; }
.tabs [role="tab"] {
  font: 700 0.92rem/1 var(--sans); color: var(--muted); background: transparent; border: 0; border-radius: 999px;
  padding: 10px 14px; min-height: 44px; cursor: pointer;
}
.tabs [role="tab"][aria-selected="true"] { background: var(--sage-deep); color: #fff; }
.card {
  background: var(--paper); border: 1.5px solid var(--line); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: 0 4px 14px rgba(90, 60, 30, 0.07);
}
.card h2 { margin: 0 0 10px; font: 700 1.05rem/1 var(--serif); }
.text {
  width: 100%; font: 500 1rem var(--sans); color: var(--ink); background: #fff; border: 1.5px solid var(--line);
  border-radius: 12px; padding: 10px 12px; margin-top: 6px; min-height: 44px;
}
select.text { cursor: pointer; }
.check { display: flex; gap: 10px; align-items: center; margin-top: 12px; font-size: 0.92rem; cursor: pointer; min-height: 44px; }
.check input { width: 20px; height: 20px; accent-color: var(--sage-deep); }
.text.mono { font: 0.82rem/1.4 ui-monospace, Menlo, Consolas, monospace; resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.note { margin: 10px 0 0; font-size: 0.82rem; color: var(--muted); }
.error { min-height: 1.3em; margin: 8px 0 0; color: #b4432f; font-weight: 600; font-size: 0.9rem; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button {
  display: flex; flex-direction: column; gap: 3px; align-items: center; padding: 10px 4px; min-height: 52px;
  font: 700 0.85rem/1.1 var(--sans); color: var(--ink); background: var(--cream); border: 1.5px solid var(--line);
  border-radius: 14px; cursor: pointer; text-align: center;
}
.seg small { font-weight: 500; color: var(--muted); font-size: 0.75rem; }
.seg button[aria-pressed="true"], .seasons button[aria-pressed="true"], .animals button[aria-pressed="true"] {
  border-color: var(--sage-deep); background: #e3efdd; box-shadow: inset 0 0 0 1.5px var(--sage-deep);
}

.seasons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seasons button {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; min-height: 44px;
  font: 700 0.9rem/1 var(--sans); color: var(--ink); background: var(--cream); border: 1.5px solid var(--line);
  border-radius: 14px; cursor: pointer;
}
.dot { width: 16px; height: 16px; border-radius: 50%; display: inline-block; border: 1.5px solid rgba(0, 0, 0, 0.12); }
.dot.spring { background: #aed26f; } .dot.summer { background: #ebbc4d; } .dot.autumn { background: #dc8439; } .dot.winter { background: #dbe6f0; }

.animals { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.animals button {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 4px 7px; min-height: 76px;
  font: 700 0.78rem/1 var(--sans); color: var(--ink); background: var(--cream); border: 1.5px solid var(--line);
  border-radius: 14px; cursor: pointer;
}
.animals canvas { width: 44px; height: 44px; }

/* modals */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(50, 40, 25, 0.5); backdrop-filter: blur(3px); }
.modal-card {
  position: relative; width: min(520px, 100%); max-height: calc(100vh - 32px); overflow: auto;
  background: var(--paper); border-radius: 22px; padding: 22px; box-shadow: 0 24px 60px rgba(40, 25, 10, 0.4);
}
.modal-card h2 { margin: 0 0 10px; font: 700 1.5rem/1.1 var(--serif); }
.modal-card p { margin-top: 0; line-height: 1.45; }
.modal-card .label { margin-top: 12px; }
.help { padding-left: 1.2em; line-height: 1.5; margin: 0 0 6px; }
.help li { margin-bottom: 8px; }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 16px; }

/* art is the default; maze-only controls appear when Maze is on */
body[data-purpose="art"] .maze-only { display: none; }
.btn[aria-pressed="true"]:not(.tog) { box-shadow: inset 0 0 0 2px var(--sage-deep); background: #e3efdd; }
.mode-switch button:disabled { opacity: 0.45; cursor: not-allowed; }
.gate-status { margin: 8px 0 0; font-size: 0.85rem; color: var(--muted); min-height: 2.4em; }

/* seasonal tint on page chrome */
body[data-season="spring"] { --sage-deep: #4f8a54; }
body[data-season="autumn"] { --sage-deep: #8a6a3a; }
body[data-season="winter"] { --sage-deep: #4e7a96; }

/* tablets and phones */
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; }
  .stage { height: auto; min-height: 0; }
  .canvas-wrap { flex: none; height: min(66vh, 118vw); height: min(66dvh, 118vw); min-height: 300px; }
  .hud { padding-right: 0; margin-top: 46px; }
  .tabpanel { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .tabpanel .card:first-child { grid-column: 1 / -1; }
}
@media (max-width: 960px), (pointer: coarse) {
  .dpad { display: grid; }
}
@media (max-width: 620px) {
  .app { padding: 8px 8px 24px; }
  .tagline { display: none; }
  .topbar { gap: 6px; padding-bottom: 8px; }
  .top-actions { justify-content: flex-start; }
  .top-actions .btn { padding-left: 10px; padding-right: 10px; font-size: 0.85rem; min-height: 40px; }
  .now-bar { flex-basis: 100%; max-width: none; flex-wrap: wrap; border-radius: 22px; }
  .now-bar .now-skip { min-height: 40px; }
  .now-bar .now-select { font-size: 16px; min-height: 40px; }   /* 16px keeps iOS from zooming the page on focus */
  .now-status { flex-basis: 100%; padding: 0 8px 4px; }
  .stage { padding: 8px; border-radius: 20px; }
  .tabpanel { grid-template-columns: 1fr; }
  .stat { padding: 6px 9px; gap: 6px; }
  .hud { gap: 5px; }
  .canvas-wrap { height: min(46vh, calc(100vh - 440px)); height: min(46svh, calc(100svh - 440px)); min-height: 200px; }
  .win { padding: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, .btn { transition: none; }
}

/* field toolbar (top border of the stage) */
.stage-col { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.card > .note:first-child { margin-top: 0; }
.field-toolbar { position: relative; z-index: 6; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.field-toolbar .hud { padding-right: 0; margin: 0; }
.field-toolbar .btn { min-height: 48px; }
.field-toolbar .mode-switch { position: static; }
.field-toolbar .mode-switch button { min-height: 48px; }
.field-toolbar .menu { display: block; }
#share-menu { margin-left: auto; }
.menu > summary { list-style: none; display: flex; align-items: center; cursor: pointer; user-select: none; -webkit-user-select: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu > summary::after { content: "\25BE"; margin-left: 8px; font-size: 0.8em; }
.menu[open] > summary { background: #e3efdd; box-shadow: inset 0 0 0 2px var(--sage-deep); }
.menu > summary:focus-visible { outline: 3px solid #3f7fb0; outline-offset: 2px; }
.menu-panel {
  max-height: min(460px, 50svh); overflow-y: auto;
  position: absolute; top: calc(100% + 6px); z-index: 20; width: min(300px, 100%);
  display: flex; flex-direction: column; gap: 6px; padding: 8px;
  background: var(--paper); border: 1.5px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
}
.menu-left { left: 0; }
.menu-right { right: 0; }
.menu-panel .btn { width: 100%; min-height: 48px; text-align: center; }
.menu-panel .mode-switch { align-self: stretch; }
.menu-panel .mode-switch button { flex: 1; }
.menu-panel .gate-status { margin: 0; min-height: 0; padding: 0 4px; font-size: 0.85rem; }
body[data-purpose="maze"] .hud { display: none; }
#field-support { text-align: center; }
#field-support .support { margin: 0; }
.top-actions a.btn { display: inline-flex; align-items: center; text-decoration: none; }
@media (max-width: 620px) {
  .field-toolbar { gap: 6px; flex-wrap: nowrap; }
  .field-toolbar .btn { padding-left: 9px; padding-right: 9px; font-size: 0.8rem; white-space: nowrap; }
  .field-toolbar .stat { padding: 4px 7px; }
  .menu > summary::after { margin-left: 4px; }
  .top-actions .btn { min-height: 48px; }
  .canvas-wrap { height: min(38svh, calc(100svh - 540px)); min-height: 160px; }
  .dpad { grid-template-rows: repeat(2, 50px); gap: 4px; }
}
@media (max-width: 360px) {
  /* Narrow phones wrap art tools; share the same vertical budget but reserve less canvas height. */
  .now-bar .now-select { min-width: 0; }
  .field-toolbar { flex-wrap: wrap; }
  .canvas-wrap { height: min(34svh, calc(100svh - 540px)); min-height: 140px; }
}
