:root {
  --wood-dark: #2c1a10;
  --wood: #6b4327;
  --wood-mid: #7d4f2e;
  --wood-light: #a5713f;
  --tray: #1f130b;
  --bg: #ece1cf;
  --bg2: #e2d3ba;
  --panel: #fffbf4;
  --ink: #2a1d14;
  --muted: #8a7a68;
  --muted2: #8a7a68;
  --border: #e2d3bd;
  --accent: #b5442e;
  --accent2: #d15b3f;
  --red: #c23b2c;
  --red-light: #e8624a;
  --blue: #2b5f8a;
  --blue-light: #4f8ab8;
  --white-piece: #f6f1e6;
  --gold: #c99a3e;
  --gold-light: #e6bd6a;
  --bronze: #a3623a;
  --silver: #9aa3ab;
  --shadow: rgba(20, 10, 4, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17110a;
    --bg2: #1d1611;
    --panel: #241a11;
    --ink: #f0e3cd;
    --muted: #b39d81;
    --muted2: #b39d81;
    --border: #3d2d1d;
    --shadow: rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background:
    radial-gradient(ellipse at top, var(--bg2) 0%, var(--bg) 55%),
    repeating-linear-gradient(180deg, rgba(0,0,0,0.015) 0 3px, transparent 3px 7px);
  color: var(--ink);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 16px max(20px, env(safe-area-inset-left));
  background: linear-gradient(180deg, var(--wood-dark), #201209);
  color: #f4ece0;
  box-shadow: 0 3px 10px var(--shadow);
  position: relative;
  z-index: 2;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { color: var(--gold-light); font-size: 20px; text-shadow: 0 0 10px rgba(230,189,106,0.6); }
.brand h1 {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", "Book Antiqua", serif;
  font-size: 22px; margin: 0; letter-spacing: 0.2px; font-weight: 700;
}
.brand h1 em { color: var(--gold-light); font-style: normal; font-weight: 400; opacity: 0.85; }
.player-area { display: flex; align-items: center; gap: 10px; font-size: 13px; color: #d8c7ae; }
.link-btn { background: none; border: none; color: var(--gold-light); cursor: pointer; font-size: 12.5px; text-decoration: underline; padding: 4px; }

.layout { max-width: 1080px; margin: 0 auto; padding: 26px max(16px, env(safe-area-inset-left)) 60px; }

.screen { display: none; }
.screen.active { display: block; animation: fadeIn 0.25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .screen.active { animation: none; }
}

.panel-header { margin-bottom: 20px; display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.panel-header h2 { margin: 0; font-size: 22px; font-family: Georgia, "Iowan Old Style", serif; }
.panel-header .sub { margin: 4px 0 0; color: var(--muted); font-size: 13.5px; width: 100%; line-height: 1.5; }

.back-btn {
  background: none; border: 1px solid var(--border); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 13px;
  min-height: 40px;
}
.back-btn:active { background: var(--bg2); }

/* Levels screen layout */
.levels-layout { display: grid; grid-template-columns: 1fr 300px; gap: 22px; align-items: start; }

.level-list { display: flex; flex-direction: column; gap: 9px; }
.level-row {
  display: grid; grid-template-columns: 40px 1fr auto; align-items: center; gap: 14px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 18px; cursor: pointer; text-align: left; font-size: 15px; color: var(--ink);
  box-shadow: 0 1px 2px var(--shadow);
  transition: border-color 0.15s ease, transform 0.12s ease, box-shadow 0.15s ease;
  min-height: 44px;
}
.level-row:hover { border-color: var(--gold); transform: translateX(3px); box-shadow: 0 4px 14px var(--shadow); }
.level-row:active { transform: translateX(1px) scale(0.995); }
.level-num {
  width: 36px; height: 36px; border-radius: 50%;
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 14px; font-variant-numeric: tabular-nums;
  box-shadow: inset 0 -3px 4px rgba(0,0,0,0.25), inset 0 2px 2px rgba(255,255,255,0.2);
}
.level-name { font-weight: 700; }
.level-tag { display: block; font-size: 11.5px; color: var(--muted); font-weight: 400; margin-top: 1px; }
.level-arrow { color: var(--gold); font-size: 18px; opacity: 0.7; }

/* Tier accent colors, escalating grandeur across the 10 levels */
.tier-1, .tier-2 { background: linear-gradient(160deg, #8a6a4a, var(--bronze)); }
.tier-3, .tier-4 { background: linear-gradient(160deg, #7d8a94, var(--silver)); }
.tier-5, .tier-6 { background: linear-gradient(160deg, #6b8fae, var(--blue)); }
.tier-7, .tier-8 { background: linear-gradient(160deg, #e6bd6a, var(--gold)); }
.tier-9, .tier-10 { background: linear-gradient(160deg, #f0d68a, #c99a3e); box-shadow: 0 0 12px rgba(201,154,62,0.55), inset 0 -3px 4px rgba(0,0,0,0.25); }

/* Side panel (Level Leaders + per-level leaderboard) */
.side-panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; box-shadow: 0 2px 10px var(--shadow);
}
.side-panel-title {
  font-weight: 800; font-size: 13.5px; margin-bottom: 10px; color: var(--ink);
  display: flex; align-items: center; gap: 6px; letter-spacing: 0.2px;
}
.trophy { font-size: 15px; filter: drop-shadow(0 1px 2px rgba(0,0,0,0.3)); }

.leaders-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.leader-row {
  display: grid; grid-template-columns: 28px 1fr auto; align-items: center; gap: 10px;
  padding: 7px 8px; border-radius: 8px; font-size: 12.5px;
  background: rgba(201,154,62,0.05);
}
.leader-row.current { outline: 1.5px solid var(--gold); background: rgba(201,154,62,0.14); }
.leader-medal {
  width: 26px; height: 26px; border-radius: 50%; color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 11px;
  box-shadow: inset 0 -2px 3px rgba(0,0,0,0.25);
}
.leader-info { min-width: 0; }
.leader-level { font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.3px; }
.leader-name-line { display: flex; align-items: baseline; gap: 5px; flex-wrap: wrap; }
.leader-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 110px; }
.leader-compliment { font-size: 11px; font-style: italic; color: var(--accent2); }
.leader-empty { color: var(--muted); font-size: 11px; font-style: italic; }
.leader-score { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); white-space: nowrap; }

.lb-divider { height: 1px; background: var(--border); margin: 14px 0; }

.lb-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lb-list li { display: flex; justify-content: space-between; font-size: 13px; padding: 4px 0; border-bottom: 1px dashed var(--border); }
.lb-list li:first-child .lb-name::before { content: "🏆 "; }
.lb-name { font-weight: 600; }
.lb-score { color: var(--muted); font-variant-numeric: tabular-nums; }
.lb-empty, .lb-loading { color: var(--muted); font-size: 12.5px; font-style: italic; }

/* Game layout */
.game-layout { display: grid; grid-template-columns: 1fr 300px; gap: 22px; align-items: start; }
.game-main { min-width: 0; }

.game-topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 8px;
}
.level-label { font-weight: 700; font-size: 14px; flex: 1; min-width: 140px; }
.stats { display: flex; gap: 16px; font-size: 13px; color: var(--muted); }
.stats b { color: var(--ink); font-variant-numeric: tabular-nums; }
.reset-btn {
  background: linear-gradient(160deg, var(--accent2), var(--accent)); color: #fff; border: none; padding: 9px 16px;
  border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 700; min-height: 40px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.reset-btn:active { transform: translateY(1px); }
.skip-btn {
  background: transparent; color: var(--ink); border: 1px solid var(--border); padding: 9px 14px;
  border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600; min-height: 40px;
}
.skip-btn:hover { border-color: var(--blue-light); color: var(--blue); }
.skip-btn:active { transform: translateY(1px); }

.streak-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  font-size: 12.5px; color: var(--muted); margin: 6px 0 12px;
}
.streak-dots { display: flex; gap: 4px; }
.streak-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--border); transition: background 0.2s ease; }
.streak-dot.filled { background: var(--gold); box-shadow: 0 0 4px rgba(201,154,62,0.6); }

.goal-hint { color: var(--muted); font-size: 13px; margin: 6px 0 16px; }
.white-dot-label { font-weight: 700; color: var(--ink); }

.board-wrap { display: flex; justify-content: center; padding: 10px 0 20px; touch-action: pan-y; }
.board {
  position: relative;
  background:
    repeating-linear-gradient(90deg, rgba(255,255,255,0.02) 0 2px, transparent 2px 10px),
    linear-gradient(160deg, #241509, var(--tray) 60%);
  border-radius: 12px;
  box-shadow: inset 0 0 0 7px var(--wood), inset 0 0 0 9px rgba(0,0,0,0.3), 0 10px 28px var(--shadow);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.cell {
  position: absolute;
  border-radius: 5px;
  background: rgba(0,0,0,0.12);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.3), inset 0 2px 4px rgba(0,0,0,0.25);
}
.goal-cell {
  box-shadow: inset 0 0 0 2px var(--gold-light), inset 0 0 12px rgba(230,189,106,0.35);
  background: rgba(230,189,106,0.09);
  animation: goalPulse 2.2s ease-in-out infinite;
}
@keyframes goalPulse {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--gold-light), inset 0 0 10px rgba(230,189,106,0.3); }
  50% { box-shadow: inset 0 0 0 2px var(--gold-light), inset 0 0 20px rgba(230,189,106,0.6); }
}
@media (prefers-reduced-motion: reduce) {
  .goal-cell { animation: none; }
}

.piece {
  position: absolute;
  border-radius: 10px;
  cursor: grab;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 8px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.18), inset 0 -4px 6px rgba(0,0,0,0.2);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  will-change: left, top;
}
.piece-red { background: linear-gradient(160deg, #9c6640, #6b4327 65%); }
.piece-blue { background: linear-gradient(160deg, #8c6a48, #5f4128 65%); }
.piece-white { background: linear-gradient(160deg, #a5754a, #6b4327 65%); }
.piece.selected { outline: 3px solid var(--gold-light); outline-offset: -3px; }
.piece.dragging {
  transition: none !important; cursor: grabbing; z-index: 20;
  box-shadow: 0 10px 22px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.25);
  filter: brightness(1.06);
}
.piece:not(.dragging) { transition: left 0.16s cubic-bezier(.2,.7,.3,1), top 0.16s cubic-bezier(.2,.7,.3,1); }
.piece.locked-shake { animation: shake 0.28s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}
@media (prefers-reduced-motion: reduce) {
  .piece.locked-shake { animation: none; }
}

.dot {
  width: 44%; height: 44%; min-width: 26px; min-height: 26px;
  border-radius: 50%;
  box-shadow: inset -3px -4px 7px rgba(0,0,0,0.35), inset 3px 3px 5px rgba(255,255,255,0.3), 0 2px 4px rgba(0,0,0,0.35);
}
.dot-red { background: radial-gradient(circle at 32% 28%, #ef7a63, var(--red) 72%); }
.dot-blue { background: radial-gradient(circle at 32% 28%, #6ea3c9, var(--blue) 72%); }
.dot-white { background: radial-gradient(circle at 32% 28%, #ffffff, var(--white-piece) 72%); }

/* Modals */
.modal {
  display: none;
  position: fixed; inset: 0; background: rgba(10,6,3,0.6);
  backdrop-filter: blur(2px);
  align-items: center; justify-content: center; z-index: 50;
  padding: 20px;
}
.modal.open { display: flex; }
.modal-card {
  background: var(--panel); border-radius: 14px; padding: 26px 28px;
  width: min(340px, 100%); text-align: center; box-shadow: 0 16px 48px rgba(0,0,0,0.5);
  border: 1px solid var(--border);
}
.modal-card h3 { margin: 0 0 8px; font-family: Georgia, serif; }
.modal-card p { color: var(--muted); font-size: 13px; margin: 0 0 16px; line-height: 1.5; }
.modal-card input {
  width: 100%; padding: 12px; border-radius: 8px; border: 1px solid var(--border);
  font-size: 16px; margin-bottom: 14px; background: var(--bg); color: var(--ink);
  min-height: 44px;
}
.primary-btn {
  width: 100%; padding: 12px; border-radius: 8px; border: none;
  background: linear-gradient(160deg, var(--accent2), var(--accent)); color: #fff;
  font-weight: 700; font-size: 14px; cursor: pointer; min-height: 44px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.3);
}
.primary-btn:active { transform: translateY(1px); }
.secondary-btn {
  width: 100%; padding: 12px; border-radius: 8px; margin-top: 8px; min-height: 44px;
  border: 1px solid var(--border); background: transparent; color: var(--ink);
  font-weight: 700; font-size: 14px; cursor: pointer;
}
.secondary-btn:hover { border-color: var(--gold); }
.win-badge {
  display: inline-block; background: linear-gradient(160deg, var(--gold-light), var(--gold));
  color: #2a1a10; font-weight: 800;
  padding: 7px 18px; border-radius: 20px; margin-bottom: 12px; font-size: 14px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.25);
}
.win-stats { font-size: 13.5px; color: var(--muted); }

@media (max-width: 760px) {
  .levels-layout { grid-template-columns: 1fr; }
  .game-layout { grid-template-columns: 1fr; }
  .brand h1 { font-size: 19px; }
  .panel-header h2 { font-size: 19px; }
}
