:root {
  --bg-deep: #14100d;
  --bg-panel: #211a14;
  --bg-panel-hi: #2e241c;
  --border-warm: rgba(245, 198, 130, 0.2);
  --border-strong: rgba(245, 198, 130, 0.4);
  --text-main: #f8efe2;
  --text-muted: #c2b09a;
  --amber: #f59e0b;
  --amber-soft: #fde68a;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 14px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  touch-action: none;
}

body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Shared Portal Masthead */
.masthead {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
  background: rgba(24, 18, 14, 0.96);
  border-bottom: 1px solid var(--border-warm);
  z-index: 20;
}

.masthead-left {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.masthead h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--amber-soft);
  white-space: nowrap;
}

.masthead nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.masthead button {
  appearance: none;
  border: 1px solid var(--border-warm);
  background: var(--bg-panel);
  color: var(--text-main);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.14s ease, border-color 0.14s ease, transform 0.1s ease;
}

.masthead button:hover {
  background: var(--bg-panel-hi);
  border-color: var(--border-strong);
}

.masthead button[aria-pressed="true"] {
  background: rgba(245, 158, 11, 0.2);
  border-color: var(--amber);
  color: var(--amber-soft);
}

/* Main Game Shell — Portrait First */
.shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 8px 10px 10px;
  gap: 8px;
}

/* Top HUD Strip */
.hud-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.stage-pill {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 6px 12px;
  border-radius: 12px;
  border: 1px solid var(--border-warm);
  background: linear-gradient(180deg, #2b2119, #1f1711);
  color: var(--text-main);
  cursor: pointer;
  text-align: left;
  min-width: 0;
  transition: border-color 0.14s ease, transform 0.1s ease;
}

.stage-pill:hover {
  border-color: var(--amber);
}

.stage-chapter {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--amber);
}

.stage-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
}

.stat-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 64px;
  padding: 5px 9px;
  border-radius: 11px;
  background: var(--bg-panel);
  border: 1px solid var(--border-warm);
}

.stat-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  text-transform: uppercase;
}

.stat-val {
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
}

.stat-boxes .stat-val {
  color: var(--amber-soft);
}

/* Warehouse Canvas Stage */
.board-wrap {
  flex: 1 1 auto;
  min-height: 180px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border-warm);
  background: #14100d;
  box-shadow: inset 0 0 32px rgba(0, 0, 0, 0.55);
}

#warehouse-canvas {
  display: block;
  touch-action: none;
  cursor: pointer;
}

.status-toast {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  background: rgba(26, 19, 14, 0.92);
  border: 1px solid var(--border-strong);
  color: var(--amber-soft);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45);
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-toast.is-danger {
  background: rgba(127, 29, 29, 0.94);
  border-color: rgba(248, 113, 113, 0.75);
  color: #fee2e2;
}

/* Bottom Thumb Control Deck */
.control-deck {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.util-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 9px 8px;
  border-radius: 12px;
  border: 1px solid var(--border-warm);
  background: linear-gradient(180deg, #2d2219, #201711);
  color: var(--text-main);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.14s ease, background 0.14s ease;
}

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

.util-btn:hover {
  border-color: var(--border-strong);
}

.util-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.util-hint {
  border-color: rgba(245, 158, 11, 0.35);
  color: var(--amber-soft);
}

.btn-icon {
  font-size: 14px;
  line-height: 1;
}

/* Mobile Ergonomic D-pad */
.dpad-cluster,
.dpad-grid,
.dpad-btn,
.dpad-center {
  touch-action: none;
}

.dpad-cluster {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.dpad-grid {
  display: grid;
  grid-template-columns: 56px 56px 56px;
  grid-template-rows: 46px 46px 46px;
  gap: 5px;
  align-items: center;
  justify-items: center;
}

.dpad-btn {
  appearance: none;
  width: 56px;
  height: 46px;
  border-radius: 13px;
  border: 1px solid var(--border-strong);
  background: linear-gradient(180deg, #3a2b1e, #241a12);
  color: var(--amber-soft);
  font-size: 17px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.45);
  transition: transform 0.07s ease, background 0.1s ease;
}

.dpad-btn:active {
  transform: translateY(2px) scale(0.96);
  background: linear-gradient(180deg, #523b26, #322316);
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-center {
  grid-column: 2;
  grid-row: 2;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(245, 158, 11, 0.1);
  border: 1px solid var(--border-warm);
  color: var(--amber);
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

.swipe-tip {
  margin: 0;
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
}

/* Desktop / Wide Layout: maximize board size while keeping clean horizontal controls */
@media (min-width: 760px) and (min-height: 620px) {
  .shell {
    padding: 12px 20px 14px;
    gap: 10px;
  }

  .control-deck {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .action-strip {
    flex: 1 1 auto;
    max-width: 460px;
  }

  .dpad-cluster {
    flex-direction: row;
    gap: 12px;
  }

  .dpad-grid {
    grid-template-columns: 42px 42px 42px;
    grid-template-rows: 34px 34px 34px;
    gap: 4px;
  }

  .dpad-btn {
    width: 42px;
    height: 34px;
    font-size: 13px;
    border-radius: 9px;
  }

  .dpad-center {
    width: 24px;
    height: 24px;
    font-size: 10px;
  }

  .swipe-tip {
    max-width: 210px;
    text-align: left;
  }
}

/* Short-screen mobile portrait tuning */
@media (max-height: 700px) and (max-width: 520px) {
  .shell {
    padding: 6px 8px 6px;
    gap: 5px;
  }
  .dpad-grid {
    grid-template-columns: 50px 50px 50px;
    grid-template-rows: 40px 40px 40px;
    gap: 4px;
  }
  .dpad-btn {
    width: 50px;
    height: 40px;
    font-size: 15px;
  }
  .swipe-tip {
    display: none;
  }
}

/* Modals */
.overlay-root {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(12, 9, 6, 0.78);
  backdrop-filter: blur(6px);
}

.overlay-card {
  width: 100%;
  max-width: 520px;
  max-height: min(84vh, 680px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  background: linear-gradient(180deg, #271e17, #1b140f);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  color: var(--text-main);
}

.overlay-card * {
  touch-action: pan-y;
}

.chapter-tabs,
.chapter-tabs * {
  touch-action: pan-x pan-y;
}

.overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.overlay-card h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 800;
  color: var(--amber-soft);
}

.overlay-sub {
  margin: 0 0 14px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-muted);
}

.close-x {
  appearance: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border-warm);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
  font-size: 16px;
  cursor: pointer;
}

.primary-btn,
.secondary-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 18px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  border: 1px solid transparent;
  transition: transform 0.08s ease, filter 0.14s ease;
}

.primary-btn {
  background: linear-gradient(180deg, #f59e0b, #d97706);
  color: #1c1208;
  box-shadow: 0 4px 14px rgba(245, 158, 11, 0.32);
}

.secondary-btn {
  background: var(--bg-panel-hi);
  border-color: var(--border-warm);
  color: var(--text-main);
}

.primary-btn:active,
.secondary-btn:active {
  transform: scale(0.97);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

.modal-actions > button {
  flex: 1 1 120px;
}

/* Victory Star Row */
.star-banner {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 8px 0 14px;
  font-size: 34px;
}

.star-on {
  color: #fbbf24;
  text-shadow: 0 0 18px rgba(251, 191, 36, 0.65);
}

.star-off {
  color: rgba(255, 255, 255, 0.18);
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 10px;
}

.result-box {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border-warm);
  border-radius: 12px;
  padding: 10px 12px;
  text-align: center;
}

.result-box span {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.result-box strong {
  font-size: 18px;
  color: var(--text-main);
}

.result-box small {
  display: block;
  font-size: 11px;
  color: var(--amber);
  margin-top: 2px;
}

.par-notes {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  margin: 8px 0 0;
}

/* Chapter & 50-Stage Selector */
.chapter-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 10px;
}

.chapter-tab {
  appearance: none;
  flex: 0 0 auto;
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid var(--border-warm);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.chapter-tab.is-active {
  background: rgba(245, 158, 11, 0.2);
  border-color: var(--amber);
  color: var(--amber-soft);
}

.chapter-desc {
  margin: 0 0 12px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-muted);
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.stage-tile {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  padding: 10px 11px;
  border-radius: 12px;
  border: 1px solid var(--border-warm);
  background: rgba(255, 255, 255, 0.035);
  color: var(--text-main);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.14s ease, background 0.14s ease;
}

.stage-tile:hover:not(:disabled) {
  border-color: var(--amber);
  background: rgba(245, 158, 11, 0.1);
}

.stage-tile.is-current {
  border-color: var(--amber);
  background: rgba(245, 158, 11, 0.16);
}

.stage-tile:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.stage-tile-top {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 800;
  color: var(--amber-soft);
}

.stage-tile-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-main);
}

.stage-tile-meta {
  font-size: 11px;
  color: var(--text-muted);
}

/* Guide List */
.guide-list {
  margin: 0 0 12px;
  padding-left: 18px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-main);
}

.guide-list li {
  margin-bottom: 6px;
}

.controls-box {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border-warm);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.55;
  color: var(--text-muted);
}

.controls-box strong {
  display: block;
  color: var(--amber-soft);
  margin-bottom: 4px;
}
