:root {
  --ink: #2a1d12;
  --parch: #f4e6c4;
  --parch-2: #e9d4a4;
  --bronze: #b07a2a;
  --bronze-dk: #7d5216;
  --sky: #8fc1d9;
  --muted: #6b5640;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #2a1d12;
  color: var(--ink);
  font-family: Inter, system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

canvas#game {
  position: fixed;
  inset: 0;
  display: block;
  touch-action: none;
  cursor: grab;
}
canvas#game.dragging { cursor: grabbing; }

[hidden] { display: none !important; }

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  pointer-events: none;
  z-index: 5;
}
.hud-group { display: flex; align-items: center; gap: 10px; pointer-events: auto; }
#hud-level {
  font-family: Cinzel, serif;
  font-weight: 800;
  font-size: 18px;
  color: var(--ink);
  text-shadow: 0 1px 0 rgba(255,255,255,.5);
}
#hud-score {
  font-family: Cinzel, serif;
  font-weight: 800;
  font-size: 22px;
  min-width: 80px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 0 rgba(255,255,255,.5);
}
body.dark-sky #hud-level, body.dark-sky #hud-score { color: #f4e6c4; text-shadow: 0 1px 2px rgba(0,0,0,.6); }
#hud-stones { display: flex; gap: 6px; pointer-events: none; }
#hud-stones i {
  width: 18px; height: 15px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #e4e0d6, #8d877b 70%);
  box-shadow: 0 1px 2px rgba(0,0,0,.4);
}
#hud-stones i.spent { background: rgba(42,29,18,.18); box-shadow: inset 0 0 0 1.5px rgba(42,29,18,.35); }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 0;
  background: rgba(244,230,196,.85);
  box-shadow: 0 2px 0 var(--bronze-dk), 0 4px 10px rgba(0,0,0,.2);
  display: grid; place-items: center;
  cursor: pointer;
  color: var(--ink);
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 0 0 var(--bronze-dk); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#btn-sound .off, #btn-music .off { display: none; }
#btn-sound.muted .on, #btn-music.muted .on { display: none; }
#btn-sound.muted .off, #btn-music.muted .off { display: block; }

/* ---------- stone picker ---------- */
#stone-picker {
  position: fixed;
  left: max(16px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
  z-index: 5;
}
.stone-btn {
  position: relative;
  width: 48px; height: 48px;
  border: 0;
  border-radius: 12px;
  background: rgba(244,230,196,.9);
  box-shadow: 0 0 0 2px transparent inset, 0 2px 0 var(--bronze-dk), 0 4px 10px rgba(0,0,0,.2);
  display: grid; place-items: center;
  cursor: pointer;
}
.stone-btn i { display: block; border-radius: 50%; box-shadow: 0 1px 2px rgba(0,0,0,.4); }
.stone-btn span {
  position: absolute; right: 3px; bottom: 1px;
  font: 700 12px Inter, sans-serif; color: var(--ink);
}
.stone-btn.on { box-shadow: 0 0 0 3px var(--bronze) inset, 0 2px 0 var(--bronze-dk), 0 4px 10px rgba(0,0,0,.2); background: #fff8e6; }
.stone-btn[disabled] { opacity: .4; cursor: not-allowed; }

/* ---------- daily + story + leaderboard ---------- */
.daily {
  display: flex; align-items: baseline; justify-content: center; gap: 10px; flex-wrap: wrap;
  width: 100%;
  margin: 0 0 12px;
  padding: 10px 14px;
  border: 0; border-radius: 10px;
  background: linear-gradient(90deg, #8e3b2a, #b07a2a);
  color: #fff8e6;
  box-shadow: 0 3px 0 #5a2418;
  cursor: pointer;
  font: inherit;
}
.daily b { font-family: Cinzel, serif; font-size: 16px; }
.daily span { font-size: 13px; opacity: .9; }
.daily:active { transform: translateY(2px); box-shadow: 0 1px 0 #5a2418; }
.link {
  border: 0; background: none; padding: 0;
  font: inherit; color: var(--bronze-dk); text-decoration: underline; cursor: pointer;
}

.panel.story-panel { width: min(600px, 100%); }
.story-art svg { display: block; width: 100%; height: auto; border-radius: 8px; box-shadow: 0 0 0 2px var(--bronze) inset; }
.story-art { margin-bottom: 14px; }
.story-text { text-align: left; font-size: 15px; line-height: 1.55; }
.story-text p { margin: 0 0 10px; }

/* leaderboards: the results card's board and the Leaderboards screen share these */
.board { margin: 14px auto 0; max-width: 380px; text-align: left; }
.board h3, .lb-board h3 { font-family: Cinzel, serif; font-size: 15px; margin: 0 0 6px; text-align: center; }
.board .lb-rows { max-height: 170px; overflow-y: auto; }
.lb-rows { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.lb-rows li { display: flex; align-items: center; gap: 8px; padding: 3px 8px; border-radius: 6px; }
.lb-rows li:nth-child(odd) { background: rgba(125,82,22,.08); }
.lb-rows li.me { background: rgba(217,154,30,.35); font-weight: 600; }
.lb-rank { width: 1.6em; text-align: right; color: var(--muted); font-family: Cinzel, serif; font-size: 12px; }
.lb-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.lb-name small { color: var(--muted); font-weight: 400; }
.lb-rows b { color: #8a2b12; white-space: nowrap; }
.lb-empty { margin: 6px 0; font-size: 14px; color: var(--muted); text-align: center; }
.lb-mine, .lb-sub { margin: 6px 0 0; font-size: 13px; color: var(--muted); text-align: center; }
.lb-sub { margin: 0 0 8px; }
.lb-name-row {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between;
  margin: 10px 0; padding: 8px 10px; border-radius: 8px; background: rgba(125,82,22,.08);
  font-size: 14px; text-align: left;
}
.lb-edit { display: flex; gap: 6px; }
.lb-edit input, .lb-pick select {
  min-width: 0; border: 0; border-radius: 8px; padding: 8px 10px;
  font: 14px Inter, sans-serif; background: #fff8e6; color: var(--ink);
  box-shadow: 0 0 0 2px var(--bronze) inset;
}
.lb-edit input { width: 11rem; max-width: 46vw; }
.lb-pick { display: block; margin: 0 0 8px; }
.lb-pick select { width: 100%; }
.panel.boards-panel { width: min(940px, 100%); }
.lb-intro { font-size: 14px; color: var(--muted); margin: 0 0 6px; }
.lb-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 12px; text-align: left; }
.lb-board { background: rgba(255,255,255,.35); border: 1px solid rgba(125,82,22,.3); border-radius: 10px; padding: 10px; }
.menu-row { display: flex; gap: 10px; margin: 0 0 12px; }
.menu-row .daily { flex: 1; margin: 0; }
.boards-btn {
  border: 0; border-radius: 10px; padding: 10px 16px; cursor: pointer; font: inherit;
  background: #fff8e6; color: var(--ink);
  box-shadow: 0 0 0 2px var(--bronze) inset, 0 3px 0 var(--bronze-dk);
}
.boards-btn b { font-family: Cinzel, serif; font-size: 15px; }
.boards-btn:active { transform: translateY(2px); }
.btn.small { padding: 9px 14px; font-size: 14px; }
.btn[disabled] { opacity: .7; cursor: default; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
  background: rgba(42,29,18,.35);
  overflow-y: auto;
}
.panel {
  width: min(560px, 100%);
  background: linear-gradient(var(--parch), var(--parch-2));
  border-radius: 14px;
  padding: 28px 28px 24px;
  box-shadow: 0 0 0 3px var(--bronze-dk) inset, 0 20px 50px rgba(0,0,0,.4);
  text-align: center;
}
.eyebrow { margin: 0; font-size: 12px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }
h1, h2 { font-family: Cinzel, serif; font-weight: 800; margin: 6px 0 8px; line-height: 1.1; }
h1 { font-size: clamp(32px, 7vw, 52px); }
h2 { font-size: clamp(24px, 5vw, 34px); }
.tagline { margin: 0 0 20px; color: var(--muted); }
blockquote { font-family: Cinzel, serif; font-size: 17px; margin: 14px 0 6px; line-height: 1.5; }
cite { display: block; font-size: 13px; color: var(--muted); font-style: normal; }
.goal { margin: 16px 0 0; font-weight: 600; }
.tap { margin: 18px 0 0; font-size: 13px; color: var(--muted); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .4; } }
#intro { cursor: pointer; background: rgba(42,29,18,.2); }

.panel.menu-panel { width: min(680px, 100%); }
.star-total { margin: -8px 0 12px; font-family: Cinzel, serif; font-weight: 800; color: var(--bronze-dk); }
#levels {
  max-height: min(52vh, 460px);
  overflow-y: auto;
  margin: 0 -6px 16px;
  padding: 0 6px 4px;
  text-align: left;
}
.chapter h3 {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin: 12px 0 8px;
  font-family: Cinzel, serif;
  font-size: 15px;
  border-bottom: 1px solid rgba(125,82,22,.35);
  padding-bottom: 4px;
}
.chapter h3 small { font-family: Inter, sans-serif; font-weight: 600; font-size: 12px; color: var(--bronze-dk); }
.chapter .grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
}
.lvl {
  border: 0;
  border-radius: 9px;
  padding: 7px 2px 5px;
  background: #fff8e6;
  box-shadow: 0 0 0 2px var(--bronze) inset, 0 3px 0 var(--bronze-dk);
  cursor: pointer;
  font: inherit;
  color: var(--ink);
}
.lvl:active { transform: translateY(2px); box-shadow: 0 0 0 2px var(--bronze) inset; }
.lvl b { display: block; font-family: Cinzel, serif; font-size: 19px; line-height: 1.1; }
.lvl .s { margin-top: 2px; font-size: 10px; letter-spacing: 1px; color: var(--bronze); }
.lvl[disabled] { opacity: .45; cursor: not-allowed; }

.toggle { display: flex; gap: 8px; align-items: center; justify-content: center; font-size: 14px; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--bronze-dk); }
.how { margin: 12px 0 0; font-size: 14px; color: var(--muted); }
.credit { margin: 16px 0 0; font-size: 12px; letter-spacing: .06em; color: var(--muted); }

.stars { font-size: 40px; letter-spacing: 6px; color: rgba(42,29,18,.2); margin: 4px 0; }
.stars .on { color: #d99a1e; text-shadow: 0 2px 0 var(--bronze-dk); }
#result-score { font-family: Cinzel, serif; font-weight: 800; font-size: 20px; margin: 4px 0; }
.actions { display: flex; gap: 10px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.btn {
  border: 0;
  border-radius: 10px;
  padding: 12px 22px;
  font: 600 16px Inter, sans-serif;
  background: var(--bronze);
  color: #fff8e6;
  box-shadow: 0 3px 0 var(--bronze-dk);
  cursor: pointer;
}
.btn.ghost { background: #fff8e6; color: var(--ink); box-shadow: 0 0 0 2px var(--bronze) inset, 0 3px 0 var(--bronze-dk); }
.btn:active { transform: translateY(2px); }

@media (max-width: 520px) {
  .chapter .grid { grid-template-columns: repeat(4, 1fr); }
  .panel { padding: 22px 16px 18px; }
  #hud-level { font-size: 14px; max-width: 30vw; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  #hud-score { font-size: 17px; min-width: 0; }
  #hud-stones i { width: 13px; height: 11px; }
  .icon-btn { width: 36px; height: 36px; }
}
@media (max-height: 480px) {
  .panel { padding: 14px 16px; }
  .menu-panel .eyebrow, .menu-panel .how { display: none; }
  h1 { font-size: 30px; margin: 0 0 4px; }
  h2 { font-size: 24px; }
  .tagline { font-size: 14px; }
  #levels { max-height: 46vh; margin-bottom: 8px; }
  .chapter h3 { margin: 6px 0 4px; font-size: 13px; }
  .lvl b { font-size: 16px; }
  .credit { margin-top: 8px; }
  .panel.menu-panel { width: min(760px, 100%); }
  blockquote { font-size: 15px; margin: 8px 0 4px; }
  .tagline { margin-bottom: 10px; }
  .lvl { min-height: 0; padding: 8px 6px; }
}
