@import "../css/tokens.css";

/* ==========================================================================
   Tile Merge — Phone-First 2048 Styling & Animation Engine
   ========================================================================== */

:root {
  --color-brand: #F97316;
  --color-brand-hover: #EA580C;
  --color-brand-subtle: #FFF7ED;
  --board-bg: #bbada0;
  --board-padding: clamp(8px, 2.5vw, 14px);
  --board-gap: clamp(8px, 2.5vw, 14px);
  --tile-radius: clamp(6px, 2vw, 10px);
  --board-radius: clamp(10px, 3vw, 16px);
  --cell-bg: rgba(238, 228, 218, 0.38);
}

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

html, body {
  background-color: var(--color-paper, #faf8f5);
  color: var(--color-ink, #282420);
  font-family: var(--font-body, "Plus Jakarta Sans", sans-serif);
  min-height: 100%;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
  padding-bottom: env(safe-area-inset-bottom, 24px);
}

/* --------------------------------------------------------------------------
   Hub Navigation Bar
   -------------------------------------------------------------------------- */
.hub-bar {
  width: 100%;
  background: var(--color-paper-2, #ffffff);
  border-bottom: 1px solid var(--color-rule, rgba(0, 0, 0, 0.08));
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 1rem;
  font-size: var(--text-sm, 0.875rem);
  font-family: var(--font-label, "JetBrains Mono", monospace);
}

.hub-bar a {
  color: var(--color-ink, #282420);
  text-decoration: none;
  font-weight: 600;
  transition: color var(--dur-micro, 120ms) ease;
}

.hub-bar a:hover {
  color: var(--color-brand);
}

.hub-bar__title {
  color: var(--color-muted, #786f66);
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 0.5rem;
}

/* --------------------------------------------------------------------------
   Ad Rail
   -------------------------------------------------------------------------- */
.ad-rail {
  width: 100%;
  max-width: 728px;
  margin: 0.5rem auto 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 0.5rem;
}

.ad-well {
  width: 100%;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-well, rgba(0, 0, 0, 0.02));
  border-radius: 8px;
  overflow: hidden;
}

.ad-empty {
  font-family: var(--font-label);
  font-size: var(--text-xs, 0.75rem);
  color: var(--color-dim, rgba(0, 0, 0, 0.35));
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* --------------------------------------------------------------------------
   Game App Container & HUD
   -------------------------------------------------------------------------- */
.game-container {
  width: 100%;
  max-width: 480px;
  padding: 1rem 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.game-hud {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 0.75rem;
  gap: 0.75rem;
}

.hud-brand {
  flex: 1;
}

.game-title {
  font-size: clamp(1.75rem, 6vw, 2.25rem);
  font-weight: 800;
  line-height: 1.1;
  color: var(--color-brand);
  letter-spacing: -0.02em;
}

.game-subtitle {
  font-size: var(--text-xs, 0.75rem);
  color: var(--color-muted, #786f66);
  margin-top: 0.2rem;
}

.game-subtitle strong {
  color: var(--color-ink, #282420);
}

.hud-scores {
  display: flex;
  gap: 0.5rem;
}

.score-card {
  position: relative;
  background: var(--board-bg);
  border-radius: 10px;
  padding: 0.4rem 0.75rem;
  min-width: 72px;
  text-align: center;
  color: #ffffff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.score-card__label {
  display: block;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  color: #f4eae0;
  line-height: 1;
  margin-bottom: 0.2rem;
}

.score-card__val {
  display: block;
  font-family: var(--font-label, "JetBrains Mono", monospace);
  font-size: clamp(1rem, 3.5vw, 1.25rem);
  font-weight: 700;
  line-height: 1.1;
}

.score-add {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.score-add__item {
  font-family: var(--font-label, "JetBrains Mono", monospace);
  font-size: 0.9rem;
  font-weight: 800;
  color: var(--color-brand);
  background: #ffffff;
  padding: 2px 6px;
  border-radius: 12px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.15);
  animation: scoreFloat 650ms cubic-bezier(0.2, 0.8, 0.3, 1) forwards;
}

@keyframes scoreFloat {
  0% {
    opacity: 0;
    transform: translate(-50%, 0) scale(0.7);
  }
  20% {
    opacity: 1;
    transform: translate(-50%, -10px) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -32px) scale(0.9);
  }
}

/* --------------------------------------------------------------------------
   Toolbar Controls
   -------------------------------------------------------------------------- */
.game-toolbar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
  gap: 0.5rem;
}

.tool-btn {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  background: var(--color-paper-2, #ffffff);
  color: var(--color-ink, #282420);
  border: 1px solid var(--color-rule, rgba(0, 0, 0, 0.12));
  border-radius: var(--radius-input, 12px);
  padding: 0.55rem 0.5rem;
  font-family: var(--font-body);
  font-size: var(--text-sm, 0.875rem);
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: all var(--dur-micro, 120ms) ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
}

.tool-btn:hover:not(:disabled) {
  background: #fdfaf6;
  border-color: var(--color-brand);
  color: var(--color-brand);
}

.tool-btn:active:not(:disabled) {
  transform: translateY(1px);
  box-shadow: none;
}

.tool-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  background: #f3efe9;
}

.tool-icon {
  width: 17px;
  height: 17px;
  flex-shrink: 0;
}

.tool-btn[aria-pressed="false"] .tool-icon--sound-on {
  display: none;
}
.tool-btn[aria-pressed="false"] .tool-icon--sound-off {
  display: inline-block;
}
.tool-btn[aria-pressed="true"] .tool-icon--sound-on {
  display: inline-block;
}
.tool-btn[aria-pressed="true"] .tool-icon--sound-off {
  display: none;
}

/* --------------------------------------------------------------------------
   4x4 Grid Board
   -------------------------------------------------------------------------- */
.board-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  position: relative;
}

.grid-board {
  position: relative;
  width: 100%;
  max-width: 440px;
  aspect-ratio: 1 / 1;
  background: var(--board-bg);
  border-radius: var(--board-radius);
  padding: var(--board-padding);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--board-gap);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 10px 30px -6px rgba(0, 0, 0, 0.15), inset 0 2px 4px rgba(255, 255, 255, 0.25);
  outline: none;
}

.grid-cell {
  background: var(--cell-bg);
  border-radius: var(--tile-radius);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.12);
}

.tile-layer {
  position: absolute;
  top: var(--board-padding);
  left: var(--board-padding);
  right: var(--board-padding);
  bottom: var(--board-padding);
  pointer-events: none;
  --cell-size: calc((100% - 3 * var(--board-gap)) / 4);
}

/* --------------------------------------------------------------------------
   Tile Styling & Transforms
   -------------------------------------------------------------------------- */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: var(--tile-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display, "Plus Jakarta Sans", sans-serif);
  font-weight: 800;
  text-align: center;
  box-sizing: border-box;
  z-index: 1;
  will-change: transform;
  transform: translate(
    calc(var(--col) * (var(--cell-size) + var(--board-gap))),
    calc(var(--row) * (var(--cell-size) + var(--board-gap)))
  );
  transition: transform 110ms cubic-bezier(0.2, 0.8, 0.25, 1);
}

.tile--spawn {
  animation: tileSpawn 150ms ease-out backwards;
}

.tile--merged {
  z-index: 2;
  animation: tilePop 180ms cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

.tile--fading {
  z-index: 0;
  opacity: 0;
  transition: transform 110ms cubic-bezier(0.2, 0.8, 0.25, 1), opacity 80ms ease-out;
}

@keyframes tileSpawn {
  0% {
    opacity: 0;
    transform: translate(
      calc(var(--col) * (var(--cell-size) + var(--board-gap))),
      calc(var(--row) * (var(--cell-size) + var(--board-gap)))
    ) scale(0);
  }
  60% {
    opacity: 1;
    transform: translate(
      calc(var(--col) * (var(--cell-size) + var(--board-gap))),
      calc(var(--row) * (var(--cell-size) + var(--board-gap)))
    ) scale(1.12);
  }
  100% {
    opacity: 1;
    transform: translate(
      calc(var(--col) * (var(--cell-size) + var(--board-gap))),
      calc(var(--row) * (var(--cell-size) + var(--board-gap)))
    ) scale(1);
  }
}

@keyframes tilePop {
  0% {
    transform: translate(
      calc(var(--col) * (var(--cell-size) + var(--board-gap))),
      calc(var(--row) * (var(--cell-size) + var(--board-gap)))
    ) scale(0.9);
  }
  50% {
    transform: translate(
      calc(var(--col) * (var(--cell-size) + var(--board-gap))),
      calc(var(--row) * (var(--cell-size) + var(--board-gap)))
    ) scale(1.18);
  }
  100% {
    transform: translate(
      calc(var(--col) * (var(--cell-size) + var(--board-gap))),
      calc(var(--row) * (var(--cell-size) + var(--board-gap)))
    ) scale(1);
  }
}

/* --------------------------------------------------------------------------
   Pastel & Vibrant Tile Palettes
   -------------------------------------------------------------------------- */
.tile-2 {
  background: #ede4db;
  color: #5f4b32;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
  font-size: clamp(1.6rem, 6vw, 2.25rem);
}

.tile-4 {
  background: #ede0c8;
  color: #5f4b32;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
  font-size: clamp(1.6rem, 6vw, 2.25rem);
}

.tile-8 {
  background: #f59563;
  color: #ffffff;
  box-shadow: 0 3px 8px rgba(245, 149, 99, 0.35);
  font-size: clamp(1.6rem, 6vw, 2.25rem);
}

.tile-16 {
  background: #f67c5f;
  color: #ffffff;
  box-shadow: 0 3px 10px rgba(246, 124, 95, 0.4);
  font-size: clamp(1.5rem, 5.5vw, 2.1rem);
}

.tile-32 {
  background: #f65e3b;
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(246, 94, 59, 0.45);
  font-size: clamp(1.5rem, 5.5vw, 2.1rem);
}

.tile-64 {
  background: #edcf72;
  color: #ffffff;
  box-shadow: 0 4px 14px rgba(237, 207, 114, 0.5);
  font-size: clamp(1.5rem, 5.5vw, 2.1rem);
}

.tile-128 {
  background: #edcc61;
  color: #ffffff;
  box-shadow: 0 0 16px rgba(237, 204, 97, 0.55), inset 0 0 0 1px rgba(255, 255, 255, 0.4);
  font-size: clamp(1.25rem, 4.8vw, 1.8rem);
}

.tile-256 {
  background: #edc850;
  color: #ffffff;
  box-shadow: 0 0 20px rgba(237, 200, 80, 0.65), inset 0 0 0 1px rgba(255, 255, 255, 0.5);
  font-size: clamp(1.25rem, 4.8vw, 1.8rem);
}

.tile-512 {
  background: #edc53f;
  color: #ffffff;
  box-shadow: 0 0 24px rgba(237, 197, 63, 0.75), inset 0 0 0 1.5px rgba(255, 255, 255, 0.6);
  font-size: clamp(1.25rem, 4.8vw, 1.8rem);
}

.tile-1024 {
  background: #e4b82d;
  color: #ffffff;
  box-shadow: 0 0 28px rgba(228, 184, 45, 0.8), inset 0 0 0 2px rgba(255, 255, 255, 0.7);
  font-size: clamp(0.95rem, 3.8vw, 1.45rem);
}

.tile-2048 {
  background: linear-gradient(135deg, #ffd700 0%, #ff8c00 100%);
  color: #ffffff;
  box-shadow: 0 0 32px rgba(255, 140, 0, 0.85), inset 0 0 0 2px rgba(255, 255, 255, 0.9);
  font-size: clamp(0.95rem, 3.8vw, 1.45rem);
}

.tile-4096 {
  background: linear-gradient(135deg, #a855f7 0%, #6366f1 100%);
  color: #ffffff;
  box-shadow: 0 0 32px rgba(168, 85, 247, 0.85), inset 0 0 0 2px rgba(255, 255, 255, 0.9);
  font-size: clamp(0.95rem, 3.8vw, 1.45rem);
}

.tile-8192 {
  background: linear-gradient(135deg, #ec4899 0%, #f43f5e 100%);
  color: #ffffff;
  box-shadow: 0 0 36px rgba(244, 63, 94, 0.9), inset 0 0 0 2px rgba(255, 255, 255, 0.9);
  font-size: clamp(0.95rem, 3.8vw, 1.45rem);
}

.tile-super {
  background: linear-gradient(135deg, #10b981 0%, #06b6d4 100%);
  color: #ffffff;
  box-shadow: 0 0 36px rgba(6, 182, 212, 0.9);
  font-size: clamp(0.8rem, 3.2vw, 1.2rem);
}

/* --------------------------------------------------------------------------
   Swipe Hint
   -------------------------------------------------------------------------- */
.swipe-hint {
  font-size: var(--text-xs, 0.75rem);
  color: var(--color-muted, #786f66);
  margin-top: 0.85rem;
  text-align: center;
}

.swipe-hint strong {
  color: var(--color-ink, #282420);
}

/* --------------------------------------------------------------------------
   Overlay Modals (Victory / Game Over)
   -------------------------------------------------------------------------- */
.overlay-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(40, 36, 32, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-short, 220ms) ease;
}

.overlay-modal:not([hidden]) {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: var(--color-paper-2, #ffffff);
  border-radius: var(--radius-card, 20px);
  padding: 1.75rem 1.5rem;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.3);
  transform: scale(0.9);
  transition: transform var(--dur-short, 220ms) var(--ease-spring);
}

.overlay-modal:not([hidden]) .modal-card {
  transform: scale(1);
}

.modal-badge {
  font-size: 2.5rem;
  line-height: 1;
  margin-bottom: 0.5rem;
}

.modal-title {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--color-ink, #282420);
  margin-bottom: 0.35rem;
}

.modal-desc {
  font-size: var(--text-sm, 0.875rem);
  color: var(--color-muted, #786f66);
  margin-bottom: 1.25rem;
  line-height: 1.4;
}

.modal-stats {
  display: flex;
  justify-content: center;
  gap: 1rem;
  background: var(--color-paper, #faf8f5);
  border: 1px solid var(--color-rule, rgba(0, 0, 0, 0.08));
  border-radius: 12px;
  padding: 0.75rem;
  margin-bottom: 1.25rem;
}

.modal-stat {
  flex: 1;
}

.modal-stat__label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-muted);
  font-weight: 700;
  margin-bottom: 0.2rem;
}

.modal-stat__val {
  display: block;
  font-family: var(--font-label);
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--color-ink);
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-pill, 999px);
  font-family: var(--font-body);
  font-size: var(--text-md, 1rem);
  font-weight: 700;
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: all var(--dur-micro, 120ms) ease;
}

.btn--primary {
  background: var(--color-brand);
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(249, 115, 22, 0.35);
}

.btn--primary:hover {
  background: var(--color-brand-hover);
  box-shadow: 0 6px 16px rgba(249, 115, 22, 0.45);
}

.btn--primary:active {
  transform: translateY(1px);
}

.btn--secondary {
  background: var(--color-paper, #faf8f5);
  color: var(--color-ink, #282420);
  border: 1px solid var(--color-rule, rgba(0, 0, 0, 0.12));
}

.btn--secondary:hover {
  background: #f0eae1;
}

/* --------------------------------------------------------------------------
   Article / Below Section (SEO & Game Guide)
   -------------------------------------------------------------------------- */
.article-section {
  width: 100%;
  max-width: 680px;
  padding: 2rem 1.25rem;
  margin-top: 1rem;
  border-top: 1px solid var(--color-rule, rgba(0, 0, 0, 0.08));
}

.article-section h2 {
  font-size: clamp(1.25rem, 4vw, 1.5rem);
  font-weight: 800;
  color: var(--color-ink, #282420);
  margin: 1.75rem 0 0.75rem;
  letter-spacing: -0.01em;
}

.article-section h2:first-of-type {
  margin-top: 0;
}

.article-section p {
  font-size: var(--text-sm, 0.875rem);
  line-height: 1.65;
  color: var(--color-ink-2, #3c3630);
  margin-bottom: 1rem;
}

.guide-steps {
  list-style: none;
  counter-reset: guide-counter;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.guide-steps li {
  counter-increment: guide-counter;
  position: relative;
  padding-left: 2.25rem;
  font-size: var(--text-sm, 0.875rem);
  line-height: 1.5;
  color: var(--color-ink-2);
}

.guide-steps li::before {
  content: counter(guide-counter);
  position: absolute;
  left: 0;
  top: 0;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: var(--color-brand-subtle);
  color: var(--color-brand);
  border: 1px solid var(--color-brand);
  font-family: var(--font-label);
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.guide-steps strong {
  color: var(--color-ink);
}

.tips-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin: 1rem 0 1.5rem;
}

.tip-card {
  background: var(--color-paper-2, #ffffff);
  border: 1px solid var(--color-rule, rgba(0, 0, 0, 0.08));
  border-radius: 14px;
  padding: 1rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03);
}

.tip-card h3 {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--color-brand);
  margin-bottom: 0.4rem;
}

.tip-card p {
  font-size: 0.825rem;
  line-height: 1.5;
  color: var(--color-muted);
  margin-bottom: 0;
}

.faq-list {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  margin: 1rem 0 1.5rem;
}

.faq-item {
  background: var(--color-paper-2, #ffffff);
  border: 1px solid var(--color-rule, rgba(0, 0, 0, 0.08));
  border-radius: 12px;
  overflow: hidden;
}

.faq-item summary {
  padding: 0.85rem 1rem;
  font-size: var(--text-sm, 0.875rem);
  font-weight: 700;
  color: var(--color-ink);
  cursor: pointer;
  user-select: none;
}

.faq-item summary:hover {
  color: var(--color-brand);
}

.faq-body {
  padding: 0 1rem 0.85rem;
  font-size: 0.825rem;
  line-height: 1.55;
  color: var(--color-ink-2);
}

.faq-body code {
  font-family: var(--font-label);
  background: var(--color-paper);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  font-size: 0.8em;
}

/* --------------------------------------------------------------------------
   Footer
   -------------------------------------------------------------------------- */
.game-footer {
  width: 100%;
  max-width: 680px;
  text-align: center;
  padding: 1.5rem 1rem 2rem;
  border-top: 1px solid var(--color-rule, rgba(0, 0, 0, 0.08));
}

.footer-links {
  font-size: var(--text-sm, 0.875rem);
  margin-bottom: 0.5rem;
}

.footer-links a {
  color: var(--color-ink, #282420);
  text-decoration: none;
  font-weight: 600;
}

.footer-links a:hover {
  color: var(--color-brand);
}

.footer-copy {
  font-size: var(--text-xs, 0.75rem);
  color: var(--color-muted);
}
