/* =========================================================================
   style.css — GABUNG!
   Tema gelap (default) & terang, layout responsif, animasi ringan berbasis
   transform/opacity (bukan top/left) agar hemat reflow/repaint.
   ========================================================================= */

:root,
[data-theme="dark"] {
  --navy: #0D1B2A;
  --navy-2: #1A2D42;
  --teal: #1B998B;
  --teal-light: #2BC0B4;
  --accent: #E8A838;
  --ink: #FAF8F4;
  --ink-mid: rgba(255, 255, 255, 0.65);
  --ink-dim: rgba(255, 255, 255, 0.35);
  --line: rgba(255, 255, 255, 0.08);
  --overlay-bg: rgba(13, 27, 42, 0.92);
  --panel-bg: #14263A;
  --cell-bg: rgba(255, 255, 255, 0.04);
}

[data-theme="light"] {
  --navy: #F3EFE7;
  --navy-2: #FFFFFF;
  --teal: #1B998B;
  --teal-light: #12897C;
  --accent: #C9862A;
  --ink: #1E2430;
  --ink-mid: rgba(20, 25, 35, 0.65);
  --ink-dim: rgba(20, 25, 35, 0.4);
  --line: rgba(20, 25, 35, 0.1);
  --overlay-bg: rgba(243, 239, 231, 0.94);
  --panel-bg: #FFFFFF;
  --cell-bg: rgba(20, 25, 35, 0.045);
}

* , *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--navy);
  color: var(--ink);
  min-height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  outline: none;
  transition: background-color 0.25s ease, color 0.25s ease;
}

a { color: inherit; }

.page {
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
  padding: clamp(1.5rem, 5vw, 2.5rem) clamp(1.1rem, 4vw, 1.5rem) 3rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(1.1rem, 3vw, 1.5rem);
}

/* ---------- Back button ---------- */
.btn-back {
  align-self: flex-start;
  display: inline-flex; align-items: center; gap: 8px;
  font-family: 'DM Mono', monospace; font-size: 0.72rem; font-weight: 500;
  letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--ink-mid); text-decoration: none;
  background: transparent; border: 1.5px solid var(--line);
  padding: 0.55rem 1.1rem; border-radius: 4px;
  transition: border-color 0.2s ease, color 0.2s ease;
  cursor: pointer;
}
.btn-back svg { transition: transform 0.2s ease; }
.btn-back:hover, .btn-back:focus-visible { border-color: var(--teal-light); color: var(--teal-light); }
.btn-back:hover svg, .btn-back:focus-visible svg { transform: translateX(-3px); }

/* ---------- Header ---------- */
header {
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.section-tag {
  font-family: 'DM Mono', monospace; font-size: 0.68rem; color: var(--teal-light);
  letter-spacing: 0.12em; text-transform: uppercase;
  display: flex; align-items: center; gap: 8px; margin-bottom: 0.4rem;
}
.section-tag::after { content: ''; flex: 0 0 28px; height: 1px; background: var(--teal-light); }
.title {
  font-family: 'DM Serif Display', serif;
  font-weight: 400;
  font-size: clamp(2rem, 8vw, 2.6rem);
  line-height: 1;
  color: var(--ink);
}
.title em { font-style: italic; color: var(--teal-light); }
.subtitle { font-size: 0.85rem; color: var(--ink-mid); margin-top: 6px; font-weight: 400; }

.scores { display: flex; gap: 8px; }
.score-box {
  background: var(--navy-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  text-align: center;
  min-width: 64px;
}
.score-box .label {
  font-family: 'DM Mono', monospace;
  font-size: 0.58rem; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--ink-dim); font-weight: 500;
}
.score-box .value {
  font-family: 'DM Serif Display', serif;
  font-size: 1.3rem; font-weight: 400; color: var(--accent);
}

/* ---------- Toolbar ---------- */
.toolbar {
  width: 100%;
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}
.hint {
  font-family: 'DM Mono', monospace;
  font-size: 0.72rem; color: var(--ink-dim); font-weight: 500;
  letter-spacing: 0.01em;
}
.toolbar-actions { display: flex; gap: 8px; }

.icon-btn, .newgame-btn {
  font-family: 'DM Mono', monospace;
  font-weight: 500; font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase;
  background: transparent;
  color: var(--teal-light);
  border: 1.5px solid rgba(27, 153, 139, 0.45);
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.icon-btn:hover, .newgame-btn:hover { background: rgba(27, 153, 139, 0.12); border-color: var(--teal-light); }
.icon-btn:active, .newgame-btn:active { transform: translateY(1px); }
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.icon-btn:disabled:hover { background: transparent; border-color: rgba(27, 153, 139, 0.45); }

/* ---------- Board ---------- */
.board-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--navy-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.25);
  touch-action: none;
  -ms-touch-action: none;
  overscroll-behavior: contain;
  -webkit-user-select: none;
  user-select: none;
  transition: box-shadow 0.3s ease;
}
.board-frame.glow-pulse { animation: boardGlow 0.7s ease; }
@keyframes boardGlow {
  0% { box-shadow: 0 14px 34px rgba(0, 0, 0, 0.25), 0 0 0 rgba(232, 168, 56, 0); }
  40% { box-shadow: 0 14px 34px rgba(0, 0, 0, 0.25), 0 0 26px rgba(232, 168, 56, 0.55); }
  100% { box-shadow: 0 14px 34px rgba(0, 0, 0, 0.25), 0 0 0 rgba(232, 168, 56, 0); }
}

.board {
  position: relative; width: 100%; height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
}
.cell-bg {
  background: var(--cell-bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.tiles-layer {
  position: absolute; inset: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
  pointer-events: none;
}

.tile {
  position: absolute;
  top: 0; left: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px;
  font-family: 'DM Mono', monospace;
  font-weight: 500;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25), inset 0 -2px 0 rgba(0, 0, 0, 0.12);
  /* transform dipakai untuk posisi (bukan top/left) supaya browser hanya
     perlu compositing, bukan layout ulang, saat tile bergeser. */
  transition: transform 0.12s ease;
  will-change: transform;
}
.tile.pop { animation: tilePop 0.14s ease; }
@keyframes tilePop {
  0% { transform: scale(0.55); opacity: 0.4; }
  70% { transform: scale(1.06); }
  100% { transform: scale(1); }
}
.tile.merged { animation: tileMerge 0.18s ease; }
@keyframes tileMerge {
  0% { transform: scale(1); }
  35% { transform: scale(1.16); }
  100% { transform: scale(1); }
}
.tile.special {
  box-shadow: 0 0 0 1.5px var(--accent), 0 6px 16px rgba(232, 168, 56, 0.35);
}

/* Pastikan animasi transform (scale) di atas tidak menimpa transform posisi.
   Kelas .pop/.merged dipakai sesaat lalu dilepas oleh animation.js. */

/* ---------- Confetti (efek menang) ---------- */
.confetti-piece {
  position: absolute;
  top: -10px;
  width: 7px; height: 12px;
  border-radius: 2px;
  opacity: 0.95;
  animation: confettiFall 1.3s cubic-bezier(.25,.46,.45,.94) forwards;
  pointer-events: none;
}
@keyframes confettiFall {
  0% { transform: translate(0, -10px) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--drift), 340px) rotate(340deg); opacity: 0; }
}

/* ---------- Overlay (menang/kalah) ---------- */
.overlay {
  position: absolute; inset: 0;
  border-radius: 12px;
  background: var(--overlay-bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; text-align: center; padding: 20px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease;
}
.overlay.show { opacity: 1; pointer-events: auto; }
.overlay .msg {
  font-family: 'DM Serif Display', serif;
  font-size: 1.75rem; font-weight: 400; color: var(--accent);
}
.overlay .sub { color: var(--ink-mid); font-size: 0.88rem; font-weight: 400; }
.overlay-btns { display: flex; gap: 10px; margin-top: 6px; flex-wrap: wrap; justify-content: center; }
.overlay button {
  font-family: 'DM Mono', monospace;
  font-weight: 500; font-size: 0.78rem; letter-spacing: 0.04em; text-transform: uppercase;
  border-radius: 6px; padding: 10px 18px; cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}
.overlay .primary { background: var(--teal); color: #fff; border: 1.5px solid var(--teal); }
.overlay .primary:hover { background: var(--teal-light); border-color: var(--teal-light); }
.overlay .secondary { background: transparent; color: var(--ink-mid); border: 1.5px solid var(--line); }
.overlay .secondary:hover { border-color: var(--teal-light); color: var(--teal-light); }

/* ---------- Panel (Settings & Statistik) ---------- */
.panel-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 40;
}
.panel-backdrop.show { opacity: 1; pointer-events: auto; }

.panel {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -46%);
  width: min(92vw, 400px);
  max-height: 82vh;
  overflow-y: auto;
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1.4rem;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.4);
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.panel.show { opacity: 1; pointer-events: auto; transform: translate(-50%, -50%); }

.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1.1rem;
}
.panel-header h2 {
  font-family: 'DM Serif Display', serif;
  font-weight: 400; font-size: 1.3rem;
}
.panel-close {
  background: transparent; border: none; color: var(--ink-mid);
  cursor: pointer; font-size: 1.1rem; line-height: 1; padding: 4px;
  border-radius: 4px;
}
.panel-close:hover, .panel-close:focus-visible { color: var(--teal-light); }

.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.setting-row:last-of-type { border-bottom: none; }
.setting-row span.label-text { font-size: 0.9rem; color: var(--ink); }
.setting-row span.label-sub { display: block; font-size: 0.72rem; color: var(--ink-dim); margin-top: 2px; }

.switch { position: relative; width: 42px; height: 24px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0; background: var(--line); border-radius: 999px;
  transition: background-color 0.2s ease;
}
.switch .thumb {
  position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  background: var(--ink); border-radius: 50%;
  transition: transform 0.2s ease;
}
.switch input:checked ~ .track { background: var(--teal); }
.switch input:checked ~ .thumb { transform: translateX(18px); background: #fff; }
.switch input:focus-visible ~ .track { outline: 2px solid var(--teal-light); outline-offset: 2px; }

select.select-input {
  font-family: 'DM Mono', monospace; font-size: 0.78rem;
  background: var(--navy); color: var(--ink);
  border: 1.5px solid var(--line); border-radius: 6px;
  padding: 6px 10px; cursor: pointer;
}
select.select-input:focus-visible { outline: 2px solid var(--teal-light); outline-offset: 2px; }

.danger-btn {
  width: 100%;
  font-family: 'DM Mono', monospace; font-size: 0.74rem; letter-spacing: 0.03em; text-transform: uppercase;
  background: transparent; color: #E8746A;
  border: 1.5px solid rgba(232, 116, 106, 0.4); border-radius: 6px;
  padding: 9px 12px; margin-top: 10px; cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}
.danger-btn:hover, .danger-btn:focus-visible { background: rgba(232, 116, 106, 0.1); border-color: #E8746A; }

.stats-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
}
.stat-card {
  background: var(--navy); border: 1px solid var(--line); border-radius: 8px;
  padding: 12px; text-align: center;
}
.stat-card .stat-value {
  font-family: 'DM Serif Display', serif; font-size: 1.4rem; color: var(--accent);
}
.stat-card .stat-label {
  font-family: 'DM Mono', monospace; font-size: 0.62rem; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--ink-dim); margin-top: 4px;
}

/* ---------- Footer ---------- */
footer {
  color: var(--ink-dim);
  font-family: 'DM Mono', monospace;
  font-size: 0.7rem; text-align: center; line-height: 1.6; letter-spacing: 0.01em;
}

/* ---------- Aksesibilitas ---------- */
:focus-visible { outline: 2px solid var(--teal-light); outline-offset: 2px; }
button, select, a { -webkit-tap-highlight-color: transparent; }

@media (prefers-reduced-motion: reduce) {
  .tile, .tile.pop, .tile.merged,
  .board-frame.glow-pulse,
  .confetti-piece {
    animation: none !important;
    transition: none !important;
  }
}

/* ---------- Responsif ---------- */
@media (max-width: 380px) {
  .toolbar-actions { gap: 6px; }
  .icon-btn, .newgame-btn { padding: 7px 10px; font-size: 0.66rem; }
}

@media (min-width: 640px) {
  .page { max-width: 520px; }
}
