:root {
  --bg: #0f1226;
  --bg2: #161a35;
  --panel: #1c2142;
  --line: #333b6b;
  --ink: #eef2ff;
  --ink2: #b4bfe6;
  --muted: #7782ad;
  --gold: #ff7eb6;
  --gold2: #ffa8d1;
  --ember: #ff5c9a;
  --good: #6fd3b0;
  --bad: #ff6b7a;
  --arcane: #6cb6ff;
  --serif: "Crimson Pro", Georgia, serif;
  --display: "Cinzel", "Crimson Pro", serif;
  --sans: "Inter", system-ui, sans-serif;
  --r: 12px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 70% -10%, #3a1c4a 0%, transparent 60%), radial-gradient(900px 500px at 0% 110%, #12306a 0%, transparent 60%), var(--bg);
  color: var(--ink); font-family: var(--sans); font-size: 15px;
  display: flex; flex-direction: column; height: 100dvh; overflow: hidden;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select {
  font: inherit; color: var(--ink); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus { outline: none; border-color: var(--gold); }
label { display: block; color: var(--ink2); font-size: 13px; margin: 10px 0; }
label input, label textarea { margin-top: 6px; }
small { color: var(--muted); font-weight: 400; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: rgba(15,18,38,.85); backdrop-filter: blur(8px); flex: none;
}
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--display); font-weight: 700; letter-spacing: .08em; color: var(--gold); }
.brand img { width: 24px; height: 24px; }
.title { flex: 1; font-family: var(--display); color: var(--ink2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.icon-btn { background: none; border: 1px solid transparent; border-radius: 8px; width: 36px; height: 36px; display: grid; place-items: center; }
.icon-btn:hover { border-color: var(--line); }
.icon-btn svg, .send svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
body.at-home #homeBtn, body.at-home #sheetBtn, body.at-create #sheetBtn { visibility: hidden; }

.screen { flex: 1; min-height: 0; overflow: auto; }
.btn { border-radius: 10px; padding: 10px 18px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; }
.btn.primary { background: linear-gradient(135deg, var(--gold2), var(--gold) 55%, #7fb8ff); color: #1a0d1c; border: none; }
.btn.primary:disabled { opacity: .5; }
.btn.ghost { background: none; }
.btn.danger { color: var(--bad); }

/* home */
.hero { max-width: 760px; margin: 40px auto 16px; padding: 0 20px; }
.hero h1 { font-family: var(--display); font-size: 32px; margin: 0 0 4px; color: var(--gold); }
.hero .btn { display: inline-block; text-decoration: none; color: inherit; }
.hero .btn.primary { color: #1a0d1c; }
.hero p { color: var(--ink2); font-family: var(--serif); font-size: 19px; margin: 0 0 20px; font-style: italic; }
.camp-list { max-width: 760px; margin: 0 auto 40px; padding: 0 20px; display: grid; gap: 12px; }
.camp {
  display: flex; gap: 14px; align-items: center; padding: 14px; border: 1px solid var(--line);
  border-radius: var(--r); background: var(--panel); cursor: pointer; transition: border-color .15s;
}
.camp:hover { border-color: var(--gold); }
.camp .avatar { width: 56px; height: 56px; border-radius: 10px; flex: none; }
.camp h3 { margin: 0; font-family: var(--display); font-size: 17px; }
.camp p { margin: 2px 0 0; color: var(--ink2); font-size: 13px; }
.camp .grow { flex: 1; min-width: 0; }
.camp .del { background: none; border: none; color: var(--muted); padding: 6px; }
.camp .del:hover { color: var(--bad); }
.empty { color: var(--muted); text-align: center; padding: 40px 0; font-family: var(--serif); font-size: 18px; }
.avatar { background: linear-gradient(135deg, #4a2a5c, #1f3b73); display: grid; place-items: center; font-family: var(--display); color: var(--gold); font-size: 22px; object-fit: cover; }

/* create */
.create { max-width: 860px; margin: 0 auto; padding: 24px 20px 60px; }
.create h1 { font-family: var(--display); color: var(--gold); }
.create h2 { font-family: var(--display); font-size: 17px; margin: 28px 0 10px; color: var(--ink); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.choice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.choice {
  text-align: left; border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 10px 12px;
}
.choice b { display: block; font-family: var(--display); font-size: 14px; }
.choice span { color: var(--muted); font-size: 12px; }
.choice.on { border-color: var(--gold); background: #2b2050; box-shadow: 0 0 0 1px var(--gold) inset; }
.race-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 14px 10px; }
.race-pick { background: none; border: none; padding: 4px 2px; text-align: center; color: var(--ink); }
.race-pick .pic { width: 84px; height: 84px; border-radius: 50%; margin: 0 auto 6px; display: grid; place-items: center; object-fit: cover; border: 2px solid var(--line); transition: border-color .15s, box-shadow .15s; font-size: 30px; }
.race-pick b { display: block; font-family: var(--display); font-size: 13px; }
.race-pick span { color: var(--muted); font-size: 11px; }
.race-pick:hover .pic { border-color: var(--gold); }
.race-pick.on .pic { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(255, 126, 182, .45); }
.race-pick.on b { color: var(--gold2); }
.custom-race { margin-top: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); padding: 12px; }
.custom-points { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.cp { display: flex; align-items: center; justify-content: space-between; gap: 6px; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
.cp .n { font-size: 13px; color: var(--ink2); font-family: var(--display); }
.cp .v { min-width: 22px; text-align: center; font-weight: 600; color: var(--gold); }
.cp button { width: 30px; height: 30px; padding: 0; border-radius: 8px; border: 1px solid var(--line); background: none; color: var(--ink); font-size: 16px; }
.cp button:disabled { opacity: .3; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { background: none; border: none; padding: 8px 14px; }
.seg button.on { background: var(--gold); color: #1a0d1c; font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; }
.score-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; }
.score { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); padding: 10px; text-align: center; }
.score .ab { font-family: var(--display); font-size: 13px; color: var(--ink2); }
.score select { margin: 6px 0; text-align: center; padding: 6px; }
.score .final { font-size: 22px; font-weight: 600; }
.score .m { color: var(--gold); font-size: 13px; }
.score .rb { color: var(--good); font-size: 11px; min-height: 14px; }
.skill-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 6px; }
.skill { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--panel); margin: 0; color: var(--ink); font-size: 14px; cursor: pointer; }
.skill input { width: auto; margin: 0; accent-color: var(--gold); }
.skill.dim { opacity: .4; }
.skill small { margin-left: auto; text-transform: uppercase; }
.error { color: var(--bad); min-height: 20px; }
.actions { display: flex; justify-content: flex-end; gap: 10px; }

/* play */
#play { display: grid; grid-template-columns: 1fr 360px; overflow: hidden; }
.story { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
.log { flex: 1; overflow-y: auto; padding: 24px max(20px, calc((100% - 720px) / 2)) 12px; scroll-behavior: smooth; }
.gm { font-family: var(--serif); font-size: 19px; line-height: 1.55; margin: 0 0 22px; color: var(--ink); }
.gm p { margin: 0 0 12px; }
.gm strong { color: var(--gold2); font-weight: 600; }
.gm em { color: #9fcdff; }
.gm h1, .gm h2, .gm h3 { font-family: var(--display); font-size: 18px; color: var(--gold); margin: 6px 0 10px; }
.gm ul { padding-left: 20px; margin: 0 0 12px; }
.player {
  margin: 0 0 22px auto; max-width: 80%; width: fit-content; background: #23306a; border: 1px solid #3a4a8f;
  border-radius: 14px 14px 4px 14px; padding: 10px 14px; color: var(--ink); white-space: pre-wrap;
}
.events { display: flex; flex-wrap: wrap; gap: 6px; margin: -8px 0 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; font-size: 12.5px; background: var(--bg2);
  border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px 4px 6px; color: var(--ink2);
}
.chip .t { background: var(--line); color: var(--ink); border-radius: 999px; padding: 1px 8px; font-weight: 600; min-width: 28px; text-align: center; }
.chip.ok { border-color: #3f8a73; } .chip.ok .t { background: #2c6b58; }
.chip.fail { border-color: #8f3b55; } .chip.fail .t { background: #8a2f4c; }
.chip.info { padding-left: 10px; }
.chip.ev-levelup { border-color: var(--gold); color: var(--gold2); }
.chip .d { color: var(--muted); }
.chip.big { font-size: 14px; }
.chip .t.ticking { opacity: .8; font-variant-numeric: tabular-nums; }
.chip .t.landed { animation: landpop .35s ease-out; }
@keyframes landpop { 0% { transform: scale(1.5) } 100% { transform: scale(1) } }
.die-overlay {
  position: fixed; left: 0; top: 0; will-change: transform; z-index: 60; pointer-events: none;
  display: flex; flex-direction: column; align-items: center; gap: 10px; transition: opacity .2s;
}
.die-overlay.out { opacity: 0; }
.die-overlay small { color: var(--gold2, #e6c777); font-family: var(--serif); letter-spacing: .1em; text-transform: uppercase; font-size: 12px; }
.die-overlay .die {
  width: 96px; height: 96px; display: grid; place-items: center; color: var(--ink);
  background: linear-gradient(145deg, var(--gold2, #e6c777), var(--gold, #b8923c) 55%, #6e5420);
  clip-path: polygon(50% 0, 95% 25%, 95% 75%, 50% 100%, 5% 75%, 5% 25%);
  filter: drop-shadow(0 8px 14px rgba(0, 0, 0, .55)); /* position + spin are driven from JS (roam) */
}
.die-overlay .face { font-family: var(--serif); font-weight: 700; font-size: 34px; color: #1a1420; font-variant-numeric: tabular-nums; }
.die-overlay.landed .die { animation: diepop .4s ease-out both; }
.die-overlay.crit .die { background: linear-gradient(145deg, #fff3b0, #f2c94c 55%, #a8801f); }
.die-overlay.fumble .die { background: linear-gradient(145deg, #e58aa3, #8a2f4c 60%, #4d1428); }
/* Long tumble: fast spins and big bounces first, then slower, smaller ones (ends upright: 1080deg). */
@keyframes tumble {
  0% { transform: translateY(-120px) rotate(-200deg) scale(.6); opacity: 0 }
  6% { transform: translateY(0) rotate(0deg) scale(1.05); opacity: 1 }
  18% { transform: translateY(-70px) rotate(380deg) }
  30% { transform: translateY(0) rotate(620deg) }
  46% { transform: translateY(-36px) rotate(800deg) }
  60% { transform: translateY(0) rotate(930deg) }
  76% { transform: translateY(-14px) rotate(1020deg) }
  90% { transform: translateY(0) rotate(1070deg) }
  100% { transform: translateY(0) rotate(1080deg) scale(1) }
}
@keyframes diepop { 0% { transform: scale(1) } 40% { transform: scale(1.3) } 100% { transform: scale(1.12) } }
/* Natural 20: the die swells, glows and explodes in sparks. */
.die-overlay.crit { filter: drop-shadow(0 0 28px #f2c94c) drop-shadow(0 0 60px rgba(255, 190, 60, .8)); }
.die-overlay.crit.landed .die { animation: critboom .9s cubic-bezier(.2, .8, .3, 1) both; }
@keyframes critboom { 0% { transform: scale(1) } 25% { transform: scale(2.1) rotate(-6deg) } 45% { transform: scale(1.6) rotate(5deg) } 100% { transform: scale(1.4) } }
.die-overlay.fumble.landed .die { animation: fumble .5s ease-out both; }
@keyframes fumble { 0% { transform: scale(1.2) } 30% { transform: scale(.7) rotate(-10deg) } 100% { transform: scale(.9) } }
.burst { position: fixed; z-index: 70; pointer-events: none; width: 0; height: 0; }
.burst .spark {
  position: absolute; left: 0; top: 0; width: var(--s); height: var(--s); border-radius: 50%;
  background: var(--c); box-shadow: 0 0 10px var(--c); animation: spark var(--t) cubic-bezier(.1, .7, .3, 1) forwards;
}
@keyframes spark {
  0% { transform: translate(-50%, -50%) scale(1); opacity: 1 }
  100% { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy) + 40px)) scale(.2); opacity: 0 }
}
.burst .ring { position: absolute; left: 0; top: 0; width: 40px; height: 40px; border-radius: 50%; border: 4px solid #fff3b0; transform: translate(-50%, -50%) scale(.2); animation: ring .8s ease-out forwards; }
.burst .ring.r2 { border-color: #ff9d3c; animation-delay: .12s; opacity: 0; animation-fill-mode: both; }
@keyframes ring { 0% { opacity: 1 } 100% { transform: translate(-50%, -50%) scale(10); opacity: 0; border-width: 1px } }
.crit-flash { position: fixed; inset: 0; z-index: 65; pointer-events: none; background: radial-gradient(circle at 50% 38%, rgba(255, 243, 176, .85), rgba(242, 201, 76, .25) 50%, transparent 75%); animation: critflash .9s ease-out forwards; }
@keyframes critflash { 0% { opacity: 0 } 15% { opacity: 1 } 100% { opacity: 0 } }
body.shake { animation: shake .5s linear; }
@keyframes shake { 0%, 100% { transform: none } 15% { transform: translate(-7px, 4px) } 30% { transform: translate(6px, -5px) } 45% { transform: translate(-5px, -3px) } 60% { transform: translate(4px, 4px) } 80% { transform: translate(-2px, 1px) } }
.chip.crit { border-color: var(--gold); box-shadow: 0 0 14px rgba(242, 201, 76, .7); }
.chip.crit .t { background: linear-gradient(145deg, #f2c94c, #a8801f); color: #1a1420; }
.chip.fumble { border-color: #8f3b55; }
@media (prefers-reduced-motion: reduce) { .die-overlay .die { animation: none } body.shake { animation: none } }
.thinking { padding: 6px max(20px, calc((100% - 720px) / 2)); color: var(--muted); font-family: var(--serif); font-size: 16px; }
.thinking span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--gold); animation: blink 1.2s infinite; }
.thinking span:nth-child(2) { animation-delay: .2s; } .thinking span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2 } 40% { opacity: 1 } }
.composer { border-top: 1px solid var(--line); padding: 10px max(14px, calc((100% - 740px) / 2)); padding-bottom: max(10px, env(safe-area-inset-bottom)); background: var(--bg); }
.dice-tray { display: flex; gap: 6px; margin-bottom: 8px; overflow-x: auto; }
.dice-tray button { background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; font-size: 12px; color: var(--ink2); }
.dice-tray button:hover { border-color: var(--gold); color: var(--gold); }
.input-row { display: flex; gap: 8px; align-items: flex-end; }
.input-row textarea { resize: none; max-height: 160px; font-size: 16px; border-radius: 12px; }
.send { flex: none; width: 46px; height: 46px; border-radius: 12px; border: none; background: linear-gradient(135deg, var(--gold2), var(--gold)); color: #1a0d1c; display: grid; place-items: center; }
.send:disabled { opacity: .4; }
.retry { margin: 0 0 20px; color: var(--bad); font-size: 14px; }
.retry button { margin-left: 8px; }

/* sheet */
.sheet { overflow-y: auto; background: var(--bg2); min-height: 0; }
.tabs { display: flex; position: sticky; top: 0; background: var(--bg2); border-bottom: 1px solid var(--line); z-index: 1; }
.tabs button { flex: 1; background: none; border: none; padding: 12px 4px; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--gold); border-bottom-color: var(--gold); }
.tabs button.alert { color: var(--ember); }
.tab { padding: 16px; }
.hero-card { display: flex; gap: 14px; align-items: center; margin-bottom: 14px; }
.portrait { width: 84px; height: 84px; border-radius: 12px; flex: none; border: 1px solid var(--line); }
.hero-card h2 { margin: 0; font-family: var(--display); font-size: 19px; }
.hero-card p { margin: 2px 0; color: var(--ink2); font-size: 13px; }
.linkish { background: none; border: none; color: var(--gold); padding: 0; font-size: 12px; text-decoration: underline; }
.bar { height: 10px; background: #262c55; border-radius: 99px; overflow: hidden; margin: 6px 0 2px; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, #d93a7c, #ff7eb6); transition: width .4s; }
.bar.xp i { background: linear-gradient(90deg, #2f6fd6, var(--arcane)); }
.statline { display: flex; justify-content: space-between; font-size: 12px; color: var(--ink2); }
.stats3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px; text-align: center; }
.stat b { display: block; font-size: 20px; }
.stat span { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.abil { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-bottom: 14px; }
.abil div { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 2px; text-align: center; }
.abil small { display: block; font-size: 10px; letter-spacing: .08em; }
.abil b { display: block; font-size: 17px; color: var(--gold2); }
.abil span { font-size: 11px; color: var(--ink2); }
.sec { font-family: var(--display); font-size: 13px; color: var(--gold); margin: 16px 0 8px; letter-spacing: .04em; }
.rows { display: grid; gap: 4px; }
.row { display: flex; justify-content: space-between; gap: 10px; font-size: 13.5px; padding: 6px 8px; border-radius: 6px; background: var(--panel); }
.row .r { color: var(--ink2); white-space: nowrap; }
.row.prof { box-shadow: inset 2px 0 0 var(--gold); }
.slots { display: flex; gap: 6px; flex-wrap: wrap; }
.slot { font-size: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; }
.pip { display: inline-block; width: 9px; height: 9px; border-radius: 50%; border: 1px solid var(--arcane); margin-left: 3px; }
.pip.full { background: var(--arcane); }
.tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { font-size: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; }
.tag.bad { border-color: var(--bad); color: var(--bad); }
.quest { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.quest b { font-family: var(--display); font-size: 14px; }
.quest p { margin: 4px 0 0; color: var(--ink2); font-size: 13px; }
.quest.completed { opacity: .6; } .quest.completed b::after { content: " ✓"; color: var(--good); }
.quest.failed { opacity: .5; } .quest.failed b { text-decoration: line-through; }
.npc { font-size: 13.5px; margin-bottom: 8px; } .npc b { color: var(--gold2); }
.prose { font-family: var(--serif); font-size: 16px; color: var(--ink2); line-height: 1.5; white-space: pre-wrap; }
.foe { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.foe.me { border-color: var(--gold); }
.foe.down { opacity: .45; }
.foe .top { display: flex; justify-content: space-between; font-size: 14px; }
.foe .top span { color: var(--muted); font-size: 12px; }
.scrim { display: none; }

.toast { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); background: var(--panel); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: 90vw; }
dialog { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: var(--r); padding: 24px; width: min(360px, 90vw); }
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h2 { font-family: var(--display); color: var(--gold); margin-top: 0; }
dialog .btn { margin-top: 12px; width: 100%; }

@media (max-width: 900px) {
  #play { grid-template-columns: 1fr; }
  .story { border-right: none; }
  .sheet { position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 92vw); z-index: 30; transform: translateX(100%); transition: transform .25s; border-left: 1px solid var(--line); padding-top: env(safe-area-inset-top); }
  body.sheet-open .sheet { transform: none; }
  body.sheet-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }
  .field-row { grid-template-columns: 1fr; }
  .score-grid { grid-template-columns: repeat(3, 1fr); }
  .gm { font-size: 18px; }
  .brand span { display: none; }
}
@media (min-width: 901px) { #sheetBtn { visibility: hidden; } }
.scene-art { margin: 4px 0 6px; width: 100%; } .scene-art img { width: 100%; border-radius: 12px; border: 1px solid var(--line); display: block; }
