:root {
  --ink: #12191c;
  --ink-soft: #1d292d;
  --paper: #f3f1e8;
  --paper-deep: #e6e2d5;
  --muted: #79807c;
  --line: rgba(18, 25, 28, 0.15);
  --mint: #6ed3cf;
  --coral: #ef8354;
  --yellow: #f4c95d;
  --lavender: #c8a2c8;
  --shadow: 0 18px 40px rgba(18, 25, 28, 0.12);
  font-family: "Aptos", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  background: var(--paper);
  background-image: linear-gradient(rgba(18, 25, 28, 0.025) 1px, transparent 1px), linear-gradient(90deg, rgba(18, 25, 28, 0.025) 1px, transparent 1px);
  background-size: 28px 28px;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 3px solid var(--coral); outline-offset: 2px; }

.app-shell { width: min(1420px, calc(100% - 48px)); margin: 0 auto; padding: 28px 0 26px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 24px; padding-bottom: 22px; border-bottom: 1px solid var(--line); }
.brand-lockup { display: flex; align-items: center; gap: 14px; }
.brand-mark { display: grid; place-items: center; width: 38px; height: 38px; background: var(--ink); color: var(--yellow); font-size: 20px; }
.eyebrow, .micro-label { margin: 0; color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: 0.13em; text-transform: uppercase; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(22px, 3vw, 34px); letter-spacing: -0.04em; line-height: 1; }
.topbar-status { display: flex; align-items: center; gap: 8px; color: #4d5754; font-size: 13px; font-weight: 700; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--coral); box-shadow: 0 0 0 4px rgba(239, 131, 84, 0.16); }

.intro-band { display: grid; grid-template-columns: minmax(0, 1fr) minmax(280px, 440px); align-items: end; gap: 40px; padding: 40px 0 30px; }
.intro-band h2 { max-width: 720px; margin: 7px 0 0; font-size: clamp(30px, 4.2vw, 62px); line-height: 0.98; letter-spacing: -0.06em; }
.intro-copy { max-width: 390px; margin: 0 0 4px; color: #5e6763; font-size: 15px; line-height: 1.65; }

.workspace { display: grid; grid-template-columns: minmax(0, 1fr); gap: 22px; align-items: start; }
.stage-frame { overflow: hidden; border: 1px solid var(--ink); background: var(--ink); box-shadow: var(--shadow); }
.stage-toolbar { display: flex; align-items: center; justify-content: space-between; min-height: 52px; padding: 0 16px; color: var(--paper); border-bottom: 1px solid rgba(243, 241, 232, 0.18); }
.stage-label { display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }
.live-pill { padding: 4px 6px; background: var(--coral); color: var(--ink); font-size: 9px; letter-spacing: 0.08em; }
.stage-actions { display: flex; gap: 6px; }
.icon-button { display: flex; align-items: center; gap: 6px; padding: 7px 9px; border: 1px solid rgba(243, 241, 232, 0.25); background: transparent; color: var(--paper); font-size: 11px; }
.icon-button:hover:not(:disabled) { border-color: var(--yellow); color: var(--yellow); }
.icon-button:disabled { cursor: not-allowed; opacity: 0.35; }
.canvas-wrap { position: relative; padding: 14px; background: #162125; }
#gameCanvas { display: block; width: 100%; height: auto; aspect-ratio: 8 / 5; background: #19343a; image-rendering: auto; }
.canvas-hint { position: absolute; right: 26px; bottom: 24px; padding: 7px 9px; background: rgba(18, 25, 28, 0.86); color: var(--paper); font-size: 11px; pointer-events: none; }
.game-footer { display: grid; grid-template-columns: 1.35fr 0.65fr 0.8fr 0.9fr; gap: 18px; padding: 16px; background: var(--ink-soft); color: var(--paper); }
.progress-block { min-width: 0; }
.progress-track { height: 6px; margin: 8px 0 6px; background: rgba(243, 241, 232, 0.15); }
.progress-fill { width: 0; height: 100%; background: var(--mint); transition: width 180ms ease; }
.progress-number, .timer-block strong, .code-block strong { font-size: 12px; }
.timer-block strong, .code-block strong { display: block; margin-top: 8px; color: var(--yellow); }
.code-block strong { color: var(--mint); }
.danger-block strong { display: block; margin-top: 8px; color: var(--coral); font-size: 12px; }
.danger-block small { display: block; margin-top: 5px; color: rgba(243, 241, 232, 0.58); font-size: 10px; }
.code-entry { display: flex; gap: 5px; margin-top: 8px; }
.code-entry[hidden] { display: none; }
.code-entry input { width: 62px; padding: 6px 7px; border: 1px solid rgba(243, 241, 232, 0.28); background: rgba(243, 241, 232, 0.08); color: var(--paper); font-size: 11px; }
.code-entry button { padding: 0 7px; border: 1px solid var(--mint); background: var(--mint); color: var(--ink); font-size: 10px; font-weight: 900; }

.mobile-controls { position: absolute; left: 28px; bottom: 28px; z-index: 2; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 7px; border: 1px solid rgba(243, 241, 232, 0.26); background: rgba(18, 25, 28, 0.72); backdrop-filter: blur(5px); touch-action: manipulation; }
.mobile-controls > div { display: flex; gap: 4px; }
.mobile-controls button { width: 40px; height: 36px; border: 1px solid rgba(243, 241, 232, 0.36); background: rgba(243, 241, 232, 0.9); color: var(--ink); font-size: 18px; line-height: 1; }
.mobile-controls button:hover, .mobile-controls button:active { background: var(--yellow); }
.field-notes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
.note-card { display: grid; grid-template-columns: 34px 1fr; gap: 10px; min-height: 100px; padding: 15px; border: 1px solid var(--line); background: rgba(243, 241, 232, 0.7); }
.accent-note { background: #dceee9; border-color: rgba(110, 211, 207, 0.55); }
.note-index { color: var(--coral); font-size: 12px; font-weight: 900; }
.note-card p:last-child { margin: 6px 0 0; color: #4e5a56; font-size: 13px; line-height: 1.5; }

.control-column { display: grid; gap: 14px; }
.panel { padding: 19px; border: 1px solid var(--line); background: rgba(243, 241, 232, 0.84); }
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.panel-heading h3 { margin: 5px 0 0; font-size: 20px; letter-spacing: -0.04em; }
.panel-tag, .version-label { flex: 0 0 auto; padding: 5px 7px; background: var(--ink); color: var(--yellow); font-size: 9px; font-weight: 900; letter-spacing: 0.08em; }
.accent-tag { background: var(--coral); color: var(--ink); }
.field-label { display: block; margin: 0 0 7px; color: #5e6763; font-size: 11px; font-weight: 800; letter-spacing: 0.04em; text-transform: uppercase; }
.inline-field { display: flex; gap: 7px; }
input, textarea { width: 100%; border: 1px solid rgba(18, 25, 28, 0.25); border-radius: 0; background: #f9f7ef; color: var(--ink); }
input { min-width: 0; padding: 10px 11px; }
textarea { resize: vertical; padding: 11px; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: #9aa09b; }
.small-button, .secondary-button { border: 1px solid var(--ink); background: var(--paper); color: var(--ink); font-size: 12px; font-weight: 800; white-space: nowrap; }
.small-button { padding: 0 12px; }
.small-button:hover, .secondary-button:hover { background: var(--yellow); }
.swatches { display: flex; gap: 8px; margin: 13px 0 10px; }
.swatch { width: 24px; height: 24px; padding: 0; border: 2px solid transparent; border-radius: 50%; background: var(--swatch); }
.swatch.selected { border-color: var(--ink); box-shadow: 0 0 0 2px var(--paper); }
.helper-text, .panel-copy { margin: 0; color: #727b76; font-size: 12px; line-height: 1.5; }
.panel-copy { margin-bottom: 16px; }
.primary-button { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; margin-top: 10px; padding: 12px; border: 0; background: var(--ink); color: var(--paper); font-size: 12px; font-weight: 800; }
.primary-button:hover { background: #2e4349; }
.translation-box { margin-top: 16px; padding: 13px; border-left: 3px solid var(--coral); background: #e8e7dc; }
.translation-header { display: flex; justify-content: space-between; gap: 8px; }
.ready-label { color: #397c75; font-size: 9px; font-weight: 900; letter-spacing: 0.08em; }
.translation-summary { margin: 10px 0; color: var(--ink); font-size: 13px; line-height: 1.5; }
.rule-diff { display: grid; gap: 6px; }
.diff-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 11px; }
.diff-row span { padding: 7px; background: rgba(255, 255, 255, 0.48); }
.diff-row span:last-child { background: rgba(110, 211, 207, 0.28); font-weight: 800; }
.translation-actions { display: flex; align-items: center; gap: 12px; margin-top: 13px; }
.secondary-button { padding: 9px 11px; }
.text-button { padding: 0; border: 0; background: transparent; color: #64736d; font-size: 11px; text-decoration: underline; }
.compact-heading { margin-bottom: 10px; align-items: center; }
.rule-list { margin: 0; }
.rule-list div { display: flex; justify-content: space-between; gap: 20px; padding: 10px 0; border-top: 1px solid var(--line); }
.rule-list dt { color: #737b77; font-size: 12px; }
.rule-list dd { margin: 0; color: var(--ink); font-size: 12px; font-weight: 800; text-align: right; }
.page-footer { display: flex; justify-content: space-between; gap: 20px; padding-top: 22px; color: #7a827e; font-size: 10px; letter-spacing: 0.03em; }
.toast { position: fixed; left: 50%; bottom: 26px; z-index: 5; max-width: min(420px, calc(100% - 30px)); padding: 11px 14px; transform: translate(-50%, 18px); background: var(--ink); color: var(--paper); font-size: 12px; opacity: 0; pointer-events: none; transition: opacity 180ms ease, transform 180ms ease; }
.toast.visible { transform: translate(-50%, 0); opacity: 1; }

.password-modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 20px; }
.password-modal[hidden] { display: none; }
.modal-scrim { position: absolute; inset: 0; background: rgba(18, 25, 28, 0.72); backdrop-filter: blur(4px); }
.modal-card { position: relative; width: min(430px, 100%); padding: 28px; border: 1px solid var(--ink); background: var(--paper); box-shadow: 0 24px 70px rgba(0, 0, 0, 0.32); }
.modal-kicker { color: var(--coral); font-size: 10px; font-weight: 900; letter-spacing: 0.13em; }
.modal-card h2 { margin: 8px 0 10px; font-size: 32px; letter-spacing: -0.05em; }
.modal-card p { color: #5e6763; font-size: 14px; line-height: 1.6; }
.modal-card label { display: block; margin: 20px 0 7px; color: #5e6763; font-size: 11px; font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase; }
.modal-card input { padding: 13px; border: 1px solid var(--ink); background: #fffdf5; font-size: 22px; letter-spacing: 0.3em; text-align: center; }
.modal-error { min-height: 21px; margin: 8px 0 0 !important; color: var(--coral) !important; font-size: 12px !important; }
.modal-actions { display: flex; align-items: center; gap: 14px; margin-top: 13px; }

@media (max-width: 980px) {
  .workspace { grid-template-columns: 1fr; }
}

@media (max-width: 680px) {
  .app-shell { width: min(100% - 26px, 560px); padding-top: 16px; }
  .topbar { align-items: flex-start; }
  .topbar-status { font-size: 0; }
  .intro-band { display: block; padding: 28px 0 22px; }
  .intro-band h2 { font-size: 39px; }
  .intro-copy { margin-top: 18px; font-size: 14px; }
  .stage-toolbar { min-height: 48px; padding: 0 11px; }
  .icon-button span { display: none; }
  .canvas-wrap { padding: 8px; }
  .game-footer { grid-template-columns: 1.35fr 0.75fr; gap: 12px; padding: 13px 11px; }
  .code-block { grid-column: 2; grid-row: 1; }
  .danger-block { grid-column: 2; grid-row: 2; }
  .mobile-controls { left: 16px; bottom: 16px; }
  .field-notes { grid-template-columns: 1fr; }
  .page-footer { display: block; line-height: 1.7; }
  .page-footer span { display: block; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}

/* v4: controls live below the map, so they never cover playable cells. */
.canvas-wrap { padding-bottom: 14px; }
.mobile-controls { position: static; display: flex; flex-direction: column; align-items: center; gap: 4px; width: fit-content; margin: 0 auto; padding: 10px 12px 12px; border: 0; border-top: 1px solid rgba(243, 241, 232, 0.12); background: var(--ink); backdrop-filter: none; }
.mobile-controls > div { display: flex; gap: 4px; }
.mobile-controls button { width: 46px; height: 40px; border: 1px solid rgba(243, 241, 232, 0.34); background: rgba(243, 241, 232, 0.94); color: var(--ink); font-size: 18px; line-height: 1; touch-action: manipulation; }
.mobile-controls button:hover, .mobile-controls button:active { background: var(--yellow); }
.event-modal { position: fixed; inset: 0; z-index: 11; display: grid; place-items: center; padding: 20px; }
.event-modal[hidden] { display: none; }
.event-card { position: relative; width: min(440px, 100%); padding: 26px; border: 1px solid var(--ink); background: var(--paper); box-shadow: 0 24px 70px rgba(0, 0, 0, 0.32); }
.event-card h2 { margin: 8px 0 10px; font-size: 28px; letter-spacing: -0.05em; }
.event-card p { margin-bottom: 18px; color: #5e6763; font-size: 14px; line-height: 1.65; }
.event-card .secondary-button { padding: 10px 17px; }
