:root {
  --bg: #0b0c16;
  --bg-panel: #15172b;
  --bg-card: #1d203b;
  --border-light: #4d558a;
  --border-dark: #070810;
  --text: #eef1ff;
  --text-dim: #9aa3cc;
  --accent: #38e584;
  --accent-dark: #167a44;
  --amber: #ffbe3b;
  --crimson: #ff476f;
  --cyan: #38bdf8;
  --purple: #b17bff;
  --pixel-shadow: 0 4px 0 #070810;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'SF Mono', 'Cascadia Mono', ui-monospace, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', monospace;
  overflow: hidden;
  user-select: none;
}

#app-shell {
  width: 100%;
  max-width: 560px;
  height: 100dvh;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Pixel Borders */
.pixel-panel {
  background: var(--bg-panel);
  border: 2px solid var(--border-light);
  box-shadow:
    inset 0 0 0 2px var(--border-dark),
    0 4px 0 var(--border-dark);
  border-radius: 2px;
}

.pixel-frame {
  position: relative;
  background: #070812;
  border: 3px solid var(--border-light);
  box-shadow:
    inset 0 0 0 2px var(--border-dark),
    0 4px 0 var(--border-dark);
}

/* HUD */
#hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  gap: 6px;
  flex-shrink: 0;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 6px;
}

.pixel-badge {
  background: var(--accent-dark);
  color: #fff;
  border: 1px solid var(--accent);
  padding: 2px 6px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.5px;
}

.pixel-clock {
  font-size: 12px;
  font-weight: 700;
  color: var(--amber);
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-chip {
  display: flex;
  align-items: center;
  gap: 3px;
  background: var(--bg-card);
  border: 1px solid #323862;
  padding: 2px 5px;
  font-size: 11px;
}

.stat-label {
  color: var(--text-dim);
}

.stat-val {
  color: var(--cyan);
  font-weight: 800;
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Buttons */
.pixel-btn {
  appearance: none;
  background: var(--bg-card);
  color: var(--text);
  border: 2px solid var(--border-light);
  box-shadow: 0 3px 0 var(--border-dark);
  padding: 8px 12px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.06s ease, box-shadow 0.06s ease, background 0.12s;
  text-align: center;
  line-height: 1.3;
}

.pixel-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--border-dark);
}

.pixel-btn.mini {
  padding: 3px 7px;
  font-size: 11px;
  box-shadow: 0 2px 0 var(--border-dark);
}

.pixel-btn.primary {
  background: #17663c;
  border-color: var(--accent);
  color: #ffffff;
}

.pixel-btn.amber {
  background: #6b490c;
  border-color: var(--amber);
  color: #fff6d9;
}

.pixel-btn.crimson {
  background: #6b1429;
  border-color: var(--crimson);
  color: #ffe6ec;
}

.pixel-btn.cyan {
  background: #0f4c6e;
  border-color: var(--cyan);
  color: #e6f8ff;
}

.pixel-btn.purple {
  background: #422275;
  border-color: var(--purple);
  color: #f3e8ff;
}

.pixel-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Main Stage */
#stage-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#canvas-frame {
  flex: 0 0 auto;
  width: 100%;
  aspect-ratio: 320 / 210;
  max-height: 39dvh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-canvas {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  display: block;
  touch-action: none;
}

.crt-overlay {
  pointer-events: none;
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0px,
    rgba(0, 0, 0, 0) 2px,
    rgba(0, 0, 0, 0.14) 3px,
    rgba(0, 0, 0, 0.14) 4px
  );
  mix-blend-mode: multiply;
}

.stage-toast {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(12, 14, 28, 0.92);
  border: 2px solid var(--amber);
  color: var(--amber);
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
  z-index: 5;
  pointer-events: none;
  animation: popIn 0.18s ease-out;
}

@keyframes popIn {
  from { opacity: 0; transform: translate(-50%, -6px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* Interactive Deck */
#deck {
  flex: 1;
  min-height: 0;
  padding: 10px 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-light) var(--bg-panel);
}

.deck-title {
  margin: 0;
  font-size: 15px;
  color: var(--amber);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.deck-subtitle {
  font-size: 11px;
  color: var(--cyan);
  font-weight: 700;
}

.dialogue-box {
  background: #101222;
  border: 2px solid #353b68;
  padding: 9px 10px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text);
}

.dialogue-speaker {
  display: inline-block;
  font-size: 11px;
  font-weight: 800;
  color: var(--accent);
  margin-bottom: 4px;
  text-transform: uppercase;
}

.quote-box {
  border-left: 3px solid var(--purple);
  background: rgba(177, 123, 255, 0.08);
  padding: 6px 9px;
  font-size: 12px;
  color: #dcd3ff;
  line-height: 1.45;
}

.choice-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.choice-btn {
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 9px 10px;
}

.choice-btn .choice-tag {
  font-size: 10px;
  color: var(--amber);
  font-weight: 800;
}

.choice-btn .choice-main {
  font-size: 13px;
  font-weight: 700;
}

.choice-btn .choice-sub {
  font-size: 11px;
  color: var(--text-dim);
  font-weight: 400;
}

/* Progress / HP Bars */
.bar-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bar-meta {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 700;
}

.pixel-bar {
  width: 100%;
  height: 12px;
  background: #090a14;
  border: 2px solid var(--border-light);
  padding: 1px;
}

.pixel-bar-fill {
  height: 100%;
  transition: width 0.25s steps(8);
}

.fill-green { background: var(--accent); }
.fill-red { background: var(--crimson); }
.fill-amber { background: var(--amber); }
.fill-cyan { background: var(--cyan); }
.fill-purple { background: var(--purple); }

/* Protocol / Inputs / Tags */
.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag-chip {
  background: #1a1e38;
  border: 1px solid #485085;
  color: var(--text);
  font-family: inherit;
  font-size: 11px;
  padding: 5px 8px;
  cursor: pointer;
}

.tag-chip.selected {
  background: #17663c;
  border-color: var(--accent);
  color: #fff;
}

.pixel-input, .pixel-textarea {
  width: 100%;
  background: #0b0c18;
  border: 2px solid var(--border-light);
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
  padding: 8px;
  outline: none;
  border-radius: 0;
}

.pixel-input:focus, .pixel-textarea:focus {
  border-color: var(--accent);
}

.pixel-textarea {
  min-height: 58px;
  resize: vertical;
}

.row-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.row-actions > * {
  flex: 1;
  min-width: 120px;
}

/* Touch Controls */
.touch-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--bg-panel);
  border: 2px solid var(--border-light);
  box-shadow: 0 3px 0 var(--border-dark);
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 38px);
  grid-template-rows: repeat(3, 34px);
  gap: 3px;
}

.dpad-btn {
  background: var(--bg-card);
  color: var(--text);
  border: 2px solid var(--border-light);
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.dpad-btn:active {
  background: var(--accent-dark);
  border-color: var(--accent);
}

.dpad-btn.up { grid-column: 2; grid-row: 1; }
.dpad-btn.left { grid-column: 1; grid-row: 2; }
.dpad-btn.center { grid-column: 2; grid-row: 2; font-size: 10px; color: var(--text-dim); }
.dpad-btn.right { grid-column: 3; grid-row: 2; }
.dpad-btn.down { grid-column: 2; grid-row: 3; }

.action-pad {
  display: flex;
  align-items: center;
  gap: 8px;
}

.action-a {
  min-width: 96px;
  min-height: 54px;
  background: #17663c;
  border-color: var(--accent);
  font-size: 14px;
}

.action-b {
  min-width: 78px;
  min-height: 54px;
  background: #0f4c6e;
  border-color: var(--cyan);
  font-size: 13px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(5, 6, 14, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 40;
}

.modal-box {
  width: 100%;
  max-width: 520px;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 10px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2 {
  margin: 0;
  font-size: 15px;
  color: var(--amber);
}

.modal-body {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 12px;
  line-height: 1.55;
}

.codex-card {
  background: #101222;
  border: 2px solid #353b68;
  padding: 9px 10px;
}

.codex-card h3 {
  margin: 0 0 4px 0;
  font-size: 13px;
  color: var(--accent);
}

.codex-card p {
  margin: 0;
  color: var(--text-dim);
}

.summary-card-preview {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}

.summary-row {
  background: #101222;
  border-left: 3px solid var(--accent);
  padding: 6px 8px;
  font-size: 12px;
}

.summary-row.anti {
  border-left-color: var(--crimson);
}

.summary-row.boss {
  border-left-color: var(--amber);
}

.summary-row strong {
  display: block;
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 2px;
}

.hidden {
  display: none !important;
}

@media (min-height: 760px) {
  #canvas-frame {
    max-height: 41dvh;
  }
}
