:root {
  --bg: #14161b;
  --bg2: #1b1e25;
  --bg3: #232731;
  --bg4: #2c313d;
  --line: #333948;
  --tx: #e9eaee;
  --tx2: #a5abb8;
  --tx3: #737a89;
  --acc: #e8a33d;
  --acc2: #f2b75a;
  --red: #e05d6f;
  --green: #6cc56b;
  --blue: #4fb3d9;
  --r: 10px;
  --side-w: 340px;
  --top-h: 46px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--tx); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
body { overflow: hidden; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
input, select, textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-width: 0; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--acc); }
input[type="color"] { padding: 2px; height: 34px; width: 44px; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--acc); }
input[type="range"] { accent-color: var(--acc); padding: 0; border: 0; background: none; }
textarea { resize: vertical; width: 100%; }
.muted { color: var(--tx2); }
.small { font-size: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--bg4); border: 1px solid transparent; border-radius: 8px; padding: 8px 12px; font-weight: 600; white-space: nowrap;
}
.btn:hover { background: #363c4b; }
.btn.primary { background: var(--acc); color: #1d1404; }
.btn.primary:hover { background: var(--acc2); }
.btn.danger { background: transparent; border-color: #5a2d36; color: var(--red); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.sm { padding: 5px 9px; font-size: 13px; }
.btn svg { width: 17px; height: 17px; }
.icon-btn { background: none; border: 0; padding: 6px; border-radius: 6px; color: var(--tx2); display: inline-flex; }
.icon-btn:hover { background: var(--bg4); color: var(--tx); }
.icon-btn svg { width: 18px; height: 18px; }

/* ---------- Lobby ---------- */
.lobby { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; overflow: auto;
  background: radial-gradient(1200px 600px at 70% -10%, #2a2216 0%, transparent 60%), var(--bg); }
.lobby-card { width: min(420px, 100%); background: var(--bg2); border: 1px solid var(--line); border-radius: 16px; padding: 24px; display: grid; gap: 14px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--acc); }
.brand svg { width: 30px; height: 30px; }
.brand h1 { margin: 0; font-size: 26px; letter-spacing: .5px; color: var(--tx); }
.lobby-card p { margin: 0; }
.fld { display: grid; gap: 6px; }
.fld > span { font-size: 12px; color: var(--tx2); font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; padding: 0; }
.swatch.on { border-color: #fff; box-shadow: 0 0 0 2px var(--bg2) inset; }
.lobby-actions { display: grid; gap: 10px; margin-top: 4px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; }
.or { display: flex; align-items: center; gap: 10px; color: var(--tx3); font-size: 12px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.recent { display: grid; gap: 6px; }
.recent:empty { display: none; }
.recent h4 { margin: 4px 0 0; font-size: 12px; color: var(--tx2); text-transform: uppercase; letter-spacing: .5px; }
.recent button { text-align: left; background: var(--bg3); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; display: flex; justify-content: space-between; }
.recent button small { color: var(--tx3); }
.mode-note { font-size: 12px; color: var(--tx3); }
.lb-sec, #lb-auth-fb, #lb-auth-local { display: grid; gap: 12px; }
.btn.google { background: #fff; color: #1f1f1f; padding: 10px 12px; }
.btn.google:hover { background: #ececec; }
.btn.google svg { width: 18px; height: 18px; }
.seg.wide { display: flex; }
.seg.wide button { flex: 1; }
.linkbtn { background: none; border: 0; color: var(--tx2); padding: 2px 0; text-decoration: underline; text-underline-offset: 3px; font-size: 12.5px; }
.linkbtn:hover { color: var(--acc); }
.me-row { display: flex; align-items: center; gap: 10px; background: var(--bg3); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.me-row .av { width: 34px; height: 34px; font-size: 13px; }
.recent .role { font-size: 10.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .4px; padding: 2px 7px; border-radius: 999px; }
.recent .role.gm { background: #3a301f; color: var(--acc); }
.recent .role.pl { background: #1f2c3a; color: var(--blue); }
.recent .rm { display: flex; gap: 6px; align-items: center; }
.role-chip { font-size: 10.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .4px; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.role-chip.gm { background: #3a301f; color: var(--acc); }
.role-chip.pl { background: #1f2c3a; color: var(--blue); }
.mode-note b { color: var(--acc); }

/* ---------- App layout ---------- */
.app { position: fixed; inset: 0; display: grid; grid-template-columns: auto 1fr var(--side-w); grid-template-rows: var(--top-h) 1fr; grid-template-areas: "top top side" "tools stage side"; }
.app.side-closed { grid-template-columns: auto 1fr 0; }
.app.side-closed .side { display: none; }
.topbar { grid-area: top; display: flex; align-items: center; gap: 10px; padding: 0 10px; background: var(--bg2); border-bottom: 1px solid var(--line); min-width: 0; }
.room-btn { background: none; border: 0; font-weight: 700; padding: 6px 8px; border-radius: 8px; max-width: 30vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-btn:hover { background: var(--bg3); }
.scene-pick { display: flex; align-items: center; gap: 6px; min-width: 0; }
.scene-pick select { padding: 5px 8px; max-width: 200px; }
.scene-pick .live { font-size: 11px; color: var(--green); font-weight: 700; display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.scene-pick .live::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.scene-pick .notlive { font-size: 11px; color: var(--acc); font-weight: 700; white-space: nowrap; }
.turn-strip { display: flex; align-items: center; gap: 6px; margin-left: auto; min-width: 0; overflow: hidden; }
.turn-strip .rnd { font-size: 11px; color: var(--tx3); white-space: nowrap; }
.turn-strip .cur { background: var(--bg3); border: 1px solid var(--acc); border-radius: 999px; padding: 3px 10px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }
.presence { display: flex; gap: 4px; margin-left: auto; }
.turn-strip:not([hidden]) + .presence { margin-left: 8px; }
.av { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 11px; font-weight: 800; color: #111; position: relative; flex: none; }
.av.off { opacity: .35; }
.av.gm::after { content: "★"; position: absolute; right: -4px; bottom: -4px; font-size: 10px; color: var(--acc); text-shadow: 0 0 2px #000; }

.stage { grid-area: stage; position: relative; overflow: hidden; touch-action: none; background: #121418; }
#board { display: block; touch-action: none; }
.tools { grid-area: tools; display: flex; flex-direction: column; gap: 4px; padding: 8px 6px; background: var(--bg2); border-right: 1px solid var(--line); }
.tools button { background: none; border: 0; border-radius: 9px; width: 52px; padding: 7px 2px 5px; display: grid; justify-items: center; gap: 2px; color: var(--tx2); position: relative; }
.tools button span { font-size: 10px; }
.tools button:hover { background: var(--bg3); color: var(--tx); }
.tools button.on { background: var(--bg4); color: var(--acc); }
.tools .sep { flex: 1; }
.layer-btn.l-map { color: var(--blue) !important; }
.layer-btn.l-gm { color: var(--red) !important; }
.badge { position: absolute; top: 2px; right: 6px; background: var(--red); color: #fff; font-size: 10px; min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center; padding: 0 4px; }
.app.side-closed .panel-toggle { color: var(--tx2); }
.app:not(.side-closed) .panel-toggle { color: var(--acc); }
body:not(.is-gm) .gm-only { display: none !important; }

.zoom { position: absolute; right: 10px; bottom: 10px; display: flex; flex-direction: column; gap: 4px; }
.zoom button { width: 36px; height: 36px; border-radius: 9px; border: 1px solid var(--line); background: rgba(27,30,37,.9); display: grid; place-items: center; color: var(--tx2); }
.zoom button:hover { color: var(--tx); }

/* ---------- Painel lateral ---------- */
.side { grid-area: side; background: var(--bg2); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.side-tabs { display: flex; border-bottom: 1px solid var(--line); padding: 4px; gap: 2px; flex: none; }
.side-tabs button { flex: 1; background: none; border: 0; border-radius: 8px; padding: 6px 2px 4px; display: grid; justify-items: center; gap: 1px; color: var(--tx3); min-width: 0; }
.side-tabs button span { font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.side-tabs button svg { width: 18px; height: 18px; }
.side-tabs button.on { color: var(--acc); background: var(--bg3); }
.side-tabs button:hover { color: var(--tx); }
.side-close { flex: 0 0 34px !important; }
.panel { display: none; flex: 1; min-height: 0; overflow: auto; padding: 12px; }
.panel.on { display: flex; flex-direction: column; gap: 10px; }
.panel h3, .modal-body h3 { margin: 4px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--tx2); display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.card { background: var(--bg3); border: 1px solid var(--line); border-radius: var(--r); padding: 10px; display: grid; gap: 8px; }
.empty { color: var(--tx3); font-size: 13px; text-align: center; padding: 18px 8px; border: 1px dashed var(--line); border-radius: var(--r); }
label.inline { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--tx2); }
label.stack { display: grid; gap: 4px; font-size: 12px; color: var(--tx2); }
label.stack input, label.stack select { width: 100%; color: var(--tx); }

/* ---------- Chat ---------- */
.chat-panel { padding: 0 !important; gap: 0 !important; }
.chat-log { flex: 1; overflow-y: auto; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; overscroll-behavior: contain; }
.msg { font-size: 13.5px; word-wrap: break-word; }
.msg .who { font-weight: 700; margin-right: 6px; }
.msg .when { color: var(--tx3); font-size: 11px; margin-left: 4px; }
.msg.sys { color: var(--tx3); font-style: italic; font-size: 12.5px; }
.msg.me { font-style: italic; color: var(--tx2); }
.msg.priv { background: #241f2f; border-left: 3px solid #a77de8; padding: 6px 8px; border-radius: 6px; }
.msg .tag { font-size: 10.5px; font-weight: 700; text-transform: uppercase; color: #c3a5f0; margin-right: 6px; }
.roll { background: var(--bg3); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin-top: 4px; }
.roll-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.roll-label { font-weight: 600; color: var(--tx2); font-size: 12.5px; }
.roll-formula { font-family: ui-monospace, Consolas, monospace; font-size: 12px; color: var(--tx3); }
.roll-body { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 4px; }
.roll-detail { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; color: var(--tx2); }
.roll-total { font-size: 24px; font-weight: 800; color: var(--tx); min-width: 40px; text-align: right; }
.roll.crit .roll-total { color: var(--green); }
.roll.fumble .roll-total { color: var(--red); }
.d { display: inline-block; min-width: 18px; padding: 0 3px; border-radius: 4px; background: var(--bg4); text-align: center; margin: 1px; }
.d.max { color: var(--green); font-weight: 700; }
.d.min { color: var(--red); font-weight: 700; }
.d.drop { opacity: .4; text-decoration: line-through; }
.d.hit { outline: 1px solid var(--green); }
.inline-roll { background: var(--bg4); border-radius: 5px; padding: 0 5px; font-weight: 700; cursor: help; border-bottom: 1px dotted var(--acc); }
.dice-quick { display: flex; gap: 4px; flex-wrap: wrap; padding: 8px 10px 0; border-top: 1px solid var(--line); }
.dice-quick button { background: var(--bg3); border: 1px solid var(--line); border-radius: 7px; padding: 4px 8px; font-size: 12.5px; font-weight: 600; color: var(--tx2); }
.dice-quick button:hover { color: var(--acc); border-color: var(--acc); }
.dice-quick .mod { width: 54px; padding: 4px 6px; font-size: 12.5px; }
.chat-form { display: flex; gap: 8px; padding: 8px 10px calc(10px + var(--safe-b)); }
.chat-form input { flex: 1; }

/* ---------- HUD de token ---------- */
.hud { position: absolute; z-index: 5; background: rgba(27,30,37,.97); border: 1px solid var(--line); border-radius: 12px; padding: 8px; display: grid; gap: 8px; width: 260px; box-shadow: 0 10px 30px rgba(0,0,0,.45); }
.hud .hud-head { display: flex; align-items: center; gap: 6px; }
.hud .hud-head b { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hp-row { display: flex; align-items: center; gap: 6px; }
.hp-row input { width: 52px; text-align: center; padding: 5px; }
.hp-row .hpmax { color: var(--tx3); font-size: 13px; }
.hp-row button { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg3); font-weight: 800; }
.hud-actions { display: flex; gap: 2px; flex-wrap: wrap; }
.hud-actions .icon-btn.on { color: var(--acc); }
.size-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 3px; }
.size-row button { background: var(--bg3); border: 1px solid transparent; border-radius: 7px; padding: 5px 0; font-size: 11.5px; font-weight: 600; color: var(--tx2); }
.size-row button.on { border-color: var(--acc); color: var(--acc); background: #3a301f; }
.conds { display: grid; grid-template-columns: repeat(8, 1fr); gap: 3px; }
.conds button { background: var(--bg3); border: 1px solid transparent; border-radius: 7px; padding: 3px 0; font-size: 16px; line-height: 1.3; opacity: .45; }
.conds button.on { opacity: 1; border-color: var(--acc); background: #3a301f; }

/* ---------- Opções de ferramenta ---------- */
.toolopts { position: absolute; left: 10px; top: 10px; z-index: 4; background: rgba(27,30,37,.96); border: 1px solid var(--line); border-radius: 12px; padding: 8px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; max-width: calc(100% - 70px); box-shadow: 0 6px 20px rgba(0,0,0,.35); }
.seg { display: inline-flex; flex-wrap: wrap; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg button { background: none; border: 0; padding: 5px 9px; border-radius: 6px; font-size: 12.5px; color: var(--tx2); font-weight: 600; white-space: nowrap; }
.seg button.on { background: var(--bg4); color: var(--acc); }
.dots { display: inline-flex; gap: 4px; }
.dots button { width: 22px; height: 22px; border-radius: 50%; border: 2px solid transparent; padding: 0; }
.dots button.on { border-color: #fff; }
.toolopts .lbl { font-size: 12px; color: var(--tx3); }

/* ---------- Toasts ---------- */
.toast { position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%) translateY(20px); background: #2e3341; color: var(--tx); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: 90vw; box-shadow: 0 8px 24px rgba(0,0,0,.4); }
.toast.on { opacity: 1; transform: translateX(-50%); }
.rolltoast { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); display: grid; gap: 6px; z-index: 3; pointer-events: none; width: min(320px, 80%); }
.rolltoast .roll { box-shadow: 0 8px 24px rgba(0,0,0,.4); animation: pop .25s ease-out; background: rgba(35,39,49,.97); }
.rolltoast .roll .who { font-weight: 700; font-size: 12px; }
@keyframes pop { from { transform: translateY(-8px); opacity: 0; } }

/* ---------- Modal ---------- */
.modal { background: var(--bg2); color: var(--tx); border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); }
.modal::backdrop { background: rgba(0,0,0,.6); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg2); z-index: 1; }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 14px; display: grid; gap: 12px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 14px; border-top: 1px solid var(--line); position: sticky; bottom: 0; background: var(--bg2); }

/* ---------- Listas ---------- */
.list { display: grid; gap: 6px; }
.item { display: flex; align-items: center; gap: 10px; background: var(--bg3); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.item.active { border-color: var(--acc); }
.item .grow { flex: 1; min-width: 0; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 12px; color: var(--tx3); }
.thumb { width: 40px; height: 40px; border-radius: 8px; background: var(--bg4) center/cover no-repeat; flex: none; }
.thumb.round { border-radius: 50%; }
.assets { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 8px; }
.asset { background: var(--bg3); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; display: grid; padding: 0; text-align: left; }
.asset .img { aspect-ratio: 1; background: var(--bg4) center/cover no-repeat; }
.asset .nm { font-size: 11.5px; padding: 4px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--tx2); }
.asset:hover { border-color: var(--acc); }
.dropzone { border: 1.5px dashed var(--line); border-radius: 10px; padding: 14px; text-align: center; color: var(--tx2); display: grid; gap: 8px; justify-items: center; }
.dropzone.over { border-color: var(--acc); color: var(--acc); }

/* Iniciativa */
.init-item { display: flex; align-items: center; gap: 8px; background: var(--bg3); border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px; }
.init-item.cur { border-color: var(--acc); background: #2f2819; }
.init-item .val { width: 52px; text-align: center; padding: 4px; font-weight: 700; }
.init-item .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.init-item .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }

/* Fichas */
.sheet-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 6px; }
.stat { background: var(--bg); border: 1px solid var(--line); border-radius: 9px; padding: 6px; display: grid; gap: 2px; text-align: center; }
.stat input { text-align: center; padding: 3px; border: 0; background: transparent; }
.stat .lb { font-size: 11px; color: var(--tx2); font-weight: 700; text-transform: uppercase; }
.stat .val { font-size: 18px; font-weight: 800; }
.stat .modv { font-size: 12px; color: var(--acc); }
.act-row { display: flex; gap: 6px; align-items: center; }
.act-row input:first-child { flex: 1; }
.act-row input.f { width: 120px; font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
.rollbtn { background: var(--bg3); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-weight: 600; display: inline-flex; gap: 6px; align-items: center; }
.rollbtn:hover { border-color: var(--acc); color: var(--acc); }
.rollbtn small { color: var(--tx3); font-family: ui-monospace, Consolas, monospace; }

/* ---------- Celular ---------- */
@media (max-width: 820px) {
  :root { --top-h: 44px; }
  .app { grid-template-columns: 1fr; grid-template-rows: var(--top-h) 1fr auto; grid-template-areas: "top" "stage" "tools"; }
  .app.side-closed { grid-template-columns: 1fr; }
  .tools { flex-direction: row; border-right: 0; border-top: 1px solid var(--line); padding: 4px 4px calc(4px + var(--safe-b)); overflow-x: auto; gap: 2px; }
  .tools button { width: auto; min-width: 54px; flex: 1 0 auto; }
  .tools .sep { display: none; }
  .side { position: fixed; left: 0; right: 0; bottom: 0; height: min(72dvh, 640px); border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; z-index: 20; box-shadow: 0 -10px 40px rgba(0,0,0,.5); }
  .side-tabs button span { display: none; }
  .side-tabs button { padding: 8px 2px; }
  .room-btn { max-width: 34vw; }
  .scene-pick select { max-width: 34vw; }
  .presence .av:nth-child(n+5) { display: none; }
  .turn-strip .rnd { display: none; }
  .zoom { bottom: 10px; }
  .hud { left: 8px !important; right: 8px; width: auto; top: auto !important; bottom: 8px; }
  .toolopts { left: 8px; right: 8px; max-width: none; top: 8px; }
  .toast { bottom: 90px; }
}
@media (max-width: 480px) {
  .scene-pick .live, .scene-pick .notlive { display: none; }
}
.install-link { display: block; margin: 12px auto 0; color: var(--acc); font-size: 13px; }
.install-link[hidden] { display: none; }
