/* Tiny Kingdom — styles. Cute fantasy / premium cartoon, warm medieval
 * palette. Mobile-first: designed at 375px, scales up. */

:root {
  --ink: #2b2140;
  --purple-dark: #241a35;
  --cream: #fff6e6;
  --gold: #ffd24a;
  --hp-green: #5ed36b;
  --hp-yellow: #ffcf4d;
  --hp-red: #ff5b5b;
  --panel-bg: rgba(36, 26, 53, 0.94);
  --panel-border: #6a5a90;
  --accent: #e05252;
  --stage-w: 960;
  --stage-h: 540;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: linear-gradient(180deg, #1b1330, #2b2049);
  color: var(--cream);
  font-family: 'Baloo 2', 'Trebuchet MS', system-ui, -apple-system, sans-serif;
  min-height: 100%;
  overscroll-behavior: none;
}

a { color: var(--gold); }

.site-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 8px 24px;
  gap: 10px;
}

.site-header {
  width: 100%;
  max-width: 960px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px;
}

.brand {
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 0.5px;
  color: var(--cream);
  text-decoration: none;
  opacity: 0.85;
}
.brand b { color: var(--gold); }

.ad-slot {
  width: 100%;
  max-width: 960px;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255,246,230,0.4);
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  border: 1px dashed rgba(255,246,230,0.2);
  border-radius: 10px;
  background: rgba(255,255,255,0.03);
}

#game-stage {
  position: relative;
  width: 100%;
  max-width: 960px;
  aspect-ratio: var(--stage-w) / var(--stage-h);
  background: #8fd3ff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 18px 50px rgba(0,0,0,0.45), 0 0 0 4px rgba(255,255,255,0.08) inset;
  touch-action: none;
}
#game-stage:fullscreen, #game-stage:-webkit-full-screen {
  max-width: 100vw;
  width: 100vw;
  height: 100vh;
  aspect-ratio: auto;
  border-radius: 0;
}

/* #canvas-wrap holds the canvas + #banner + .icon-row together so those
 * two overlays can anchor to the CANVAS's own box rather than the whole
 * #game-stage — needed because in portrait mode (see media query below)
 * the stage grows taller than the canvas (HUD/build-panel stack below it
 * in normal flow), and without this wrapper the banner/icon-row would
 * drift to the wrong spot relative to actual gameplay. */
#canvas-wrap {
  position: absolute;
  inset: 0;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ---------- persistent top-right icon row (never overlapping) ---------- */
.icon-row {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 8px;
  z-index: 30;
}
.icon-btn {
  width: 38px;
  height: 38px;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(36,26,53,0.72);
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 10px;
  color: var(--cream);
  cursor: pointer;
  padding: 0;
}
.icon-btn:hover { background: rgba(36,26,53,0.9); }
.icon-btn svg { display: block; width: 20px; height: 20px; }
.icon-btn svg[hidden] { display: none; }

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 20;
  background: linear-gradient(180deg, rgba(20,14,32,0.55), rgba(20,14,32,0));
}
.hud-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
/* The persistent top-right icon row (.icon-row, fullscreen/sound/music)
 * is a separate absolutely-positioned overlay that lives in the same
 * top-right corner as this row — reserve its full width here so HUD
 * content can never wrap underneath and get hidden behind those opaque
 * buttons (real bug caught in this project's own mobile screenshot
 * review: resource pills were rendering invisibly under the icon
 * buttons at narrow widths). Only this top row needs the reservation —
 * .hud-row-stats below it is always clear of the icon row's ~54px
 * vertical band since it stacks underneath in the column layout. */
.hud-row-top { padding-right: 160px; }
.hp-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(20,14,32,0.55);
  border-radius: 10px;
  padding: 4px 8px;
  flex: 1 1 140px;
  min-width: 120px;
}
.hp-bar {
  flex: 1;
  height: 12px;
  background: rgba(0,0,0,0.4);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.2);
}
.hp-fill { height: 100%; background: var(--hp-green); transition: width 0.2s ease, background 0.2s ease; }
.hp-text { font-size: 11px; font-weight: 700; white-space: nowrap; }
.stat-pill {
  display: flex;
  align-items: center;
  gap: 3px;
  background: rgba(20,14,32,0.55);
  border-radius: 10px;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.hud-right {
  margin-left: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}
.wave-pill, .score-pill {
  background: rgba(20,14,32,0.6);
  border-radius: 10px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 800;
  text-align: right;
}
.wave-pill span, .score-pill span { color: var(--gold); }

/* ---------- build panel ---------- */
#build-panel {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: flex;
  gap: 6px;
  padding: 8px;
  overflow-x: auto;
  z-index: 20;
  background: linear-gradient(0deg, rgba(20,14,32,0.7), rgba(20,14,32,0));
  scrollbar-width: thin;
}
.build-card {
  flex: 0 0 auto;
  min-width: 76px;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 6px 8px;
  background: rgba(36,26,53,0.85);
  border: 2px solid rgba(255,255,255,0.15);
  border-radius: 10px;
  color: var(--cream);
  cursor: pointer;
  font-family: inherit;
}
.build-card.afford { border-color: var(--gold); }
.build-card:disabled { opacity: 0.45; cursor: not-allowed; }
.build-card.maxed { opacity: 0.6; border-color: #6affb0; }
.bc-icon { font-size: 18px; }
.bc-name { font-size: 10px; font-weight: 700; white-space: nowrap; }
.bc-level { font-size: 9px; opacity: 0.85; }
.bc-cost { font-size: 10px; font-weight: 800; color: var(--gold); white-space: nowrap; }
.build-card.recruit .bc-cost { color: #a6d8ff; }

/* ---------- banner ---------- */
#banner {
  position: absolute;
  top: 44%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.7);
  font-size: 28px;
  font-weight: 900;
  color: var(--gold);
  text-shadow: 0 3px 0 rgba(0,0,0,0.5), 0 0 20px rgba(255,210,74,0.6);
  z-index: 40;
  pointer-events: none;
  opacity: 0;
  white-space: nowrap;
}
#banner.play { animation: bannerPop 1.1s ease forwards; }
#banner.combo { color: #ff9d4a; }
#banner.perfect { color: #6affb0; }
#banner.clear { color: #a6d8ff; }
#banner.boss { color: #ff5b5b; font-size: 24px; }
#banner.upgrade { color: #a6ffb0; font-size: 20px; }
@keyframes bannerPop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.08); }
  25% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1); }
}

/* ---------- generic screen/modal ---------- */
/* position:fixed (viewport-relative), not absolute (stage-relative): on a
 * narrow portrait phone #game-stage's locked 960:540 aspect ratio makes it
 * quite short (e.g. ~220px tall at 390px wide), which isn't enough room
 * for a modal's real content — a modal fixed to the *stage* box would
 * clip itself. Fixing to the viewport instead gives every modal the full
 * screen height to lay out and scroll within, independent of how short
 * the gameplay canvas area happens to be. */
.screen {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: radial-gradient(circle at 50% 30%, rgba(60,40,90,0.55), rgba(15,10,25,0.86));
  backdrop-filter: blur(2px);
}
.screen[hidden] { display: none !important; }
.panel {
  width: 100%;
  max-width: 420px;
  max-height: min(600px, 92vh);
  overflow-y: auto;
  background: var(--panel-bg);
  border: 3px solid var(--panel-border);
  border-radius: 18px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}
.title-lg {
  font-size: 30px;
  font-weight: 900;
  letter-spacing: 2px;
  color: var(--gold);
  text-shadow: 0 3px 0 rgba(0,0,0,0.4);
  margin: 0 0 4px;
}
.title-sm { font-size: 13px; opacity: 0.75; margin: 0 0 18px; }
.castle-glyph { font-size: 44px; margin-bottom: 4px; }

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 48px;
  margin: 8px 0;
  padding: 10px 16px;
  border: none;
  border-radius: 12px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  color: var(--purple-dark);
  background: var(--gold);
  box-shadow: 0 4px 0 #b98a1c;
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #b98a1c; }
.btn.secondary { background: rgba(255,255,255,0.12); color: var(--cream); box-shadow: 0 4px 0 rgba(0,0,0,0.3); }
.btn.ghost { background: transparent; color: var(--cream); box-shadow: none; border: 2px solid rgba(255,255,255,0.25); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.menu-best {
  margin: 6px 0 16px;
  font-size: 12px;
  color: #cbb8ff;
  min-height: 16px;
}

/* decision / event cards */
.decision-grid, #event-choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}
.decision-card {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 12px;
  min-height: 44px;
  border-radius: 12px;
  border: 2px solid rgba(255,255,255,0.18);
  background: rgba(255,255,255,0.06);
  color: var(--cream);
  cursor: pointer;
  font-family: inherit;
}
.decision-card:hover { border-color: var(--gold); }
.dc-icon { font-size: 24px; }
.dc-label { font-weight: 800; display: block; }
.dc-desc { font-size: 11px; opacity: 0.75; display: block; }
.event-btn {
  min-height: 44px;
  border-radius: 12px;
  border: none;
  background: var(--gold);
  color: var(--purple-dark);
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
}
#event-icon { font-size: 44px; color: var(--gold); }
.dc-icon { color: var(--gold); }
#event-title { margin: 4px 0; color: var(--gold); }
#event-text { opacity: 0.85; font-size: 14px; margin-bottom: 6px; }

/* game over */
.go-stats { display: flex; justify-content: center; gap: 16px; margin: 10px 0 4px; }
.go-stat { text-align: center; }
.go-stat b { display: block; font-size: 20px; color: var(--gold); }
.go-stat span { font-size: 10px; opacity: 0.7; text-transform: uppercase; letter-spacing: 0.5px; }
.new-best-tag {
  color: #6affb0;
  font-weight: 900;
  font-size: 13px;
  margin: 4px 0 10px;
  animation: pulse 1s ease infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }
#nickname-input {
  width: 100%;
  min-height: 44px;
  border-radius: 10px;
  border: 2px solid var(--panel-border);
  background: rgba(255,255,255,0.08);
  color: var(--cream);
  font-family: inherit;
  font-size: 15px;
  text-align: center;
  padding: 8px;
  margin-top: 10px;
}
.submit-status { min-height: 18px; font-size: 12px; margin-top: 6px; }
.submit-status.error { color: var(--hp-red); }
.submit-status.ok { color: #6affb0; }

/* leaderboard */
.lb-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.lb-tab {
  flex: 1;
  min-height: 40px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.05);
  color: var(--cream);
  font-family: inherit;
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.5px;
  cursor: pointer;
}
.lb-tab.active { background: var(--gold); color: var(--purple-dark); border-color: var(--gold); }
#lb-list { display: flex; flex-direction: column; gap: 4px; max-height: 46vh; overflow-y: auto; margin-bottom: 8px; }
.lb-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,0.04);
  font-size: 13px;
}
.lb-row.you { background: rgba(255,210,74,0.18); border: 1px solid var(--gold); }
.lb-rank { width: 34px; font-weight: 800; color: var(--gold); }
.lb-name { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { font-weight: 800; }
.lb-empty { padding: 20px; opacity: 0.6; font-size: 13px; }
#lb-your-rank { font-size: 12px; color: var(--gold); margin-bottom: 8px; font-weight: 700; }
.lb-pager { display: flex; align-items: center; justify-content: center; gap: 12px; }
.lb-pager button { min-width: 44px; min-height: 40px; }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 48px;
  padding: 8px 4px;
}

@media (max-width: 480px) {
  .title-lg { font-size: 24px; }
  .stat-pill, .hp-text { font-size: 10px; }
  .bc-name { display: none; }
  .build-card { min-width: 56px; padding: 6px; }
}

/* ---------- inline SVG icons (replace emoji — see js/icons.js) ---------- */
.ui-icon { display: inline-block; vertical-align: -4px; color: currentColor; }
.icon-lg { width: 44px; height: 44px; vertical-align: -12px; }

/* ---------- portrait phones: stack everything vertically instead of
 * overlaying a very short, aspect-locked canvas ----------------------------
 * On a narrow PORTRAIT phone, locking #game-stage to the 960:540 desktop
 * aspect ratio makes it quite short (~220px tall at 390px wide) — too
 * short for the HUD + build panel to overlay on top of the canvas without
 * clipping or crowding (this project's own screenshot review caught
 * exactly that in an earlier round). Rather than distorting the game art
 * to fill a taller box, the canvas keeps its correct 16:9 proportions and
 * the HUD/build panel move into normal document flow BELOW it, so the
 * whole page simply grows taller and everything stays legible — matching
 * the user's own suggested fix. Landscape (phone rotated) and desktop are
 * completely unaffected: the canvas overlay layout above is unchanged
 * there. Only the persistent icon row (fullscreen/sound/music) stays
 * overlaid on the canvas corner in both orientations, switching to a
 * vertical stack in portrait since there's no longer a horizontal HUD
 * row sharing that corner to reserve space against. */
@media (orientation: portrait) {
  #game-stage {
    aspect-ratio: auto;
    height: auto;
    display: flex;
    flex-direction: column;
  }
  /* Back in flow (was position:absolute;inset:0 for the landscape overlay
   * layout) so it takes its place as the first item in #game-stage's flex
   * column, sized to exactly wrap the canvas below — #banner/.icon-row
   * stay position:absolute and now anchor to THIS box instead of the
   * whole (much taller) stage, keeping them correctly placed over
   * gameplay instead of drifting down into the HUD/build-panel area. */
  #canvas-wrap {
    position: relative;
    inset: auto;
    width: 100%;
    flex: none;
  }
  #game-canvas {
    position: static;
    width: 100%;
    height: auto;
    aspect-ratio: 960 / 540;
    display: block;
    flex: none;
  }
  .icon-row { flex-direction: column; }
  #hud, #build-panel {
    position: static;
    background: rgba(20, 14, 32, 0.5);
  }
  #hud { padding: 10px; }
  .hud-row-top { padding-right: 0; }
  #build-panel {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    overflow: visible;
    padding: 10px;
  }
  .build-card { min-width: 0; }
  .bc-name { display: block; }
  #banner { position: absolute; }
}
