/* Coding Island · Mobile-First 2.5D Pixel Arcade & Inline Command Builder */
:root {
  --bg-deep: #071628;
  --panel-bg: #0b1e36;
  --panel-card: #112948;
  --border-cyan: rgba(125, 211, 252, 0.28);
  --text-main: #f0f9ff;
  --text-muted: #93c5fd;
  --gold: #facc15;
  --amber: #f59e0b;
  --blue: #38bdf8;
  --green: #22c55e;
  --red: #ef4444;
  --mono: "JetBrains Mono", "Fira Code", "Consolas", ui-monospace, monospace;
  --sans: "PingFang HK", "Microsoft JhengHei", "Noto Sans TC", -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text-main);
  font-family: var(--sans);
  touch-action: manipulation;
}

/* Mobile-First Default: Vertical Stack (Top Stage + Bottom Builder) */
.arcade-shell {
  position: relative;
  width: 100vw;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Region 1: 2.5D Pixel Island Stage (Non-overlapping header + canvas + mission strip) */
.stage-pane {
  position: relative;
  width: 100%;
  background: #0c4d7f;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  border-bottom: 2px solid rgba(125, 211, 252, 0.26);
}

.hud-top {
  width: 100%;
  height: 40px;
  padding: 0 8px;
  background: rgba(7, 19, 36, 0.94);
  border-bottom: 1px solid rgba(125, 211, 252, 0.18);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 5px;
  flex-shrink: 0;
  overflow: hidden;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 1;
  overflow: hidden;
}

.hud-right {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.stage-dots {
  display: inline-flex;
  background: #050f1e;
  border: 1px solid var(--border-cyan);
  border-radius: 7px;
  padding: 2px;
  gap: 2px;
  flex-shrink: 0;
}

.stage-dot {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 900;
  padding: 3px 6px;
  border-radius: 5px;
  cursor: pointer;
}

.stage-dot.active {
  background: var(--gold);
  color: #091526;
}

.stage-dot.cleared {
  color: var(--green);
}

.stage-title-text {
  font-size: 12px;
  font-weight: 900;
  color: #e0f2fe;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.hud-timer-pill,
.hud-btn {
  height: 28px;
  padding: 0 7px;
  background: rgba(15, 35, 62, 0.92);
  border: 1px solid var(--border-cyan);
  border-radius: 7px;
  color: var(--text-main);
  font-size: 11.5px;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  flex-shrink: 0;
}

.hud-timer-pill {
  font-family: var(--mono);
  font-size: 11px;
  color: #bae6fd;
}

.hud-btn {
  cursor: pointer;
}

.hud-btn:active {
  transform: scale(0.96);
}

.hud-icon-btn {
  width: 28px;
  padding: 0;
  justify-content: center;
}

.hud-icon {
  width: 13px;
  height: 13px;
  fill: currentColor;
  flex-shrink: 0;
}

.canvas-wrap {
  width: 100%;
  height: min(26dvh, 212px);
  background: #0e5a94;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#game {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: pointer;
}

/* Non-Overlapping Mission / Status Bar right under Canvas */
.hud-objective {
  min-height: 34px;
  padding: 5px 10px;
  background: rgba(8, 21, 38, 0.96);
  border-top: 1px solid rgba(125, 211, 252, 0.16);
  display: flex;
  align-items: center;
  gap: 8px;
}

.hud-objective.status-error {
  background: rgba(68, 12, 12, 0.96);
  border-top-color: var(--red);
}

.hud-objective.status-win {
  background: rgba(7, 54, 25, 0.96);
  border-top-color: var(--green);
}

.obj-tag {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 900;
  padding: 2px 7px;
  border-radius: 5px;
  background: rgba(56, 189, 248, 0.22);
  color: var(--blue);
  flex-shrink: 0;
}

.hud-objective.status-error .obj-tag {
  background: rgba(239, 68, 68, 0.28);
  color: #fca5a5;
}

.hud-objective.status-win .obj-tag {
  background: rgba(34, 197, 94, 0.28);
  color: #86efac;
}

.obj-text {
  font-size: 12px;
  font-weight: 700;
  color: #f8fafc;
  line-height: 1.3;
}

/* Bottom Section: Thumb-Friendly Command Builder */
.builder-pane {
  flex: 1;
  background: var(--panel-bg);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* Region 2: Slim Top Bar (Single-Line Lens Toggle + Line Meter) */
.builder-top-bar {
  padding: 6px 10px;
  background: rgba(7, 18, 33, 0.88);
  border-bottom: 1px solid rgba(125, 211, 252, 0.14);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.lens-tabs {
  display: inline-flex;
  background: #051020;
  padding: 2px;
  border-radius: 8px;
  border: 1px solid rgba(125, 211, 252, 0.24);
  gap: 2px;
}

.lens-tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 800;
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}

.lens-tab.active {
  background: #1d4ed8;
  color: #ffffff;
}

.line-meter {
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 900;
  padding: 4px 9px;
  border-radius: 6px;
  background: rgba(56, 189, 248, 0.14);
  color: var(--blue);
  border: 1px solid rgba(56, 189, 248, 0.3);
  white-space: nowrap;
  flex-shrink: 0;
}

.line-meter.at-limit {
  background: rgba(245, 158, 11, 0.22);
  color: var(--gold);
  border-color: var(--gold);
}

/* Region 3: Command Toolbox (+ Add Command Pills) */
.toolbox-bar {
  padding: 7px 10px;
  background: rgba(12, 30, 54, 0.96);
  border-bottom: 1px solid rgba(125, 211, 252, 0.16);
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.toolbox-buttons {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  width: 100%;
}

.tool-add-btn {
  height: 30px;
  border: 1.5px solid rgba(255, 255, 255, 0.28);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 900;
  color: #ffffff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  white-space: nowrap;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.4);
}

.tool-add-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.tool-add-btn:disabled {
  opacity: 0.32;
  cursor: not-allowed;
}

.t-move { background: #2563eb; }
.t-build { background: #059669; }
.t-jump { background: #0284c7; }
.t-repeat { background: #d97706; }
.t-light { background: #7c3aed; }

/* Region 4: Single-List Code Editor with Inline Selected-Row Tray */
.editor-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cmd-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px 9px;
  border-radius: 10px;
  background: rgba(15, 36, 64, 0.85);
  border: 2px solid rgba(148, 163, 184, 0.22);
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

.cmd-row.selected-row {
  border-color: var(--blue);
  background: rgba(21, 50, 88, 0.96);
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2);
}

.cmd-row.indented {
  margin-left: 18px;
  border-left: 5px solid var(--amber);
}

.cmd-row.type-repeat {
  background: rgba(146, 64, 14, 0.45);
  border-color: rgba(251, 191, 36, 0.5);
}

.cmd-row-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  min-height: 30px;
}

.cmd-main {
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.unit-label {
  font-size: 12px;
  font-weight: 800;
  color: #fde68a;
}

.line-num-pill {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 900;
  color: #64748b;
  width: 14px;
  text-align: center;
  flex-shrink: 0;
}

.block-tag {
  font-size: 12.5px;
  font-weight: 900;
  padding: 3px 7px;
  border-radius: 6px;
  color: #ffffff;
  white-space: nowrap;
}

.tag-move { background: #2563eb; }
.tag-build_pier { background: #059669; }
.tag-jump { background: #0284c7; }
.tag-repeat { background: #d97706; }
.tag-light_lighthouse { background: #7c3aed; }

/* Direction Pill Button with Caret */
.dir-pill-btn {
  height: 28px;
  background: #071324;
  color: var(--gold);
  border: 1.5px solid var(--gold);
  border-radius: 7px;
  padding: 0 8px;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 900;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.dir-pill-btn.picker-open {
  background: var(--gold);
  color: #071324;
}

.dir-caret {
  font-size: 10px;
  opacity: 0.85;
}

/* Thumb-Sized Stepper [- 3 +] */
.num-ctrl {
  height: 28px;
  display: inline-flex;
  align-items: center;
  background: #071324;
  border: 1.5px solid var(--blue);
  border-radius: 7px;
  overflow: hidden;
}

.num-btn {
  border: none;
  background: rgba(56, 189, 248, 0.18);
  color: var(--blue);
  font-family: var(--mono);
  font-weight: 900;
  font-size: 14px;
  width: 26px;
  height: 100%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.num-btn:active:not(:disabled) {
  background: var(--blue);
  color: #071324;
}

.num-btn:disabled {
  opacity: 0.28;
}

.num-val {
  padding: 0 7px;
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 900;
  color: #ffffff;
}

/* Live Python Code Mirror Pill on Right Side of Row */
.py-mirror-pill {
  font-family: var(--mono);
  font-size: 11px;
  background: rgba(5, 13, 25, 0.84);
  border: 1px solid rgba(148, 163, 184, 0.24);
  padding: 3px 7px;
  border-radius: 6px;
  color: #cbd5e1;
  white-space: nowrap;
  flex-shrink: 0;
}

.kw { color: #fb923c; font-weight: 800; }
.fn { color: #38bdf8; font-weight: 700; }
.bi { color: #fde047; }
.str { color: #a7f3d0; }

/* Inline Selected-Row Edit Tray */
.cmd-inline-tray {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding-top: 5px;
  border-top: 1px solid rgba(125, 211, 252, 0.16);
}

.dir-picker-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}

.dir-opt-btn {
  height: 30px;
  border: 1.5px solid rgba(250, 204, 21, 0.45);
  background: #071324;
  color: #fef08a;
  border-radius: 7px;
  font-size: 12px;
  font-weight: 900;
  cursor: pointer;
}

.dir-opt-btn.active-dir {
  background: var(--gold);
  color: #091526;
  border-color: #fef08a;
}

.inline-actions-bar {
  display: grid;
  grid-template-columns: 1.45fr 0.7fr 0.7fr 1.05fr;
  gap: 5px;
}

.inline-act-btn {
  height: 30px;
  border: 1.5px solid rgba(148, 163, 184, 0.3);
  background: rgba(11, 25, 45, 0.9);
  color: #e2e8f0;
  border-radius: 7px;
  font-size: 11.5px;
  font-weight: 900;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

.inline-act-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.inline-act-btn:disabled {
  opacity: 0.28;
  cursor: default;
}

.inline-act-btn.indent-btn:not(:disabled) {
  border-color: var(--gold);
  color: var(--gold);
  background: rgba(250, 204, 21, 0.15);
}

.inline-act-btn.del-btn:not(:disabled) {
  border-color: rgba(239, 68, 68, 0.52);
  color: #fca5a5;
}

/* Execution Highlight & Bug Flash */
.active-line {
  outline: 2.5px solid var(--gold) !important;
  background: rgba(250, 204, 21, 0.22) !important;
  box-shadow: 0 0 14px rgba(250, 204, 21, 0.45) !important;
}

.error-line {
  outline: 2.5px solid var(--red) !important;
  background: rgba(127, 29, 29, 0.85) !important;
}

/* Region 5: Big Tactile Run & Reset Footer */
.builder-footer {
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: #061120;
  border-top: 1px solid rgba(125, 211, 252, 0.2);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.arcade-run-btn {
  flex: 1;
  height: 44px;
  background: linear-gradient(180deg, #facc15 0%, #eab308 100%);
  color: #091526;
  border: 2px solid #fef08a;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 900;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 3px 0 #a16207;
}

.arcade-run-btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #a16207;
}

.arcade-reset-btn {
  height: 44px;
  padding: 0 14px;
  background: rgba(30, 58, 95, 0.85);
  color: #bae6fd;
  border: 1.5px solid rgba(125, 211, 252, 0.3);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.btn-svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}

/* Overlays */
.overlay-backdrop {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: rgba(6, 16, 30, 0.68);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.overlay-backdrop.hidden {
  display: none !important;
}

.arcade-card {
  position: relative;
  width: min(450px, 100%);
  background: rgba(11, 25, 45, 0.96);
  border: 2px solid rgba(125, 211, 252, 0.42);
  border-radius: 16px;
  padding: 22px 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  text-align: center;
}

.victory-card {
  width: min(500px, 100%);
}

.modal-close-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid rgba(125, 211, 252, 0.32);
  background: rgba(23, 47, 79, 0.85);
  color: #cbd5e1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.modal-close-btn:hover {
  border-color: var(--gold);
  color: #ffffff;
}

.card-kicker {
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 1.5px;
  color: var(--gold);
  margin-bottom: 5px;
}

.pixel-title {
  font-family: var(--mono);
  font-size: clamp(24px, 6vw, 34px);
  font-weight: 900;
  letter-spacing: 1.5px;
  color: #ffffff;
  text-shadow: 0 3px 0 #1e3a8a;
}

.pixel-subtitle {
  font-size: 14px;
  font-weight: 800;
  color: #7dd3fc;
  margin: 4px 0 12px;
}

.modal-heading {
  font-size: 20px;
  font-weight: 900;
  margin-bottom: 8px;
}

.card-premise {
  font-size: 13px;
  line-height: 1.55;
  color: #cbd5e1;
  margin-bottom: 16px;
}

.mode-selector,
.pause-actions {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-bottom: 16px;
}

.mode-btn {
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 11px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.primary-mode {
  background: linear-gradient(180deg, #facc15 0%, #eab308 100%);
  color: #091526;
  border: 2px solid #fef08a;
  box-shadow: 0 4px 0 #a16207;
}

.secondary-mode {
  background: rgba(23, 47, 79, 0.85);
  color: #f8fafc;
  border: 1.5px solid rgba(125, 211, 252, 0.35);
}

.mode-name {
  font-size: 14px;
  font-weight: 900;
}

.mode-meta {
  font-size: 11.5px;
  opacity: 0.88;
  font-weight: 600;
}

.ext-link {
  white-space: nowrap;
}

.card-footer-links {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12px;
  color: #64748b;
}

.card-footer-links a {
  color: #93c5fd;
  text-decoration: none;
  font-weight: 700;
}

.pause-btn {
  width: 100%;
  padding: 11px 14px;
  border-radius: 10px;
  background: rgba(23, 47, 79, 0.85);
  color: #f8fafc;
  border: 1.5px solid rgba(125, 211, 252, 0.35);
  font-size: 13.5px;
  font-weight: 800;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.pause-btn.primary {
  background: var(--gold);
  color: #091526;
}

.victory-stats {
  font-size: 13px;
  color: #cbd5e1;
  margin: 6px 0 12px;
}

.victory-stats strong {
  font-family: var(--mono);
  color: var(--gold);
}

.concept-chips {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 7px;
  margin-bottom: 12px;
}

.c-chip {
  background: rgba(19, 41, 71, 0.85);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 8px;
  padding: 8px 7px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.c-chip strong {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--gold);
}

.c-chip span {
  font-size: 11px;
  font-weight: 700;
  color: #e2e8f0;
}

.victory-bridge-text {
  font-size: 12.5px;
  color: #bae6fd;
  line-height: 1.5;
  margin-bottom: 14px;
}

.victory-cta-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.victory-cta-primary {
  background: linear-gradient(180deg, #facc15 0%, #eab308 100%);
  color: #091526;
  border: 2px solid #fef08a;
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 14px;
  font-weight: 900;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 3px 0 #a16207;
}

.victory-cta-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.victory-cta-sec {
  background: rgba(23, 47, 79, 0.85);
  color: #e2e8f0;
  border: 1.5px solid rgba(125, 211, 252, 0.32);
  border-radius: 8px;
  padding: 9px 8px;
  font-size: 11.5px;
  font-weight: 800;
  text-decoration: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

#btn-play-again {
  grid-column: 1 / -1;
}

/* Desktop Expansion (>= 860px): Side-by-Side 2-Column Arcade Layout */
@media (min-width: 860px) {
  .arcade-shell {
    display: grid;
    grid-template-columns: 1.15fr 0.85fr;
  }

  .stage-pane {
    height: 100dvh;
    border-bottom: none;
    border-right: 2px solid rgba(125, 211, 252, 0.24);
  }

  .canvas-wrap {
    flex: 1;
    height: auto;
    max-height: none;
  }

  .builder-pane {
    height: 100dvh;
  }
}
