/* Beat Map styles. Colors are tokens on :root so light/dark stay in sync.
   Every text color below has at least 4.5:1 contrast on its background. */
:root {
  --bg: #fbfaf7;
  --surface: #ffffff;
  --surface-2: #f3f1ec;
  --ink: #1c1a17;
  --muted: #57524b;
  --line: #d9d3c9;
  --accent: #a3262a;
  --accent-ink: #ffffff;
  --accent-soft: #f7e6e4;
  --focus: #1f5fbf;
  --warn: #8a5300;
  --warn-soft: #fdf3e1;
  --ok: #1d6b4f;
  --t-physical: #1d6b4f;
  --t-tonal: #7a3fa0;
  --t-topical: #1f5fbf;
  --t-tactical: #9a4a00;
  --select: #fff2c4;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --radius: 10px;
  --script-size: 1.25rem;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16151a; --surface: #1f1e24; --surface-2: #29282f; --ink: #ece9e4; --muted: #b8b2a8;
    --line: #3b3942; --accent: #ff8a80; --accent-ink: #1a0b0a; --accent-soft: #3a2224; --focus: #8ab8ff;
    --warn: #ffc46b; --warn-soft: #33281a; --ok: #6fd3a8;
    --t-physical: #6fd3a8; --t-tonal: #d3a6f5; --t-topical: #8ab8ff; --t-tactical: #ffb46b;
    --select: #4a3f1a; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16151a; --surface: #1f1e24; --surface-2: #29282f; --ink: #ece9e4; --muted: #b8b2a8;
  --line: #3b3942; --accent: #ff8a80; --accent-ink: #1a0b0a; --accent-soft: #3a2224; --focus: #8ab8ff;
  --warn: #ffc46b; --warn-soft: #33281a; --ok: #6fd3a8;
  --t-physical: #6fd3a8; --t-tonal: #d3a6f5; --t-topical: #8ab8ff; --t-tactical: #ffb46b;
  --select: #4a3f1a; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 var(--sans);
}
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
a { color: var(--focus); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -999px; top: 0; background: var(--surface); padding: .5rem 1rem; z-index: 50; }
.skip:focus { left: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; color: var(--accent); }
.brand span { color: var(--ink); }
.logo { width: 28px; height: 28px; }
.doc-picker { flex: 1 1 180px; min-width: 0; }
.doc-picker select { width: 100%; max-width: 420px; }
.top-actions { display: flex; gap: .5rem; align-items: center; }

/* Buttons and fields */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  min-height: 40px; padding: .4rem .85rem; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--accent); }
.btn.small { min-height: 32px; padding: .2rem .6rem; font-size: .875rem; }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
select, input[type="text"], input[type="search"], textarea {
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
  padding: .5rem .65rem; min-height: 40px; width: 100%;
}
textarea { resize: vertical; line-height: 1.5; }
label { font-weight: 600; display: block; margin-bottom: .25rem; }
.field { margin-bottom: 1rem; }
.hint { font-size: .875rem; color: var(--muted); margin-top: .25rem; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* Menu */
.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 230px; z-index: 30;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: .35rem; display: flex; flex-direction: column;
}
.menu-list[hidden] { display: none; }
.menu-list button {
  text-align: left; background: none; border: 0; padding: .55rem .7rem; border-radius: 6px; cursor: pointer;
}
.menu-list button:hover, .menu-list button:focus-visible { background: var(--surface-2); }
.menu-list .danger { color: var(--accent); }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: .3rem 0; width: 100%; }

/* Tabs */
.tabs {
  display: flex; gap: .25rem; padding: .5rem 16px 0; overflow-x: auto;
  border-bottom: 1px solid var(--line); background: var(--bg);
  position: sticky; top: var(--topbar-h, 57px); z-index: 15; scrollbar-width: none;
}
.tabs button {
  border: 0; background: none; padding: .6rem .85rem; cursor: pointer; white-space: nowrap;
  border-bottom: 3px solid transparent; color: var(--muted); font-weight: 600; border-radius: 6px 6px 0 0;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.tabs .n {
  display: inline-grid; place-items: center; width: 1.4rem; height: 1.4rem; border-radius: 50%;
  background: var(--surface-2); font-size: .8rem; margin-right: .2rem;
}
.tabs button[aria-selected="true"] .n { background: var(--accent); color: var(--accent-ink); }

main { padding: 1.25rem 0 4rem; outline: none; }
main:focus-visible { outline: none; }
.panel-head { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.panel-head h2 { margin: 0; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem; box-shadow: var(--shadow);
}
.card + .card { margin-top: 1rem; }
details.help { background: var(--surface-2); border-radius: var(--radius); padding: .6rem .9rem; margin-bottom: 1rem; }
details.help summary { cursor: pointer; font-weight: 600; }
details.help dl { margin: .5rem 0 0; }
details.help dt { font-weight: 700; margin-top: .5rem; }
details.help dd { margin: 0; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }

/* Setup */
.setup-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 1.25rem; }
#scriptText { min-height: 340px; font-family: var(--serif); font-size: 1.05rem; }

/* Tips */
.tips { list-style: none; padding: 0; margin: .5rem 0 0; display: grid; gap: .4rem; }
.tip { background: var(--warn-soft); border-left: 4px solid var(--warn); padding: .45rem .65rem; border-radius: 6px; font-size: .9rem; }
.tip strong { display: block; color: var(--warn); }
.test-sentence { font-family: var(--serif); font-style: italic; color: var(--muted); margin-top: .35rem; }
.test-sentence.good::before { content: "✓ "; color: var(--ok); font-style: normal; font-weight: 700; }

/* Version bar */
.version-bar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.version-bar select { width: auto; min-width: 180px; }
.version-bar label { margin: 0; }

/* Beats editor */
.beats-layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 1.25rem; align-items: start; }
.inspector { position: sticky; top: calc(var(--topbar-h, 57px) + 56px); max-height: calc(100vh - var(--topbar-h, 57px) - 72px); overflow: auto; }
.inspector .close-sheet { display: none; }
.marktext {
  font-family: var(--serif); font-size: var(--script-size); line-height: 2.35;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem 1.25rem; user-select: none; -webkit-user-select: none;
}
.marktext .line { margin: 0; }
.marktext .line.gap-before { margin-top: .9rem; }
.speaker { font-family: var(--sans); font-weight: 700; font-size: .78em; letter-spacing: .04em; text-transform: uppercase; margin-right: .35em; }
.partner { color: var(--muted); }
.w { cursor: pointer; border-radius: 3px; }
.w:hover { background: var(--surface-2); }
.w.dir { font-style: italic; color: var(--muted); }
.w.in-sel { background: var(--select); }
.gap { display: inline; cursor: pointer; border-radius: 4px; }
.gap.has-beat { display: inline-block; }
.gap:focus-visible { outline: none; display: inline-block; text-align: center; box-shadow: 0 0 0 3px var(--focus); background: var(--surface-2); min-width: .7em; }
.gap:focus-visible::after { content: "+"; font-family: var(--sans); font-size: .7em; color: var(--focus); }
.gap.has-beat:focus-visible::after, .gap.opening:focus-visible::after { content: none; }
.beatmark { display: inline-flex; align-items: baseline; gap: .25em; margin: 0 .2em; white-space: nowrap; }
.beatmark .slash { color: var(--accent); font-weight: 700; font-size: 1.15em; font-family: var(--sans); }
.pill {
  font-family: var(--sans); font-size: .62em; font-weight: 700; letter-spacing: .03em; line-height: 1.2;
  padding: .15em .5em; border-radius: 999px; background: var(--accent-soft); color: var(--accent);
  text-transform: uppercase; max-width: 16em; overflow: hidden; text-overflow: ellipsis; display: inline-block; vertical-align: middle;
}
.pill.empty-tactic { background: var(--surface-2); color: var(--muted); }
.pill .num { opacity: .85; margin-right: .3em; }
.gap.selected .pill, .gap.selected .slash { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 999px; }
.pill.warn::after { content: " •"; color: var(--warn); }

.type-tag { font-family: var(--sans); font-weight: 700; font-size: .7rem; letter-spacing: .05em; text-transform: uppercase; padding: .05rem .4rem; border-radius: 4px; border: 1px solid currentColor; }
.type-physical { color: var(--t-physical); }
.type-tonal { color: var(--t-tonal); }
.type-topical { color: var(--t-topical); }
.type-tactical { color: var(--t-tactical); }
.type-none { color: var(--muted); }

.type-choices { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; margin: 0; padding: 0; border: 0; }
.type-choices legend { font-weight: 600; margin-bottom: .25rem; padding: 0; }
.type-choice { position: relative; }
.type-choice input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.type-choice span {
  display: block; border: 1px solid var(--line); border-radius: 8px; padding: .45rem .55rem; font-weight: 600; font-size: .92rem;
}
.type-choice small { display: block; font-weight: 400; color: var(--muted); font-size: .78rem; line-height: 1.3; margin-top: .15rem; }
.type-choice input:checked + span { border-color: currentColor; background: var(--surface-2); box-shadow: inset 0 0 0 1px currentColor; }
.type-choice input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.check { display: flex; gap: .5rem; align-items: flex-start; font-weight: 400; }
.check input { margin-top: .3rem; width: 1.1rem; height: 1.1rem; flex: none; }

.beat-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .25rem; }
.beat-list button {
  width: 100%; text-align: left; display: flex; gap: .5rem; align-items: center; background: none;
  border: 1px solid transparent; border-radius: 8px; padding: .35rem .5rem; cursor: pointer;
}
.beat-list button:hover { background: var(--surface-2); }
.beat-list button[aria-current="true"] { border-color: var(--accent); background: var(--accent-soft); }
.beat-list .num { font-weight: 700; color: var(--muted); min-width: 1.6rem; }
.beat-list .tac { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.list-under { display: none; }
@media (max-width: 900px) {
  .list-under { display: block; }
  .inspector .list-in { display: none; }
}

/* Combobox / picker */
.combo { position: relative; }
.combo-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 25; margin: 0; padding: .25rem;
  list-style: none; background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); max-height: 260px; overflow: auto;
}
.combo-list[hidden] { display: none; }
.combo-list li { padding: .45rem .6rem; border-radius: 6px; cursor: pointer; }
.combo-list li[aria-selected="true"], .combo-list li:hover { background: var(--accent-soft); }
.combo-list li .to { color: var(--muted); }
.combo-list li.add { color: var(--focus); }

/* Dialog */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--ink);
  width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 48px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
.dialog-head { display: flex; align-items: center; gap: .5rem; padding: .9rem 1rem; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dialog-head h2 { margin: 0; font-size: 1.15rem; flex: 1; }
.dialog-body { padding: 1rem; }
.verb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .25rem; margin-top: .75rem; }
.verb-grid button { text-align: left; border: 1px solid transparent; background: none; border-radius: 6px; padding: .35rem .5rem; cursor: pointer; }
.verb-grid button:hover, .verb-grid button:focus-visible { background: var(--accent-soft); }
.verb-grid .mine { color: var(--focus); }
.letter-head { grid-column: 1 / -1; font-weight: 700; color: var(--accent); margin-top: .5rem; border-bottom: 1px solid var(--line); }
.sample-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.sample-list button { width: 100%; text-align: left; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 140%); transition: transform .25s;
  background: var(--ink); color: var(--bg); padding: .65rem 1rem; border-radius: 10px; max-width: calc(100vw - 32px);
  z-index: 60; box-shadow: var(--shadow);
}
.toast.show { transform: translate(-50%, 0); }
.toast:empty { display: none; }

/* Script view (screen + print) */
.script-sheet {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 2rem 2.25rem; max-width: 880px; margin: 0 auto; box-shadow: var(--shadow);
}
.sheet-header { border-bottom: 2px solid var(--ink); padding-bottom: .6rem; margin-bottom: 1.1rem; }
.sheet-header dl { display: grid; grid-template-columns: max-content 1fr; gap: .15rem .75rem; margin: 0; font-size: 1.05rem; }
.sheet-header dt { font-weight: 700; }
.sheet-header dd { margin: 0; }
.sheet-header .version-name { color: var(--muted); font-size: .85rem; margin-top: .35rem; }
.sheet-body { font-family: var(--serif); font-size: var(--script-size); line-height: 1.55; }
.sheet-body.cols-2 { column-count: 2; column-gap: 2rem; column-rule: 1px solid var(--line); }
.sec-title { font-family: var(--sans); font-weight: 800; font-size: .72em; letter-spacing: .06em; text-transform: uppercase; margin: 1em 0 .2em; break-after: avoid; }
.chunk { margin: 0 0 .55em; break-inside: avoid; }
.chunk-tactic {
  font-family: var(--sans); font-weight: 800; font-size: .66em; letter-spacing: .05em; text-transform: uppercase;
  color: var(--accent); display: flex; gap: .5em; align-items: center; flex-wrap: wrap; break-after: avoid;
}
.chunk-tactic .type-tag { font-size: .85em; }
.chunk-tactic .disc { color: var(--muted); font-weight: 700; }
.chunk-note { font-family: var(--sans); font-size: .7em; color: var(--muted); font-style: italic; }
.chunk-text .slash { color: var(--accent); font-weight: 700; font-family: var(--sans); margin-right: .25em; }
.chunk-text .sp { font-family: var(--sans); font-weight: 700; font-size: .75em; letter-spacing: .04em; text-transform: uppercase; }
.chunk-text .dir { font-style: italic; color: var(--muted); }
.chunk-text .partner { color: var(--muted); }
.chunk-text .br { display: block; height: 0; }
.chunk-text .br.para { height: .5em; }
.sheet-tools { max-width: 880px; margin: 0 auto 1rem; }
/* While printing (and while measuring for one-page fit), the sheet takes the
   width of a printed page and the size chosen by BM.fitForPrint(). */
.print-mode .script-sheet { width: 7.5in; max-width: 7.5in; padding: 0; border: 0; box-shadow: none; }
.print-mode .sheet-body { font-size: var(--print-size, 12pt); }
.print-mode .sheet-header dl { font-size: calc(var(--print-size, 12pt) * .95); }
.print-mode .sheet-body.cols-2 { column-count: 2; }
.toggle { display: inline-flex; align-items: center; gap: .4rem; font-weight: 400; margin: 0; }
.toggle input { width: 1.1rem; height: 1.1rem; }

/* Versions */
.versions-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.version-card h3 { display: flex; gap: .5rem; align-items: center; }
.version-card .badge { font-size: .7rem; font-weight: 700; background: var(--accent); color: var(--accent-ink); padding: .1rem .45rem; border-radius: 999px; }
.version-card .meta { font-size: .85rem; color: var(--muted); margin-bottom: .5rem; }
.compare-table { width: 100%; border-collapse: collapse; font-size: .98rem; }
.compare-table th, .compare-table td { border-bottom: 1px solid var(--line); padding: .55rem .6rem; vertical-align: top; text-align: left; }
.compare-table th { background: var(--surface); border-bottom: 2px solid var(--ink); }
.compare-table td.text { font-family: var(--serif); width: 50%; }
.compare-table tr.differs td.tac { background: var(--warn-soft); }
.compare-table .cont { color: var(--muted); font-size: .85rem; }
.compare-table .tacname { font-weight: 700; color: var(--accent); }

/* Practice */
.practice-tools { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.practice-sheet { font-family: var(--serif); font-size: calc(var(--script-size) * 1.08); line-height: 1.8; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.5rem; max-width: 880px; margin: 0 auto; }
.fl-word { background: none; border: 0; padding: 0 .05em; margin: 0; cursor: pointer; font: inherit; color: inherit; border-radius: 4px; }
.fl-word.hidden-word { color: var(--accent); font-weight: 700; letter-spacing: .02em; }
.fl-word:hover { background: var(--surface-2); }
.cue-card { max-width: 720px; margin: 0 auto; text-align: left; }
.cue-card .cue-tactic { font-family: var(--sans); font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); font-size: 1.1rem; }
.cue-card .cue-prev { font-family: var(--serif); color: var(--muted); font-size: 1.05rem; border-left: 3px solid var(--line); padding-left: .75rem; margin: .75rem 0 1rem; }
.cue-card .cue-line { font-family: var(--serif); font-size: calc(var(--script-size) * 1.08); line-height: 1.7; min-height: 4rem; }
.cue-card .cue-line.hidden { color: var(--muted); font-style: italic; font-family: var(--sans); font-size: 1rem; }
.progress { font-size: .9rem; color: var(--muted); }

/* Phone and tablet */
@media (max-width: 900px) {
  .beats-layout { grid-template-columns: 1fr; }
  .inspector {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; max-height: 62vh; z-index: 40;
    border-radius: 16px 16px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.25);
  }
  .inspector[hidden] { display: none; }
  .inspector .close-sheet { display: inline-flex; }
  .beats-layout.sheet-open { padding-bottom: 62vh; }
  .setup-grid { grid-template-columns: 1fr; }
  .script-sheet { padding: 1.25rem 1rem; }
  .sheet-body.cols-2 { column-count: 1; }
}
@media (max-width: 560px) {
  :root { --script-size: 1.12rem; }
  .brand span { display: none; }
  .topbar { flex-wrap: nowrap; gap: .5rem; }
  .doc-picker { flex: 1 1 0; }
  .top-actions .btn { padding: .4rem .6rem; }
  .tabs { gap: 0; padding: .35rem 8px 0; }
  .tabs button { padding: .55rem .5rem; font-size: .92rem; }
  .tabs .n { display: none; }
  .marktext { padding: .75rem; }
  .compare-table, .compare-table tbody, .compare-table tr, .compare-table td { display: block; width: 100% !important; }
  .compare-table thead { display: none; }
  .compare-table td.tac::before { content: attr(data-label) ": "; font-weight: 700; color: var(--muted); }
  .compare-table tr { border-bottom: 2px solid var(--line); padding: .4rem 0; }
  .compare-table td { border: 0; padding: .25rem 0; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
