:root {
  --bg: #0a0a0d;
  --fg: #f1f1f4;
  --dim: #8e8e9c;
  --faint: #5c5c6a;
  --card: #121218;
  --card-2: #191921;
  --line: #262631;
  --ok: #78e696;
  --warn: #ffbe50;
  --bad: #ff5a5a;
  --pink: #ff4fa3;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.45 var(--sans);
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100svh; overflow-x: hidden; }

body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 0 0 transparent;
  transition: box-shadow .6s;
  z-index: 50;
}
body.state-danger::after { box-shadow: inset 0 0 120px 10px rgba(255, 40, 40, .28); animation: pulse 1.6s ease-in-out infinite; }
body.state-dead::after { box-shadow: inset 0 0 160px 30px rgba(0, 0, 0, .7); }

@keyframes pulse { 50% { box-shadow: inset 0 0 70px 0 rgba(255, 40, 40, .12); } }

button { font: inherit; color: inherit; }

#app {
  max-width: 1240px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 16px 28px;
}

/* ---------- header */
#top { text-align: center; padding: 6px 0 12px; }

h1.q {
  margin: 4px 0 12px;
  font: 600 12px/1 var(--mono);
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--dim);
}

#answer { display: block; margin: 0 auto; image-rendering: pixelated; }

.pctline {
  margin-top: 10px;
  font: 700 20px/1.1 var(--mono);
  color: var(--dim);
}
#pct { color: var(--ok); }
.state-warn #pct { color: var(--warn); }
.state-danger #pct, .state-dead #pct { color: var(--bad); }

#meter {
  display: block;
  width: min(560px, 100%);
  height: 12px;
  margin: 10px auto 8px;
  border-radius: 3px;
  image-rendering: pixelated;
}

#meta { font: 13px/1.5 var(--mono); color: var(--dim); }
#meta .dot { margin: 0 .5em; color: var(--faint); }

/* ---------- layout */
#play {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "stage" "tools" "panel";
  gap: 12px 16px;
  align-items: start;
}
#stage { grid-area: stage; }
#tools { grid-area: tools; }
#panel { grid-area: panel; }


#stage {
  position: relative;
  width: min(100%, calc(100svh - 300px));
  min-width: min(100%, 320px);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  box-shadow: 0 0 0 1px var(--line), 0 0 0 0 transparent;
  transition: box-shadow .4s;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.surging #stage { box-shadow: 0 0 0 2px var(--bad), 0 0 40px 0 rgba(255, 60, 60, .35); }
.shake #stage { animation: shake .35s linear; }
@keyframes shake {
  20% { transform: translate(-3px, 1px); }
  40% { transform: translate(3px, -2px); }
  60% { transform: translate(-2px, 2px); }
  80% { transform: translate(2px, -1px); }
}

#board { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; touch-action: none; }

#banner {
  position: absolute;
  left: 10px; right: 10px; top: 10px;
  padding: 8px 12px;
  border-radius: 6px;
  background: rgba(10, 10, 13, .86);
  border: 1px solid var(--line);
  font: 600 13px/1.35 var(--mono);
  text-align: center;
  pointer-events: none;
  backdrop-filter: blur(4px);
}
#banner.bad { border-color: var(--bad); color: #ffd0d0; }
#banner.good { border-color: var(--ok); color: #d6ffe0; }

#conn {
  position: absolute;
  left: 50%; bottom: 12px;
  transform: translateX(-50%);
  padding: 5px 10px;
  border-radius: 99px;
  background: rgba(0, 0, 0, .75);
  font: 12px var(--mono);
  color: var(--dim);
  pointer-events: none;
}
#conn[hidden] { display: none; }

/* coach */
#coach {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  pointer-events: none;
  background: radial-gradient(closest-side, rgba(0,0,0,.55), rgba(0,0,0,0));
}
#coach[hidden] { display: none; }
.coach-card {
  text-align: center;
  padding: 18px 22px;
  border-radius: 10px;
  background: rgba(10, 10, 13, .82);
  border: 1px solid var(--line);
  max-width: 280px;
}
.coach-card p { margin: 6px 0; }
.coach-card b { font-size: 18px; }
.coach-card .hint { color: var(--dim); font-size: 13px; }
.coach-hand {
  width: 18px; height: 18px; margin: 0 auto 8px;
  border-radius: 50%;
  background: var(--fg);
  animation: swipe 1.8s ease-in-out infinite;
}
@keyframes swipe {
  0% { transform: translateX(-40px); opacity: 0; }
  20% { opacity: 1; }
  80% { opacity: 1; }
  100% { transform: translateX(40px); opacity: 0; }
}

/* funeral */
#funeral {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: 16px;
  background: rgba(5, 5, 7, .78);
  backdrop-filter: grayscale(1) blur(1px);
  text-align: center;
}
#funeral[hidden] { display: none; }
.stone {
  width: min(300px, 90%);
  padding: 26px 18px 18px;
  border-radius: 120px 120px 8px 8px;
  background: linear-gradient(#5a5a66, #3d3d47);
  box-shadow: inset 0 -6px 0 rgba(0,0,0,.25), 0 10px 30px rgba(0,0,0,.6);
  color: #ececf2;
  font-family: var(--mono);
}
.rip { font: 800 30px/1 var(--mono); letter-spacing: .12em; margin-bottom: 8px; }
.stone-title { font-size: 18px; font-weight: 700; }
.stone-dates { font-size: 12px; color: #c9c9d3; margin: 4px 0 10px; }
.stone-life, .stone-cause { font-size: 14px; }
.stone-stats { font-size: 12px; color: #c9c9d3; margin-top: 8px; }
.f-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.f-next { font: 13px var(--mono); color: var(--dim); }

/* ---------- tools */
#tools { width: min(100%, calc(100svh - 300px)); min-width: min(100%, 320px); margin: 0 auto; }

#palette {
  display: grid;
  grid-template-columns: repeat(15, minmax(0, 1fr));
  gap: 4px;
}
.sw {
  aspect-ratio: 1 / 1;
  min-height: 22px;
  border-radius: 5px;
  border: 0;
  padding: 0;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.12);
  transition: transform .08s;
}
.sw[aria-checked="true"] { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--fg); transform: scale(1.06); }

#toolrow { display: flex; gap: 6px; margin-top: 8px; align-items: stretch; }

#ink {
  position: relative;
  flex: 1;
  min-width: 70px;
  border-radius: 6px;
  background: var(--card-2);
  overflow: hidden;
  box-shadow: inset 0 0 0 1px var(--line);
}
#ink-fill {
  position: absolute; inset: 0 auto 0 0;
  width: 100%;
  background: linear-gradient(90deg, #2f6bff, #9b51e0, #ff4fa3);
  opacity: .85;
}
#ink.empty #ink-fill { background: var(--bad); }
#ink.empty { animation: nudge .25s linear; }
@keyframes nudge { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
#ink-label {
  position: relative;
  display: block;
  padding: 0 10px;
  line-height: 38px;
  font: 600 12px/38px var(--mono);
  text-shadow: 0 1px 2px rgba(0,0,0,.7);
  white-space: nowrap;
}

.tool, .btn {
  border: 0;
  border-radius: 6px;
  background: var(--card-2);
  box-shadow: inset 0 0 0 1px var(--line);
  padding: 0 11px;
  min-height: 38px;
  cursor: pointer;
  font: 600 13px var(--mono);
  white-space: nowrap;
}
.tool:hover, .btn:hover { background: #22222c; }
.tool:active, .btn:active { transform: translateY(1px); }
.tool .ico { font-size: 15px; }
.tool .lbl { margin-left: 4px; }
.primary { background: var(--fg); color: #0a0a0d; box-shadow: none; }
.primary:hover { background: #fff; }
.state-danger #b-share { background: var(--bad); color: #fff; }

.brush-dots { display: inline-block; border-radius: 50%; background: var(--fg); vertical-align: middle; }

@media (max-width: 420px) {
  .tool .lbl { display: none; }
  .tool, .btn { padding: 0 9px; }
}

/* ---------- panel */
#panel { display: grid; gap: 12px; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
}
.card p { margin: 0 0 8px; }
.card p:last-child { margin-bottom: 0; }
.label {
  font: 600 11px/1 var(--mono);
  text-transform: uppercase;
  letter-spacing: .14em;
  color: var(--dim);
  margin-bottom: 10px;
}
.sub { color: var(--dim); font-size: 13px; }
.model { display: flex; align-items: center; gap: 10px; font: 800 24px/1.1 var(--mono); }
#v-surge {
  font: 700 11px/1 var(--mono);
  letter-spacing: .12em;
  padding: 4px 6px;
  border-radius: 4px;
  background: var(--bad);
  color: #fff;
  animation: blink 1s steps(2) infinite;
}
#v-surge[hidden] { display: none; }
@keyframes blink { 50% { opacity: .35; } }
.note { margin: 8px 0 6px; font-style: italic; color: #d8d8e2; }
.note::before { content: "Release notes: "; font-style: normal; color: var(--dim); }

.war { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.war b { display: block; font: 800 22px/1.1 var(--mono); }
.war div:first-child b { color: #b9b9c6; }
.war div:last-child b { color: var(--ok); }
.war span { font-size: 12px; color: var(--dim); }
#tug { display: block; width: 100%; height: 8px; margin-top: 10px; border-radius: 3px; image-rendering: pixelated; }

.learn { margin-top: 10px; font-size: 12px; }

#countries { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
#countries li {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 8px;
  row-gap: 4px;
  font-size: 14px;
}
#countries li.empty { display: block; color: var(--dim); font-size: 13px; }
#countries .flag { font-size: 18px; line-height: 1; }
#countries .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#countries .n { font: 700 13px var(--mono); color: var(--ok); }
#countries .bar { grid-column: 2 / 4; height: 3px; border-radius: 2px; background: var(--line); overflow: hidden; }
#countries .bar i { display: block; height: 100%; background: linear-gradient(90deg, #2f6bff, #9b51e0, #ff4fa3); }
#countries li.me .name { font-weight: 700; }
#countries li.me .name::after { content: " (you)"; color: var(--dim); font-weight: 400; }
#you-cc { margin-top: 10px; }

.you b { font: 800 18px var(--mono); color: var(--ok); }
#y-all-wrap { color: var(--dim); font-size: 13px; }

#graves { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 6px; }
#graves li {
  display: flex; align-items: center; gap: 8px;
  font: 13px var(--mono);
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--card-2);
}
#graves li.empty { background: none; padding: 0; color: var(--dim); font-family: var(--sans); }
#graves .g-run { font-weight: 700; min-width: 34px; }
#graves .g-life { flex: 1; }
#graves .g-ver { color: var(--dim); }
#graves button {
  border: 0; border-radius: 4px; background: #2a2a35; cursor: pointer;
  font: 600 12px var(--mono); padding: 4px 7px;
}
#graves li.best .g-life::after { content: " ★"; color: var(--warn); }

.kbd {
  font: 600 12px var(--mono);
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--card-2);
  border: 1px solid var(--line);
}

#foot {
  margin-top: 26px;
  text-align: center;
  font-size: 12px;
  color: var(--faint);
  display: grid;
  gap: 6px;
}
#foot a { color: var(--dim); }
#totals { font: 600 13px var(--mono); color: var(--dim); }
#totals[hidden] { display: none; }

/* ---------- share menu */
#share-menu {
  position: fixed;
  z-index: 60;
  display: grid;
  gap: 6px;
  padding: 8px;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
}
#share-menu[hidden] { display: none; }
#share-menu .btn { min-width: 150px; }

/* ---------- modal */
#modal {
  position: fixed; inset: 0; z-index: 70;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(0,0,0,.75);
}
#modal[hidden] { display: none; }
.modal-card {
  position: relative;
  width: min(520px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: 16px;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--line);
}
.close {
  position: absolute; top: 8px; right: 10px;
  border: 0; background: none; font-size: 26px; line-height: 1; cursor: pointer; color: var(--dim);
}
.m-title { font: 800 18px var(--mono); margin: 0 30px 10px 0; }
#tl { display: block; width: 100%; height: auto; aspect-ratio: 1; border-radius: 6px; background: #000; image-rendering: pixelated; }
.m-info { display: flex; justify-content: space-between; font: 13px var(--mono); color: var(--dim); margin: 8px 0 4px; }
#m-scrub { width: 100%; }
.m-actions { display: flex; gap: 8px; margin: 10px 0 6px; }
.m-actions .btn { flex: 1; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Wide screens: the board takes the full height on the left; tools and
   the rest stack in a column on the right. */
@media (min-width: 980px) {
  #play {
    grid-template-columns: minmax(0, 1fr) 330px;
    grid-template-areas: "stage tools" "stage panel";
    grid-template-rows: auto 1fr;
  }
  #stage { width: min(100%, calc(100svh - 236px)); }
  #tools {
    width: auto;
    min-width: 0;
    margin: 0;
    padding: 12px;
    border-radius: 10px;
    background: var(--card);
    border: 1px solid var(--line);
  }
  #palette { grid-template-columns: repeat(8, minmax(0, 1fr)); }
  #toolrow { flex-wrap: wrap; }
  #ink { flex-basis: 100%; }
  #toolrow .tool { flex: 1; }
}
