* { box-sizing: border-box; }

/* Block colours, also reused by the statblock so related things share a colour. */
:root {
  --color-start: #f6d97a;
  --color-attack: #f2a07b;
  --color-damage: #c8a8e9;
  --color-heal: #9fdc9f;
  --color-temp-hp: #c8eab9;
  --color-multiplier: #9cc7ee;
  --color-drain: #d7a6c9;
  --color-condition: #f0a6c4;
  --color-modifier: #d3dd7a;
  --color-ongoing: #e3b58f;
  --color-if: #9fd8cb;
  --border-block: rgba(0, 0, 0, 0.25);
  /* Results view: each creature keeps one colour (borrowed from the blocks). */
  --color-side-a: var(--color-multiplier);
  --color-side-b: var(--color-attack);
  --color-both-down: #b9b9b9;
  --color-unresolved: #e6e6e6;
}
[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: system-ui, sans-serif;
  font-size: 14px;
}

/* ------------------------------------------------------------ layout */

.navbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem 0.5rem 0.5rem;
  border-bottom: 1px solid #ccc;
  background: #f4f4f4;
}
/* The wide (cropped) icon; slightly taller than the buttons, trimmed by negative margins. */
.brand-logo { height: 32px; width: auto; margin: -6px 0; }
.brand { font-weight: bold; margin-right: 1rem; }
/* Fixed width, so the bold active tab doesn't grow. */
.nav-btn { width: 10.5em; }
.nav-btn.active { font-weight: bold; background: #ddd; }
/* Fixed width, so it doesn't change size when it reads "Stop" during a run. */
.simulate-btn { margin-left: 0.5rem; font-weight: bold; width: 6.5em; }
.sim-progress { width: 10em; height: 0.75em; border: 1px solid #aaa; background: var(--color-unresolved); }
.sim-progress-fill { height: 100%; width: 0; background: var(--color-heal); transition: width 0.1s linear; }
.help-btn { margin-left: auto; width: 2em; font-weight: bold; }

main { flex: 1; min-height: 0; }

#view-setup, #view-results {
  display: grid;
  grid-template-columns: 1fr 204px 1fr;
  height: 100%;
}

.panel {
  overflow: auto;
  padding: 0.75rem 1rem;
  border-right: 1px solid #ccc;
}
.panel:last-child { border-right: none; }

h3 { margin: 1rem 0 0.5rem; }
.hint, .empty-hint { color: #777; font-style: italic; }
.empty-hint { font-size: 12px; padding: 2px; }

/* --------------------------------------------------------- statblock */

/* Toolbar stays pinned to the top of the panel while it scrolls. The bottom padding lets
   the panel scroll a bit less than a window height past its end, so the bottom of the last
   action can be brought up to just under the toolbar (navbar + toolbar ≈ 6rem). */
.creature { padding-top: 0; padding-bottom: calc(100vh - 6rem); }
.toolbar {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  gap: 0.25rem;
  margin: 0 -1rem 0.75rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid #ddd;
  background: #fff;
}
.name-field input { width: 16em; }
input.num { width: 3.5em; }
input.text { width: 9em; }

.section-label { font-weight: bold; margin-top: 0.75rem; }

/* Boxes share the row evenly and wrap onto more rows once they'd get narrower than the minimum. */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(5em, 1fr)); gap: 0.5rem; margin: 0.5rem 0; }
.stat-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px;
  border: 1px solid var(--border-block);
  border-radius: 4px;
  background: #fafafa;
}
.stat-hp   { background: var(--color-heal); }
.stat-ac   { background: var(--color-multiplier); }
.stat-init { background: var(--color-start); }
.stat-save { background: var(--color-attack); }
.stat-label { font-weight: bold; }
.stat-box input { width: 100%; max-width: 4em; text-align: center; }

.defense { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin: 4px 0; }
/* Shared label column so pill rows and trait rows line up. */
.defense-label, .trait-label { width: 10.5em; flex-shrink: 0; }
.defense select { width: 9em; }
.defense .tags { flex-basis: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.tag { background: #e6e6e6; border-radius: 3px; padding: 1px 2px 1px 6px; }
/* Statblock pills take the colour of what they list: damage types, absorbed (healing) damage, conditions. */
.defense .tag { background: var(--color-damage); border: 1px solid var(--border-block); }
.defense[data-defense="absorptions"] .tag { background: var(--color-temp-hp); }
.defense[data-defense="condition_immunities"] .tag { background: var(--color-condition); }
.tag button { border: none; background: none; cursor: pointer; padding: 0 3px; }

/* Collapsible statblock sections (Defenses, Special traits). */
.statblock-section { margin: 0.75rem 0; }
.statblock-section > summary { font-weight: bold; cursor: pointer; }
.statblock-section > summary .hint { font-weight: normal; }
.trait-row { display: flex; align-items: center; gap: 4px; margin: 4px 0; }

/* ------------------------------------------------------------ blocks */

.block, .palette-block {
  border: 1px solid var(--border-block);
  border-radius: 4px;
  padding: 4px 6px;
  margin: 4px 0;
  cursor: grab;
}
.palette-block { margin-bottom: 8px; }
.sequence > .block { margin: 2px 0; }

.block-start  { background: var(--color-start); }
.block-attack, .block-save { background: var(--color-attack); }
.block-damage { background: var(--color-damage); }
.block-heal   { background: var(--color-heal); }
.block-multiplier { background: var(--color-multiplier); }
.block-drain      { background: var(--color-drain); }
.block-condition  { background: var(--color-condition); }
.block-if         { background: var(--color-if); }
.block-temp_hp    { background: var(--color-temp-hp); }
.block-modifier   { background: var(--color-modifier); }
.block-ongoing    { background: var(--color-ongoing); }

/* Top row of a block: the content wraps within its own area, so the buttons always stay
   in the top-right corner (with blank space below them once the content wraps). */
.block-header { display: flex; align-items: flex-start; gap: 4px; }
.header-content { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.header-buttons { flex: none; display: flex; gap: 4px; }
/* Further rows of block settings below the top row. */
.block-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-top: 3px; }
.inline-check { display: inline-flex; align-items: center; gap: 2px; }
.warning { color: #a3201b; font-size: 12px; font-style: italic; }
/* Selects are as wide as their longest option; never let them overflow a narrow block. */
.block select { max-width: 100%; }

/* Start block variants get a coloured left edge so they stand out in the action list. */
.block-start.start-reaction  { border-left: 5px solid #e07b39; }
.block-start.start-legendary { border-left: 5px solid #8e5cc2; }
.block-start.start-auto      { border-left: 5px solid #5f9a45; }

.options-toggle.active { background: #fff; }
.collapse-toggle { min-width: 1.6em; padding: 0 4px; border: none; background: none; font-size: 16px; line-height: 1; cursor: pointer; }
.block-start > .block-header input.text { flex: 1; max-width: 20em; }

.start-options {
  margin: 4px 0 2px 8px;
  padding: 2px 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.55);
}
.option-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin: 3px 0; }
.option-label { width: 10em; font-size: 12px; }

/* Summary of non-default options while the panel is closed; clicking opens it. */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin: 3px 0 0 8px;
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
}
.chip {
  padding: 0 6px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.7);
  font-size: 11px;
}

.sequence {
  min-height: 24px;
  margin: 2px 0 2px 8px;
  padding: 1px 2px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.55);
}
.branch-toggle {
  display: block;
  padding: 0 4px;
  border: none;
  background: none;
  font-size: 12px;
  font-style: italic;
  cursor: pointer;
}

/* Dashed containers: action list and resistance/vulnerability/immunity pills. */
.action-list, .defense .tags {
  padding: 6px;
  border: 1px dashed #aaa;
  border-radius: 4px;
}
.action-list { min-height: 80px; }

/* ------------------------------------------------------ drag and drop */

.drop-marker { height: 4px; margin: -2px 0; border-radius: 2px; background: #2a6ef5; }
.dragging { opacity: 0.4; }
.palette.drop-delete { background: #fde2e2; }

/* ----------------------------------------------------------- results */

.side-a { background: var(--color-side-a); }
.side-b { background: var(--color-side-b); }
.both-down { background: var(--color-both-down); }
.unresolved { background: var(--color-unresolved); }

/* Centre column: the overview on top, the timeline scrolling below it. */
.results-center { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.overview { padding: 0.75rem 1rem; border-bottom: 1px solid #ccc; font-size: 12px; }
.timeline { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.no-results { text-align: center; font-weight: bold; color: #777; margin: 1rem 0; }

.message-frame {
  border: 2px solid #d33;
  border-radius: 4px;
  background: #fff4f4;
  padding: 4px 6px;
  margin-bottom: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-wrap: anywhere;
}
.message-error { font-weight: bold; color: #a00; }

.overview-label { font-weight: bold; margin: 0.5rem 0 2px; }
.overview-label:first-of-type { margin-top: 0; }
.result-bar {
  display: flex;
  height: 14px;
  border: 1px solid var(--border-block);
  border-radius: 4px;
  overflow: hidden;
  background: #fafafa;
}
.result-bar > span { flex-shrink: 0; }
.result-bar > span + span { border-left: 1px solid #fff; }
.overview-split { display: flex; justify-content: space-between; color: #555; }
.legend { display: flex; flex-direction: column; gap: 1px; margin-top: 3px; }
.legend-item { display: flex; align-items: center; gap: 4px; }
.swatch { width: 10px; height: 10px; border: 1px solid var(--border-block); border-radius: 2px; flex: none; }
.overview-rounds { margin-top: 3px; font-weight: bold; }
.breakdown { margin-top: 0.5rem; color: #555; display: flex; flex-direction: column; gap: 1px; }
.breakdown > div { display: flex; justify-content: space-between; gap: 4px; }
.breakdown-who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.breakdown-value { flex: none; }

/* Turn-order switch: two options, the active one filled with its creature's colour. */
.order-switch {
  display: flex;
  flex-direction: column;
  margin: 0.75rem 1rem 0.25rem;
  border: 1px solid var(--border-block);
  border-radius: 4px;
  overflow: hidden;
}
.order-option {
  display: flex;
  justify-content: space-between;
  gap: 4px;
  border: none;
  padding: 3px 6px;
  font: inherit;
  font-size: 12px;
  text-align: left;
  cursor: pointer;
  background: #fafafa;
}
.order-option + .order-option { border-top: 1px solid var(--border-block); }
.order-option.side-a, .order-option.side-b { background: #fafafa; }
.order-option.side-a.active { background: var(--color-side-a); font-weight: bold; }
.order-option.side-b.active { background: var(--color-side-b); font-weight: bold; }
.order-option > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.timeline-scroll { flex: 1; min-height: 0; overflow: auto; padding: 0 1rem 1rem; }
.timeline-svg { display: block; margin: 0 auto; max-width: 100%; height: auto; }
.round-line { stroke: #ddd; stroke-width: 1; }
.round-text { fill: #999; font-size: 10px; }
.flow { stroke: #c8c8c8; stroke-linecap: round; }
.tl-node { cursor: pointer; }
.tl-dot { stroke: var(--border-block); stroke-width: 1; }
.tl-dot.side-a { fill: var(--color-side-a); }
.tl-dot.side-b { fill: var(--color-side-b); }
.tl-dot.setup { fill: #ddd; }
.tl-ended.side-a { fill: var(--color-side-a); }
.tl-ended.side-b { fill: var(--color-side-b); }
.tl-ended.both-down { fill: var(--color-both-down); }
.tl-ended { stroke: var(--border-block); stroke-width: 0.5; }
.tl-hit { fill: transparent; }
.tl-node.selected .tl-dot { stroke: #000; stroke-width: 2.5; }
.timeline-note { text-align: center; font-size: 12px; }

/* Creature panels. Empty: a logo in the middle. */
.results-creature { container-type: inline-size; }
.results-creature.empty { display: flex; align-items: center; justify-content: center; }
.results-logo { width: 96px; height: 96px; }
.results-name { margin: 0.25rem 0 0; font-size: 1.25rem; }
.stat-value { font-size: 1.1rem; }
.stat-win { background: var(--color-heal); }
.stat-down { background: var(--color-condition); }

.result-table { border-collapse: collapse; width: 100%; max-width: 32em; }
.result-table td { padding: 2px 4px; border-top: 1px solid #eee; vertical-align: top; }
.result-table td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Charts: columns in a wide panel, rows ("sideways") in a narrow one. */
.chart { max-width: 32em; }
.chart-cols { display: block; width: 100%; height: 86px; }
.chart-bar { fill: var(--color-both-down); }
.chart-bar.side-a { fill: var(--color-side-a); }
.chart-bar.side-b { fill: var(--color-side-b); }
.chart-bar.down { fill: var(--color-condition); }
.chart-bar { stroke: var(--border-block); stroke-width: 0.5; }
.chart-axis { stroke: #ccc; stroke-width: 1; }
.chart-labels { display: flex; justify-content: space-between; font-size: 11px; color: #777; margin-top: -12px; }
.chart-rows { display: none; font-size: 11px; }
.chart-row { display: grid; grid-template-columns: 4.5em 1fr 3.5em; align-items: center; gap: 4px; }
.chart-row-label { color: #555; text-align: right; }
.chart-row-track { height: 8px; }
.chart-row-bar { display: block; height: 100%; min-width: 1px; border-radius: 2px; background: var(--color-both-down); }
.chart-row-bar.side-a { background: var(--color-side-a); }
.chart-row-bar.side-b { background: var(--color-side-b); }
.chart-row-bar.down { background: var(--color-condition); }
.chart-row-value { text-align: right; font-variant-numeric: tabular-nums; }
@container (max-width: 300px) {
  .chart-cols-wrap { display: none; }
  .chart-rows { display: block; }
}

/* ------------------------------------------------------ Load from SRD */

.srd-dialog { width: min(36rem, calc(100vw - 2rem)); max-height: calc(100vh - 4rem); padding: 0.75rem; display: none; flex-direction: column; gap: 0.5rem; border-radius: 8px; }
.srd-dialog[open] { display: flex; }
.srd-head { display: flex; align-items: center; justify-content: space-between; }
.srd-head h3 { margin: 0; }
.srd-search { width: 100%; }
.srd-list { overflow-y: auto; min-height: 6rem; border: 1px solid #ccc; }
.srd-table { width: 100%; border-collapse: collapse; }
.srd-table th { position: sticky; top: 0; background: #f4f4f4; border-bottom: 1px solid #ccc; padding: 0; text-align: left; }
.srd-table th.num { text-align: right; }
.srd-table th button { width: 100%; padding: 0.3rem 0.5rem; border: 0; background: none; font: inherit; font-weight: bold; text-align: inherit; cursor: pointer; white-space: nowrap; }
.srd-table th[aria-sort="ascending"] button::after { content: " ▲"; }
.srd-table th[aria-sort="descending"] button::after { content: " ▼"; }
.srd-table td { padding: 0.3rem 0.5rem; border-bottom: 1px solid #eee; color: #555; white-space: nowrap; }
.srd-table td.num { text-align: right; }
.srd-row { cursor: pointer; }
.srd-row:hover, .srd-row:focus { background: #eef4fb; outline: none; }
.srd-table td.srd-name { width: 100%; color: inherit; font-weight: bold; white-space: normal; }
.srd-table td.srd-legendary { color: #8e5cc2; }
.srd-status { margin: 0; }
.srd-attribution { margin: 0; font-size: 11px; color: #666; }

/* ------------------------------------------------------------ help */

.help-dialog { width: min(42rem, calc(100vw - 2rem)); max-height: calc(100vh - 4rem); padding: 0.5rem 1rem 1rem; border-radius: 8px; line-height: 1.4; }
.help-head { display: flex; align-items: center; justify-content: space-between; }
.help-dialog h2 { margin: 0.5rem 0; font-size: 18px; }
.help-dialog h3 { margin: 1rem 0 0.25rem; }
.help-dialog ul, .help-dialog ol { margin: 0.25rem 0; padding-left: 1.5rem; }
.help-dialog li { margin: 0.2rem 0; }
.getting-started { margin: 0 0 0.75rem; padding: 0.5rem; border: 1px solid #b9d3ee; border-radius: 4px; background: #eef4fb; }
