:root {
  --bg: #f6f7fb; --panel: #ffffff; --text: #1b1f2a; --muted: #4a5266; --line: #d5d9e4;
  --accent: #b3261e; --accent-text: #ffffff; --focus: #1a5fd0; --good: #1e7a35; --warn: #8a5a00; --bad: #b3261e;
  --chip: #eef0f6;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141a; --panel: #1b1e27; --text: #e8eaf1; --muted: #aab1c4; --line: #333847;
    --accent: #ff6b5e; --accent-text: #1b1010; --focus: #7fb0ff; --good: #6fd38a; --warn: #f0c060; --bad: #ff8a80;
    --chip: #262a36;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
.top { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1.5rem; padding: 1rem 1.25rem; border-bottom: 1px solid var(--line); background: var(--panel); }
.top h1 { margin: 0; font-size: 1.35rem; }
.sub { margin: 0; color: var(--muted); flex: 1 1 18rem; }
.top-actions { display: flex; gap: .5rem; }
button { font: inherit; border-radius: 8px; padding: .45rem .9rem; border: 1px solid var(--line); background: var(--chip); color: var(--text); cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
.grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr) minmax(0, 1fr); gap: 1rem; padding: 1rem; }
@media (max-width: 1100px) { .grid { grid-template-columns: minmax(0, 1fr); } }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 1rem; min-width: 0; }
h2 { margin: 0 0 .6rem; font-size: 1.1rem; }
h3 { margin: .8rem 0 .4rem; font-size: 1rem; }
label { display: flex; flex-direction: column; gap: .15rem; font-size: .85rem; color: var(--muted); min-width: 0; }
label.check { flex-direction: row; align-items: center; gap: .35rem; color: var(--text); }
.grow { flex: 1 1 10rem; }
input, select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .3rem .45rem; min-width: 0; width: 100%; }
input[type=number] { width: 5rem; }
input[type=checkbox] { width: auto; }
.row { display: flex; flex-wrap: wrap; gap: .6rem; align-items: flex-end; margin-bottom: .55rem; }
.row.small input[type=number] { width: 4rem; }
.types { display: flex; gap: .35rem; margin-bottom: .55rem; min-height: 1.6rem; flex-wrap: wrap; }
.type { font-size: .78rem; font-weight: 600; padding: .12rem .55rem; border-radius: 999px; color: #fff; text-shadow: 0 1px 1px rgba(0,0,0,.45); }
.hp { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .6rem; margin-bottom: .6rem; }
.hp-max { color: var(--muted); padding-bottom: .35rem; }
.hpbar { flex: 1 1 8rem; height: .7rem; background: var(--chip); border-radius: 999px; overflow: hidden; margin-bottom: .45rem; border: 1px solid var(--line); }
.hpfill { height: 100%; width: 100%; background: var(--good); transition: width .2s; }
table.stats { width: 100%; border-collapse: collapse; font-size: .85rem; }
table.stats th, table.stats td { padding: .2rem .25rem; text-align: left; border-bottom: 1px solid var(--line); }
table.stats td input { width: 3.6rem; padding: .2rem .3rem; }
table.stats td.total { font-weight: 600; }
td.plus { color: var(--good); } td.minus { color: var(--bad); }
.checks { display: flex; flex-wrap: wrap; gap: .3rem .9rem; margin-bottom: .6rem; }
.checks label { flex-direction: row; align-items: center; gap: .3rem; color: var(--text); font-size: .85rem; }
.sides { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
@media (max-width: 640px) { .sides { grid-template-columns: 1fr; } }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: .4rem .6rem; min-width: 0; }
legend { font-size: .85rem; color: var(--muted); padding: 0 .3rem; }
.move-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: .4rem; align-items: center; margin-bottom: .35rem; }
.move-row input[type=number] { width: 3.6rem; }
#results { margin-top: .8rem; display: grid; gap: .7rem; }
.result { border: 1px solid var(--line); border-radius: 10px; padding: .7rem .8rem; background: var(--bg); }
.result h4 { margin: 0 0 .3rem; font-size: 1rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.result .desc { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem; overflow-wrap: anywhere; color: var(--muted); margin: .2rem 0 .45rem; }
.result .big { font-size: 1.2rem; font-weight: 700; }
.result .ko { font-weight: 600; }
.ko.good { color: var(--good); } .ko.warn { color: var(--warn); }
.dmgbar { position: relative; height: .8rem; background: var(--chip); border-radius: 999px; overflow: hidden; margin: .35rem 0; border: 1px solid var(--line); }
.dmgbar .rest { position: absolute; inset: 0 auto 0 0; background: var(--good); }
.dmgbar .range { position: absolute; top: 0; bottom: 0; background: var(--warn); }
.kv { display: flex; flex-wrap: wrap; gap: .25rem .9rem; font-size: .85rem; }
.kv span b { font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .35rem; }
.chip { font-size: .75rem; background: var(--chip); border: 1px solid var(--line); border-radius: 999px; padding: .08rem .5rem; }
.actions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .5rem; }
#rolllog { display: grid; gap: .3rem; margin-top: .6rem; }
.log { font-size: .85rem; border-left: 3px solid var(--accent); padding: .3rem .6rem; background: var(--panel); }
.err { color: var(--bad); font-weight: 600; }
.muted { color: var(--muted); }

/* Battle stage */
.battle-stage { position: relative; margin: 1rem 1rem 0; height: 190px; border-radius: 14px; overflow: hidden; border: 1px solid var(--line);
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 0 2rem; isolation: isolate;
  background: radial-gradient(ellipse at 22% 85%, rgba(0,0,0,.18) 0 18%, transparent 19%), radial-gradient(ellipse at 72% 85%, rgba(0,0,0,.18) 0 18%, transparent 19%),
    linear-gradient(180deg, #8ec5fc 0%, #c9e7ff 55%, #9bd27a 56%, #6fae4f 100%); }
.battle-stage.w-sun { background-color: #ffd36b; filter: saturate(1.15); }
.battle-stage.w-sun::before, .battle-stage.w-rain::before, .battle-stage.w-sand::before, .battle-stage.w-snow::before, .battle-stage.t-electric::after, .battle-stage.t-grassy::after, .battle-stage.t-psychic::after, .battle-stage.t-misty::after {
  content: ""; position: absolute; inset: 0; z-index: -1; pointer-events: none; }
.battle-stage.w-sun::before { background: radial-gradient(circle at 85% 10%, rgba(255,220,90,.95) 0 6%, rgba(255,190,60,.35) 14%, transparent 40%); z-index: 0; }
.battle-stage.w-rain::before { z-index: 0; background: linear-gradient(rgba(20,32,55,.82), rgba(25,40,60,.55)); }
.battle-stage.w-sand::before { z-index: 0; background: linear-gradient(rgba(70,50,25,.7), rgba(110,80,40,.5)); }
.battle-stage.w-snow::before { z-index: 0; background: linear-gradient(rgba(60,75,100,.7), rgba(150,170,195,.45)); }
.battle-stage.t-electric::after { background: linear-gradient(transparent 55%, rgba(255,214,0,.45) 56%, rgba(255,214,0,.25)); }
.battle-stage.t-grassy::after { background: linear-gradient(transparent 55%, rgba(60,170,60,.5) 56%, rgba(40,140,40,.35)); }
.battle-stage.t-psychic::after { background: linear-gradient(transparent 55%, rgba(220,80,180,.45) 56%, rgba(180,60,160,.3)); }
.battle-stage.t-misty::after { background: linear-gradient(transparent 55%, rgba(255,170,220,.5) 56%, rgba(255,200,235,.35)); }
.stage-mon { position: relative; z-index: 2; background: rgba(255,255,255,.88); color: #1b1f2a; border-radius: 10px; padding: .5rem .75rem; max-width: 18rem; box-shadow: 0 2px 8px rgba(0,0,0,.18); }
.stage-mon.right { justify-self: end; }
.stage-vs { z-index: 2; font-weight: 800; font-size: 1.4rem; color: #fff; text-shadow: 0 2px 4px rgba(0,0,0,.5); }
.sm-name { font-weight: 700; }
.sm-types { display: flex; gap: .25rem; margin: .2rem 0; }
.sm-hp { height: .55rem; background: #d9dde6; border-radius: 999px; overflow: hidden; }
.sm-hp div { height: 100%; width: 100%; background: #1e7a35; transition: width .5s ease, background .5s; }
.sm-hptext { font-size: .78rem; color: #3b4254; margin-top: .15rem; }
.stage-mon.hit { animation: hit .45s ease; }
@keyframes hit { 20% { transform: translateX(-8px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(3px); } }
#stage-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; z-index: 1; mix-blend-mode: screen; opacity: 0; transition: opacity .6s; pointer-events: none; }
#stage-video.on { opacity: 1; }
#stage-fx { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.fx-p { position: absolute; opacity: 0; -webkit-mask-size: contain; mask-size: contain; -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
  -webkit-mask-position: center; mask-position: center; animation: fxburst .9s ease-out forwards; filter: saturate(1.4) drop-shadow(0 0 6px rgba(0,0,0,.25)); }
@keyframes fxburst { 0% { opacity: 0; transform: scale(.2) rotate(var(--rot)); } 25% { opacity: 1; } 100% { opacity: 0; transform: scale(1.4) rotate(calc(var(--rot) + 60deg)); } }
.fx-flip { position: absolute; top: calc(50% - 100px); width: 200px; height: 200px; background-repeat: no-repeat; background-size: 900% 100%;
  animation-name: flip; animation-timing-function: steps(8); animation-fill-mode: forwards; }
.fx-explosion { background-image: url(/static/fx/flipbooks/explosion.webp); }
.fx-flash { background-image: url(/static/fx/flipbooks/flash.webp); }
.fx-puff { background-image: url(/static/fx/flipbooks/puff.webp); }
.fx-smoke { background-image: url(/static/fx/flipbooks/smoke.webp); }
@keyframes flip { from { background-position: 0 0; } to { background-position: 100% 0; } }
@media (max-width: 640px) { .battle-stage { padding: 0 .6rem; height: 170px; } .stage-mon { max-width: 11rem; } }
