/* ===========================
   Design System Variables
   =========================== */
:root {
  --bg-primary: #0D1117;
  --bg-secondary: #161B22;
  --bg-tertiary: #21262D;
  --text-primary: #F0F6FC;
  --text-secondary: #8B949E;
  --text-muted: #484F58;
  --accent: #3B82F6;
  --border: #30363D;
  --btn-bg: #21262D;
  --btn-active: #30363D;
  --overlay-bg: rgba(0, 0, 0, 0.85);
  --board-bg: #0A0E14;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg-primary: #FFFFFF;
    --bg-secondary: #F6F8FA;
    --bg-tertiary: #E9ECF0;
    --text-primary: #1B1E28;
    --text-secondary: #57606A;
    --text-muted: #8C959F;
    --accent: #3182F6;
    --border: #D0D7DE;
    --btn-bg: #E9ECF0;
    --btn-active: #D0D7DE;
    --overlay-bg: rgba(255, 255, 255, 0.9);
    --board-bg: #F0F2F5;
  }
}

/* ===========================
   Reset & Base
   =========================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Pretendard', 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

/* ===========================
   Screens
   =========================== */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 10;
}

.screen.active {
  opacity: 1;
  pointer-events: all;
}

.screen-content {
  width: 100%;
  max-width: 480px;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  min-height: 100dvh;
  justify-content: center;
}

/* ===========================
   Title
   =========================== */
.title-block {
  text-align: center;
}

.game-title {
  font-size: clamp(2.5rem, 12vw, 4rem);
  font-weight: 800;
  letter-spacing: 0.15em;
  background: linear-gradient(135deg, var(--accent) 0%, #8B5CF6 50%, #EC4899 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1.1;
  margin-bottom: 0.35rem;
}

.game-subtitle {
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 500;
  letter-spacing: 0.05em;
}

/* ===========================
   Size Selector
   =========================== */
.size-selector {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.selector-label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.size-buttons {
  display: flex;
  gap: 0.5rem;
  background: var(--bg-secondary);
  padding: 0.3rem;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.size-btn {
  padding: 0.55rem 1.2rem;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
}

.size-btn:hover {
  color: var(--text-primary);
  background: var(--btn-bg);
}

.size-btn.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.35);
}

/* ===========================
   Best Records
   =========================== */
.best-records {
  width: 100%;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.best-records-title {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0.25rem;
}

.best-record-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.best-record-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.best-record-values {
  display: flex;
  gap: 1rem;
}

.best-record-val {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.best-record-val span {
  font-size: 0.72rem;
  color: var(--text-secondary);
  font-weight: 400;
}

.no-record {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  padding: 0.25rem 0;
}

/* ===========================
   Buttons
   =========================== */
.btn-primary {
  width: 100%;
  max-width: 280px;
  padding: 0.85rem 2rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  letter-spacing: 0.02em;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
}

.btn-primary:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.btn-secondary {
  width: 100%;
  max-width: 280px;
  padding: 0.85rem 2rem;
  background: var(--btn-bg);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  background: var(--btn-active);
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--btn-bg);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.18s ease;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--btn-active);
  color: var(--text-primary);
}

.icon-btn:active {
  transform: scale(0.93);
}

/* ===========================
   How To
   =========================== */
.how-to {
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.how-to p {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ===========================
   Game Screen
   =========================== */
.game-wrapper {
  width: 100%;
  max-width: 480px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 0.75rem 1rem;
  gap: 0.75rem;
}

/* Game Header */
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-shrink: 0;
}

.stats {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.5rem 1rem;
  flex: 1;
  justify-content: center;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
}

.stat-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  min-width: 3ch;
  text-align: center;
}

.stat-divider {
  width: 1px;
  height: 28px;
  background: var(--border);
}

/* Board Container */
.board-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

/* Board */
.board {
  background: var(--board-bg);
  border-radius: 16px;
  border: 1px solid var(--border);
  padding: 8px;
  display: grid;
  gap: 6px;
  position: relative;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

/* Tiles */
.tile {
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  cursor: pointer;
  position: relative;
  transition: transform 0.13s ease, box-shadow 0.13s ease;
  will-change: transform;

  /* Tile appearance */
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-primary);
}

.tile:hover {
  background: var(--btn-active);
  border-color: var(--accent);
  transform: scale(0.97);
}

.tile:active {
  transform: scale(0.93);
}

.tile.sliding {
  transition: transform 0.13s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  z-index: 5;
}

.tile.empty {
  background: transparent;
  border: 1px dashed var(--border);
  cursor: default;
  pointer-events: none;
  opacity: 0.3;
}

.tile.empty:hover {
  transform: none;
  background: transparent;
  border-color: var(--border);
}

/* Tile number color accent based on value groups */
.tile[data-group="1"] { color: #60A5FA; }
.tile[data-group="2"] { color: #A78BFA; }
.tile[data-group="3"] { color: #F472B6; }
.tile[data-group="4"] { color: #34D399; }

/* Victory tile animation */
.tile.victory-pulse {
  animation: victoryPulse 0.4s ease forwards;
}

@keyframes victoryPulse {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.08); background: var(--accent); color: #fff; border-color: var(--accent); }
  100% { transform: scale(1); }
}

/* Slide animations — JS sets transform directly */
.tile.slide-up    { animation: slideUp    0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94); }
.tile.slide-down  { animation: slideDown  0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94); }
.tile.slide-left  { animation: slideLeft  0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94); }
.tile.slide-right { animation: slideRight 0.12s cubic-bezier(0.25, 0.46, 0.45, 0.94); }

@keyframes slideUp    { from { transform: translateY(100%) scale(0.96); } to { transform: translateY(0) scale(1); } }
@keyframes slideDown  { from { transform: translateY(-100%) scale(0.96); } to { transform: translateY(0) scale(1); } }
@keyframes slideLeft  { from { transform: translateX(100%) scale(0.96); } to { transform: translateX(0) scale(1); } }
@keyframes slideRight { from { transform: translateX(-100%) scale(0.96); } to { transform: translateX(0) scale(1); } }

/* Game Footer */
.game-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-shrink: 0;
  min-height: 24px;
}

.size-indicator {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
  letter-spacing: 0.05em;
}

/* ===========================
   Victory Screen
   =========================== */
.victory-icon {
  font-size: 4rem;
  line-height: 1;
  animation: bounceIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes bounceIn {
  from { transform: scale(0.4); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

.victory-title {
  font-size: 2.2rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--accent) 0%, #8B5CF6 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.victory-subtitle {
  font-size: 0.95rem;
  color: var(--text-secondary);
}

.result-card {
  width: 100%;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.1rem 1.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-label {
  font-size: 0.88rem;
  color: var(--text-secondary);
}

.result-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.result-best {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 600;
  text-align: center;
  padding-top: 0.2rem;
  min-height: 1rem;
}

.victory-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
}

/* ===========================
   Utility / Animations
   =========================== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.screen.active .screen-content > * {
  animation: fadeIn 0.35s ease both;
}

.screen.active .screen-content > *:nth-child(1) { animation-delay: 0.05s; }
.screen.active .screen-content > *:nth-child(2) { animation-delay: 0.10s; }
.screen.active .screen-content > *:nth-child(3) { animation-delay: 0.15s; }
.screen.active .screen-content > *:nth-child(4) { animation-delay: 0.20s; }
.screen.active .screen-content > *:nth-child(5) { animation-delay: 0.25s; }
.screen.active .screen-content > *:nth-child(6) { animation-delay: 0.30s; }

/* ===========================
   Responsive tweaks
   =========================== */
@media (max-height: 600px) {
  .screen-content {
    gap: 1rem;
    padding: 1rem 1.5rem;
  }
  .game-title {
    font-size: 2rem;
  }
}
