:root {
  --stone: #1c1915;
  --stone-2: #2a251f;
  --stone-3: #3a332a;
  --parchment: #f2e6cc;
  --parchment-2: #e6d5b0;
  --ink: #2b2118;
  --ink-soft: #5a4a3a;
  --blood: #a3301f;
  --gold: #d4a53c;
  --gold-dark: #9a7424;
  --graph: #9bbbe0;
  --ok: #4f8a3a;
  --font-title: 'Cinzel', Georgia, serif;
  --font-body: 'Crimson Pro', Georgia, serif;
  --font-hand: 'Patrick Hand', 'Comic Sans MS', cursive;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--stone); color: var(--parchment); font-family: var(--font-body); font-size: 18px; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.screen { display: none; position: fixed; inset: 0; flex-direction: column; padding-top: var(--safe-t); }
.screen.active { display: flex; }

/* ---------- buttons ---------- */
.btn { background: var(--blood); color: #fff6e6; border: 2px solid #5e1a10; border-radius: 10px; padding: 10px 18px; font-family: var(--font-title); font-weight: 600; letter-spacing: .03em; box-shadow: 0 3px 0 #5e1a10; transition: transform .08s, box-shadow .08s; }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #5e1a10; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.big { font-size: 1.15rem; padding: 14px 26px; min-width: 260px; }
.btn.gold { background: var(--gold); color: var(--ink); border-color: var(--gold-dark); box-shadow: 0 3px 0 var(--gold-dark); }
.btn.ghost { background: transparent; border-color: var(--parchment-2); color: var(--parchment); box-shadow: none; }
.icon-btn { background: var(--stone-2); border: 1px solid var(--stone-3); border-radius: 10px; min-width: 44px; height: 44px; font-size: 1.2rem; position: relative; font-family: var(--font-title); }
.icon-btn.small { min-width: 34px; height: 34px; font-size: 1rem; }
.badge { position: absolute; top: 6px; right: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--gold); }

/* ---------- title ---------- */
#title-screen { align-items: center; justify-content: center; background: radial-gradient(ellipse at 50% 30%, #3b3127 0%, var(--stone) 70%); }
.title-card { text-align: center; padding: 24px; }
.title-card h1 { font-family: var(--font-title); font-weight: 800; font-size: clamp(3rem, 12vw, 5.5rem); line-height: .95; margin: 12px 0 8px; color: var(--gold); text-shadow: 0 3px 0 #000, 0 0 30px rgba(212,165,60,.25); letter-spacing: .04em; }
.tagline { font-style: italic; font-size: 1.25rem; color: var(--parchment-2); margin: 0 0 32px; }
.title-buttons { display: flex; flex-direction: column; gap: 14px; align-items: center; }
.fineprint { margin-top: 28px; font-size: .8rem; color: #8a7d6a; }
.title-die { width: 92px; height: 92px; margin: 0 auto; background: url(icon.svg) center/contain no-repeat; animation: bob 4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-8px) rotate(8deg); } }

/* ---------- party rolling ---------- */
.party-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; }
.party-head h2 { font-family: var(--font-title); color: var(--gold); margin: 0; font-size: 1.4rem; }
.party-cards { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; padding: 6px 16px 16px; align-content: start; }
.party-foot { padding: 12px 16px calc(14px + var(--safe-b)); display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; background: linear-gradient(transparent, var(--stone) 30%); }
.hero-card { background: var(--parchment); color: var(--ink); border-radius: 12px; padding: 12px; box-shadow: 0 4px 0 #0006; display: grid; grid-template-columns: 92px 1fr; gap: 10px; min-height: 210px; position: relative; }
.hero-card.empty { place-items: center; grid-template-columns: 1fr; color: var(--ink-soft); font-family: var(--font-title); font-size: 1.1rem; }
.hero-card .portrait { width: 92px; height: 110px; }
.hero-card h3 { margin: 0; font-family: var(--font-title); font-size: 1.05rem; }
.hero-card .cls { font-style: italic; color: var(--ink-soft); font-size: .95rem; }
.stat-rows { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 8px; align-items: center; margin-top: 6px; font-size: .95rem; }
.stat-rows .sn { font-family: var(--font-title); font-weight: 600; font-size: .8rem; }
.stat-rows .sv { font-weight: 600; text-align: right; min-width: 2.2em; }
.stat-rows .sv.hi { color: var(--ok); }
.stat-rows .sv.lo { color: var(--blood); }
.d6row { display: flex; gap: 4px; }
.items-line { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; font-size: .9rem; }
.chip { background: var(--parchment-2); border: 1px solid #c9b48a; border-radius: 999px; padding: 2px 10px; color: var(--ink); }
button.chip { cursor: pointer; }

/* 3D d6 */
.d6 { width: 18px; height: 18px; position: relative; transform-style: preserve-3d; transition: transform .9s cubic-bezier(.2,.8,.3,1); }
.d6 .f { position: absolute; inset: 0; background: #fffaf0; border: 1px solid #9a8a6a; border-radius: 3px; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: 2px; backface-visibility: hidden; }
.d6 .f i { width: 3px; height: 3px; border-radius: 50%; background: var(--ink); place-self: center; }
.d6 .f1 { transform: translateZ(9px); }
.d6 .f6 { transform: rotateY(180deg) translateZ(9px); }
.d6 .f2 { transform: rotateY(90deg) translateZ(9px); }
.d6 .f5 { transform: rotateY(-90deg) translateZ(9px); }
.d6 .f3 { transform: rotateX(90deg) translateZ(9px); }
.d6 .f4 { transform: rotateX(-90deg) translateZ(9px); }

/* ---------- game layout ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; background: var(--stone); border-bottom: 1px solid var(--stone-3); z-index: 2; }
.topbar-title { font-family: var(--font-title); color: var(--gold); text-align: center; font-size: 1rem; line-height: 1.1; overflow: hidden; }
.gold-count { display: block; font-family: var(--font-body); color: var(--parchment-2); font-size: .85rem; }
.game-body { flex: 1; display: grid; grid-template-columns: 1fr minmax(340px, 40%); min-height: 0; }
.game-body.chat-hidden { grid-template-columns: 1fr 0; }
.game-body.chat-hidden .chat-pane { display: none; }
.map-pane { position: relative; min-height: 0; overflow: hidden; background: #efe9da; }
#map { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#map:active { cursor: grabbing; }
.map-controls { position: absolute; top: 10px; right: 10px; display: flex; flex-direction: column; gap: 6px; }
.map-controls .icon-btn { background: rgba(28,25,21,.85); color: var(--parchment); }

.party-strip { position: absolute; left: 0; right: 0; bottom: 0; display: flex; gap: 8px; padding: 8px 8px calc(8px + var(--safe-b)); justify-content: center; pointer-events: none; }
.hero-mini { pointer-events: auto; background: rgba(28,25,21,.88); border: 2px solid var(--stone-3); border-radius: 12px; padding: 4px 6px 6px; width: 86px; text-align: center; cursor: pointer; position: relative; }
.hero-mini.ko { border-color: var(--blood); }
.hero-mini .portrait { width: 64px; height: 76px; display: block; margin: 0 auto; }
.hero-mini .nm { font-size: .78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.1; }
.hpbar { height: 6px; background: #000a; border-radius: 3px; margin-top: 3px; overflow: hidden; }
.hpbar i { display: block; height: 100%; background: var(--ok); transition: width .5s; }
.hpbar i.mid { background: var(--gold); }
.hpbar i.low { background: var(--blood); }
.float-num { position: absolute; left: 50%; top: 20px; transform: translateX(-50%); font-family: var(--font-title); font-weight: 800; font-size: 1.4rem; pointer-events: none; animation: floatup 1.3s ease-out forwards; text-shadow: 0 2px 0 #000; }
.float-num.dmg { color: #ff6b57; }
.float-num.heal { color: #8fe07a; }
@keyframes floatup { to { transform: translate(-50%, -40px); opacity: 0; } }

/* ---------- chat ---------- */
.chat-pane { display: flex; flex-direction: column; min-height: 0; background: var(--parchment); color: var(--ink); border-left: 3px solid var(--gold-dark); }
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 10px; font-family: var(--font-title); font-weight: 600; background: var(--parchment-2); border-bottom: 1px solid #c9b48a; }
.chat-head .icon-btn { background: transparent; border-color: #c9b48a; color: var(--ink); }
.chat-log { flex: 1; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 12px; }
.msg { line-height: 1.4; font-size: 1.08rem; }
.msg.dm { padding-left: 2px; }
.msg.dm::first-letter { font-family: var(--font-title); font-size: 1.6em; color: var(--blood); }
.msg.player { align-self: flex-end; background: var(--ink); color: var(--parchment); padding: 6px 12px; border-radius: 14px 14px 2px 14px; max-width: 85%; font-family: var(--font-hand); font-size: 1.1rem; }
.msg.sys { font-style: italic; color: var(--ink-soft); font-size: .95rem; text-align: center; }
.msg.thinking { color: var(--ink-soft); font-style: italic; }
.msg.thinking::after { content: ''; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }
.suggestions { display: flex; gap: 6px; padding: 6px 10px 0; flex-wrap: wrap; }
.chat-form { display: flex; gap: 8px; padding: 8px 10px calc(8px + var(--safe-b)); }
.chat-form input { flex: 1; min-width: 0; font: inherit; font-family: var(--font-hand); font-size: 1.15rem; padding: 10px 12px; border-radius: 10px; border: 2px solid #c9b48a; background: #fffaf0; color: var(--ink); }
.chat-form input:focus { outline: none; border-color: var(--gold-dark); }

/* ---------- overlays ---------- */
.dice-overlay { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; background: rgba(10,8,6,.45); }
.roll-card { text-align: center; color: #fff; text-shadow: 0 2px 4px #000; pointer-events: none; }
.roll-card .label { font-family: var(--font-title); font-size: 1.1rem; margin-bottom: 6px; padding: 0 16px; }
.roll-card svg { width: 140px; height: 140px; filter: drop-shadow(0 8px 12px #000a); }
.roll-card.spin svg { animation: spin .7s cubic-bezier(.2,.7,.3,1); }
@keyframes spin { from { transform: rotate(-540deg) scale(.3); } to { transform: rotate(0) scale(1); } }
.roll-card .math { font-size: 1.1rem; margin-top: 4px; }
.roll-card .verdict { font-family: var(--font-title); font-weight: 800; font-size: 2rem; letter-spacing: .06em; animation: pop .3s; }
.roll-card .verdict.good { color: #9be27f; }
.roll-card .verdict.bad { color: #ff7a66; }
@keyframes pop { from { transform: scale(2); opacity: 0; } }
.dice-skip { position: fixed; bottom: calc(16px + var(--safe-b)); left: 0; right: 0; text-align: center; color: #fff9; font-size: .9rem; }

.modal, .blocker { position: fixed; inset: 0; z-index: 30; background: rgba(10,8,6,.7); display: grid; place-items: center; padding: 16px; }
.blocker { z-index: 50; }
.modal-card { background: var(--parchment); color: var(--ink); border-radius: 14px; padding: 18px; width: min(520px, 100%); max-height: 88vh; overflow-y: auto; box-shadow: 0 10px 40px #000a; }
.modal-card h2 { font-family: var(--font-title); margin: 0 0 4px; }
.modal-card .row { display: flex; gap: 14px; align-items: flex-start; }
.modal-card .portrait { width: 120px; height: 144px; flex: none; }
.modal-card .menu-list { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.modal-card .menu-list .btn { width: 100%; }
.modal-card .item-desc { margin-top: 10px; padding: 8px 10px; background: var(--parchment-2); border-radius: 8px; min-height: 2.4em; }
.end-banner { text-align: center; }
.end-banner h2 { font-size: 2rem; color: var(--blood); }

/* ---------- phones (portrait) ---------- */
@media (max-width: 760px), (max-aspect-ratio: 4/5) {
  .game-body { grid-template-columns: 1fr; grid-template-rows: 1fr minmax(0, 46%); }
  .game-body.chat-hidden { grid-template-rows: 1fr 0; grid-template-columns: 1fr; }
  .chat-pane { border-left: 0; border-top: 3px solid var(--gold-dark); }
  .hero-mini { width: 74px; padding: 3px 4px 5px; }
  .hero-mini .portrait { width: 52px; height: 62px; }
  .hero-card { grid-template-columns: 80px 1fr; }
  .hero-card .portrait { width: 80px; height: 96px; }
  .btn.big { min-width: 0; width: 100%; max-width: 340px; }
  .msg { font-size: 1.02rem; }
}
