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

:root {
  --bg-base:       #1a1a1a;
  --bg-surface:    #242424;
  --bg-elevated:   #2a2a2a;
  --border:        rgba(255, 255, 255, 0.08);
  --text-primary:  #f5f5f5;
  --text-muted:    #737373;
  --orange:        #FF5B11;
  --orange-light:  #FF9758;
  --tile-bg:       #2a2a2a;
  --tile-border:   rgba(255, 255, 255, 0.1);
  --tile-hover:    #333333;
  --tile-glow:     rgba(255, 91, 17, 0.25);
  --radius-tile:   10px;
  --gap:           6px;
  --transition:    160ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
               'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-base);
  color: var(--text-primary);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  overflow: auto;
}

/* ---- LAYOUT ---- */

#gameContainer {
  width: 100%;
  max-width: 540px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 20px 16px 24px;
}

/* ---- HEADER ---- */

#header {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#titleArea {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

#gameTitle {
  font-size: clamp(1.25rem, 5vw, 1.6rem);
  font-weight: 700;
  background: linear-gradient(135deg, #FF5B11 0%, #FF9758 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

#sizeSelector {
  display: flex;
  gap: 4px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
}

.size-btn {
  padding: 5px 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

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

.size-btn.active {
  background: linear-gradient(135deg, #FF5B11 0%, #FF9758 100%);
  color: #fff;
  box-shadow: 0 2px 8px rgba(255, 91, 17, 0.35);
}

#statsBar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-box {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
}

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

.stat-value {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* ---- BUTTONS ---- */

.action-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border: none;
  border-radius: 10px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.action-btn svg { width: 16px; height: 16px; flex-shrink: 0; }

.action-btn.primary {
  background: linear-gradient(135deg, #FF5B11 0%, #FF9758 100%);
  color: #fff;
  box-shadow: 0 4px 15px rgba(255, 91, 17, 0.3);
}

.action-btn.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(255, 91, 17, 0.45);
}

.action-btn.primary:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(255, 91, 17, 0.3);
}

.action-btn.secondary {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.action-btn.secondary:hover {
  background: #333;
  border-color: rgba(255, 255, 255, 0.15);
}

#shuffleBtn { margin-left: auto; flex-shrink: 0; }

/* ---- BOARD ---- */

#boardWrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

#board {
  display: grid;
  position: relative;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.5),
    0 0 40px rgba(255, 91, 17, 0.06);
}

/* ---- TILES ---- */

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--tile-bg);
  border: 1px solid var(--tile-border);
  border-radius: var(--radius-tile);
  color: var(--text-primary);
  font-weight: 700;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition:
    background-color var(--transition),
    border-color var(--transition),
    box-shadow var(--transition),
    transform 80ms ease;
  -webkit-tap-highlight-color: transparent;
}

.tile::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg,
    rgba(255,255,255,0.06) 0%, transparent 60%);
  border-radius: inherit;
  pointer-events: none;
}

.tile:hover {
  background: var(--tile-hover);
  border-color: rgba(255, 91, 17, 0.3);
  box-shadow: 0 0 12px var(--tile-glow);
}

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

.tile.sliding {
  background: #333;
  border-color: rgba(255, 91, 17, 0.5);
  box-shadow: 0 0 18px var(--tile-glow);
  z-index: 2;
}

.tile.correct {
  border-color: rgba(255, 91, 17, 0.45);
  background: linear-gradient(135deg,
    rgba(255,91,17,0.12) 0%,
    rgba(255,151,88,0.06) 100%);
}

.tile.empty {
  background: transparent;
  border-color: transparent;
  cursor: default;
  box-shadow: none;
  pointer-events: none;
}
.tile.empty::before { display: none; }

.tile-number {
  position: relative;
  z-index: 1;
  line-height: 1;
  letter-spacing: -0.01em;
}

/* Grid-size font scale */
#board.grid-3 .tile { font-size: clamp(1.6rem, 8vw, 2.4rem); border-radius: 12px; }
#board.grid-4 .tile { font-size: clamp(1.1rem, 5vw, 1.7rem); border-radius: var(--radius-tile); }
#board.grid-5 .tile { font-size: clamp(0.8rem, 3.5vw, 1.2rem); border-radius: 8px; }

/* Board win flash */
#board.won {
  animation: boardWin 0.6s ease-out;
}

@keyframes boardWin {
  0%   { box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 0 rgba(255,91,17,0); }
  40%  { box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 0 3px rgba(255,91,17,0.6), 0 0 60px rgba(255,91,17,0.3); }
  100% { box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 40px rgba(255,91,17,0.06); }
}

/* ---- MODAL ---- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  transition: opacity 0.25s ease;
}

.modal.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-content {
  background: var(--bg-elevated);
  border: 1px solid rgba(255,91,17,0.25);
  border-radius: 20px;
  padding: 36px 32px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  box-shadow:
    0 25px 70px rgba(0,0,0,0.6),
    0 0 40px rgba(255,91,17,0.1);
  animation: modalIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes modalIn {
  from { transform: scale(0.88) translateY(12px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}

.win-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 16px;
  background: linear-gradient(135deg, #FF5B11 0%, #FF9758 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(255,91,17,0.4);
}

.win-icon svg { width: 36px; height: 36px; color: #fff; }

.win-title {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.win-subtitle {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.win-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  margin-bottom: 24px;
}

.win-stat {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.win-stat.highlight {
  border-color: rgba(255,91,17,0.4);
  background: rgba(255,91,17,0.08);
}

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

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

.modal-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.modal-buttons .action-btn {
  flex: 1;
  justify-content: center;
}

/* ---- RESPONSIVE ---- */

@media (max-height: 600px) {
  #gameContainer { padding: 10px 12px 12px; gap: 8px; }
  #gameTitle     { font-size: 1.1rem; }
  .stat-value    { font-size: 1.1rem; }
  .stat-box      { padding: 7px 6px; }
  .modal-content { padding: 24px 20px; }
  .win-icon      { width: 48px; height: 48px; }
  .win-title     { font-size: 1.4rem; }
}

@media (max-width: 360px) {
  :root          { --gap: 4px; }
  #gameContainer { padding: 12px 10px 16px; }
  .action-btn    { padding: 9px 14px; font-size: 0.8rem; }
  .win-stats     { grid-template-columns: 1fr 1fr; }
}

@media (hover: none) and (pointer: coarse) {
  .tile:hover {
    background: var(--tile-bg);
    border-color: var(--tile-border);
    box-shadow: none;
  }
  .tile.correct:hover {
    border-color: rgba(255,91,17,0.45);
    background: linear-gradient(135deg,
      rgba(255,91,17,0.12) 0%, rgba(255,151,88,0.06) 100%);
  }
}
