:root {
  --bg: #0a0a14;
  --surface: #10101c;
  --surface2: #16162a;
  --border: #1e1e38;
  --text: #d0d0e8;
  --text-dim: #6868a0;
  --text-bright: #f0f0ff;
  --mana: #4488ff;
  --mana-glow: #4488ff40;
  --danger: #ff4466;
  --success: #44ff88;
  --ruby: #ff3355;
  --topaz: #ff8833;
  --citrine: #ffdd33;
  --peridot: #88ff33;
  --emerald: #33ff88;
  --aquamarine: #33ddff;
  --sapphire: #3355ff;
  --amethyst: #aa33ff;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

#app { width: 100%; height: 100%; position: relative; overflow: hidden; }

/* Screens */
.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  opacity: 0; pointer-events: none;
  transition: opacity 0.4s ease;
}
.screen.active { opacity: 1; pointer-events: auto; }

/* Menu */
#screen-menu { justify-content: center; align-items: center; padding: 2rem; }
.menu-bg {
  position: absolute; inset: 0; pointer-events: none;
  background:
    radial-gradient(ellipse at 30% 20%, var(--sapphire) 0%, transparent 50%),
    radial-gradient(ellipse at 70% 80%, var(--amethyst) 0%, transparent 50%),
    radial-gradient(ellipse at 50% 50%, var(--surface) 0%, var(--bg) 100%);
  opacity: 0.15;
}
.menu-content {
  position: relative; z-index: 1;
  display: flex; flex-direction: column; align-items: center; gap: 1rem;
  max-width: 320px; width: 100%;
}
.title {
  font-size: 2.5rem; font-weight: 200; letter-spacing: 0.08em;
  color: var(--text-bright);
  text-shadow: 0 0 40px var(--sapphire), 0 0 80px var(--amethyst);
}
.title-daily {
  display: block; font-size: 1rem; font-weight: 600;
  letter-spacing: 0.3em; text-transform: uppercase;
  color: var(--mana);
}
.puzzle-num { font-size: 0.85rem; color: var(--text-dim); }
.streak-display { font-size: 1.1rem; color: var(--citrine); min-height: 1.5em; }
.yesterday-score {
  margin-top: 1rem; font-size: 0.8rem; color: var(--text-dim);
  text-align: center; min-height: 2em;
}

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0.75rem 1.5rem; border: none; border-radius: 0.5rem;
  font: inherit; font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: all 0.15s ease;
  min-height: 44px; min-width: 44px;
}
.btn:active { transform: scale(0.97); filter: brightness(0.9); }
.btn-primary {
  background: linear-gradient(135deg, var(--sapphire), var(--amethyst));
  color: white; width: 100%;
}
.btn-secondary {
  background: var(--surface2); color: var(--text); width: 100%;
  border: 1px solid var(--border);
}
.btn-accent {
  background: linear-gradient(135deg, var(--mana), #6644ff);
  color: white; font-size: 0.9rem; padding: 0.5rem 1rem;
  white-space: nowrap;
}
.btn-small {
  padding: 0.4rem 0.8rem; font-size: 0.8rem;
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border);
}
.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn:disabled { opacity: 0.4; pointer-events: none; }

/* Game Screen */
#screen-game { background: var(--bg); }

#hud {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.4rem 0.75rem;
  padding-top: calc(0.4rem + var(--safe-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem; z-index: 10; flex-shrink: 0;
}
.hud-left, .hud-center, .hud-right { display: flex; align-items: center; gap: 0.75rem; }
.hud-item { display: flex; align-items: center; gap: 0.3rem; }
.hud-item b { color: var(--text-bright); }
.mana-icon { width: 14px; height: 14px; }
.mana b { color: var(--mana); font-size: 1.05rem; }
.enemy-count:empty { display: none; }
.hud-btn {
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); font: inherit; font-size: 0.8rem;
  width: 36px; height: 36px; border-radius: 0.4rem;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.hud-btn.active { background: var(--mana); color: white; border-color: var(--mana); }

#game-canvas { flex: 1; width: 100%; touch-action: none; display: block; }

/* Forge Panel */
#forge-panel {
  flex-shrink: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 0.4rem 0.6rem;
  padding-bottom: calc(0.4rem + var(--safe-bottom));
  display: flex; flex-direction: column; gap: 0.3rem;
}
.forge-info {
  font-size: 0.7rem; color: var(--text-dim);
  padding: 0 0.15rem; line-height: 1.2;
  min-height: 1em;
}
#forge-row {
  display: flex; align-items: stretch; gap: 0.5rem;
}
#gem-tray {
  display: flex; gap: 0.35rem; overflow-x: auto; flex: 1;
  padding: 0.1rem 0;
  scrollbar-width: none;
  align-items: flex-end;
}
#gem-tray::-webkit-scrollbar { display: none; }
#btn-wave {
  flex-shrink: 0; min-width: 76px;
  font-size: 0.8rem; font-weight: 700;
  padding: 0 0.75rem;
  border-radius: 0.5rem;
  white-space: nowrap;
  writing-mode: horizontal-tb;
}

.gem-slot {
  flex-shrink: 0;
  width: 50px; height: 56px;
  border-radius: 0.5rem;
  border: 2px solid var(--border);
  background: var(--surface2);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; transition: all 0.15s ease;
  position: relative; gap: 1px;
}
.gem-slot:active { transform: scale(0.95); }
.gem-slot.selected { border-color: var(--mana); box-shadow: 0 0 12px var(--mana-glow); }
.gem-slot.disabled { opacity: 0.35; pointer-events: none; }
.gem-slot .gem-cost { font-size: 0.5rem; color: var(--mana); font-weight: 600; }
.gem-slot .gem-name {
  font-size: 0.45rem; color: var(--text-dim);
  max-width: 46px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.gem-slot canvas { width: 26px; height: 26px; }
.gem-slot.inv-gem { border-style: dashed; }
.gem-slot.inv-gem .gem-cost { color: var(--text-dim); }

/* Tower Panel */
.panel {
  position: absolute; bottom: 110px; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 0.75rem; padding: 0.75rem;
  min-width: 220px; max-width: 300px; z-index: 20;
  box-shadow: 0 8px 32px rgba(0,0,0,0.6);
}
.panel.hidden { display: none; }
#tower-info { font-size: 0.8rem; margin-bottom: 0.5rem; line-height: 1.6; }
#tower-actions { display: flex; gap: 0.5rem; }
.gem-stat { font-weight: 600; }

/* Results */
#screen-results {
  justify-content: center; align-items: center;
  padding: 2rem; background: var(--bg);
}
.results-content {
  max-width: 360px; width: 100%;
  display: flex; flex-direction: column; align-items: center; gap: 1rem;
  text-align: center;
}
.results-content h2 { font-size: 1.8rem; font-weight: 300; color: var(--text-bright); }
#results-body { font-size: 0.9rem; line-height: 1.8; width: 100%; }
.result-row {
  display: flex; justify-content: space-between;
  padding: 0.3rem 0; border-bottom: 1px solid var(--border);
}
.result-row:last-child { border: none; }
.result-label { color: var(--text-dim); }
.result-value { color: var(--text-bright); font-weight: 600; }
.result-value.mana-color { color: var(--mana); }
.result-value.gold { color: var(--citrine); }
.result-value.danger { color: var(--danger); }

.gem-bar {
  display: flex; gap: 6px; justify-content: center;
  margin: 0.5rem 0 0.75rem;
}
.gem-pip {
  width: 24px; height: 24px; border-radius: 4px;
  background: var(--surface2); border: 1px solid var(--border);
  transition: all 0.3s ease;
}
.gem-pip.filled {
  background: linear-gradient(135deg, var(--sapphire), var(--amethyst));
  border-color: var(--mana);
  box-shadow: 0 0 8px var(--mana-glow);
  animation: gemPipPop 0.4s ease both;
}

.llm-section {
  width: 100%; text-align: left;
  background: var(--surface2); border-radius: 0.5rem; padding: 0.75rem;
}
.llm-section.hidden { display: none; }
.llm-section h3 { font-size: 0.85rem; color: var(--text-dim); margin-bottom: 0.5rem; }
#llm-content { font-size: 0.8rem; line-height: 1.5; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.7); backdrop-filter: blur(4px);
  display: none; align-items: center; justify-content: center;
  padding: 1rem; opacity: 0; transition: opacity 0.3s ease;
}
.modal-overlay.visible { display: flex; opacity: 1; }
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 0.75rem; padding: 1.5rem;
  max-width: 400px; width: 100%; max-height: 80vh;
  overflow-y: auto; position: relative;
}
.modal-close {
  position: absolute; top: 0.5rem; right: 0.75rem;
  background: none; border: none; color: var(--text-dim);
  font-size: 1.5rem; cursor: pointer; line-height: 1;
}
.modal h2 { margin-bottom: 1rem; font-weight: 400; }
.modal h3 { margin-top: 0.5rem; margin-bottom: 0.5rem; font-weight: 500; font-size: 0.95rem; }
.modal p, .modal li { font-size: 0.85rem; line-height: 1.6; color: var(--text); margin-bottom: 0.5rem; }
.modal ul { padding-left: 1.2rem; }
.howto-gem {
  display: inline-block; width: 12px; height: 12px;
  border-radius: 2px; vertical-align: middle; margin-right: 4px;
}

.stat-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin: 1rem 0;
}
.stat-card {
  background: var(--surface2); border-radius: 0.5rem; padding: 0.75rem; text-align: center;
}
.stat-card .stat-value { font-size: 1.5rem; font-weight: 600; color: var(--text-bright); }
.stat-card .stat-label { font-size: 0.7rem; color: var(--text-dim); margin-top: 0.25rem; }

/* Animations */
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.25); } 100% { transform: scale(1); } }
@keyframes gemPipPop { 0% { transform: scale(0); } 60% { transform: scale(1.2); } 100% { transform: scale(1); } }
@keyframes fadeSlideUp { from { transform: translate(-50%, 10px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

.animate-in { animation: slideUp 0.3s ease; }
.bump { animation: bump 0.25s ease; }

/* Responsive */
@media (min-width: 600px) {
  #game-canvas { max-width: 500px; margin: 0 auto; }
}
@media (max-height: 600px) {
  .title { font-size: 2rem; }
  #forge-panel { padding: 0.3rem 0.6rem; gap: 0.25rem; }
  .gem-slot { width: 44px; height: 50px; }
  .gem-slot canvas { width: 22px; height: 22px; }
}
