/* ================= Theme ================= */
:root {
  --bg: #f8f2ea;
  --blob1: rgba(255, 170, 120, .28);
  --blob2: rgba(150, 130, 255, .18);
  --text: #4a3f35;
  --muted: #9a8a7c;
  --card: #ffffff;
  --line: #eadfd2;
  --shadow: 0 12px 32px rgba(120, 90, 60, .14);

  --accent: #ff7f50;
  --accent-dark: #e0602f;
  --accent2: #7c6cf2;

  --board: #d9c7b2;
  --board-dark: #b39c84;
  --cell-empty: rgba(255, 255, 255, .4);

  --sd-bg: #ffffff;
  --sd-line: #e4d8ca;
  --sd-bold: #6f5f52;
  --sd-peer: #f7efe6;
  --sd-same: #ffe2cf;
  --sd-sel: #ffcaa8;
  --sd-flash: #c9f2dd;
  --sd-given: #3b322b;
  --sd-user: #6c5ce7;
  --sd-wrong: #e5484d;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17151f;
    --blob1: rgba(255, 127, 80, .14);
    --blob2: rgba(124, 108, 242, .18);
    --text: #f1ece6;
    --muted: #9c95a8;
    --card: #262333;
    --line: #1b1926;
    --shadow: 0 12px 32px rgba(0, 0, 0, .4);

    --board: #2f2b3d;
    --board-dark: #3b3650;
    --cell-empty: rgba(255, 255, 255, .06);

    --sd-bg: #221f2e;
    --sd-line: #36324a;
    --sd-bold: #8c84a8;
    --sd-peer: #2b2839;
    --sd-same: #47364a;
    --sd-sel: #5e3f4a;
    --sd-flash: #2a5546;
    --sd-given: #f1ece6;
    --sd-user: #a99cff;
    --sd-wrong: #ff6b6f;
  }
}

/* ================= Base ================= */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 0% 0%, var(--blob1), transparent 45%),
    radial-gradient(circle at 100% 40%, var(--blob2), transparent 45%);
  background-attachment: fixed;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; }
svg { fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.app { max-width: 500px; margin: 0 auto; padding: 16px 16px 40px; }

/* ================= Top bar & tabs ================= */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.brand { color: inherit; text-decoration: none; display: flex; align-items: center; gap: 8px; font-weight: 900; font-size: 17px; }
.logo {
  width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  font-size: 12px; color: #fff; background: linear-gradient(135deg, #ff9a5b, #ff5d8f);
  box-shadow: 0 4px 10px rgba(255, 93, 143, .35);
}

.tabs, .seg {
  position: relative; display: flex; padding: 4px;
  background: var(--card); box-shadow: var(--shadow);
}
.tabs { border-radius: 999px; }
.seg { border-radius: 13px; }
.tab, .seg button {
  position: relative; z-index: 1; border: 0; background: none; cursor: pointer;
  font-weight: 800; color: var(--muted); transition: color .3s;
}
.tab { text-decoration: none; padding: 8px 18px; border-radius: 999px; font-size: 15px; }
.seg button { padding: 8px 14px; border-radius: 10px; font-size: 14px; }
.tab.active, .seg button.active { color: #fff; }
.tab-indicator, .seg-indicator {
  position: absolute; top: 4px; bottom: 4px; left: 0; width: 0;
  transition: transform .45s cubic-bezier(.5, 1.6, .4, 1), width .45s cubic-bezier(.5, 1.6, .4, 1);
}
.tab-indicator { border-radius: 999px; background: linear-gradient(135deg, #ff9a5b, #ff5d8f); }
.seg-indicator { border-radius: 10px; background: linear-gradient(135deg, #8f7cff, #6c5ce7); }

.panel { display: none; }
.panel.active { display: block; animation: panelIn .45s cubic-bezier(.2, .9, .3, 1); }
@keyframes panelIn { from { opacity: 0; transform: translateY(14px) scale(.985); } }

/* ================= Shared bits ================= */
.g-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.title-block .gt { margin: 0; font-size: 46px; font-weight: 900; line-height: 1; letter-spacing: -1px; }
.title-block p { margin: 6px 0 0; color: var(--muted); font-weight: 700; font-size: 14px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 0; cursor: pointer; font-weight: 800; font-size: 15px;
  padding: 10px 18px; border-radius: 12px; color: #fff;
  background: var(--accent); box-shadow: 0 4px 0 var(--accent-dark);
  transition: transform .1s, box-shadow .1s, opacity .2s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--accent-dark); }
.btn.ghost { background: var(--card); color: var(--text); box-shadow: 0 4px 0 var(--line); }
.btn.ghost:active { box-shadow: 0 1px 0 var(--line); }
.btn:disabled { opacity: .4; cursor: default; transform: none; }
.btn svg { width: 17px; height: 17px; }

.alt-link {
  display: block; width: fit-content; margin: 16px auto 0; padding: 9px 18px; border-radius: 999px;
  font-weight: 900; font-size: 14px; text-decoration: none; color: #fff;
  background: linear-gradient(135deg, #12082b, #3a1470); box-shadow: 0 0 0 1px rgba(255, 43, 214, .5), 0 0 18px rgba(139, 92, 255, .45);
}
.alt-link:hover { box-shadow: 0 0 0 1px rgba(34, 228, 255, .8), 0 0 26px rgba(34, 228, 255, .5); }
.hint { text-align: center; color: var(--muted); font-size: 13px; font-weight: 700; margin-top: 18px; }
kbd {
  font-family: inherit; font-size: 11px; font-weight: 800; padding: 1px 6px; border-radius: 5px;
  background: var(--card); box-shadow: 0 2px 0 var(--line);
}

/* Overlay (both games) */
.overlay {
  position: absolute; inset: 0; z-index: 10; border-radius: inherit;
  display: flex; align-items: center; justify-content: center; text-align: center;
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px);
  opacity: 0; pointer-events: none; transition: opacity .35s ease;
}
.overlay.show { opacity: 1; pointer-events: auto; }
.overlay .card { transform: scale(.85) translateY(16px); transition: transform .5s cubic-bezier(.2, 1.5, .4, 1); padding: 16px; }
.overlay.show .card { transform: none; }
.overlay h2 { margin: 0 0 6px; font-size: 34px; font-weight: 900; }
.overlay p { margin: 0 0 18px; color: var(--muted); font-weight: 700; }
.overlay .row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.spinner {
  width: 46px; height: 46px; margin: 0 auto 14px; border-radius: 50%;
  border: 5px solid var(--sd-line); border-top-color: var(--accent2);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 100; }

/* ================= 2048 ================= */
.scores { display: flex; gap: 8px; }
.score-box {
  position: relative; min-width: 78px; padding: 7px 14px; border-radius: 12px; text-align: center;
  background: var(--board-dark); color: #fff;
}
.score-box span { display: block; font-size: 11px; font-weight: 800; letter-spacing: 1px; text-transform: uppercase; opacity: .8; }
.score-box strong { display: block; font-size: 22px; font-weight: 900; line-height: 1.15; }
.score-box strong.bump { animation: bump .3s ease; }
@keyframes bump { 40% { transform: scale(1.18); } }
.score-add {
  position: absolute; left: 0; right: 0; top: 30%; font-weight: 900; font-size: 20px;
  color: var(--accent); pointer-events: none; animation: scoreUp .9s ease-out forwards;
}
@keyframes scoreUp { 0% { opacity: 1; transform: translateY(0); } 100% { opacity: 0; transform: translateY(-44px); } }

.g-actions { display: flex; justify-content: flex-end; gap: 10px; margin-bottom: 14px; }

.board {
  position: relative; width: 100%; aspect-ratio: 1;
  background: var(--board); border-radius: 18px; box-shadow: var(--shadow);
  container-type: inline-size;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
/* These resolve against the board (container) when used by children */
.grid-bg, .tiles, .fx {
  --gap: 2.9cqw;
  --cell: calc((100% - 5 * var(--gap)) / 4);
}
.grid-bg {
  position: absolute; inset: 0; padding: var(--gap);
  display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); gap: var(--gap);
}
.grid-bg i { background: var(--cell-empty); border-radius: 2.4cqw; }
.tiles, .fx { position: absolute; inset: 0; }
.fx { pointer-events: none; z-index: 6; }

.tile {
  position: absolute; top: var(--gap); left: var(--gap);
  width: var(--cell); height: var(--cell); z-index: 2;
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
  transition: transform 110ms cubic-bezier(.3, .7, .4, 1);
}
.tile.leaving { z-index: 1; }
.tile.merged { z-index: 3; }
.tile-inner {
  width: 100%; height: 100%; border-radius: 2.4cqw;
  display: flex; align-items: center; justify-content: center;
  font-weight: 900; font-size: 11cqw; letter-spacing: -.03em;
  box-shadow: inset 0 -0.9cqw 0 rgba(0, 0, 0, .1);
}
.tile-inner[data-len="3"] { font-size: 9cqw; }
.tile-inner[data-len="4"] { font-size: 7cqw; }
.tile-inner[data-len="5"], .tile-inner[data-len="6"] { font-size: 5.6cqw; }

.tile.new .tile-inner { animation: appear 220ms cubic-bezier(.2, 1.4, .5, 1) 100ms backwards; }
.tile.merged .tile-inner { animation: pop 240ms ease 100ms backwards; }
.tile.fade .tile-inner { animation: fadeIn 200ms ease backwards; }
@keyframes appear { 0% { opacity: 0; transform: scale(0) rotate(-12deg); } 100% { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(0); } 55% { transform: scale(1.22); } 100% { transform: scale(1); } }
@keyframes fadeIn { from { opacity: 0; transform: scale(.9); } }

/* Tile colours — warm sunset into cool */
.tile[data-v="2"]    .tile-inner { background: #fff6ea; color: #7a6656; }
.tile[data-v="4"]    .tile-inner { background: #ffe6c4; color: #7a6656; }
.tile[data-v="8"]    .tile-inner { background: #ffbe7a; color: #fff; }
.tile[data-v="16"]   .tile-inner { background: #ff9d5c; color: #fff; }
.tile[data-v="32"]   .tile-inner { background: #ff7f6e; color: #fff; }
.tile[data-v="64"]   .tile-inner { background: #ff5c64; color: #fff; }
.tile[data-v="128"]  .tile-inner { background: #f7c548; color: #fff; box-shadow: inset 0 -0.9cqw 0 rgba(0,0,0,.1), 0 0 3cqw rgba(247, 197, 72, .55); }
.tile[data-v="256"]  .tile-inner { background: #8fd16a; color: #fff; box-shadow: inset 0 -0.9cqw 0 rgba(0,0,0,.1), 0 0 3cqw rgba(143, 209, 106, .55); }
.tile[data-v="512"]  .tile-inner { background: #3fc6a8; color: #fff; box-shadow: inset 0 -0.9cqw 0 rgba(0,0,0,.1), 0 0 3.5cqw rgba(63, 198, 168, .6); }
.tile[data-v="1024"] .tile-inner { background: #4ea6f5; color: #fff; box-shadow: inset 0 -0.9cqw 0 rgba(0,0,0,.1), 0 0 4cqw rgba(78, 166, 245, .6); }
.tile[data-v="2048"] .tile-inner { background: linear-gradient(135deg, #a07cff, #7c5cf2); color: #fff; animation: glow 1.8s ease-in-out infinite; }
.tile[data-v="super"] .tile-inner { background: linear-gradient(135deg, #3b3550, #24202f); color: #ffd36e; }
.tile[data-v="8"] .tile-inner, .tile[data-v="16"] .tile-inner, .tile[data-v="32"] .tile-inner,
.tile[data-v="64"] .tile-inner { text-shadow: 0 .4cqw 0 rgba(0, 0, 0, .08); }
.tile.merged[data-v="2048"] .tile-inner { animation: pop 240ms ease 100ms backwards, glow 1.8s ease-in-out 400ms infinite; }
@keyframes glow {
  0%, 100% { box-shadow: inset 0 -0.9cqw 0 rgba(0,0,0,.12), 0 0 3cqw rgba(140, 100, 255, .5); }
  50%      { box-shadow: inset 0 -0.9cqw 0 rgba(0,0,0,.12), 0 0 7cqw rgba(140, 100, 255, .85); }
}

.particle {
  position: absolute; width: 1.8cqw; height: 1.8cqw; border-radius: 50%;
  transform: translate(-50%, -50%); animation: burst 650ms cubic-bezier(.1, .8, .3, 1) forwards;
}
@keyframes burst { to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(0); opacity: 0; } }

.board.nudge-left  { animation: nudgeL .24s ease; }
.board.nudge-right { animation: nudgeR .24s ease; }
.board.nudge-up    { animation: nudgeU .24s ease; }
.board.nudge-down  { animation: nudgeD .24s ease; }
@keyframes nudgeL { 40% { transform: translateX(-7px); } }
@keyframes nudgeR { 40% { transform: translateX(7px); } }
@keyframes nudgeU { 40% { transform: translateY(-7px); } }
@keyframes nudgeD { 40% { transform: translateY(7px); } }

/* ================= Sudoku ================= */
.sd-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.sd-stats {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-weight: 700; font-size: 14px; color: var(--muted); margin-bottom: 10px; padding: 0 2px;
}
.sd-stats b { color: var(--text); font-weight: 900; }
.pill { padding: 3px 10px; border-radius: 999px; background: var(--card); color: var(--accent2); font-weight: 900; box-shadow: 0 2px 0 var(--line); }
.timer { display: inline-flex; align-items: center; gap: 5px; font-variant-numeric: tabular-nums; }
.timer svg { width: 16px; height: 16px; }

.sd-wrap { position: relative; border-radius: 16px; box-shadow: var(--shadow); }
.sd-board {
  display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1; background: var(--sd-bg); border: 3px solid var(--sd-bold); border-radius: 16px; overflow: hidden;
  container-type: inline-size; user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.sd-cell {
  position: relative; display: flex; align-items: center; justify-content: center;
  border-right: 1px solid var(--sd-line); border-bottom: 1px solid var(--sd-line);
  font-size: 6.3cqw; font-weight: 700; color: var(--sd-user); cursor: pointer;
  transition: background-color .18s ease;
}
.sd-cell.ec { border-right: 0; }
.sd-cell.er { border-bottom: 0; }
.sd-cell.br { border-right: 2px solid var(--sd-bold); }
.sd-cell.bb { border-bottom: 2px solid var(--sd-bold); }
.sd-cell.given { color: var(--sd-given); font-weight: 800; }
.sd-cell.peer { background: var(--sd-peer); }
.sd-cell.same { background: var(--sd-same); }
.sd-cell.sel { background: var(--sd-sel); }
.sd-cell.wrong { color: var(--sd-wrong); }
.sd-cell .v { display: block; line-height: 1; }

.notes {
  position: absolute; inset: 7%; display: grid;
  grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  font-size: 2.5cqw; font-weight: 800; line-height: 1; color: var(--muted);
}
.notes span { display: flex; align-items: center; justify-content: center; }
.notes span.hl { color: var(--accent); }

.sd-board.entering .sd-cell { animation: cellIn .45s cubic-bezier(.2, 1.4, .4, 1) var(--d) backwards; }
@keyframes cellIn { from { opacity: 0; transform: scale(.4); } }
.sd-cell.placed .v { animation: placeIn .32s cubic-bezier(.2, 1.7, .4, 1); }
@keyframes placeIn { from { transform: scale(.2); opacity: 0; } }
.sd-cell.shake { animation: shake .42s ease; }
@keyframes shake {
  20% { transform: translateX(-4px); } 40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); } 80% { transform: translateX(2px); }
}
.sd-cell.flash { animation: flash .6s ease var(--fd-flash, 0ms); }
.sd-cell.autofill .v { animation: autoIn .65s cubic-bezier(.2, 1.5, .4, 1) var(--fd-autofill, 0ms) backwards; }
@keyframes autoIn {
  0% { opacity: 0; transform: scale(.2) translateY(30%); }
  55% { opacity: 1; transform: scale(1.3); color: #2fb67c; text-shadow: 0 0 10px rgba(47, 182, 124, .6); }
  100% { transform: none; }
}
@keyframes flash { 45% { background-color: var(--sd-flash); transform: scale(.9); border-radius: 6px; } }
.sd-cell.hinted .v { animation: hintIn .8s ease; }
@keyframes hintIn {
  0% { transform: scale(0) rotate(-90deg); opacity: 0; }
  60% { transform: scale(1.35); text-shadow: 0 0 12px var(--accent); color: var(--accent); }
  100% { transform: none; }
}

.sd-controls { display: flex; justify-content: space-around; margin-top: 16px; }
.ctrl {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 5px;
  border: 0; background: none; padding: 2px 8px; cursor: pointer;
  font-size: 13px; font-weight: 800; color: var(--muted);
}
.ctrl .ic {
  width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center;
  background: var(--card); color: var(--text); box-shadow: 0 4px 0 var(--line);
  transition: background .2s, color .2s, transform .1s, box-shadow .1s;
}
.ctrl:active .ic { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.ctrl svg { width: 22px; height: 22px; }
.ctrl.on .ic { background: var(--accent2); color: #fff; }
.badge {
  position: absolute; top: -2px; right: 0; font-style: normal; font-size: 10px; font-weight: 900;
  padding: 1px 6px; border-radius: 999px; background: var(--muted); color: #fff;
}
.ctrl.on .badge { background: var(--accent); }

.sd-pad { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 6px; margin-top: 16px; }
.pad-btn {
  padding: 0; min-width: 0;
  aspect-ratio: .78; border: 0; border-radius: 12px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: var(--card); color: var(--sd-user); box-shadow: 0 4px 0 var(--line);
  transition: transform .1s, box-shadow .1s, opacity .3s;
}
.pad-btn b { font-size: clamp(20px, 6.5vw, 30px); font-weight: 800; line-height: 1; }
.pad-btn small { font-size: 11px; font-weight: 800; color: var(--muted); }
.pad-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.pad-btn.done { opacity: .25; pointer-events: none; }
.notes-mode .pad-btn b { color: var(--muted); font-size: clamp(16px, 5vw, 22px); }

@media (max-width: 380px) {
  .title-block .gt { font-size: 38px; }
  .seg button { padding: 7px 10px; font-size: 13px; }
  .tab { padding: 7px 14px; }
  .brand { font-size: 0; gap: 0; }
}

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