/* game.css — Layout & sizing only. NO hardcoded colors — every color is a
 * var(--token) from themes.css. Source of truth: docs/specs/002-theme-system.md (BR-5).
 * Responsive down to 360dp; touch targets >= 44px. */

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

html {
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  min-height: 100vh;
  overscroll-behavior: none;  /* prevent pull-to-refresh on non-WebKit */
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  transition: var(--transition);
  /* Safe-area insets (notches, home indicator). */
  padding: calc(env(safe-area-inset-top, 0px) + 12px)
           env(safe-area-inset-right, 0px)
           calc(env(safe-area-inset-bottom, 0px) + 12px)
           env(safe-area-inset-left, 0px);
  -webkit-tap-highlight-color: transparent;
}

/* ---- Header ---- */
.app-header {
  width: min(94vw, 540px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 16px;
}

.app-title {
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.scoreboard {
  display: flex;
  gap: 8px;
}

.score-box {
  background-color: var(--bg-secondary);
  border-radius: var(--border-radius);
  padding: 6px 12px;
  min-width: 64px;
  text-align: center;
  box-shadow: var(--shadow);
}

.score-label {
  display: block;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-secondary);
}

.score-value {
  display: block;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
}

/* T-16 + sound: icon buttons (theme + sound) share this style */
.icon-btn {
  min-height: 44px;
  min-width: 44px;
  border: none;
  border-radius: var(--border-radius);
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 1.3rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover,
.icon-btn:active {
  background-color: var(--accent-light);
}

#level-display {
  width: min(94vw, 540px);
  text-align: center;
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

/* ---- Grid ---- */
#grid {
  position: relative;
  width: min(94vw, 540px);
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: 2.5%;
  background-color: var(--bg-secondary);
  border-radius: var(--border-radius);
  padding: 2.5%;
  box-shadow: var(--shadow);
  touch-action: none; /* prevent scroll/zoom interfering with swipes */
}

.grid-gameover-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background-color: var(--bg-gameover-overlay);
  border-radius: var(--border-radius);
  z-index: 10;
}

.gameover-title {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text-on-accent);
  letter-spacing: 0.5px;
}

.gameover-score {
  font-size: 1rem;
  color: var(--text-on-accent);
  opacity: 0.85;
}

#game-actions {
  width: min(94vw, 540px);
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 12px;
  min-height: 48px;
}

.game-action-btn {
  min-height: 44px;
  padding: 10px 28px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  border: none;
  border-radius: var(--border-radius);
  background-color: var(--accent);
  color: var(--text-on-accent);
  transition: background-color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.game-action-btn:hover,
.game-action-btn:active {
  background-color: var(--accent-dark);
}

.milestone-toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 80px);
  left: 50%;
  transform: translateX(-50%);
  background-color: var(--accent);
  color: var(--text-on-accent);
  font-size: 0.95rem;
  font-weight: 700;
  padding: 10px 20px;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  z-index: 200;
  white-space: nowrap;
  pointer-events: none;
}

.tile-bg {
  background-color: var(--tile-empty);
  border-radius: var(--border-radius);
}

.tile-live {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--tile-default);
  color: var(--text-primary);
  border-radius: var(--border-radius);
  font-weight: 700;
  font-size: clamp(0.7rem, 3.8vw, 1.3rem);
  user-select: none;
  z-index: 1;
  /* Transition is set inline during animation; default none so initTiles is instant. */
}

.tile-live[data-family="blue"]  { background-color: var(--tile-blue);  }
.tile-live[data-family="gold"]  { background-color: var(--tile-gold);  }
.tile-live[data-family="fused"] { background-color: var(--tile-fused); }

/* Merge flash (renderer.js adds 'tile-merging' for 200ms, then removes it). */
.tile-merging {
  background-color: var(--tile-merged) !important;
  animation: merge-pop 0.2s ease;
}

@keyframes merge-pop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.18); }
  100% { transform: scale(1); }
}

.tile-spawn {
  animation: tile-spawn 150ms ease both;
}

@keyframes tile-spawn {
  0%   { transform: scale(0);   }
  80%  { transform: scale(1.1); }
  100% { transform: scale(1);   }
}

/* ---- Overlays (game over / win) — created in renderer.js ---- */
.overlay {
  position: fixed;
  inset: 0;
  display: flex; /* toggled to 'none' when hidden */
  align-items: center;
  justify-content: center;
  background-color: var(--bg-overlay);
  z-index: 100;
  padding: 24px;
}

.overlay-card {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  padding: 28px 32px;
  text-align: center;
  max-width: 90vw;
}

.overlay-title {
  font-size: 1.8rem;
  font-weight: 800;
  margin-bottom: 8px;
}

.overlay-message {
  font-size: 1rem;
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.overlay-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.overlay-btn {
  min-height: 44px;
  padding: 10px 20px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  border: none;
  border-radius: var(--border-radius);
  background-color: var(--accent);
  color: var(--text-on-accent);
  transition: background-color 0.15s ease;
}

.overlay-btn:hover,
.overlay-btn:active {
  background-color: var(--accent-dark);
}

/* ---- Game Layout: 3-column grid on desktop so board is optically centered ---- */
/* Column 1 = invisible spacer (= panel width), Column 2 = board, Column 3 = panel */
#game-layout {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  width: 100%;
  gap: 12px;
  align-items: start;
}

.board-col {
  grid-column: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ---- Fusion Recipe Panel ---- */
#fusion-panel {
  grid-column: 3;
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 160px;
  background-color: var(--bg-secondary);
  border-radius: var(--border-radius);
  padding: 10px 8px;
  box-shadow: var(--shadow);
  flex-shrink: 0;
}

.fusion-panel-label {
  font-size: 0.55rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: 8px;
  white-space: nowrap;
}

.fusion-recipes {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
}

.fusion-recipe {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 3px;
  width: 100%;
}

.fusion-input {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  height: 22px;
  border-radius: calc(var(--border-radius) * 0.5);
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--text-primary);
  flex-shrink: 0;
  padding: 0 3px;
}

.fusion-input-blue {
  background-color: var(--tile-blue);
}

.fusion-input-gold {
  background-color: var(--tile-gold);
}

.fusion-symbol {
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--text-secondary);
  flex-shrink: 0;
  line-height: 1;
  padding: 0 1px;
}

.fusion-node {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 22px;
  border-radius: calc(var(--border-radius) * 0.5);
  background-color: var(--tile-empty);
  color: var(--text-secondary);
  font-size: 0.6rem;
  font-weight: 700;
  transition: background-color 0.3s ease, box-shadow 0.3s ease, color 0.3s ease;
  flex-shrink: 0;
  padding: 0 3px;
}

.fusion-node.achieved {
  background-color: var(--tile-fused);
  color: var(--text-primary);
  box-shadow: 0 0 8px var(--tile-fused), 0 0 16px var(--tile-fused);
}

/* ---- Medium + Mobile (≤900px): single column, panel below board ---- */
@media (max-width: 900px) {
  #game-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  .board-col {
    width: min(94vw, 540px);
  }

  #fusion-panel {
    width: min(92vw, 480px);
    margin: 0 auto;
  }

  .fusion-recipes {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .fusion-recipe {
    justify-content: center;
  }
}

/* ---- Fusion node first-time electric flash ---- */
@keyframes node-electric {
  0%   { transform: scale(1);    box-shadow: 0 0 0px var(--tile-fused); }
  20%  { transform: scale(1.22); box-shadow: 0 0 18px var(--tile-fused), 0 0 36px var(--tile-fused); }
  45%  { transform: scale(0.94); box-shadow: 0 0 8px  var(--tile-fused); }
  65%  { transform: scale(1.12); box-shadow: 0 0 24px var(--tile-fused), 0 0 48px var(--tile-fused); }
  85%  { transform: scale(0.98); box-shadow: 0 0 6px  var(--tile-fused); }
  100% { transform: scale(1);    box-shadow: 0 0 10px var(--tile-fused), 0 0 20px var(--tile-fused); }
}

.fusion-node.flash-electric {
  background-color: var(--tile-fused);
  color: var(--text-primary);
  animation: node-electric 0.5s ease forwards;
}

/* ---- Feature 1: Fusion-ready tile glow (eligible cross-chain pair) ---- */
@keyframes fusion-pulse {
  0%   { box-shadow: 0 0 10px var(--tile-fused), 0 0 0 10px var(--tile-fused); }
  50%  { box-shadow: 0 0 22px var(--tile-fused), 0 0 0 15px var(--tile-fused); }
  100% { box-shadow: 0 0 10px var(--tile-fused), 0 0 0 10px var(--tile-fused); }
}

.tile-fusion-ready {
  box-shadow: 0 0 10px var(--tile-fused), 0 0 0 10px var(--tile-fused) !important;
  animation: fusion-pulse 1.1s ease-in-out infinite;
}

/* ---- Feature 1: Electric animation on cross-chain merge result ---- */
@keyframes tile-electric {
  0%   { transform: scale(1);    box-shadow: 0 0 0px  var(--tile-fused); }
  15%  { transform: scale(1.2);  box-shadow: 0 0 20px var(--tile-fused), 0 0 40px var(--tile-fused); }
  35%  { transform: scale(0.92); box-shadow: 0 0 8px  var(--tile-fused); }
  55%  { transform: scale(1.1);  box-shadow: 0 0 28px var(--tile-fused), 0 0 56px var(--tile-fused); }
  75%  { transform: scale(0.97); box-shadow: 0 0 6px  var(--tile-fused); }
  100% { transform: scale(1);    box-shadow: 0 0 0px  var(--tile-fused); }
}

.tile-fused-electric {
  animation: tile-electric 0.45s ease;
}

/* ---- How To Play collapsible (<details>/<summary>) ---- */
#how-to-play {
  max-width: min(94vw, 560px);
  margin: 1.25rem auto 0.5rem;
}

#how-to-play summary {
  cursor: pointer;
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.9rem;
  border-radius: 6px;
  border: 1px solid var(--tile-empty);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-weight: 600;
  user-select: none;
}

#how-to-play summary::-webkit-details-marker { display: none; }

#how-to-play summary::after {
  content: '▾';
  font-size: 0.75rem;
  opacity: 0.6;
}

#how-to-play[open] summary::after {
  content: '▴';
}

#how-to-play ul {
  margin: 0.6rem 0 0;
  padding-left: 1.25rem;
}

#how-to-play li {
  color: var(--text-primary);
  font-size: 0.85rem;
  margin-bottom: 0.3rem;
  opacity: 0.85;
}

/* ---- Page footer ---- */
#page-footer {
  text-align: center;
  padding: 1rem 0 2rem;
  font-size: 0.8rem;
  color: var(--text-secondary, var(--text-primary));
}

#page-footer a {
  color: var(--text-primary);
  opacity: 0.6;
  text-decoration: none;
  margin: 0 0.5rem;
}

#page-footer a:hover {
  opacity: 1;
}

/* ---- Feedback panel (shown after game over, below the New Game button) ---- */
#feedback-panel {
  max-width: min(94vw, 540px);
  width: min(94vw, 540px);
  margin: 1rem auto 0;
  padding: 1rem;
  background: var(--bg-secondary);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
}

.feedback-prompt {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.75rem;
}

.feedback-row {
  margin-bottom: 0.75rem;
}

.feedback-row label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-primary);
  margin-bottom: 0.35rem;
}

.rating-btns,
.diff-btns,
.recipe-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.rating-btn,
.diff-btn,
.recipe-btn {
  font-size: 0.8rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--tile-empty);
  border-radius: 4px;
  background: var(--bg-primary);
  color: var(--text-primary);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.1s ease;
}

.rating-btn.selected,
.diff-btn.selected,
.recipe-btn.selected {
  background: var(--tile-fused);
  border-color: var(--tile-fused);
}

#feedback-msg {
  width: 100%;
  box-sizing: border-box;
  font-size: 0.8rem;
  padding: 0.4rem;
  border: 1px solid var(--tile-empty);
  border-radius: 4px;
  background: var(--bg-primary);
  color: var(--text-primary);
  resize: vertical;
  min-height: 50px;
  font-family: inherit;
}

.feedback-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

#feedback-submit {
  padding: 0.4rem 1rem;
  background: var(--accent);
  color: var(--text-on-accent);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 700;
  min-height: 36px;
}

#feedback-submit:disabled {
  opacity: 0.5;
  cursor: default;
}

#feedback-skip {
  background: none;
  border: none;
  color: var(--text-primary);
  opacity: 0.5;
  cursor: pointer;
  font-size: 0.8rem;
}

#feedback-skip:hover {
  opacity: 1;
}

.feedback-thanks,
.feedback-error {
  font-size: 0.85rem;
  color: var(--text-primary);
  padding: 0.5rem 0;
}
