:root {
  --bg: #f4f2ed;
  --card: #ffffff;
  --ink: #1f2430;
  --muted: #68707e;
  --line: #e3ded2;
  --accent: #7c3aed;
  --accent-ink: #ffffff;
  --good: #178a4c;
  --bad: #c62f3b;
  --warn: #b97d10;
  --hp: #d64550;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(31, 36, 48, .08), 0 4px 14px rgba(31, 36, 48, .05);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); font-weight: 600; }

/* ---------- top bar ---------- */
#topbar {
  position: sticky; top: 0; z-index: 20;
  background: rgba(255,255,255,.92); backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1100px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { font-weight: 800; font-size: 18px; letter-spacing: .2px; }
.game-chip { display: flex; align-items: center; gap: 10px; }
#game-chip-code {
  font-weight: 800; font-size: 18px; letter-spacing: 1.5px;
  background: #efe9fb; color: #5b21b6; border-radius: 8px; padding: 4px 10px;
}

main { max-width: 1100px; margin: 0 auto; padding: 18px 14px 60px; }

/* ---------- generic ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
}
.card h2 { margin: 0 0 8px; font-size: 19px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.chip {
  display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 9px;
  border-radius: 999px; background: #eef0f4; color: #444c5c; white-space: nowrap;
}
.chip.ac { background: #e8f0fe; color: #1a56db; }
.chip.cond { background: #fdf0e3; color: #92580a; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

.btn {
  appearance: none; border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 10px; padding: 9px 14px; font-size: 15px; font-weight: 600;
  cursor: pointer; min-height: 42px;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: #f7f5f0; }
.btn-danger { background: #fff; border-color: #eccdd0; color: var(--bad); }
.btn-big { width: 100%; font-size: 17px; padding: 13px; }
.btn-small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: default; }

.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
input[type="text"], input[type="number"], input[type="search"] {
  width: 100%; padding: 11px 12px; font-size: 16px; border: 1px solid var(--line);
  border-radius: 10px; background: #fff; color: var(--ink);
}
input:focus { outline: 2px solid #cdbcf5; border-color: var(--accent); }

/* ---------- home ---------- */
.hero-block { padding: 14px 4px 4px; }
.hero-block h1 { font-size: 30px; margin: 0 0 6px; }
.lede { font-size: 17px; color: #3c4353; max-width: 560px; margin: 0 0 8px; }

/* ---------- GM layout ---------- */
.gm-grid { display: grid; grid-template-columns: 1fr; gap: 0; }
@media (min-width: 900px) {
  .gm-grid { grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
  .gm-grid .card { margin-bottom: 0; }
  #gm-quest-card, .danger-zone { grid-column: 1 / -1; }
}

/* stage rail */
.stage-rail { list-style: none; margin: 8px 0 14px; padding: 0; }
.stage-rail li {
  display: flex; gap: 10px; padding: 9px 10px; border-radius: 10px; align-items: flex-start;
}
.stage-rail li .stage-num {
  flex: 0 0 26px; width: 26px; height: 26px; border-radius: 50%;
  background: #eef0f4; color: #444c5c; font-weight: 800; font-size: 13px;
  display: flex; align-items: center; justify-content: center; margin-top: 1px;
}
.stage-rail li.done { color: var(--muted); }
.stage-rail li.done .stage-num { background: #dff3e6; color: var(--good); }
.stage-rail li.current { background: #f4efff; }
.stage-rail li.current .stage-num { background: var(--accent); color: #fff; }
.stage-rail .stage-title { font-weight: 700; }
.stage-rail .stage-notes { font-size: 14px; color: #3c4353; margin-top: 3px; max-width: 640px; }

/* combatant cards */
.combatant {
  display: flex; gap: 12px; padding: 10px; border: 1px solid var(--line);
  border-radius: 12px; margin-bottom: 10px; background: #fffdf9;
}
.combatant.dead { opacity: .55; }
.combatant img.portrait {
  width: 52px; height: 52px; border-radius: 10px; object-fit: cover; flex: 0 0 52px; background: #eee;
}
.combatant .c-main { flex: 1; min-width: 0; }
.combatant .c-name { font-weight: 800; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.combatant .c-sub { font-size: 13px; color: var(--muted); }
.hpbar { height: 10px; background: #f0e6e7; border-radius: 999px; overflow: hidden; margin: 6px 0 4px; }
.hpbar > div { height: 100%; background: var(--hp); border-radius: 999px; transition: width .3s; }
.hpbar.big { height: 16px; }
.hp-text { font-weight: 800; font-size: 14px; }
.hp-controls { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.hp-controls input { width: 64px; padding: 7px 8px; }
.hp-controls .btn { min-height: 34px; padding: 5px 10px; font-size: 14px; }
.cond-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; align-items: center; }
.cond-row input { width: 130px; padding: 6px 8px; font-size: 14px; }
.cond-chip { cursor: pointer; }
.enemy-form { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 8px; margin-top: 6px; }
.enemy-form #en-notes { grid-column: 1 / -1; }
.enemy-form .btn { grid-column: 1 / -1; }

/* pickers */
.picker { margin-top: 10px; }
.picker-list { max-height: 320px; overflow: auto; margin-top: 8px; }
.picker-item {
  display: flex; gap: 10px; align-items: center; width: 100%; text-align: left;
  padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
  margin-bottom: 6px; cursor: pointer; font: inherit;
}
.picker-item img { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; background: #eee; }
.picker-item .p-name { font-weight: 700; }
.picker-item .p-sub { font-size: 13px; color: var(--muted); }

/* initiative */
.init-row {
  display: flex; gap: 8px; align-items: center; padding: 7px 8px;
  border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; background: #fff;
}
.init-row.active-turn { background: #f4efff; border-color: var(--accent); }
.init-row .i-name { flex: 1; font-weight: 700; min-width: 0; }
.init-row input { width: 70px; padding: 7px 8px; }
.init-row .i-badge { font-size: 12px; font-weight: 800; color: var(--accent); }

/* log */
.log { max-height: 340px; overflow: auto; }
.log-entry { padding: 7px 2px; border-bottom: 1px dashed var(--line); font-size: 14px; }
.log-entry:last-child { border-bottom: none; }
.log-entry .l-actor { font-weight: 800; }
.log-entry .l-total { font-weight: 800; }
.outcome { font-size: 11px; font-weight: 800; padding: 2px 7px; border-radius: 999px; margin-left: 6px; }
.outcome.hit, .outcome.crit { background: #dff3e6; color: var(--good); }
.outcome.crit { background: #fdeec9; color: var(--warn); }
.outcome.miss { background: #fbe3e5; color: var(--bad); }
.log-entry.system { color: var(--muted); font-style: italic; }
.log-entry .l-dice { color: var(--muted); font-size: 13px; }

/* ---------- player ---------- */
#view-player { max-width: 620px; margin: 0 auto; }
.stage-banner {
  text-align: center; font-weight: 700; color: #5b21b6; background: #efe9fb;
  border-radius: 10px; padding: 8px 12px; margin-bottom: 12px;
}
.your-turn {
  text-align: center; font-size: 22px; font-weight: 900; letter-spacing: .5px;
  background: #178a4c; color: #fff; border-radius: 12px; padding: 12px; margin-bottom: 12px;
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.02); } }
.party-strip { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.party-strip .p-name-chip {
  font-size: 13px; font-weight: 700; background: #eef0f4; border-radius: 999px; padding: 4px 10px;
}
.party-strip .p-name-chip.turn { background: var(--accent); color: #fff; }
.party-strip .p-name-chip.me { outline: 2px solid var(--accent); }

.hero-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin-top: 10px; }
.hero-card {
  border: 1px solid var(--line); border-radius: 12px; padding: 10px; background: #fff;
  cursor: pointer; text-align: center; font: inherit; width: 100%;
}
.hero-card img { width: 76px; height: 76px; border-radius: 12px; object-fit: cover; background: #eee; }
.hero-card .h-name { font-weight: 800; margin-top: 6px; font-size: 15px; }
.hero-card .h-sub { font-size: 12px; color: var(--muted); }
.hero-card.taken { opacity: .5; cursor: default; }

.sheet-head { display: flex; gap: 14px; align-items: center; }
.sheet-head img { width: 84px; height: 84px; border-radius: 14px; object-fit: cover; background: #eee; }
.sheet-head h2 { margin: 0; font-size: 22px; }
.sheet-head .s-sub { color: var(--muted); font-size: 14px; }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0; }
.stat-box {
  border: 1px solid var(--line); border-radius: 10px; text-align: center; padding: 8px 4px;
  background: #fffdf9; cursor: pointer; font: inherit; width: 100%;
}
.stat-box .s-label { font-size: 11px; font-weight: 800; color: var(--muted); letter-spacing: 1px; }
.stat-box .s-score { font-size: 20px; font-weight: 900; }
.stat-box .s-mod { font-size: 13px; color: var(--muted); font-weight: 700; }
.save-row { display: flex; gap: 6px; flex-wrap: wrap; }
.save-btn {
  border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px;
  font-weight: 700; font-size: 14px; cursor: pointer; min-height: 36px;
}
.attack-card { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 10px; background: #fffdf9; }
.attack-card .a-name { font-weight: 900; font-size: 17px; }
.attack-card .a-stats { color: var(--muted); font-size: 14px; font-weight: 700; }
.attack-card .a-notes { font-size: 14px; margin-top: 4px; }
.ability-item { padding: 8px 0; border-bottom: 1px dashed var(--line); }
.ability-item:last-child { border-bottom: none; }
.ability-item .ab-name { font-weight: 800; }
.ability-item .ab-text { font-size: 14px; color: #3c4353; }
.dice-tray { display: flex; gap: 8px; flex-wrap: wrap; }
.dice-tray .btn { flex: 1 1 60px; font-size: 16px; }
.roll-result {
  border-radius: 12px; padding: 12px; margin: 10px 0; font-size: 16px;
  background: #f4efff; border: 1px solid #ddd0fb;
}
.roll-result .r-total { font-size: 24px; font-weight: 900; }
details.sheet-more { margin-top: 6px; }
details.sheet-more summary { font-weight: 800; cursor: pointer; padding: 6px 0; }
.toggle-row { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 14px; margin: 10px 0; }
.physical-form { display: grid; grid-template-columns: 1fr 110px; gap: 8px; margin-top: 8px; }
.physical-form .btn { grid-column: 1 / -1; }

.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: #1f2430; color: #fff; padding: 11px 18px; border-radius: 999px;
  font-weight: 700; box-shadow: var(--shadow); z-index: 50; max-width: 92vw; text-align: center;
}
.danger-zone { text-align: center; }
