:root {
  --bg: #14151a; --panel: #1e2029; --ink: #e8e8ee; --muted: #9aa0b0;
  --accent: #7c5cff; --line: #2c2f3a; --green: #2fbf6b; --yellow: #d8b431;
  --red: #d84a4a; --blue: #4a7ad8; --orange: #d8824a; --gold: #d4af37;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; background: var(--bg); color: var(--ink); }
.hidden { display: none !important; }
.drop-zone { border: 2px dashed var(--line); border-radius: 6px; padding: .5em 1em; color: var(--muted); font-size: .85em; }
.drop-zone.dragover { border-color: var(--accent); color: var(--ink); background: rgba(124,92,255,.12); }
.drop-zone.has-file { border-style: solid; color: var(--ink); }
.muted { color: var(--muted); font-size: .85em; }
.error { color: var(--red); min-height: 1em; }
button { background: var(--accent); color: #fff; border: 0; padding: .5em .9em; border-radius: 6px; cursor: pointer; }
button.secondary { background: var(--line); }
button:disabled { opacity: .4; cursor: not-allowed; }
input, select, textarea { background: #0f1015; color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: .5em; }
a { color: var(--accent); }

.topbar { display: flex; align-items: center; justify-content: space-between; padding: .6em 1.2em; background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 1.1rem; margin: 0; }
.userbox { display: flex; gap: .6em; align-items: center; }
.trap-alert { display: flex; align-items: center; gap: .8em; padding: .6em 1.2em; background: var(--orange); color: #1a1408; font-weight: 600; flex-wrap: wrap; }
.trap-alert #trap-alert-dm, .trap-alert #trap-alert-player { display: flex; align-items: center; gap: .8em; flex-wrap: wrap; }
.trap-alert .muted { color: #1a1408; opacity: .75; }

.card, .panel { background: var(--panel); margin: 1em auto; padding: 1.2em; border-radius: 10px; max-width: 900px; border: 1px solid var(--line); }
.tabs, .dash-tabs { display: flex; gap: .4em; margin-bottom: 1em; flex-wrap: wrap; }
.tab { background: var(--line); }
.tab.active { background: var(--accent); }
.stack { display: flex; flex-direction: column; gap: .6em; max-width: 320px; }
.row { display: flex; gap: .5em; flex-wrap: wrap; align-items: center; margin: .6em 0; }
.list { list-style: none; padding: 0; display: flex; flex-direction: column; gap: .4em; }
.list li { background: #0f1015; padding: .5em .7em; border-radius: 6px; display: flex; gap: .6em; align-items: center; }
.pill { background: var(--line); border-radius: 999px; padding: .1em .6em; font-size: .75em; }

/* ---- Map ---- */
.map-body { overflow: hidden; }
.map-layout { display: flex; height: calc(100vh - 52px); }
.stage { flex: 1; overflow: auto; background-size: cover; background-position: center; position: relative; }
.grid {
  display: grid; position: relative; user-select: none; -webkit-user-select: none;
  /* Only takes effect where a background-image is actually set directly on the
     grid element itself (currently just the grid editor) — sized/positioned
     relative to the grid's own true pixel dimensions (columns/rows × cell
     size) rather than the scrollable stage's visible viewport, so it scales
     with the grid instead of tiling or misaligning as you scroll. */
  background-size: cover; background-position: center; background-repeat: no-repeat;
}
.cell { border: 1px solid #000; background: rgba(255,255,255,0.02); }
.cell.reach-normal { background: rgba(47,191,107,0.35); }
.cell.reach-dash { background: rgba(216,180,49,0.35); }
.cell.selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell.blocking { background: rgba(216,74,74,0.25); }
.cell.move-blocking { box-shadow: inset 0 0 0 3px #b5651d; }
.cell.hidden-cell { background: rgba(0,0,0,1); }
.cell.hidden-marker { outline: 1px dashed var(--yellow); outline-offset: -1px; }
/* Map link marker (see map-links.js): a corner badge, so it never hides
   the underlay or a token. Locked links get a padlock; hidden (secret)
   links, only ever shown to the DM, are dimmed. */
.cell.link-cell { position: relative; }
.cell.link-cell::after {
  content: "🔗"; position: absolute; right: 1px; bottom: 0;
  font-size: 11px; line-height: 1; pointer-events: none;
  filter: drop-shadow(0 0 2px #000);
}
.cell.link-cell.link-locked::after { content: "🔒"; }
.cell.link-cell.link-hidden::after { opacity: .5; }
.link-map-fields { border: 1px solid var(--line); border-radius: 6px; margin: .6em 0; padding: .4em .6em; }
.link-map-fields legend { color: var(--muted); font-size: .85em; padding: 0 .3em; }
.link-map-fields .link-dest input { width: 4.5em; }
.link-map-fields .muted { margin: .2em 0; }
.map-view-select { width: 100%; margin-bottom: .5em; }

.token { position: absolute; display: flex; align-items: center; justify-content: center;
  border-radius: 50%; font-weight: bold; pointer-events: none; border: 2px solid var(--blue); }
.token.npc { border-color: var(--red); background: rgba(216,74,74,0.35); }
.token.pc { border-color: var(--blue); background: rgba(74,122,216,0.35); }
.token.trap { border-color: var(--orange); border-style: dashed; border-radius: 4px; background: rgba(216,130,74,0.35); }
.token.loot { border-color: var(--gold); border-style: dotted; background: rgba(212,175,55,0.35); }
.token.active { box-shadow: 0 0 0 3px var(--accent); }
.token.hurt { border-color: var(--yellow); }
.token.dead { filter: grayscale(1); opacity: .7; }
.tlabel { font-size: .7em; color: #000; }
.hpbar { position: absolute; bottom: 2px; left: 10%; width: 80%; height: 3px; background: #000; border-radius: 2px; }
.hpbar i { display: block; height: 100%; background: var(--green); }
.token.hurt .hpbar i { background: var(--yellow); }
.skull { position: absolute; font-size: 1.2em; }
/* Disambiguation badge for tokens sharing a name with another token
   currently on the map (see computeDupNumbers in map.js) — same number
   also shown next to the combatant's name in the tracker (.tracker-tnum)
   for cross-reference. */
.tnum {
  position: absolute; top: -5px; right: -5px; min-width: 14px; height: 14px;
  padding: 0 2px; border-radius: 50%; background: var(--accent); color: #fff;
  font-size: .65em; line-height: 14px; text-align: center; font-weight: bold;
}
.tracker-tnum { color: var(--muted); font-weight: normal; font-size: .85em; }

.sidebar { width: 300px; background: var(--panel); border-left: 1px solid var(--line); padding: 1em; overflow: auto; }
.turnbar { display: flex; gap: .5em; align-items: center; flex-wrap: wrap; }
.tracker { list-style: none; padding: 0; }
.tracker li { padding: .3em; border-radius: 6px; }
.tracker li.active { background: rgba(124,92,255,0.25); }
.tracker-group { margin-bottom: .6em; }
.tracker-group summary { cursor: pointer; font-weight: 600; padding: .3em 0; }
.tracker-list { list-style: none; padding: 0; margin: .2em 0 0; min-height: 1.5em; }
.tracker-list.drop-target { outline: 2px dashed var(--accent); outline-offset: 2px; border-radius: 6px; }
.combatant-name { cursor: pointer; user-select: none; }
.combatant-info { margin: .5em 0 .3em; padding-left: .3em; border-left: 2px solid var(--line); font-size: .9em; }
.combatant-info details { margin: .3em 0; }
.combatant-info summary { cursor: pointer; font-weight: 600; }
.combatant-info textarea { width: 100%; margin-top: .3em; }
.condition-list { display: flex; flex-direction: column; gap: .2em; margin-top: .3em; }
.condition-row { display: flex; align-items: center; gap: .4em; font-weight: normal; }
.condition-swatch { display: inline-block; width: .8em; height: .8em; border-radius: 50%; flex-shrink: 0; }
.drawer label { display: block; margin: .4em 0; }
.flash { position: fixed; bottom: 1em; left: 50%; transform: translateX(-50%); background: var(--red); color: #fff; padding: .6em 1em; border-radius: 8px; z-index: 100; }
details textarea { width: 100%; }
.lib-group { margin: .3em 0; }
.lib-group > summary { cursor: pointer; padding: .4em .2em; font-weight: 600; }
.lib-group .lib-group { margin-left: 1.2em; }
.lib-group .lib-group > summary { font-weight: 400; }

.panel-wide { max-width: 1400px; }
.lib-columns { display: flex; gap: 1em; align-items: flex-start; margin-top: 1em; }
.lib-col-list { flex: 1 1 40%; min-width: 0; max-height: 75vh; overflow-y: auto; }
.lib-col-detail { flex: 1 1 60%; min-width: 0; position: sticky; top: 1em; max-height: 75vh; overflow-y: auto; }
@media (max-width: 900px) {
  .lib-columns { flex-direction: column; }
  .lib-col-detail { position: static; }
}

/* ---- Library item detail rendering ---- */
.lib-detail-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1em; margin-bottom: .6em; }
.lib-detail-header h3 { margin: 0; }
.lib-subtitle { color: var(--muted); font-style: italic; margin: 0 0 .8em; }
.lib-stat-row { display: flex; flex-wrap: wrap; gap: 1em; margin: .6em 0; padding: .6em 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.lib-stat { text-align: center; min-width: 4.5em; }
.lib-stat .lib-stat-label { display: block; font-size: .72em; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.lib-stat .lib-stat-value { display: block; font-size: 1.1em; font-weight: 700; }
.lib-section { margin: .8em 0; }
.lib-section h4 { margin: 0 0 .3em; font-size: .85em; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.lib-section p { margin: .3em 0; line-height: 1.45; }
.lib-kv { display: grid; grid-template-columns: max-content 1fr; gap: .2em 1em; margin: .4em 0; }
.lib-kv dt { color: var(--muted); }
.lib-kv dd { margin: 0; }
.lib-tag-list { display: flex; flex-wrap: wrap; gap: .4em; }
.lib-ability-block { border-left: 2px solid var(--line); padding-left: .7em; margin: .5em 0; }
.lib-ability-block .lib-ability-name { font-weight: 700; font-style: italic; }
.item-feature-row { border-left: 3px solid var(--green); padding-left: .7em; }
.item-feature-row.cf-debuff { border-left-color: var(--red); }
.lib-empty { color: var(--muted); font-style: italic; }

/* ---- Encounter calculator ---- */
.calc-inputs { display: flex; flex-wrap: wrap; gap: 1em; align-items: flex-end; margin-bottom: .8em; }
.calc-inputs label { display: flex; flex-direction: column; gap: .2em; font-size: .85em; color: var(--muted); }
.calc-inputs input, .calc-inputs select { min-width: 6em; }

/* ---- Player tools (My Character panel) ---- */
#player-tools #pc-saves { flex-wrap: wrap; gap: .4em; margin: .4em 0; }
.custom-roll-inputs { display: flex; flex-wrap: wrap; gap: .8em; margin: .5em 0; }
.custom-roll-inputs label { display: flex; flex-direction: column; gap: .2em; font-size: .85em; color: var(--muted); }
.custom-roll-inputs input { width: 4em; }
.roll-result { min-height: 1.2em; font-weight: 600; }

/* ---- Shared roll log (bottom of sidebar, visible to DM and players alike) ---- */
#roll-log-section { margin-top: 1em; padding-top: .8em; border-top: 1px solid var(--line); }
#roll-log { max-height: 220px; overflow-y: auto; font-size: .85em; }
#roll-log li { padding: .2em .3em; }
.calc-table { width: 100%; border-collapse: collapse; margin: .6em 0; }
.calc-table th, .calc-table td { text-align: left; padding: .4em .6em; border-bottom: 1px solid var(--line); }
.calc-table tbody tr { cursor: pointer; }
.calc-table tbody tr:hover { background: rgba(124,92,255,0.15); }

/* ---- Game tab ---- */
.game-map-frame { width: 100%; height: 80vh; border: 1px solid var(--line); border-radius: 8px; margin-top: 1em; background: var(--bg); }

/* ---- Token drawer (left of map) ---- */
.token-drawer { width: 220px; border-left: none; border-right: 1px solid var(--line); }
.token-tray { display: flex; flex-direction: column; gap: .5em; margin-bottom: 1em; }
.tray-token { display: flex; align-items: center; gap: .5em; padding: .4em .6em; background: #0f1015; border: 1px solid var(--line); border-radius: 6px; cursor: grab; }
.tray-token:active { cursor: grabbing; }
.tray-avatar { width: 26px; height: 26px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: .7em; font-weight: 700; border: 2px solid var(--blue); flex-shrink: 0; background: rgba(74,122,216,0.25); }
.tray-token.npc .tray-avatar { border-color: var(--red); background: rgba(216,74,74,0.35); }
.tray-token.trap .tray-avatar { border-color: var(--orange); border-style: dashed; border-radius: 4px; background: rgba(216,130,74,0.35); }
.tray-token.loot .tray-avatar { border-color: var(--gold); border-style: dotted; background: rgba(212,175,55,0.35); }
.cell.drop-target { outline: 2px dashed var(--accent); outline-offset: -2px; }
.npc-set-group { display: flex; flex-direction: column; gap: .5em; margin-bottom: .8em; }
.npc-set-group h4 { margin: 0 0 .2em; font-size: .8em; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* ---- Encounter editor's embedded map editor pane ---- */
.map-editor-pane { margin: 1.5em auto; max-width: 1400px; border-top: 1px solid var(--line); padding-top: 1em; }
.map-editor-pane .map-layout { height: 550px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }

/* ---- Modal (map picker) ---- */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 200; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 1.2em; width: min(700px, 90vw); max-height: 80vh; overflow-y: auto; }
.map-picker-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .8em; margin-top: .8em; }
.map-picker-item { background: #0f1015; border: 1px solid var(--line); border-radius: 8px; padding: .5em; cursor: pointer; text-align: center; }
.map-picker-item:hover { border-color: var(--accent); }
.map-picker-item.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.map-picker-item img { width: 100%; height: 90px; object-fit: cover; border-radius: 6px; background: #000; }
.map-picker-item span { display: block; margin-top: .4em; font-size: .85em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-picker-empty { color: var(--muted); font-style: italic; grid-column: 1 / -1; }

/* Buffs / debuffs on the Game Runner tracker */
.effect-list { list-style: none; padding: 0; margin: .3em 0; display: flex; flex-direction: column; gap: .3em; }
.effect-item { display: flex; flex-wrap: wrap; align-items: center; gap: .4em; padding: .3em .5em; border-radius: 6px; border-left: 3px solid var(--green); background: rgba(255,255,255,.03); font-size: .9em; }
.effect-item.effect-debuff { border-left-color: var(--red); }
.effect-item.effect-condition { border-left-color: var(--yellow); }
.effect-kind { font-size: .7em; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.effect-item .effect-rm { margin-left: auto; padding: .15em .5em; }
.effect-apply { margin: .4em 0 .2em; }
.effect-apply select { flex: 1; min-width: 0; }
.effect-note { margin: .2em 0 0; }

/* ---- Campaign members (Players / Characters tables) ---- */
.table-wrap { overflow-x: auto; margin: .4em 0 1em; }
.member-table { width: 100%; border-collapse: collapse; font-size: .92em; }
.member-table th { text-align: left; color: var(--muted); font-weight: 600; font-size: .8em; text-transform: uppercase; letter-spacing: .04em; padding: .4em .5em; border-bottom: 1px solid var(--line); }
.member-table td { padding: .45em .5em; border-bottom: 1px solid var(--line); vertical-align: middle; }
.member-table tr.is-member td:first-child { font-weight: 600; }
.member-table tr[hidden] { display: none; }
.member-actions { text-align: right; white-space: nowrap; }
.member-add { display: inline-flex; gap: .3em; flex-wrap: wrap; justify-content: flex-end; }
.member-add select, .member-table select { padding: .3em .4em; }
.member-pill { background: var(--blue); color: #fff; }
.member-pill.dm { background: var(--accent); }
#player-filter { width: 100%; max-width: 20em; }
#member-status.error { color: var(--red); }

/* ---- Game Runner: My Character → Actions roll buttons ---- */
.pc-roll-mode { display: flex; flex-wrap: wrap; gap: .8em; font-size: .85em; margin: .3em 0 .5em; color: var(--muted); }
.pc-roll-mode label { display: inline-flex; align-items: center; gap: .25em; cursor: pointer; }
.pc-action { display: flex; flex-direction: column; gap: .3em; }
.pc-action-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: .5em; }
.pc-group { border-left-color: var(--accent); }
.pc-roll-row { display: flex; flex-wrap: wrap; gap: .35em; }
.pc-roll-btn { padding: .3em .6em; font-size: .85em; }
.pc-roll-btn:disabled { opacity: .45; cursor: default; }

/* ---- Game Runner tracker: Saves & Checks ---- */
.ability-roll-controls { display: flex; align-items: center; gap: .6em; margin: .3em 0; font-size: .85em; }
.ability-roll-controls select { padding: .2em .4em; }
.ability-roll-table { border-collapse: collapse; font-size: .85em; }
.ability-roll-table th, .ability-roll-table td { padding: .15em .45em; text-align: center; }
.ability-roll-table thead th { color: var(--muted); font-weight: 600; font-size: .85em; }
.ability-roll-table tbody th { text-align: left; color: var(--muted); }
.ability-roll-btn { min-width: 3.2em; padding: .2em .5em; font-size: .95em; background: var(--line); }
.ability-roll-btn:hover { background: var(--accent); }
.ability-roll-btn.prof { box-shadow: inset 0 0 0 1px var(--gold); }
.ability-roll-result { font-size: .85em; margin: .3em 0 0; }

/* ---- Game Runner: My Character → Hit Points ---- */
.pc-hp { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .6em 1em; padding: .6em; margin: .3em 0 .6em; border: 1px solid var(--line); border-left: 3px solid var(--green); border-radius: 6px; }
.pc-hp.down { border-left-color: var(--red); }
.pc-hp-field { display: flex; flex-direction: column; gap: .2em; font-size: .8em; color: var(--muted); }
.pc-hp-field input { width: 4em; font-size: 1.15rem; font-weight: 700; text-align: center; padding: .25em; }
.pc-hp-current { display: inline-flex; align-items: center; gap: .25em; }
.pc-hp-step { padding: .25em .55em; }
.pc-hp-adjust { display: flex; gap: .3em; align-items: center; }
.pc-hp-adjust input { width: 3.5em; padding: .3em; }
#pc-hp-status { flex-basis: 100%; margin: 0; font-size: .8em; }
#pc-hp-status:empty { display: none; }

/* ---- My Character panel: stacked rows that expand left → right ----
   Lives in the map sidebar, or in the dashboard's Game tab under the
   campaign select (#game-player-panel) when the map is embedded there. */
.game-player-panel:empty { display: none; }
.game-player-panel .player-tools { margin: .6em 0; padding: .8em 1em; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
.game-player-panel .player-tools h3 { margin: 0 0 .4em; }
.pt-rows { display: flex; flex-direction: column; gap: .4em; }
/* Rows align to the top, so a closed row is just its one-line toggle (the
   toggle never stretches to the content's height). */
.pt-row { display: flex; align-items: flex-start; gap: .5em; min-width: 0; }
.pt-toggle {
  flex: none; align-self: flex-start; width: 7.5em; text-align: left; display: flex; align-items: center; gap: .4em;
  line-height: 1.2; white-space: nowrap;
  background: var(--line); color: var(--ink); font-weight: 600;
}
.pt-row.open .pt-toggle { background: var(--accent); color: #fff; }
.pt-chevron { display: inline-block; transition: transform .25s ease; }
.pt-row.open .pt-chevron { transform: rotate(90deg); }
/* Closed: no width and no height. Open: grows right and down together. */
.pt-content {
  flex: 1; min-width: 0; max-width: 0; max-height: 0; opacity: 0; overflow: hidden; visibility: hidden;
  transition: max-width .35s ease, max-height .35s ease, opacity .25s ease, visibility 0s linear .35s;
}
.pt-row.open .pt-content {
  max-width: 100%; max-height: 60em; opacity: 1; visibility: visible;
  transition: max-width .35s ease, max-height .45s ease, opacity .25s ease .05s, visibility 0s;
}
@media (prefers-reduced-motion: reduce) { .pt-content, .pt-chevron { transition: none !important; } }
.pt-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: .4em 1em; }
.pt-toolbar .pc-roll-mode { margin: 0; }
/* Items run left to right and scroll sideways when they don't fit. */
.pt-strip { display: flex; flex-wrap: nowrap; gap: .5em; overflow-x: auto; padding: .3em 0 .5em; scroll-snap-type: x proximity; }
.pt-strip > * { flex: none; scroll-snap-align: start; }
.pt-strip .pc-action { width: 15em; margin: 0; }
.pt-strip .lib-ability-block { margin: 0; padding: .4em .6em; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); white-space: normal; }
.pt-group { display: flex; flex-direction: column; gap: .3em; padding: .4em .6em; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); max-width: 22em; }
.pt-group-wide { max-width: 34em; }
.pt-group h4 { margin: 0; font-size: .8em; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.pt-buttons { display: flex; flex-wrap: wrap; gap: .3em; }
.pt-buttons button { padding: .3em .55em; font-size: .85em; }
.pt-group .custom-roll-inputs { display: grid; grid-template-columns: repeat(4, auto); gap: .25em .5em; font-size: .85em; }
.pt-group .custom-roll-inputs input { width: 3em; padding: .2em; }
.player-tools .roll-result { margin: .2em 0 0; }
@media (max-width: 600px) {
  .pt-row { flex-direction: column; }
  .pt-toggle { width: 100%; }
  .pt-row.open .pt-content { max-width: none; }
  .pt-row .pt-content { width: 100%; }
}
