/* Theme: colours live only in these variables. Edit mode swaps them via body.editing (light "paper"). */
:root {
  --bg: #15171c; --card: #1e2128; --card2: #262a33; --line: #2e323c;
  --text: #ecebe6; --muted: #8d919c; --accent: #d9a441; --flag: #f0a33a; --warn: #f0a33a;
  --pip-off: #3a3f4b; --on-accent: #15171c; --field: #15171c; --backdrop: rgba(0, 0, 0, .6); --head: 100px; --tabs: 56px;
}
body.editing, #sheet-box.paper {
  --bg: #f3eee3; --card: #fffdf8; --card2: #f0e9d8; --line: #d9d0bb; --text: #1f1b14; --muted: #675f4e;
  --accent: #8a5a00; --flag: #b45309; --warn: #9a3412; --pip-off: #d9d0bb; --on-accent: #fffdf8; --field: #fffdf8;
  --backdrop: rgba(31, 27, 20, .45);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding: calc(var(--head) + 8px) 0 130px; }
body {
  margin: 0; background: var(--bg); color: var(--text); overflow-x: hidden;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
code { font-family: ui-monospace, Consolas, monospace; font-size: .85em; overflow-wrap: anywhere; }
[hidden] { display: none !important; }

/* Header */
#head {
  position: sticky; top: 0; z-index: 20; background: var(--bg); border-bottom: 1px solid var(--line);
  padding: env(safe-area-inset-top) 12px 8px;
}
.title-row { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.titles { flex: 1; min-width: 0; }
h1 { margin: 0; font-size: 20px; line-height: 1.2; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#subtitle { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { min-height: 44px; padding: 0 14px; border: 1px solid var(--accent); border-radius: 22px; background: none; color: var(--accent); font-weight: 600; cursor: pointer; }
body.editing #edit, body.editing #menu { display: none; }
.icon-btn { position: relative; width: 44px; height: 44px; border: 0; background: none; font-size: 24px; border-radius: 22px; cursor: pointer; }
.search { position: relative; }
#search {
  width: 100%; height: 40px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); outline: none; font-size: 16px;
}
#search:focus { border-color: var(--accent); }
#results {
  position: absolute; left: 0; right: 0; top: 44px; margin: 0; padding: 4px 0; list-style: none; z-index: 30;
  background: var(--card2); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
  max-height: 60vh; overflow-y: auto;
}
#results li { padding: 8px 14px; min-height: 44px; cursor: pointer; }
#results li:hover { background: var(--card); }
.r1 { display: flex; justify-content: space-between; gap: 12px; }
.r1 b { font-weight: 600; min-width: 0; overflow-wrap: anywhere; }
.rv { color: var(--accent); font-variant-numeric: tabular-nums; white-space: nowrap; }
.r2 { font-size: 12px; color: var(--muted); }

/* Panel */
#panel { padding: 12px 12px calc(var(--tabs) + env(safe-area-inset-bottom) + 16px); max-width: 720px; margin: 0 auto; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px 6px; margin-bottom: 12px; }
.card, .tile, .row, .item { scroll-margin-top: calc(var(--head) + 8px); }
h2 { margin: 0 0 4px; font-size: 13px; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); }
.none { margin: 4px 0 8px; color: var(--muted); }

/* Section views: header select, grouped sub-blocks */
h2.hd { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
h2.hd select { width: auto; min-height: 36px; padding: 0 8px; font-size: 14px; letter-spacing: 0; text-transform: none; color: var(--text); }
.grp { background: var(--card2); border-radius: 10px; padding: 4px 10px; margin: 8px 0; }
.grp h3 { margin: 4px 0 0; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.grp .row:first-of-type, .grp h3 + .row { border-top: 0; }

/* Rows */
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; padding: 6px 0; border-top: 1px solid var(--line); cursor: pointer; }
h2 + .row { border-top: 0; }
.row.long { display: block; }
.txt { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 2px; }
.lbl { min-width: 0; overflow-wrap: anywhere; }
.mk { color: var(--accent); }
.tag { margin-left: 6px; font-size: 12px; color: var(--muted); }
.val { display: inline-flex; align-items: center; gap: 6px; text-align: right; min-width: 0; font-variant-numeric: tabular-nums; }
.v { font-weight: 600; overflow-wrap: anywhere; }
.flag { width: 8px; height: 8px; border-radius: 50%; background: var(--flag); flex: none; }
.err { color: var(--warn); }
.pips { display: inline-flex; flex-wrap: wrap; justify-content: flex-end; }
.pip { width: 22px; height: 24px; padding: 0; border: 0; background: none; display: inline-flex; align-items: center; justify-content: center; }
button.pip { height: 44px; cursor: pointer; }
.row .pip { width: 34px; }
.tile .pips { justify-content: flex-start; }
.tile .pip { width: 20px; }
.pip::before { content: ""; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--accent); background: var(--pip-off); }
.pip.on::before { background: var(--accent); }

/* Tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; padding: 4px 0 8px; }
.tile { background: var(--card2); border-radius: 10px; padding: 8px 10px; min-height: 64px; min-width: 0; cursor: pointer; }
.ttl { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.big .v { font-size: 28px; line-height: 1.2; }
.v small { font-size: .55em; font-weight: 500; color: var(--muted); white-space: nowrap; }
.big .v.long { font-size: 18px; }
.line { display: flex; justify-content: space-between; gap: 6px; font-size: 14px; padding: 4px 0; min-height: 28px; align-items: center; }
.sh { color: var(--muted); }
.line .v { font-weight: 500; }

/* Play controls */
.step { display: inline-flex; align-items: center; gap: 4px; }
.stp { width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--card2); font-size: 22px; line-height: 1; cursor: pointer; }
.num { min-width: 52px; min-height: 44px; padding: 0 4px; border: 0; background: none; cursor: pointer; text-align: center; }
.numin { width: 84px; height: 44px; text-align: center; border: 2px solid var(--accent); border-radius: 10px; background: var(--field); font-size: 18px; }
.tile .step { display: flex; flex-wrap: wrap; width: 100%; gap: 4px; }
.tile .num { order: -1; width: 100%; min-height: 36px; padding: 0; text-align: left; }
.tile .stp { flex: 1; min-width: 0; }
.toggle { min-width: 64px; min-height: 44px; padding: 0 16px; border-radius: 22px; border: 2px solid var(--accent); background: none; font-weight: 600; cursor: pointer; }
.toggle[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

/* Lists */
.list { padding-bottom: 4px; }
.item { padding: 8px 0; border-top: 1px solid var(--line); }
h2 + .list .item:first-child { border-top: 0; }
.nm { font-weight: 700; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 4px 6px; margin-top: 4px; }
.chip { font-size: 13px; background: var(--card2); border-radius: 6px; padding: 2px 8px; overflow-wrap: anywhere; }
.chip small { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .03em; }

.item.ref { position: relative; min-height: 44px; padding-right: 24px; cursor: pointer; }
.item.ref::after { content: "›"; position: absolute; right: 4px; top: 50%; transform: translateY(-50%); font-size: 24px; color: var(--muted); }

/* Tab bar */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; background: var(--card);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
#tabs button {
  flex: 1; min-width: 0; min-height: 56px; border: 0; background: none; color: var(--muted);
  font-size: 13px; font-weight: 600; border-top: 3px solid transparent; cursor: pointer;
}
#tabs button.active { color: var(--accent); border-top-color: var(--accent); background: var(--card2); }

/* Detail bottom sheet */
#overlay { position: fixed; inset: 0; z-index: 40; background: transparent; transition: background .2s; display: flex; align-items: flex-end; }
#overlay.open { background: var(--backdrop); }
#sheet-box {
  position: relative; width: 100%; max-width: 720px; margin: 0 auto; background: var(--card); border-radius: 16px 16px 0 0;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); max-height: 80vh; overflow-y: auto;
  transform: translateY(100%); transition: transform .2s ease-out;
}
#overlay.open #sheet-box { transform: none; }
.close { z-index: 2; position: absolute; top: 4px; right: 4px; width: 44px; height: 44px; border: 0; background: none; font-size: 20px; color: var(--muted); cursor: pointer; }
.detail h3 { margin: 0 44px 0 0; font-size: 18px; }
.dval { font-size: 32px; font-weight: 700; margin: 6px 0; }
.dval .v { font-weight: 700; }
.detail p { margin: 6px 0; overflow-wrap: anywhere; }
.muted { color: var(--muted); }
.detail.ref { max-height: calc(80vh - 56px); overflow-y: auto; }
.detail h4 { margin: 14px 0 2px; font-size: 13px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.detail.ref p { white-space: pre-line; }
.kv { display: flex; gap: 12px; justify-content: space-between; padding: 4px 0; border-top: 1px solid var(--line); overflow-wrap: anywhere; }
.kv span:first-child { flex: none; }
.kv span:last-child { text-align: right; min-width: 0; }
.fine { font-size: 12px; margin-top: 16px; }
.warn { color: var(--warn); }

/* Search-jump highlight */
.hl { animation: hl 1.5s ease-out; }
@keyframes hl { from { background-color: rgba(217, 164, 65, .45); } to { background-color: transparent; } }

/* Snackbar, menu */
#snack { position: fixed; left: 12px; right: 12px; bottom: calc(var(--tabs) + env(safe-area-inset-bottom) + 8px); z-index: 35; max-width: 696px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 4px 0 16px; min-height: 48px; border-radius: 10px;
  background: var(--card2); color: var(--text); border: 1px solid var(--accent); box-shadow: 0 6px 20px rgba(0, 0, 0, .4); }
#snack button[hidden] { display: none; }
#snack button { min-width: 64px; min-height: 44px; border: 0; background: none; color: var(--accent); font-weight: 700; cursor: pointer; }
.icon-btn.dot::after { content: ""; position: absolute; top: 8px; right: 8px; width: 9px; height: 9px; border-radius: 50%; background: var(--warn); }
.detail summary { padding: 12px 0; cursor: pointer; }
.menu-item { display: block; width: 100%; min-height: 48px; margin-top: 8px; padding: 0 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--card2); text-align: left; cursor: pointer; }

/* Edit mode */
#editbar { position: fixed; left: 0; right: 0; bottom: calc(var(--tabs) + env(safe-area-inset-bottom)); z-index: 25; display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; background: var(--card); border-top: 3px solid var(--accent); }
#editbar b { flex: 1; color: var(--accent); letter-spacing: .04em; text-transform: uppercase; font-size: 14px; }
#editbar button { min-height: 44px; min-width: 84px; border-radius: 10px; border: 2px solid var(--accent); background: none; font-weight: 700; cursor: pointer; }
#editbar .primary { background: var(--accent); color: var(--on-accent); }
body.editing #panel { padding-bottom: calc(var(--tabs) * 2 + env(safe-area-inset-bottom) + 24px); }
body.editing .card { border-color: var(--accent); }
.row.edit { cursor: default; }
.row.edit > input[type=text], .row.edit > input[type=number], .row.edit > select { flex: 0 1 55%; min-width: 0; }
.row.edit.long { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
input[type=text], input[type=number], select, textarea {
  min-height: 44px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--field); width: 100%; font-size: 16px;
}
textarea { resize: none; overflow: hidden; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.row.edit.der { display: block; }
.l1 { display: flex; align-items: center; gap: 8px; }
.l1 .lbl { flex: 1; }
.calc { font-weight: 600; font-variant-numeric: tabular-nums; }
.badge { font-size: 11px; padding: 1px 6px; border: 1px solid var(--muted); border-radius: 6px; color: var(--muted); }
.cmp:not([hidden]) { margin-top: 4px; justify-content: flex-end; }
.ovr { width: 84px !important; font-size: 14px; }
.cmp { display: flex; align-items: center; gap: 6px; color: var(--warn); font-size: 14px; }
.link { min-height: 44px; padding: 0 8px; border: 0; background: none; color: var(--accent); font-weight: 700; text-decoration: underline; cursor: pointer; }
button.mk { min-width: 44px; min-height: 44px; border: 0; background: none; font-size: 20px; cursor: pointer; }
.sw { position: relative; display: inline-block; width: 52px; height: 44px; flex: none; }
.sw input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.sw i { position: absolute; left: 0; top: 9px; width: 52px; height: 26px; border-radius: 13px; background: var(--pip-off); border: 1px solid var(--line); pointer-events: none; }
.sw i::after { content: ""; position: absolute; top: 2px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: left .15s; }
.sw input:checked + i { background: var(--accent); }
.sw input:checked + i::after { left: 27px; background: var(--on-accent); }
.sw input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }
.cells { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 6px 8px; }
.cell { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.cell small { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.first { grid-column: 1 / -1; display: flex; align-items: flex-end; gap: 8px; }
.first .cell { flex: 1; }
.del { width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 8px; background: none; color: var(--warn); cursor: pointer; }
.add { min-height: 44px; margin: 4px 0 6px; padding: 0 16px; border: 1px dashed var(--accent); border-radius: 10px; background: none; color: var(--accent); font-weight: 700; cursor: pointer; }

/* Rules card header, highlighted DM note, spell form */
#sheet-box { color: var(--text); }
.rc { margin-top: -12px; }
.rh { display: flex; align-items: center; gap: 8px; min-height: 44px; position: sticky; top: 0; z-index: 1; background: var(--card); padding-right: 44px; }
.detail .rh h3 { margin: 0; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.rh .pill { flex: none; }
h3 .badge, .nm .badge { margin-left: 8px; font-size: 11px; font-weight: 400; vertical-align: middle; }
.note { margin: 10px 0; padding: 0 12px 6px; border-left: 4px solid var(--accent); border-radius: 4px; background: var(--card2); }
.ff { display: block; margin-top: 10px; }
.ff small { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; }
.fbar { position: sticky; bottom: 0; display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; padding-top: 8px; background: var(--card); border-top: 1px solid var(--line); }
.fbar button { min-height: 44px; padding: 0 18px; border: 2px solid var(--accent); border-radius: 10px; background: none; font-weight: 700; cursor: pointer; }
.fbar .primary { background: var(--accent); color: var(--on-accent); }
