* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #1a0f2e;
  --bg2: #2d1b4e;
  --fg: #ffe8a3;
  --accent: #ff6b9d;
  --accent2: #4ecdc4;
  --gold: #ffd700;
  --panel: rgba(20, 12, 40, 0.94);
  --border: #ff9f43;
  --muted: #b8a4d4;
  --ok: #2ed573;
  --locked: #666;
  --font: "Segoe UI", system-ui, -apple-system, "Trebuchet MS", sans-serif;
  --font-game: "Courier New", Courier, monospace;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

html {
  height: 100%;
  height: 100dvh;
  background: var(--bg);
}

html, body {
  min-height: 100%;
  min-height: 100dvh;
  background: radial-gradient(ellipse at 50% 0%, #3d2066 0%, var(--bg) 55%, #0d0818 100%);
  font-family: var(--font);
  color: var(--fg);
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  padding: 16px 8px;
  -webkit-tap-highlight-color: transparent;
}

body.night {
  background: radial-gradient(ellipse at 50% 0%, #0a1628 0%, #050810 60%, #000 100%);
}

#wrap {
  text-align: center;
  max-width: 860px;
  width: 100%;
  padding: 8px;
  position: relative;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
  min-width: 0;
}

h1 {
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: linear-gradient(90deg, #ff9f43, #ff6b9d, #4ecdc4);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: none;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.4));
  flex-shrink: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta-hud {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
  min-width: 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
  background: rgba(0,0,0,0.35);
  border: 1px solid rgba(255,159,67,0.5);
  color: var(--fg);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.coin-chip b { color: var(--gold); }
.title-chip { color: var(--accent2); border-color: rgba(78,205,196,0.5); }
.reward-chip { color: #b388ff; border-color: rgba(179,136,255,0.55); }
.reward-chip b { color: #e1bee7; }
.skin-card.can-buy { outline: 2px solid #b388ff; opacity: 1; }
.skins-rp-note { font-family: monospace; }

.canvas-shell {
  position: relative;
  display: inline-block;
  max-width: 100%;
  border-radius: 10px;
  box-shadow:
    0 0 0 3px var(--border),
    0 0 24px rgba(255, 107, 157, 0.35),
    0 12px 40px rgba(0,0,0,0.45);
  overflow: hidden;
}

#game {
  display: block;
  width: min(1000px, 100vw - 24px);
  height: auto;
  aspect-ratio: 800 / 450;
  background: #ffd4a3;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: pointer;
  touch-action: none;
  vertical-align: top;
}

.toolbar {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.btn {
  font-family: var(--font);
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.04em;
  padding: 8px 14px;
  border-radius: 8px;
  border: 2px solid var(--border);
  background: linear-gradient(180deg, #5a3a8a 0%, #3a2060 100%);
  color: var(--fg);
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s, filter 0.15s;
  box-shadow: 0 3px 0 #2a1540;
}

.btn:hover {
  filter: brightness(1.12);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #2a1540;
}

.btn.primary {
  background: linear-gradient(180deg, #ff9f43 0%, #e67e22 100%);
  border-color: #ffd700;
  color: #2a1500;
  box-shadow: 0 3px 0 #a85a10;
}

.btn.active {
  outline: 2px solid var(--accent2);
  outline-offset: 2px;
}

.btn-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}

.btn-close:hover { color: var(--accent); }

.hint {
  margin-top: 10px;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
}

/* Touch bar — hidden on desktop / fine pointer */
.touch-bar {
  display: none;
  gap: 12px;
  margin-top: 10px;
  padding: 0 4px;
  justify-content: stretch;
  width: 100%;
}

.touch-btn {
  flex: 1;
  font-family: var(--font);
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 14px 12px;
  min-height: 48px;
  border-radius: 12px;
  border: 2px solid var(--border);
  color: var(--fg);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  box-shadow: 0 4px 0 #2a1540;
}

.touch-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #2a1540;
}

.touch-jump {
  background: linear-gradient(180deg, #ff9f43 0%, #e67e22 100%);
  border-color: #ffd700;
  color: #2a1500;
  box-shadow: 0 4px 0 #a85a10;
}

.touch-duck {
  background: linear-gradient(180deg, #4ecdc4 0%, #2a9d8f 100%);
  border-color: #7ee8e0;
  color: #062a28;
  box-shadow: 0 4px 0 #1a6b62;
}

/* Panels */
.panel {
  margin: 14px auto 0;
  max-width: 800px;
  text-align: left;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  animation: panelIn 0.2s ease-out;
}

.panel.hidden { display: none; }

@keyframes panelIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: none; }
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.panel-head h2 {
  font-size: 1.05rem;
  color: var(--gold);
  letter-spacing: 0.06em;
}

.panel-note {
  margin-top: 10px;
  font-size: 0.75rem;
  color: var(--muted);
}

.goals-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 340px;
  overflow-y: auto;
}

.goal-item {
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,159,67,0.25);
}

.goal-item.unlocked {
  border-color: rgba(46,213,115,0.55);
  background: rgba(46,213,115,0.08);
}

.goal-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
  margin-bottom: 6px;
}

.goal-name {
  font-weight: 700;
  font-size: 0.9rem;
}

.goal-reward {
  font-size: 0.72rem;
  color: var(--accent2);
}

.goal-status {
  font-size: 0.72rem;
  font-weight: 700;
}

.goal-status.done { color: var(--ok); }
.goal-status.lock { color: var(--locked); }

.bar {
  height: 8px;
  border-radius: 999px;
  background: rgba(0,0,0,0.35);
  overflow: hidden;
}

.bar > i {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent2), var(--gold));
  width: 0%;
  transition: width 0.25s ease;
}

.goal-progress-text {
  margin-top: 4px;
  font-size: 0.7rem;
  color: var(--muted);
  font-family: var(--font-game);
}

.skins-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.skin-card {
  padding: 12px;
  border-radius: 10px;
  border: 2px solid rgba(255,159,67,0.3);
  background: rgba(0,0,0,0.25);
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s, transform 0.1s;
}

.skin-card:hover:not(.locked) {
  border-color: var(--accent2);
  transform: translateY(-2px);
}

.skin-card.equipped {
  border-color: var(--gold);
  box-shadow: 0 0 12px rgba(255,215,0,0.35);
}

.skin-card.locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.skin-swatch {
  width: 36px;
  height: 36px;
  margin: 0 auto 8px;
  border-radius: 8px;
  border: 2px solid rgba(255,255,255,0.3);
}
.skin-preview {
  display: block;
  width: 72px;
  height: 56px;
  margin: 0 auto 8px;
  border-radius: 8px;
  border: 2px solid rgba(255,255,255,0.25);
  image-rendering: pixelated;
  background: #1a1208;

  background: #f0d5a8;
  border-radius: 6px;
}

.skin-card h3 {
  font-size: 0.85rem;
  margin-bottom: 4px;
}

.skin-card p {
  font-size: 0.7rem;
  color: var(--muted);
}

.board-list {
  list-style: none;
  font-family: var(--font-game);
  font-size: 0.85rem;
}

.board-list li {
  display: grid;
  grid-template-columns: 36px 1fr 80px 64px;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  align-items: center;
}

.board-list li:nth-child(odd) {
  background: rgba(255,255,255,0.04);
}

.board-list li.pb {
  background: rgba(255,215,0,0.15);
  border: 1px solid rgba(255,215,0,0.4);
}

.board-list .rank { color: var(--gold); font-weight: 700; }
.board-list .empty { color: var(--muted); grid-column: 1 / -1; text-align: center; padding: 16px; }

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(8, 4, 20, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding: max(16px, var(--safe-t)) max(16px, var(--safe-r)) max(16px, var(--safe-b)) max(16px, var(--safe-l));
  animation: panelIn 0.2s ease-out;
}

.modal.hidden { display: none; }

.modal-card {
  background: var(--panel);
  border: 2px solid var(--gold);
  border-radius: 14px;
  padding: 20px 22px;
  max-width: 360px;
  width: 100%;
  text-align: left;
  box-shadow: 0 0 40px rgba(255,107,157,0.3);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}

.modal-card h2 {
  color: var(--gold);
  margin-bottom: 8px;
  font-size: 1.2rem;
}

.modal-sub {
  color: var(--accent2);
  font-family: var(--font-game);
  margin-bottom: 14px;
  font-size: 0.9rem;
}

.modal-card label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.modal-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 2px solid var(--border);
  background: #120a22;
  color: var(--fg);
  font-size: 1rem;
  font-family: var(--font-game);
  margin-bottom: 14px;
  outline: none;
}

.modal-card input:focus {
  border-color: var(--accent2);
}

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

.summary-stats {
  list-style: none;
  margin: 12px 0;
  font-family: var(--font-game);
  font-size: 0.9rem;
}

.summary-stats li {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.summary-unlocks {
  margin-bottom: 14px;
  font-size: 0.85rem;
  color: var(--ok);
  min-height: 1.2em;
}

.summary-card .btn { width: 100%; }

body.night h1 {
  background: linear-gradient(90deg, #7ec8ff, #c792ea, #ff6b9d);
  -webkit-background-clip: text;
  background-clip: text;
}

.diff-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: 4px;
  font-size: 12px;
  color: var(--muted, #a89bb8);
}
.diff-select {
  appearance: none;
  border: 1px solid rgba(255,255,255,0.18);
  background: rgba(20,12,40,0.85);
  color: #f5eefc;
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  cursor: pointer;
}
.diff-select:focus {
  outline: 2px solid var(--accent, #ff6b9d);
  outline-offset: 1px;
}

/* ==========================================================================
   Phone / narrow / coarse pointer — maximize playfield
   ========================================================================== */
@media (max-width: 900px), (pointer: coarse) {
  html, body {
    padding: 0;
    height: 100%;
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    align-items: stretch;
    justify-content: stretch;
  }

  body {
    display: flex;
    flex-direction: column;
  }

  #wrap {
    max-width: none;
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-height: 100dvh;
    padding:
      max(4px, var(--safe-t))
      max(8px, var(--safe-r))
      max(4px, var(--safe-b))
      max(8px, var(--safe-l));
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  .top-bar {
    margin-bottom: 4px;
    gap: 6px;
    flex-wrap: nowrap;
    flex-shrink: 0;
  }

  h1 {
    font-size: 1rem;
    letter-spacing: 0.04em;
  }

  .chip {
    font-size: 0.72rem;
    padding: 3px 8px;
  }

  .canvas-shell {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    box-shadow:
      0 0 0 2px var(--border),
      0 0 16px rgba(255, 107, 157, 0.3);
  }

  /* Portrait: fill WIDTH, keep aspect, cap by shell height */
  #game {
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 800 / 450;
  }

  .toolbar {
    margin-top: 6px;
    gap: 4px;
    flex-shrink: 0;
    padding-bottom: max(2px, env(safe-area-inset-bottom, 0px));
  }

  .btn {
    font-size: 0.72rem;
    padding: 6px 8px;
  }

  .diff-wrap {
    font-size: 0.7rem;
    margin-left: 0;
  }

  .diff-select {
    padding: 4px 8px;
    font-size: 0.72rem;
  }

  .hint {
    display: none;
  }

  .touch-bar {
    display: flex;
    flex-shrink: 0;
    margin-top: 6px;
  }

  .panel {
    position: fixed;
    left: max(8px, var(--safe-l));
    right: max(8px, var(--safe-r));
    top: max(8px, var(--safe-t));
    bottom: max(8px, var(--safe-b));
    margin: 0;
    max-width: none;
    z-index: 50;
    overflow-y: auto;
    max-height: none;
  }

  .goals-list {
    max-height: none;
  }
}

/* Very small / phone width — no rounded playfield chrome */
@media (max-width: 520px) {
  h1 { font-size: 0.95rem; }
  .canvas-shell { border-radius: 0; }
  .board-list li {
    grid-template-columns: 28px 1fr 64px 52px;
    font-size: 0.75rem;
  }
}

/* Short screens — hide hint even on desktop-ish widths */
@media (max-height: 700px) {
  .hint { display: none; }
}

/* ==========================================================================
   Landscape phone — collapse chrome, maximize canvas height
   ========================================================================== */
@media (orientation: landscape) and (max-height: 500px) {
  html, body {
    padding: 0;
    height: 100%;
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
  }

  body {
    display: flex;
    flex-direction: column;
  }

  #wrap {
    max-width: none;
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-height: 100dvh;
    padding:
      max(2px, var(--safe-t))
      max(10px, var(--safe-r))
      max(2px, var(--safe-b))
      max(10px, var(--safe-l));
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  /* Ultra-compact single-row header OR hide title to free height */
  .top-bar {
    margin-bottom: 2px;
    gap: 6px;
    flex-wrap: nowrap;
    min-height: 0;
    flex-shrink: 0;
    padding: 0 2px;
  }

  /* Hide big title — frees vertical space; chips stay for coins/title */
  .top-bar h1 {
    display: none;
  }

  .meta-hud {
    width: 100%;
    justify-content: flex-end;
    gap: 6px;
  }

  .chip {
    font-size: 0.68rem;
    padding: 2px 7px;
  }

  .canvas-shell {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0;
    box-shadow: 0 0 0 1px rgba(255,159,67,0.55);
  }

  /* Landscape: fill HEIGHT, keep aspect, cap by shell width */
  #game {
    height: 100%;
    width: auto;
    max-height: 100%;
    max-width: 100%;
    aspect-ratio: 800 / 450;
  }

  .hint {
    display: none !important;
  }

  /* Slim bottom toolbar row */
  .toolbar {
    margin-top: 2px;
    gap: 3px;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    padding: 0 2px max(2px, var(--safe-b));
    flex-shrink: 0;
  }

  .btn {
    font-size: 0.65rem;
    padding: 4px 7px;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .diff-wrap {
    font-size: 0.65rem;
    white-space: nowrap;
  }

  .diff-select {
    padding: 3px 6px;
    font-size: 0.65rem;
  }

  /* Compact side-by-side touch controls */
  .touch-bar {
    display: flex;
    flex-shrink: 0;
    margin-top: 2px;
    gap: 8px;
    max-width: 280px;
    align-self: center;
  }

  .touch-btn {
    padding: 8px 10px;
    min-height: 40px;
    font-size: 0.85rem;
    border-radius: 8px;
  }
}

/* Body class hooks from game.js (extra specificity / standalone PWA) */
body.phone {
  /* inherited by media queries; hooks for future tweaks */
}

body.phone.landscape .top-bar h1 {
  display: none;
}

body.phone.landscape .hint {
  display: none !important;
}

body.phone.portrait #game {
  width: 100%;
  height: auto;
  max-height: 100%;
}

body.phone.landscape #game {
  height: 100%;
  width: auto;
  max-width: 100%;
}

/* Standalone PWA (no browser chrome) — even more playfield */
@media (display-mode: standalone), (display-mode: fullscreen) {
  html, body {
    padding: 0;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }

  #wrap {
    max-width: none;
    height: 100%;
    height: 100dvh;
    padding:
      max(4px, var(--safe-t))
      max(8px, var(--safe-r))
      max(4px, var(--safe-b))
      max(8px, var(--safe-l));
  }

  .hint { display: none; }
}

@media (display-mode: standalone) and (orientation: landscape),
       (display-mode: fullscreen) and (orientation: landscape) {
  .top-bar h1 { display: none; }

  #wrap {
    padding:
      max(2px, var(--safe-t))
      max(12px, var(--safe-r))
      max(2px, var(--safe-b))
      max(12px, var(--safe-l));
  }

  #game {
    height: 100%;
    width: auto;
    max-width: 100%;
  }
}

/* Fullscreen: cover the whole display — no letterbox bars, keep 16:9 proportions */
.canvas-shell:fullscreen,
.canvas-shell:-webkit-full-screen {
  width: 100vw;
  width: 100dvw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  border-radius: 0;
  display: block;
  background: #ffd4a3;
  box-shadow: none;
  overflow: hidden;
}
body.night .canvas-shell:fullscreen,
body.night .canvas-shell:-webkit-full-screen {
  background: #0a1028;
}
.canvas-shell:fullscreen #game,
.canvas-shell:-webkit-full-screen #game {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  max-width: none;
  max-height: none;
  aspect-ratio: auto;
  object-fit: cover; /* fill screen, no letterbox bars */
  object-position: top center; /* keep HUD; crop bottom when aspect differs */
}
body.is-fs {
  padding: 0;
  overflow: hidden;
}
body.is-fs .canvas-shell {
  box-shadow: none;
  position: relative;
}
#btnFullscreen.active {
  outline: 2px solid var(--accent2);
  outline-offset: 2px;
}

#btnInstall:not([hidden]) {
  background: linear-gradient(135deg, #ff4d6d, #c77dff);
  color: #fff;
  border-color: transparent;
  font-weight: 700;
}
