/* =========================================================
   2048 Browser Game — styles
   ========================================================= */

/* ---------- Design tokens (light) ---------- */
:root {
  color-scheme: light dark;

  /* Surfaces */
  --bg: #F5F8FB;
  --fg: #1A2740;
  --fg-muted: #5C6B82;
  --surface: #FFFFFF;
  --surface-2: #E6EEF6;
  --border: #D5DEEA;

  /* Board */
  --board-bg: #BBD3E5;
  --cell-bg: #D8E4EF;

  /* Brand */
  --accent: #1976D2;
  --accent-fg: #FFFFFF;
  --accent-hover: #1565C0;

  /* Tile colors (hue-stable sky blue ramp) */
  --tile-2-bg:    #E3F2FD;  --tile-2-fg:    #1A237E;
  --tile-4-bg:    #BBDEFB;  --tile-4-fg:    #1A237E;
  --tile-8-bg:    #90CAF9;  --tile-8-fg:    #FFFFFF;
  --tile-16-bg:   #64B5F6;  --tile-16-fg:   #FFFFFF;
  --tile-32-bg:   #42A5F5;  --tile-32-fg:   #FFFFFF;
  --tile-64-bg:   #2196F3;  --tile-64-fg:   #FFFFFF;
  --tile-128-bg:  #1E88E5;  --tile-128-fg:  #FFFFFF;
  --tile-256-bg:  #1976D2;  --tile-256-fg:  #FFFFFF;
  --tile-512-bg:  #1565C0;  --tile-512-fg:  #FFFFFF;
  --tile-1024-bg: #0D47A1;  --tile-1024-fg: #FFFFFF;
  --tile-super-bg:#082B6B;  --tile-super-fg:#FFFFFF;

  /* Sizing */
  --radius: 8px;
  --radius-lg: 12px;
  --shadow-card: 0 2px 8px rgba(20, 40, 80, 0.06);
  --shadow-overlay: 0 8px 40px rgba(10, 20, 50, 0.25);

  /* Board geometry */
  --board-size: min(92vw, 500px);
  --board-padding: 10px;
  --gap: 10px;
  --cell-size: calc((var(--board-size) - 2 * var(--board-padding) - 3 * var(--gap)) / 4);

  /* Animation */
  --move-duration: 130ms;
  --pop-duration: 180ms;
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

/* ---------- Dark tokens ---------- */
:root[data-theme="dark"] {
  --bg: #0E1626;
  --fg: #E6EEF6;
  --fg-muted: #8FA0BC;
  --surface: #182236;
  --surface-2: #1F2C45;
  --border: #2A3855;

  --board-bg: #1B2A47;
  --cell-bg: #243453;

  --accent: #2196F3;
  --accent-fg: #FFFFFF;
  --accent-hover: #1E88E5;

  /* Tile colors: same hues, slightly desaturated text for light shades */
  --tile-2-bg:    #1F3A5F;  --tile-2-fg:    #E3F2FD;
  --tile-4-bg:    #234A7A;  --tile-4-fg:    #E3F2FD;
  --tile-8-bg:    #2E6AB0;  --tile-8-fg:    #FFFFFF;
  --tile-16-bg:   #3D85D8;  --tile-16-fg:   #FFFFFF;
  --tile-32-bg:   #4A98EC;  --tile-32-fg:   #FFFFFF;
  --tile-64-bg:   #2196F3;  --tile-64-fg:   #FFFFFF;
  --tile-128-bg:  #1E88E5;  --tile-128-fg:  #FFFFFF;
  --tile-256-bg:  #1976D2;  --tile-256-fg:  #FFFFFF;
  --tile-512-bg:  #1565C0;  --tile-512-fg:  #FFFFFF;
  --tile-1024-bg: #0D47A1;  --tile-1024-fg: #FFFFFF;
  --tile-super-bg:#082B6B;  --tile-super-fg:#FFFFFF;

  --shadow-card: 0 2px 8px rgba(0, 0, 0, 0.4);
  --shadow-overlay: 0 8px 40px rgba(0, 0, 0, 0.6);
}

/* ---------- Reset / base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  background: var(--bg);
  color: var(--fg);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; cursor: pointer; }

.hidden { display: none !important; }

/* ---------- Header ---------- */
.app-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 24px 16px 8px;
  width: 100%;
  max-width: var(--board-size);
  margin: 0 auto;
}

.app-title {
  margin: 0;
  font-size: 56px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--accent);
  text-align: center;
  line-height: 1;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 12px;
}

.scores {
  display: flex;
  gap: 10px;
}

.score-box {
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: 6px 14px;
  min-width: 70px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.score-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--fg-muted);
}

.score-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--fg);
  line-height: 1.2;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ---------- Buttons ---------- */
.primary-btn,
.secondary-btn,
.icon-btn {
  border: none;
  border-radius: var(--radius);
  padding: 8px 14px;
  font-weight: 600;
  transition: background-color 120ms ease, transform 120ms ease, opacity 120ms ease;
}

.primary-btn {
  background: var(--accent);
  color: var(--accent-fg);
}
.primary-btn:hover { background: var(--accent-hover); }
.primary-btn:active { transform: scale(0.98); }

.secondary-btn {
  background: var(--surface-2);
  color: var(--fg);
}
.secondary-btn:hover { background: var(--border); }

.icon-btn {
  background: var(--surface-2);
  color: var(--fg);
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}
.icon-btn:hover { background: var(--border); }
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Main / Board ---------- */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 20px;
  padding: 24px 16px;
  position: relative;
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  padding: var(--board-padding);
  background: var(--board-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  outline: none;
  /* 스와이프 제스처가 페이지 스크롤/풀투리프레시로 가로채지지 않도록 함 */
  touch-action: none;
}

.board-bg {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  width: 100%;
  height: 100%;
}

.cell {
  background: var(--cell-bg);
  border-radius: var(--radius);
}

.tiles {
  position: absolute;
  inset: var(--board-padding);
  pointer-events: none;
}

/* ---------- Tile ---------- */
.tile {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(20px, calc(var(--cell-size) * 0.42), 48px);
  line-height: 1;
  user-select: none;
  /* Position via CSS custom properties set by JS */
  transform: translate(
    calc(var(--col, 0) * (var(--cell-size) + var(--gap))),
    calc(var(--row, 0) * (var(--cell-size) + var(--gap)))
  );
  transition: transform var(--move-duration) var(--ease);
  will-change: transform;
}

/* Tile color by value */
.tile[data-value="2"]    { background: var(--tile-2-bg);    color: var(--tile-2-fg); }
.tile[data-value="4"]    { background: var(--tile-4-bg);    color: var(--tile-4-fg); }
.tile[data-value="8"]    { background: var(--tile-8-bg);    color: var(--tile-8-fg); }
.tile[data-value="16"]   { background: var(--tile-16-bg);   color: var(--tile-16-fg); }
.tile[data-value="32"]   { background: var(--tile-32-bg);   color: var(--tile-32-fg); }
.tile[data-value="64"]   { background: var(--tile-64-bg);   color: var(--tile-64-fg); }
.tile[data-value="128"]  { background: var(--tile-128-bg);  color: var(--tile-128-fg); font-size: clamp(18px, calc(var(--cell-size) * 0.36), 40px); }
.tile[data-value="256"]  { background: var(--tile-256-bg);  color: var(--tile-256-fg); font-size: clamp(18px, calc(var(--cell-size) * 0.36), 40px); }
.tile[data-value="512"]  { background: var(--tile-512-bg);  color: var(--tile-512-fg); font-size: clamp(18px, calc(var(--cell-size) * 0.36), 40px); }
.tile[data-value="1024"] { background: var(--tile-1024-bg); color: var(--tile-1024-fg); font-size: clamp(16px, calc(var(--cell-size) * 0.30), 34px); }
.tile[data-value="2048"],
.tile[data-value="4096"],
.tile[data-value="8192"],
.tile[data-value="16384"] { background: var(--tile-super-bg); color: var(--tile-super-fg); font-size: clamp(16px, calc(var(--cell-size) * 0.30), 34px); }

/* Tile animations (added by render.js) */
.tile.tile-new {
  animation: tile-pop var(--pop-duration) var(--ease);
}

.tile.tile-merged {
  animation: tile-merge calc(var(--pop-duration) + 40ms) var(--ease);
  z-index: 2;
}

@keyframes tile-pop {
  0%   { transform: translate(
           calc(var(--col, 0) * (var(--cell-size) + var(--gap))),
           calc(var(--row, 0) * (var(--cell-size) + var(--gap)))
         ) scale(0); }
  100% { transform: translate(
           calc(var(--col, 0) * (var(--cell-size) + var(--gap))),
           calc(var(--row, 0) * (var(--cell-size) + var(--gap)))
         ) scale(1); }
}

@keyframes tile-merge {
  0%   { transform: translate(
           calc(var(--col, 0) * (var(--cell-size) + var(--gap))),
           calc(var(--row, 0) * (var(--cell-size) + var(--gap)))
         ) scale(1); }
  50%  { transform: translate(
           calc(var(--col, 0) * (var(--cell-size) + var(--gap))),
           calc(var(--row, 0) * (var(--cell-size) + var(--gap)))
         ) scale(1.15); }
  100% { transform: translate(
           calc(var(--col, 0) * (var(--cell-size) + var(--gap))),
           calc(var(--row, 0) * (var(--cell-size) + var(--gap)))
         ) scale(1); }
}

/* ---------- Board controls ---------- */
.board-controls {
  display: flex;
  gap: 8px;
}

.undo-btn {
  background: var(--surface-2);
  color: var(--fg);
  border: none;
  border-radius: 12px;
  padding: 12px 28px;
  font-weight: 600;
  font-size: 15px;
  min-width: 140px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: var(--shadow-card);
  transition: background-color 120ms ease, transform 120ms ease, opacity 120ms ease;
}

.undo-btn:hover:not(:disabled) { background: var(--border); }
.undo-btn:active:not(:disabled) { transform: scale(0.98); }
.undo-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.undo-icon {
  display: inline-block;
  flex-shrink: 0;
}

/* ---------- Overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 35, 60, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 50;
  animation: fade-in 180ms var(--ease);
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.overlay-card {
  background: var(--surface);
  color: var(--fg);
  border-radius: var(--radius-lg);
  padding: 28px 32px;
  min-width: 280px;
  max-width: 380px;
  text-align: center;
  box-shadow: var(--shadow-overlay);
  animation: pop-in 220ms var(--ease);
}

@keyframes pop-in {
  from { transform: scale(0.9); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

.overlay-title {
  margin: 0 0 18px;
  font-size: 28px;
  font-weight: 800;
  color: var(--accent);
}

.overlay-stats {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin: 0 0 22px;
}

.overlay-stats > div { display: flex; flex-direction: column; gap: 2px; }

.overlay-stats dt {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--fg-muted);
  margin: 0;
}

.overlay-stats dd {
  font-size: 22px;
  font-weight: 700;
  color: var(--fg);
  margin: 0;
}

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

/* ---------- Footer ---------- */
.app-footer {
  padding: 12px 24px 20px;
  text-align: center;
  color: var(--fg-muted);
}

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
  .app-header { padding: 16px 12px 8px; gap: 12px; }
  .app-title { font-size: 44px; }
  .score-value { font-size: 16px; }
  .score-box { min-width: 60px; padding: 4px 10px; }
}
