/* ============================================================
   2048 — The Master Edition | Global Styles
   ============================================================ */

/* ---- Reset & Base ---- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  /* Channel 3 Palette */
  --coffee:        #7F5539;
  --lion:          #A68A64;
  --almond:        #EDE0D4;
  --reseda:        #656D4A;
  --ebony:         #414833;

  /* Semantic mappings */
  --bg:            var(--almond);
  --board-bg:      var(--ebony);
  --cell-empty:    var(--reseda);
  --text-dark:     var(--ebony);
  --text-light:    var(--almond);
  --text-muted:    var(--lion);

  /* UI */
  --btn-primary:      var(--coffee);
  --btn-danger:       #c0392b;
  --radius:           8px;
  --radius-lg:        16px;
  --screen-transition: 220ms;
  --shadow-soft:      0 4px 20px rgba(65,72,51,0.12);
  --shadow-card:      0 8px 32px rgba(65,72,51,0.22);
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--font);
  color: var(--text-dark);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ============================================================
   APP CONTAINER
   ============================================================ */
#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ============================================================
   SCREENS — base + transition
   ============================================================ */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--screen-transition) ease;
  overflow: hidden;

  /* Safe-area insets for notched phones */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

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

/* ============================================================
   MENU SCREEN
   ============================================================ */
#screen-menu {
  background: var(--bg);
  align-items: center;
  justify-content: center;
}

/* Animated tile background */
.menu-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  opacity: 0.06;
}

.menu-content {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  padding: 20px;
  width: 100%;
  max-width: 420px;
  z-index: 1;
}

.menu-title-wrap {
  text-align: center;
}

.title {
  font-size: clamp(40px, 11vw, 72px);
  font-weight: 900;
  color: var(--text-dark);
  letter-spacing: -2px;
  line-height: 0.9;
  text-shadow: 0 6px 24px rgba(119,110,101,0.20);
}

.subtitle {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-top: 10px;
}

.subtitle a {
  color: var(--coffee);
  text-decoration: none;
  transition: color 150ms;
}

.subtitle a:hover {
  color: var(--text-dark);
}

.menu-scores {
  display: flex;
  gap: 12px;
}

.score-chip {
  background: var(--board-bg);
  border-radius: var(--radius);
  padding: 12px 28px;
  text-align: center;
  min-width: 110px;
  box-shadow: var(--shadow-soft);
}

.score-chip .score-label {
  display: block;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 2px;
  color: rgba(249,246,242,0.75);
  margin-bottom: 4px;
}

.score-chip .score-value {
  display: block;
  font-size: 24px;
  font-weight: 800;
  color: var(--text-light);
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

.menu-row {
  display: flex;
  gap: 10px;
}

.menu-row .btn {
  flex: 1;
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 15px 20px;
  border: none;
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 1px;
  cursor: pointer;
  user-select: none;
  transition: transform 80ms ease, filter 100ms ease, box-shadow 100ms ease;
  outline: none;
  touch-action: manipulation;
}

.btn:active {
  transform: scale(0.95);
}

.btn-primary {
  background: var(--btn-primary);
  color: var(--text-light);
  box-shadow: 0 4px 12px rgba(143,122,102,0.3);
}
.btn-primary:hover { filter: brightness(1.08); }

.btn-secondary {
  background: var(--board-bg);
  color: var(--text-light);
}
.btn-secondary:hover { filter: brightness(1.15); }

.btn-danger {
  background: var(--btn-danger);
  color: white;
}
.btn-danger:hover { filter: brightness(1.1); }

.btn-sm {
  padding: 10px 16px;
  font-size: 12px;
}

/* ============================================================
   GAME SCREEN
   ============================================================ */
#screen-game {
  background: var(--bg);
  /* No extra padding — canvas handles all internal layout */
  padding-top: 0;
  padding-bottom: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#game-canvas {
  display: block;
  width: 100%;
  flex: 0 0 auto;
  touch-action: manipulation;
}

/* ============================================================
   SCREEN HEADER (shared: History, Settings)
   ============================================================ */
.screen-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  gap: 12px;
  border-bottom: 2px solid rgba(187,173,160,0.4);
  flex-shrink: 0;
}

.screen-header h2 {
  font-size: 22px;
  font-weight: 800;
  color: var(--text-dark);
}

.btn-back {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--btn-primary);
  font-weight: 700;
  padding: 6px 10px;
  border-radius: var(--radius);
  transition: background 100ms;
  line-height: 1;
}
.btn-back:hover { background: rgba(0,0,0,0.05); }

/* ============================================================
   HISTORY SCREEN
   ============================================================ */
#screen-history {
  background: var(--bg);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  font-size: 15px;
  margin-top: 40px;
}

.history-date-group {
  margin-bottom: 24px;
}

.history-date-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 8px;
  padding-left: 4px;
}

.history-entry {
  background: white;
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow-soft);
  gap: 12px;
}

.history-entry.daily-best {
  background: linear-gradient(135deg, var(--coffee), var(--ebony));
}
.history-entry.daily-best .he-score,
.history-entry.daily-best .he-meta { color: white; }
.history-entry.daily-best .he-max-tile { color: rgba(255,255,255,0.85); }

.he-left { display: flex; flex-direction: column; gap: 3px; }

.he-score {
  font-size: 22px;
  font-weight: 800;
  color: var(--text-dark);
}

.he-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.he-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.he-max-tile {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
}

.he-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  justify-content: flex-end;
}

/* ============================================================
   TAG PILLS
   ============================================================ */
.tag-pill {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 100px;
  color: white;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

/* Game-over large pills */
.tag-pill-lg {
  font-size: 13px;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 100px;
  color: white;
  letter-spacing: 0.5px;
  opacity: 0;
  transform: scale(0.6) translateY(8px);
  animation: tagPop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes tagPop {
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.tag-purist     { background: #6c5ce7; }
.tag-fossil     { background: #fd9644; }
.tag-summit     { background: linear-gradient(135deg,#f9ca24,#f0932b); }
.tag-overclock  { background: linear-gradient(135deg,#e55039,#c0392b); }
.tag-tortoise   { background: #27ae60; }
.tag-speedrun   { background: #00b894; }
.tag-cleansweep { background: #74b9ff; color: #2d3436; }
.tag-surgeon    { background: #a29bfe; }

/* ---- Tag tooltips ---- */
.tag-pill,
.tag-pill-lg {
  position: relative;
  cursor: pointer;
  user-select: none;
}

.tag-pill::after,
.tag-pill-lg::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%) scale(0.85);
  background: rgba(30,26,22,0.94);
  color: #f9f6f2;
  font-size: 11px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 150ms, transform 150ms;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0,0,0,0.32);
  letter-spacing: 0.1px;
  max-width: 200px;
  white-space: normal;
  text-align: center;
}

.tag-pill.tooltip-open::after,
.tag-pill-lg.tooltip-open::after {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

/* ============================================================
   SETTINGS SCREEN
   ============================================================ */
#screen-settings {
  background: var(--bg);
}

.settings-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

.settings-section {
  margin-bottom: 24px;
}

.settings-section-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0 4px;
  margin-bottom: 8px;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background: white;
  border-radius: var(--radius);
  margin-bottom: 6px;
  gap: 16px;
  box-shadow: var(--shadow-soft);
}

.setting-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.setting-label span:first-child {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-dark);
}

.setting-desc {
  font-size: 12px;
  color: var(--text-muted) !important;
  font-weight: 400 !important;
}

/* Toggle Switch */
.toggle-wrap {
  flex-shrink: 0;
  cursor: pointer;
}

.toggle {
  appearance: none;
  -webkit-appearance: none;
  width: 50px;
  height: 28px;
  background: var(--cell-empty);
  border-radius: 14px;
  position: relative;
  cursor: pointer;
  transition: background 200ms ease;
  display: block;
}

.toggle:checked {
  background: var(--btn-primary);
}

.toggle::after {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  top: 3px;
  left: 3px;
  transition: left 200ms ease;
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}

.toggle:checked::after { left: 25px; }

/* Range slider */
input[type="range"] {
  width: 120px;
  accent-color: var(--btn-primary);
  cursor: pointer;
}

.cheat-buttons {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.cheat-buttons .btn {
  flex: 1;
  font-size: 12px;
  padding: 10px;
}

.admin-footer {
  text-align: center;
  padding: 16px 20px calc(16px + env(safe-area-inset-bottom));
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
  letter-spacing: 1px;
  flex-shrink: 0;
}

/* ============================================================
   OVERLAYS (Win, Game Over, Admin)
   ============================================================ */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(237, 224, 212, 0.84);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease;
  z-index: 200;
  padding: 24px;
}

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

.overlay-card {
  background: white;
  border-radius: var(--radius-lg);
  padding: 32px 28px 28px;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: cardIn 200ms cubic-bezier(0.34, 1.4, 0.64, 1) forwards;
}

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

.overlay-sub {
  color: var(--text-muted);
  font-size: 14px;
}

.overlay-buttons {
  display: flex;
  gap: 10px;
}

.overlay-buttons .btn {
  flex: 1;
}

/* ---- Win Overlay ---- */
.win-glow {
  font-size: 52px;
  line-height: 1;
  filter: drop-shadow(0 0 16px rgba(237,194,46,0.6));
  animation: winBounce 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes winBounce {
  0%   { transform: scale(0); }
  70%  { transform: scale(1.2); }
  100% { transform: scale(1); }
}

.win-title {
  font-size: 64px;
  font-weight: 900;
  background: linear-gradient(135deg, #edcf72 0%, var(--coffee) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -2px;
  line-height: 1;
}

.win-timer-bar {
  height: 4px;
  background: rgba(0,0,0,0.08);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 4px;
}

.win-timer-fill {
  height: 100%;
  background: var(--btn-primary);
  border-radius: 2px;
  width: 100%;
  transform-origin: left center;
}

/* ---- Game Over Overlay ---- */
.go-title {
  font-size: 36px;
  font-weight: 900;
  color: var(--text-dark);
}

.go-score-wrap {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.go-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--text-muted);
}

.go-score {
  font-size: 52px;
  font-weight: 900;
  color: var(--text-dark);
  line-height: 1.1;
}

.go-new-best {
  font-size: 14px;
  font-weight: 700;
  color: #f0932b;
}

.go-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  min-height: 28px;
}

/* ---- Admin Modal ---- */
.admin-card {
  gap: 20px;
}

.admin-dots {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin: 4px 0;
}

.dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--cell-empty);
  transition: background 150ms ease, transform 150ms ease;
}

.dot.filled {
  background: var(--btn-primary);
  transform: scale(1.2);
}

.dot.error {
  background: var(--btn-danger);
  animation: shake 0.3s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-4px); }
  40%, 80% { transform: translateX(4px); }
}

.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.numpad-key {
  padding: 16px;
  border: none;
  border-radius: var(--radius);
  background: var(--bg);
  font-family: var(--font);
  font-size: 20px;
  font-weight: 700;
  color: var(--text-dark);
  cursor: pointer;
  transition: transform 80ms ease, background 100ms;
  touch-action: manipulation;
}

.numpad-key:active { transform: scale(0.9); }
.numpad-key:hover  { background: var(--cell-empty); }
.numpad-cancel     { color: var(--btn-danger); }
.numpad-del        { color: var(--text-muted); }

/* ============================================================
   SCROLLBAR
   ============================================================ */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--board-bg); border-radius: 2px; }

/* ============================================================
   SCORE BREAKDOWN (tag bonuses shown on game-over)
   ============================================================ */
.go-score-breakdown {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
  margin-top: 8px;
  max-width: 300px;
}

.go-base-score {
  width: 100%;
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.go-bonus-line {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 20px;
  background: rgba(237,194,46,0.18);
  border: 1px solid rgba(237,194,46,0.45);
  color: #7a6020;
  font-size: 11px;
  font-weight: 700;
}

/* ============================================================
   DISCOVERY PANEL — thin floating strip, game stays full-size
   ============================================================ */
.discovery-panel {
  position: absolute;
  bottom: 8px;
  left: 0;         /* overridden by JS to match board width */
  right: 0;        /* overridden by JS */
  height: 0;
  overflow: hidden;
  background: rgba(65, 72, 51, 0.97);  /* Ebony on-palette */
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(237,224,212,0.14);
  border-radius: 10px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.35);
  transition: height 320ms cubic-bezier(0.34, 1.1, 0.64, 1);
  z-index: 5;
}

.discovery-panel.visible {
  height: 56px;
}

.discovery-inner {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  padding: 4px 8px;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  scroll-snap-type: y mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;          /* hide scrollbar — swipe to scroll */
}
.discovery-inner::-webkit-scrollbar { display: none; }

/* Individual discovery chip — tiny square thumb + title */
.disc-card {
  flex: 0 0 40px;
  height: 40px;
  width: 100%;
  box-sizing: border-box;
  scroll-snap-align: start;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 7px;
  background: rgba(237,224,212,0.08);
  border: 1px solid rgba(237,224,212,0.12);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  text-decoration: none;
  padding-right: 10px;
  transition: background 120ms, transform 100ms;
  animation: discSlideIn 0.35s cubic-bezier(0.34, 1.4, 0.64, 1) both;
  white-space: nowrap;
}

.disc-card:hover,
.disc-card:active {
  background: rgba(237,224,212,0.16);
  transform: scale(1.04);
}

@keyframes discSlideIn {
  from { opacity: 0; transform: translateY(10px) scale(0.9); }
  to   { opacity: 1; transform: translateY(0)    scale(1);   }
}

/* Tiny square thumbnail */
.disc-thumb {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  object-fit: cover;
  display: block;
}

/* Title */
.disc-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--almond);
  letter-spacing: 0.2px;
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Tile-value badge on the right side of each card */
.disc-num {
  flex: 0 0 auto;
  font-size: 10px;
  font-weight: 800;
  color: var(--almond);
  background: rgba(237,224,212,0.12);
  border: 1px solid rgba(237,224,212,0.18);
  border-radius: 5px;
  padding: 2px 7px;
  letter-spacing: 0.3px;
  opacity: 0.7;
}

/* NEW dot indicator */
.disc-card.is-new .disc-title::before {
  content: '· ';
  color: #edcf72;
  animation: discPulse 1.2s ease infinite;
}

@keyframes discPulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.3; }
}

/* Discovery toast notification */
.disc-toast {
  position: absolute;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(65,72,51,0.94);
  color: var(--almond);
  border: 1px solid rgba(237,224,212,0.20);
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.3px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 20;
  animation: discToastAnim 2.6s ease forwards;
}

@keyframes discToastAnim {
  0%   { opacity: 0; transform: translateX(-50%) translateY(-6px); }
  12%  { opacity: 1; transform: translateX(-50%) translateY(0); }
  75%  { opacity: 1; }
  100% { opacity: 0; transform: translateX(-50%) translateY(-4px); }
}

/* ============================================================
   UTILITY
   ============================================================ */
.hidden { display: none !important; }
