/* ===========================================================================
   base.css — structure, layout and motion. All colour comes from CSS variables
   defined per theme in themes.css; this file never hard-codes a theme colour
   (only the neutral modal scrim and tile inner-highlight, which read fine on
   any palette). The tile-value -> variable mapping lives here because it is the
   same for every theme; only the values change.
   =========================================================================== */

@font-face {
  font-family: 'Fredoka';
  font-style: normal;
  font-weight: 300 700;
  font-display: swap;
  src: url('../fonts/fredoka.woff2') format('woff2');
}

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

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Fredoka', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-weight: 500;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  transition: background-color 0.25s ease;
}

button {
  font-family: inherit;
  color: inherit;
}

/* ------------------------------- layout --------------------------------- */
.app {
  display: flex;
  flex-direction: column;
  gap: clamp(12px, 3.5vw, 22px);
  max-width: 30rem;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 14px) calc(env(safe-area-inset-right) + 16px)
    calc(env(safe-area-inset-bottom) + 14px) calc(env(safe-area-inset-left) + 16px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
}
.brand__badge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--wm-bg);
  color: var(--wm-ink);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.02em;
}
.brand__name {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--text);
}

.controls {
  display: flex;
  gap: 8px;
}

.iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 44px;
  height: 44px;
  padding: 0 11px;
  border: 1.5px solid var(--surface-border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text-dim);
  cursor: pointer;
  transition: transform 0.08s ease, color 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
}
.iconbtn svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.iconbtn:active {
  transform: scale(0.92);
}
.iconbtn:hover {
  color: var(--text);
  border-color: var(--accent);
}
.iconbtn:disabled {
  opacity: 0.38;
  cursor: default;
}
.iconbtn:disabled:hover {
  color: var(--text-dim);
  border-color: var(--surface-border);
}
.iconbtn--undo {
  padding: 0 12px 0 11px;
}
.iconbtn__count {
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: inherit;
}
.iconbtn--plain {
  border: none;
  background: none;
  min-width: 40px;
  height: 40px;
  padding: 0;
}

/* ------------------------------- scores --------------------------------- */
.scores {
  display: flex;
  gap: 10px;
}
.score {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 8px 14px;
  border: 1px solid var(--surface-border);
  border-radius: 14px;
  background: var(--surface);
}
.score__label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.score__value {
  display: inline-block;
  font-size: 23px;
  font-weight: 600;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* -------------------------------- board --------------------------------- */
.boardwrap {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  position: relative;
  width: min(100%, 26rem);
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  background: var(--board);
  touch-action: none;
}
.board__grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(7px, 2.4%, 11px);
  padding: clamp(7px, 2.4%, 11px);
}
.board__cell {
  background: var(--cell);
  border-radius: calc(var(--tile-radius) - 1px);
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  will-change: transform;
  transition: transform 0.11s ease-in-out;
}
.tile__face {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--tile-radius);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  box-shadow: var(--tile-shadow);
  /* fallback for tiles above the explicit ramp */
  background: var(--tbig-bg);
  color: var(--tbig-fg);
}

/* tile value -> palette variable (values supplied by the active theme) */
.tile[data-v='2'] .tile__face { background: var(--t2-bg); color: var(--t2-fg); }
.tile[data-v='4'] .tile__face { background: var(--t4-bg); color: var(--t4-fg); }
.tile[data-v='8'] .tile__face { background: var(--t8-bg); color: var(--t8-fg); }
.tile[data-v='16'] .tile__face { background: var(--t16-bg); color: var(--t16-fg); }
.tile[data-v='32'] .tile__face { background: var(--t32-bg); color: var(--t32-fg); }
.tile[data-v='64'] .tile__face { background: var(--t64-bg); color: var(--t64-fg); }
.tile[data-v='128'] .tile__face { background: var(--t128-bg); color: var(--t128-fg); }
.tile[data-v='256'] .tile__face { background: var(--t256-bg); color: var(--t256-fg); }
.tile[data-v='512'] .tile__face { background: var(--t512-bg); color: var(--t512-fg); }
.tile[data-v='1024'] .tile__face { background: var(--t1024-bg); color: var(--t1024-fg); }
.tile[data-v='2048'] .tile__face { background: var(--t2048-bg); color: var(--t2048-fg); }
.tile[data-v='4096'] .tile__face { background: var(--t4096-bg); color: var(--t4096-fg); }
.tile[data-v='8192'] .tile__face { background: var(--t8192-bg); color: var(--t8192-fg); }

.tile--new .tile__face {
  animation: tileAppear 0.16s ease-out;
}
.tile--merged .tile__face {
  animation: tileMerge 0.17s ease-out;
}

.scorepop {
  position: absolute;
  top: 7%;
  left: 50%;
  z-index: 5;
  font-size: clamp(18px, 5.5vw, 24px);
  font-weight: 700;
  color: var(--accent);
  pointer-events: none;
  animation: floatUp 0.7s ease-out forwards;
}

/* -------------------------------- hint ---------------------------------- */
.hint {
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  color: var(--hint);
  min-height: 1.2em;
  transition: opacity 0.3s ease;
}
.hint--hidden {
  opacity: 0;
}

/* --------------------------- board overlays ----------------------------- */
.boardoverlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  border-radius: 18px;
  text-align: center;
  background: var(--overlay);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  animation: fadeIn 0.18s ease;
}
.boardoverlay[hidden] {
  display: none;
}
.boardoverlay__inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 13px;
}
.boardoverlay__title {
  font-size: clamp(20px, 6vw, 27px);
  font-weight: 700;
  color: var(--text);
}
.boardoverlay__sub {
  max-width: 18rem;
  font-size: 14px;
  color: var(--text-dim);
}
.boardoverlay__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 4px;
}

.badge {
  padding: 3px 11px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.badge[hidden] {
  display: none;
}

.summary {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px 18px;
  margin: 2px 0;
}
.summary__item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.summary__label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.summary__value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* ------------------------------- buttons -------------------------------- */
.btn {
  border: none;
  border-radius: 13px;
  padding: 11px 20px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--tile-shadow);
  transition: transform 0.08s ease, filter 0.12s ease;
}
.btn:hover {
  filter: brightness(1.04);
}
.btn:active {
  transform: scale(0.96);
}
.btn--ghost {
  background: transparent;
  color: var(--text);
  border: 1.5px solid var(--surface-border);
  box-shadow: none;
}

/* -------------------------------- sheets -------------------------------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(12, 12, 18, 0.42);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}
.sheet[hidden] {
  display: none;
}
.sheet__card {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: min(100%, 22rem);
  max-height: 90dvh;
  overflow: auto;
  padding: 20px;
  border: 1px solid var(--surface-border);
  border-radius: 20px;
  background: var(--surface);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
  animation: sheetIn 0.22s cubic-bezier(0.2, 0.9, 0.2, 1);
}
.sheet__card--narrow {
  width: min(100%, 18rem);
  gap: 14px;
}
.sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sheet__title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}
.sheet__foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 14px;
  border-top: 1px solid var(--surface-border);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.field--row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}
.field__label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

/* theme chips */
.themes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.themechip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 8px;
  border: 2px solid var(--surface-border);
  border-radius: 14px;
  background: transparent;
  cursor: pointer;
  transition: border-color 0.12s ease, transform 0.08s ease;
}
.themechip:active {
  transform: scale(0.97);
}
.themechip.is-selected {
  border-color: var(--accent);
}
.themechip__swatch {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.themechip__swatch--blossom {
  background: linear-gradient(135deg, #f2b6c4 0%, #c9c2ee 50%, #aedfd0 100%);
}
.themechip__swatch--daylight {
  background: linear-gradient(135deg, #f4c188 0%, #ee8050 55%, #ecc850 100%);
}
.themechip__swatch--midnight {
  background: linear-gradient(135deg, #222d38 0%, #1fa39a 60%, #5bd9b0 100%);
}
.themechip__name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}

/* toggle switch */
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  border: none;
  background: none;
  cursor: pointer;
}
.toggle__track {
  position: relative;
  width: 46px;
  height: 28px;
  border-radius: 999px;
  background: var(--surface-border);
  transition: background 0.16s ease;
}
.toggle__knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.16s ease;
}
.toggle.is-on .toggle__track {
  background: var(--accent);
}
.toggle.is-on .toggle__knob {
  transform: translateX(18px);
}
.toggle__state {
  min-width: 2.2em;
  text-align: left;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
}

/* segmented control */
.segmented {
  display: inline-flex;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--surface-border);
  border-radius: 11px;
}
.segmented__btn {
  border: none;
  background: transparent;
  padding: 7px 14px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.segmented__btn.is-selected {
  background: var(--accent);
  color: var(--accent-ink);
}

.link {
  border: none;
  background: none;
  padding: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
}
.muted {
  font-size: 13px;
  color: var(--text-dim);
}
.about__line {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
}
.about__copy {
  margin-top: 2px;
}
.confirm__actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* score bump on increase */
.bump {
  animation: bump 0.22s ease;
}

/* ------------------------------ keyframes ------------------------------- */
@keyframes tileAppear {
  from {
    transform: scale(0.5);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}
@keyframes tileMerge {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes floatUp {
  0% {
    opacity: 0;
    transform: translate(-50%, 6px);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -28px);
  }
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes sheetIn {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
@keyframes bump {
  0% {
    transform: scale(1);
  }
  35% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

/* ----------------------------- accessibility ---------------------------- */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:focus:not(:focus-visible) {
  outline: none;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
