:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1d2126;
  --ink-2: #555b63;
  --ink-3: #8a9099;
  --line: #e3e1db;
  --line-2: #d2cfc7;
  --accent: #1f4f82;
  --accent-ink: #ffffff;
  --accent-soft: #e8eff7;
  --danger: #b3261e;
  --ok: #2e7d4f;
  --warn: #9a6400;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 20, 20, .05), 0 2px 8px rgba(20, 20, 20, .04);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
p { margin: 0; }
code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: .86em;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}
[hidden] { display: none !important; }
.muted { color: var(--ink-2); }
.small { font-size: .86rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--serif); font-size: 1.25rem; font-weight: 600; letter-spacing: .01em; }
.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; }
.tabs button {
  border: 0; background: none; font: inherit; color: var(--ink-2);
  padding: 8px 12px; border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.tabs button:hover { background: var(--surface-2); color: var(--ink); }
.tabs button[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.save-status {
  border: 0; background: none; font: inherit; font-size: .85rem; color: var(--ink-3);
  white-space: nowrap; cursor: default; padding: 4px 8px; border-radius: 6px;
}
.save-status.error { color: var(--danger); background: #fbeaea; cursor: pointer; }
.save-status.ok::before { content: "✓ "; color: var(--ok); }

.main { max-width: 1440px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin-top: 4px; max-width: 70ch; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: .92rem; line-height: 1;
  padding: 8px 12px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink);
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); border-color: var(--ink-3); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #173d66; border-color: #173d66; }
.btn-ghost { border-color: transparent; background: transparent; }
.danger { color: var(--danger); }
.btn-ghost.danger:hover { background: #fbeaea; border-color: transparent; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.link { border: 0; background: none; font: inherit; font-size: .86rem; color: var(--accent); cursor: pointer; padding: 2px 4px; }
.link:hover { text-decoration: underline; }
.icon-btn {
  width: 28px; height: 28px; display: inline-grid; place-items: center;
  border: 1px solid transparent; background: transparent; border-radius: 6px;
  color: var(--ink-2); cursor: pointer; font-size: .75rem; padding: 0;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--line-2); color: var(--ink); }
.icon-btn:disabled { opacity: .3; cursor: default; }
.icon-btn.remove:hover:not(:disabled) { color: var(--danger); }

input[type="text"], input[type="search"], input[type="number"], input[type="date"], input[type="password"], input:not([type]), select, textarea {
  font: inherit; color: inherit; width: 100%;
  padding: 8px 10px; border: 1px solid var(--line-2); border-radius: 8px; background: var(--surface);
}
textarea { resize: vertical; line-height: 1.45; }
textarea.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .88rem; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: .86rem; font-weight: 600; color: var(--ink-2); }
label > input, label > select, label > textarea { margin-top: 5px; font-weight: 400; }
.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; color: var(--ink); cursor: pointer; }
.check input { margin: 0; }
.hint { font-size: .82rem; font-weight: 400; color: var(--ink-3); margin-top: 4px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.stack > * + * { margin-top: 10px; }
.count {
  display: inline-block; min-width: 22px; padding: 1px 7px; margin-left: 6px;
  font-size: .75rem; font-weight: 600; color: var(--ink-2);
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 99px; text-align: center;
}
.empty { text-align: center; padding: 32px 16px; color: var(--ink-2); }
.empty strong { display: block; color: var(--ink); margin-bottom: 4px; }
.empty .btn { margin-top: 12px; }

/* ---------- script builder ---------- */
.script-bar { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 16px; }
.script-bar .grow { flex: 1 1 260px; }
.script-bar .date { flex: 0 0 170px; }
.script-bar .owner { flex: 0 0 200px; }
.script-bar .picker-select { flex: 0 1 280px; }

.builder { display: grid; grid-template-columns: minmax(260px, 1fr) minmax(260px, 1fr) minmax(340px, 1.45fr); gap: 16px; align-items: start; }
@media (max-width: 1100px) { .builder { grid-template-columns: 1fr 1fr; } .panel-preview { grid-column: 1 / -1; } }
@media (max-width: 720px) { .builder { grid-template-columns: 1fr; } }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 0; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.panel-body { padding: 12px 14px; }
.step {
  display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 6px;
  border-radius: 50%; background: var(--accent); color: #fff; font-size: .75rem; vertical-align: 1px;
}
.picker, .arrange { max-height: calc(100vh - 290px); min-height: 200px; overflow-y: auto; }

.pick-group + .pick-group { margin-top: 10px; }
.pick-group-head {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px;
  font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2);
  background: var(--surface-2); cursor: pointer; position: sticky; top: 0; z-index: 1;
}
.pick-group-head .count { margin-left: auto; text-transform: none; letter-spacing: 0; }
.pick-item { border-radius: 6px; }
.pick-item:hover { background: var(--surface-2); }
.pick-main { display: flex; align-items: baseline; gap: 8px; padding: 6px 8px; font-weight: 400; color: var(--ink); cursor: pointer; font-size: .92rem; }
.pick-main input { position: relative; top: 1px; }
.pick-name { white-space: nowrap; }
.pick-detail { color: var(--ink-3); font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.arr-group { border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.arr-group + .arr-group { margin-top: 10px; }
.arr-group-head { display: flex; align-items: center; gap: 6px; padding: 6px 8px; background: var(--surface-2); border-bottom: 1px solid var(--line); border-radius: 8px 8px 0 0; }
.arr-title { flex: 1; font-weight: 600; font-size: .92rem; }
.arr-people { list-style: none; margin: 0; padding: 4px 0; counter-reset: n; }
.arr-people li { display: flex; align-items: center; gap: 6px; padding: 2px 8px 2px 12px; font-size: .92rem; counter-increment: n; }
.arr-people li::before { content: counter(n) "."; color: var(--ink-3); font-size: .8rem; width: 1.4em; }
.arr-people li:hover { background: var(--surface-2); }
.arr-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.arr-actions { display: flex; }
.pick-family { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; padding: 0 8px 7px 26px; }
.pick-family .hint { margin: 0; }
.family-toggle {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: .78rem; font-weight: 500; color: var(--ink-3); cursor: pointer;
  padding: 1px 8px 1px 5px; border: 1px dashed var(--line-2); border-radius: 99px; white-space: nowrap;
}
.family-toggle input { margin: 0; }
.family-toggle.on { color: var(--accent); background: var(--accent-soft); border: 1px solid transparent; }
.child-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.child-row input { flex: 1; min-width: 0; }
.reorder { display: flex; flex-direction: column; }
.arr-group-head .reorder { flex-direction: row; }
.nowrap { white-space: nowrap; }
.arr-mode { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 6px; padding: 6px 12px; border-bottom: 1px solid var(--line); font-size: .86rem; background: var(--accent-soft); }
.arr-mode .hint { margin: 0; }
.threshold-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.threshold-row input { width: 80px; }
.group-rule { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.group-rule .tpl { flex: 1; min-width: 200px; }
.arrange-foot { display: flex; gap: 12px; justify-content: flex-end; padding-top: 8px; }

.options { display: flex; gap: 16px; flex-wrap: wrap; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.reading-format { align-items: flex-end; gap: 12px; }
.reading-format label { flex: 0 1 auto; }
.reading-format select { width: auto; max-width: 220px; }
.reading-format .check { padding-bottom: 8px; }
.size-input { display: flex; align-items: center; gap: 4px; margin-top: 5px; font-weight: 400; color: var(--ink-2); }
.size-input input { width: 62px; }
.closing-field { padding: 10px 14px; border-bottom: 1px solid var(--line); }
.closing-field textarea { font-size: .92rem; }
.doc-outro { margin-top: 8px; }
.doc-closing { font-family: var(--serif); margin-top: 20px; padding-top: 12px; border-top: 1px solid var(--line); }
.script-preview { padding: 8px 14px 16px; max-height: calc(100vh - 330px); min-height: 200px; overflow-y: auto; }

@font-face {
  font-family: "Atkinson Hyperlegible";
  src: url("fonts/atkinson-regular.woff2") format("woff2");
  font-weight: 400; font-style: normal; font-display: swap;
}
@font-face {
  font-family: "Atkinson Hyperlegible";
  src: url("fonts/atkinson-bold.woff2") format("woff2");
  font-weight: 700; font-style: normal; font-display: swap;
}

/* The script document (preview and print) */
.script-doc {
  font-family: var(--doc-font, var(--serif));
  font-size: calc(var(--doc-pt, 15) * 1.25px);
  line-height: 1.55; color: var(--ink);
}
.script-doc h1 { font-size: 1.35em; text-align: center; margin: 10px 0 2px; }
.script-doc .doc-date { text-align: center; color: var(--ink-2); font-style: italic; margin-bottom: 8px; }
.doc-group { margin-top: 18px; }
.doc-group h2 {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: .7em; letter-spacing: .09em; text-transform: uppercase; color: var(--accent);
  border-bottom: 1px solid var(--line); padding-bottom: 4px; margin-bottom: 8px;
}
.doc-intro { font-style: italic; color: var(--ink-2); margin-bottom: 6px; }
.doc-line { margin: 6px 0 0; }
.doc-note {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: .72em; color: var(--warn); margin: 1px 0 0 1.2em;
}
.doc-note::before { content: "Reader’s note — "; font-weight: 600; }

/* ---------- lists & tables ---------- */
.filters { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.filters input { flex: 1 1 260px; width: auto; }
.filters select { flex: 0 1 240px; width: auto; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th { text-align: left; font-size: .76rem; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); background: var(--surface-2); padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.row-link { cursor: pointer; }
.row-link:hover td { background: var(--surface-2); }
.table .intro-cell { font-family: var(--serif); color: var(--ink); max-width: 44ch; }
.table .name-cell { font-weight: 600; white-space: nowrap; }
.table .actions-cell { text-align: right; white-space: nowrap; }
.detail { display: inline-block; margin: 0 10px 2px 0; color: var(--ink-2); }
.detail b { font-weight: 500; color: var(--ink-3); }
.tag { display: inline-block; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-size: .8rem; white-space: nowrap; }
.tag.none { background: var(--surface-2); color: var(--ink-3); }

.card-list { list-style: none; margin: 0; padding: 0; }
.card-list > li + li { margin-top: 10px; }
.pos-card { display: flex; gap: 12px; align-items: flex-start; }
.pos-main { flex: 1; min-width: 0; }
.pos-main > * + * { margin-top: 6px; }
.pos-title { font-weight: 600; font-size: 1.02rem; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.order { display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 6px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--line); font-size: .75rem; color: var(--ink-2); }
.tpl { display: block; padding: 6px 8px; white-space: pre-wrap; word-break: break-word; }
.example { font-family: var(--serif); color: var(--ink-2); }
.card-actions { display: flex; gap: 6px; }
.builtins { margin-bottom: 14px; font-size: .9rem; line-height: 2; }

/* ---------- modal ---------- */
dialog { border: 0; padding: 0; border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.25); width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 48px); color: var(--ink); }
dialog.wide { width: min(820px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(25, 28, 32, .45); }
.modal { display: flex; flex-direction: column; max-height: calc(100vh - 48px); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { display: flex; gap: 8px; align-items: center; padding: 12px 20px; border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 14px 14px; }
.spacer { flex: 1; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .span2 { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
.section-label { grid-column: 1 / -1; font-size: .74rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); border-bottom: 1px solid var(--line); padding-bottom: 4px; margin-top: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  font: inherit; font-size: .8rem; padding: 3px 9px; border-radius: 99px; cursor: pointer;
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink-2);
}
.chip:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.chip code { background: none; border: 0; padding: 0; font-size: .92em; color: inherit; }
.preview-box { background: var(--surface-2); border: 1px dashed var(--line-2); border-radius: 8px; padding: 10px 12px; }
.preview-box .label { font-size: .74rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.preview-box .label select { width: auto; padding: 2px 6px; font-size: .8rem; text-transform: none; letter-spacing: 0; }
.preview-text { font-family: var(--serif); font-size: 1.05rem; margin-top: 4px; white-space: pre-wrap; }
.preview-text.dim { color: var(--ink-3); font-style: italic; }
.key-input { display: flex; align-items: center; gap: 4px; margin-top: 5px; font-family: ui-monospace, Menlo, monospace; color: var(--ink-3); font-weight: 400; }
.key-input input { margin: 0; font-family: inherit; color: var(--ink); }
.error-text { color: var(--danger); font-size: .84rem; font-weight: 500; }
details > summary { cursor: pointer; font-size: .9rem; font-weight: 600; color: var(--accent); }
details[open] > summary { margin-bottom: 8px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: all .2s ease; z-index: 100;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- print ---------- */
#print-root { display: none; }
@media print {
  @page { margin: 0.8in; }
  body { background: #fff; }
  .app, dialog, #toast { display: none !important; }
  #print-root { display: block; }
  #print-root .script-doc { font-size: calc(var(--doc-pt, 15) * 1pt); line-height: 1.6; }
  #print-root .doc-group h2 { color: #000; break-after: avoid; }
  #print-root .doc-line { margin-top: .7em; break-inside: avoid; }
  #print-root .doc-note { color: #555; }
}

/* ---------- sign in ---------- */
.signin { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.signin .card { width: 100%; max-width: 380px; padding: 24px; }
.signin .brand { margin-bottom: 4px; }
.signin form > * + * { margin-top: 14px; }
.signin label input { margin-top: 4px; }
.signin .btn { width: 100%; justify-content: center; }
.topbar .signout { font-size: .85rem; padding-left: 10px; padding-right: 10px; }
/* Too narrow for the brand, every tab and the account buttons on one line: tabs get their own row. */
@media (max-width: 1180px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px 0; gap: 4px 12px; }
  .topbar .brand { margin-right: auto; }
  .tabs { order: 3; flex-basis: 100%; padding-bottom: 6px; }
}
