:root {
  --bg-night: #080a14;
  --bg-panel: rgba(12, 15, 28, 0.92);
  --bg-panel-solid: #101425;
  --border-ink: rgba(244, 211, 148, 0.24);
  --crimson: #e11d48;
  --crimson-bright: #ff3b5c;
  --gold: #f5c451;
  --gold-soft: #fde68a;
  --jade: #34d399;
  --ink-text: #f6f1e7;
  --ink-muted: #9ea7c0;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100%;
  background: radial-gradient(circle at 50% 0%, #151c36 0%, #090b16 65%, #05060c 100%);
  color: var(--ink-text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
  user-select: none;
}

.hidden {
  display: none !important;
}

#app-shell {
  width: 100%;
  max-width: 1040px;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 8px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Top Bar */
#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: rgba(11, 14, 26, 0.88);
  border: 1px solid var(--border-ink);
  border-radius: 10px;
  flex-wrap: wrap;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-emblem {
  font-size: 18px;
}

.brand-title {
  font-family: "Cinzel", Georgia, serif;
  font-weight: 900;
  font-size: 16px;
  letter-spacing: 0.08em;
  color: #f8f2e4;
  white-space: nowrap;
}

.accent-ko {
  color: var(--crimson-bright);
  text-shadow: 0 0 12px rgba(225, 29, 72, 0.55);
}

.pill-badge {
  font-family: "Press Start 2P", monospace;
  font-size: 9px;
  padding: 4px 7px;
  border-radius: 6px;
  background: rgba(225, 29, 72, 0.2);
  border: 1px solid rgba(255, 59, 92, 0.5);
  color: #fecdd3;
  white-space: nowrap;
}

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

.stat-pill {
  font-size: 12px;
  font-weight: 700;
  padding: 4px 8px;
  border-radius: 7px;
  background: rgba(245, 196, 81, 0.14);
  border: 1px solid rgba(245, 196, 81, 0.35);
  color: var(--gold-soft);
  white-space: nowrap;
}

.streak-pill {
  background: rgba(249, 115, 22, 0.15);
  border-color: rgba(251, 146, 60, 0.4);
  color: #fed7aa;
}

.icon-btn {
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: var(--ink-text);
  border-radius: 7px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease;
}

.icon-btn:hover {
  background: rgba(245, 196, 81, 0.18);
  border-color: rgba(245, 196, 81, 0.55);
}

.icon-btn:active {
  transform: scale(0.95);
}

/* Main Stage */
#stage-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
}

#canvas-frame {
  position: relative;
  width: 100%;
  max-width: 960px;
  aspect-ratio: 16 / 9;
  background: #060812;
  border: 2px solid rgba(245, 196, 81, 0.35);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 18px 46px rgba(0, 0, 0, 0.65), 0 0 28px rgba(225, 29, 72, 0.12);
}

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

/* Challenge Banner */
.challenge-banner {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(15, 10, 26, 0.94);
  border: 1px solid var(--gold);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 700;
  color: var(--gold-soft);
  max-width: 92%;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.55);
}

/* Overlays */
.stage-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: radial-gradient(circle at center, rgba(8, 10, 20, 0.58) 0%, rgba(5, 6, 14, 0.86) 100%);
  backdrop-filter: blur(3px);
  overflow-y: auto;
}

.overlay-card {
  width: 100%;
  max-width: 560px;
  max-height: 100%;
  overflow-y: auto;
  background: var(--bg-panel);
  border: 1px solid rgba(245, 196, 81, 0.38);
  border-radius: 14px;
  padding: 18px 20px;
  text-align: center;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.72);
}

.compact-card {
  max-width: 360px;
}

.modal-wide {
  max-width: 660px;
  text-align: left;
}

.kamon-tag {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.16em;
  color: var(--gold);
  text-transform: uppercase;
  margin-bottom: 4px;
}

.hero-title {
  font-family: "Cinzel", Georgia, serif;
  font-size: clamp(22px, 4vw, 38px);
  font-weight: 900;
  margin: 0 0 8px;
  letter-spacing: 0.08em;
  line-height: 1.08;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.8);
}

.hero-sub {
  font-size: 13px;
  line-height: 1.45;
  color: #dbe2f4;
  margin: 0 auto 14px;
  max-width: 470px;
}

.title-cta-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

#btn-start-2p {
  grid-column: 1 / -1;
}

.btn-primary, .btn-secondary, .btn-ghost, .btn-share, .btn-gold-sm {
  font-family: inherit;
  font-weight: 800;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.1s ease, filter 0.15s ease, border-color 0.15s ease;
}

.btn-primary {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 11px 12px;
  border: 1px solid transparent;
  color: #fff;
}

.btn-crimson {
  background: linear-gradient(135deg, #be123c 0%, #e11d48 55%, #9f1239 100%);
  border-color: rgba(255, 165, 180, 0.5);
  box-shadow: 0 6px 20px rgba(225, 29, 72, 0.4);
}

.btn-gold {
  background: linear-gradient(135deg, #b45309 0%, #d97706 55%, #92400e 100%);
  border-color: rgba(253, 230, 138, 0.55);
  box-shadow: 0 6px 18px rgba(217, 119, 6, 0.35);
}

.cta-main {
  font-size: 14px;
  letter-spacing: 0.04em;
}

.cta-sub {
  font-size: 11px;
  font-weight: 600;
  opacity: 0.9;
}

.btn-secondary {
  padding: 9px 14px;
  background: rgba(99, 102, 241, 0.22);
  border: 1px solid rgba(165, 180, 252, 0.45);
  color: #e0e7ff;
  font-size: 13px;
}

.btn-ghost {
  padding: 7px 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: #e5e9f5;
  font-size: 12px;
}

.btn-share {
  padding: 11px 14px;
  background: linear-gradient(135deg, #0f766e 0%, #10b981 100%);
  border: 1px solid rgba(167, 243, 208, 0.55);
  color: #041d17;
  font-size: 13px;
}

.btn-gold-sm {
  padding: 8px 12px;
  background: linear-gradient(135deg, #d97706, #f59e0b);
  border: 1px solid #fde68a;
  color: #1c1202;
  font-size: 12px;
  white-space: nowrap;
}

.btn-primary:hover, .btn-secondary:hover, .btn-ghost:hover, .btn-share:hover, .btn-gold-sm:hover {
  filter: brightness(1.12);
}

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

.title-meta-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.glint-legend {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px;
}

.glint-chip {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.16);
}

.glint-white { color: #f8fafc; border-color: rgba(248, 250, 252, 0.45); }
.glint-amber { color: #fcd34d; border-color: rgba(252, 211, 77, 0.5); }
.glint-red { color: #fb7185; border-color: rgba(251, 113, 133, 0.55); }

/* Result Card */
.result-heading, .modal-heading {
  font-family: "Cinzel", Georgia, serif;
  margin: 0 0 4px;
  font-size: clamp(18px, 3vw, 26px);
  letter-spacing: 0.06em;
}

.result-sub, .modal-sub {
  margin: 0 0 12px;
  font-size: 12px;
  color: var(--ink-muted);
}

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

.stat-box {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 7px 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-k {
  font-size: 9px;
  font-weight: 700;
  color: var(--ink-muted);
  letter-spacing: 0.06em;
}

.stat-v {
  font-size: 15px;
  font-weight: 900;
}

.gold-text { color: var(--gold); }
.crimson-text { color: #fda4af; }

/* Cliffhanger */
.cliffhanger-box {
  background: rgba(245, 196, 81, 0.09);
  border: 1px solid rgba(245, 196, 81, 0.34);
  border-radius: 9px;
  padding: 8px 10px;
  margin-bottom: 10px;
  text-align: left;
}

.cliff-top {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 700;
  color: var(--gold-soft);
  margin-bottom: 5px;
  gap: 8px;
}

.cliff-bar-track {
  height: 7px;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 999px;
  overflow: hidden;
}

.cliff-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #f59e0b, #fbbf24, #fde68a);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.lb-submit-row {
  display: flex;
  gap: 6px;
  margin-bottom: 4px;
}

#callsign-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(5, 7, 15, 0.8);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
}

.inline-feedback {
  font-size: 11px;
  min-height: 16px;
  color: var(--jade);
  margin-bottom: 8px;
  font-weight: 700;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}

.result-secondary-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.share-preview {
  margin: 8px 0 0;
  padding: 8px 10px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(52, 211, 153, 0.45);
  border-radius: 8px;
  font-size: 11px;
  color: #d1fae5;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
}

.modal-btn-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Scrolls & Ranks Modals */
.modal-header-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 10px;
}

.modal-right-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mini-close {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.22);
  color: #fff;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  font-weight: 800;
  cursor: pointer;
}

.scrolls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.scroll-item {
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 6px;
}

.scroll-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 800;
}

.scroll-lvl {
  font-size: 11px;
  color: var(--gold);
}

.scroll-desc {
  font-size: 11px;
  color: #cbd5e1;
  line-height: 1.35;
}

.scroll-buy-btn {
  padding: 6px 10px;
  border-radius: 7px;
  border: 1px solid rgba(245, 196, 81, 0.5);
  background: rgba(245, 196, 81, 0.16);
  color: var(--gold-soft);
  font-weight: 800;
  font-size: 11px;
  cursor: pointer;
}

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

.mastery-footer {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 11px;
  color: var(--ink-muted);
}

.mastery-footer strong {
  color: var(--gold-soft);
}

.ranks-table {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 290px;
  overflow-y: auto;
}

.rank-row {
  display: grid;
  grid-template-columns: 36px 1fr 90px 80px;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  font-size: 12px;
}

.rank-row.top-1 { border-color: rgba(245, 196, 81, 0.55); background: rgba(245, 196, 81, 0.1); }
.rank-num { font-weight: 900; color: var(--gold); }
.rank-name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-score { font-weight: 900; text-align: right; color: var(--gold-soft); }
.rank-wave { text-align: right; color: var(--ink-muted); font-size: 11px; }
.ranks-empty { text-align: center; padding: 24px 10px; color: var(--ink-muted); font-size: 13px; }

/* Touch Controls */
#touch-controls {
  width: 100%;
  max-width: 960px;
  display: none;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
}

.touch-left, .touch-right {
  display: grid;
  gap: 6px;
}

.touch-left {
  grid-template-columns: 1fr 1fr;
  width: 40%;
}

.touch-left .dash-btn {
  grid-column: 1 / -1;
}

.touch-right {
  grid-template-columns: 1fr 1fr;
  width: 58%;
}

.touch-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 52px;
  padding: 6px 4px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(18, 22, 40, 0.92);
  color: #fff;
  font-size: 17px;
  font-weight: 900;
  touch-action: none;
}

.touch-btn span {
  font-size: 10px;
  letter-spacing: 0.05em;
}

.touch-btn:active, .touch-btn.pressed {
  transform: scale(0.94);
  filter: brightness(1.25);
}

.parry-btn { background: linear-gradient(135deg, #b45309, #eab308); color: #1a1002; border-color: #fef08a; }
.quick-btn { background: linear-gradient(135deg, #be123c, #e11d48); border-color: #fda4af; }
.heavy-btn { background: linear-gradient(135deg, #9a3412, #ea580c); border-color: #fed7aa; }
.feint-btn { background: linear-gradient(135deg, #4338ca, #7c3aed); border-color: #ddd6fe; }
.dash-btn { background: linear-gradient(135deg, #0f766e, #0ea5e9); border-color: #bae6fd; min-height: 44px; flex-direction: row; gap: 6px; }

/* Footer Bar */
.footer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 6px 10px;
  background: rgba(10, 12, 24, 0.86);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 9px;
  font-size: 11px;
  color: var(--ink-muted);
}

.controls-hint {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.controls-hint strong {
  color: var(--gold-soft);
}

/* Mobile Responsive (390x844 verified 0px overflow) */
@media (max-width: 820px), (pointer: coarse) {
  #touch-controls {
    display: flex;
  }
}

@media (max-width: 640px) {
  #app-shell {
    padding: 6px 6px 8px;
    gap: 6px;
  }
  #top-bar {
    padding: 5px 7px;
  }
  .brand-title {
    font-size: 13px;
  }
  .btn-label {
    display: none;
  }
  .pill-badge {
    font-size: 8px;
    padding: 3px 5px;
  }
  .stat-pill {
    font-size: 11px;
    padding: 3px 6px;
  }
  .icon-btn {
    padding: 5px 7px;
    font-size: 12px;
  }
  #canvas-frame {
    aspect-ratio: 16 / 10;
    border-radius: 9px;
  }
  .stage-overlay {
    padding: 6px;
    align-items: flex-start;
  }
  .overlay-card {
    padding: 12px 12px;
    border-radius: 10px;
  }
  .hero-title {
    font-size: 20px;
  }
  .hero-sub {
    font-size: 11px;
    margin-bottom: 10px;
  }
  .title-cta-grid {
    grid-template-columns: 1fr;
    gap: 6px;
  }
  .result-stats-grid {
    grid-template-columns: 1fr 1fr;
  }
  .result-actions {
    grid-template-columns: 1fr;
  }
  .scrolls-grid {
    grid-template-columns: 1fr;
  }
  .controls-hint {
    display: none;
  }
  .footer-bar {
    justify-content: center;
    padding: 6px;
  }
}
