:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --sunken: #efede7;
  --line: #dedbd2;
  --text: #1c1f1d;
  --muted: #666c66;
  --accent: #1f6f54;
  --accent-text: #ffffff;
  --accent-soft: #e2f0ea;
  --danger: #b3261e;
  --warn-soft: #fbefd5;
  --warn-text: #7a5200;
  --new: #1d5fbf;
  --new-soft: #e3edfb;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 5%);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141715;
    --surface: #1d211e;
    --sunken: #171a18;
    --line: #313733;
    --text: #ecefe9;
    --muted: #9aa39b;
    --accent: #58b993;
    --accent-text: #0c1a14;
    --accent-soft: #1d3329;
    --danger: #f2877f;
    --warn-soft: #3a2f14;
    --warn-text: #f0cf85;
    --new: #8db8f5;
    --new-soft: #1b2a41;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.5rem; line-height: 1.25; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.boot { padding: 4rem 1rem; text-align: center; color: var(--muted); }
[hidden] { display: none !important; }

/* Controls */
button, input, select, textarea { font: inherit; color: inherit; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem; min-height: 40px; padding: 0 0.9rem; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); cursor: pointer; text-decoration: none; color: var(--text); font-weight: 500; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.quiet { border-color: transparent; background: none; color: var(--muted); }
.btn.quiet:hover { color: var(--text); background: var(--sunken); }
.btn.danger { color: var(--danger); }
.btn.sm { min-height: 32px; padding: 0 0.6rem; font-size: 0.875rem; }
.btn:disabled { opacity: 0.55; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input[type=text], input[type=email], input[type=password], input[type=search], input[type=url], textarea, select, .rich {
  width: 100%; padding: 0.55rem 0.7rem; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); min-height: 40px;
}
textarea { resize: vertical; min-height: 5.5rem; }
label.field { display: grid; gap: 0.3rem; }
label.field > span, .label { font-size: 0.875rem; font-weight: 600; }
.stack { display: grid; gap: 1rem; }
.row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.error { color: var(--danger); font-size: 0.9rem; }

/* Shell */
.top { position: sticky; top: 0; z-index: 5; background: var(--surface); border-bottom: 1px solid var(--line); }
.top-in { max-width: 1040px; margin: 0 auto; padding: 0.6rem 1rem 0; }
.brand { font-weight: 700; letter-spacing: -0.01em; }
.top select { width: auto; max-width: 60vw; font-weight: 600; min-height: 36px; padding-block: 0.3rem; }
.tabs { display: flex; gap: 0.25rem; margin-top: 0.5rem; overflow-x: auto; }
.tabs a { padding: 0.5rem 0.75rem; text-decoration: none; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a[aria-current=page] { color: var(--text); border-bottom-color: var(--accent); }
.count { display: inline-block; min-width: 1.4em; padding: 0 0.4em; margin-left: 0.3em; border-radius: 99px; background: var(--new-soft); color: var(--new); font-size: 0.78rem; text-align: center; font-variant-numeric: tabular-nums; }
main { max-width: 1040px; margin: 0 auto; padding: 1.25rem 1rem 7rem; }
.head { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 1rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.empty { padding: 2.5rem 1rem; text-align: center; color: var(--muted); }

/* Sign in */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth .card { width: 100%; max-width: 380px; padding: 1.5rem; box-shadow: var(--shadow); }

/* Leads */
.chips { display: flex; gap: 0.4rem; overflow-x: auto; padding-bottom: 2px; }
.chip { flex: none; padding: 0.3rem 0.75rem; border: 1px solid var(--line); border-radius: 99px; background: var(--surface); cursor: pointer; font-size: 0.9rem; }
.chip[aria-pressed=true] { background: var(--text); color: var(--bg); border-color: var(--text); }
.leads { display: grid; margin-top: 0.75rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.lead { display: grid; grid-template-columns: 1fr auto; gap: 0.1rem 0.75rem; padding: 0.8rem 1rem; text-decoration: none; color: inherit; border-top: 1px solid var(--line); }
.lead:first-child { border-top: 0; }
.lead:hover { background: var(--sunken); }
.lead .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead.is-new .who::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 0.45rem; border-radius: 50%; background: var(--new); vertical-align: 1px; }
.lead .snippet { grid-column: 1 / -1; color: var(--muted); font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead .when { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.pill { display: inline-block; padding: 0.05rem 0.55rem; border-radius: 99px; font-size: 0.78rem; font-weight: 600; background: var(--sunken); color: var(--muted); }
.pill.new { background: var(--new-soft); color: var(--new); }
.pill.won { background: var(--accent-soft); color: var(--accent); }
.pill.edited { background: var(--warn-soft); color: var(--warn-text); }
.detail { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr); align-items: start; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 0.5rem 1rem; margin: 0; }
dl.facts dt { color: var(--muted); font-size: 0.9rem; }
dl.facts dd { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.note { padding: 0.6rem 0; border-top: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }
.note:first-child { border-top: 0; padding-top: 0; }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { min-height: 40px; border: 0; border-left: 1px solid var(--line); background: var(--surface); cursor: pointer; font-size: 0.9rem; }
.seg button:first-child { border-left: 0; }
.seg button[aria-pressed=true] { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* Editor */
.editor { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 1.25rem; align-items: start; }
.pages { position: sticky; top: 7rem; display: grid; gap: 2px; }
.pages button { text-align: left; padding: 0.5rem 0.7rem; border: 0; border-radius: 8px; background: none; cursor: pointer; display: flex; justify-content: space-between; gap: 0.5rem; }
.pages button[aria-current=true] { background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); font-weight: 600; }
.pages .dot { color: var(--warn-text); }
.region { display: grid; gap: 0.4rem; padding: 1rem 0; border-top: 1px solid var(--line); }
.region:first-child { border-top: 0; padding-top: 0; }
.region-head { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.rich { min-height: 4.5rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.rich:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.rich-tools { display: flex; gap: 0.25rem; }
.rich-tools .btn { min-width: 32px; }
.image-edit { display: grid; grid-template-columns: 120px 1fr; gap: 0.75rem; align-items: start; }
.thumb { width: 120px; height: 90px; border-radius: 8px; border: 1px solid var(--line); background: var(--sunken) center / cover no-repeat; display: grid; place-items: center; color: var(--muted); font-size: 0.8rem; text-align: center; }
.item { border: 1px solid var(--line); border-radius: var(--radius); padding: 0.85rem; display: grid; gap: 0.7rem; background: var(--bg); }
.item-head { display: flex; align-items: center; gap: 0.25rem; }
.savebar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; background: var(--surface); border-top: 1px solid var(--line); box-shadow: 0 -4px 16px rgb(0 0 0 / 8%); }
.savebar-in { max-width: 1040px; margin: 0 auto; padding: 0.7rem 1rem calc(0.7rem + env(safe-area-inset-bottom)); display: flex; gap: 0.5rem; align-items: center; }

/* Tables (admin) */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.93rem; }
th, td { text-align: left; padding: 0.6rem 0.75rem 0.6rem 0; border-top: 1px solid var(--line); vertical-align: top; }
th { border-top: 0; color: var(--muted); font-weight: 600; font-size: 0.82rem; white-space: nowrap; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.88em; }
.copybox { display: flex; gap: 0.5rem; }
.copybox input { font-family: ui-monospace, Menlo, monospace; font-size: 0.85rem; }

dialog { width: min(520px, calc(100vw - 2rem)); border: 1px solid var(--line); border-radius: 14px; padding: 1.25rem; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
dialog::backdrop { background: rgb(0 0 0 / 45%); }
.checks { display: grid; gap: 0.35rem; }
.checks label { display: flex; gap: 0.5rem; align-items: center; }

#toasts { position: fixed; left: 50%; bottom: 5rem; transform: translateX(-50%); z-index: 50; display: grid; gap: 0.5rem; width: max-content; max-width: calc(100vw - 2rem); }
.toast { padding: 0.65rem 1rem; border-radius: 10px; background: var(--text); color: var(--bg); box-shadow: var(--shadow); font-size: 0.95rem; }
.toast.bad { background: var(--danger); color: #fff; }

@media (max-width: 760px) {
  .detail, .editor { grid-template-columns: 1fr; }
  .pages { position: static; display: flex; overflow-x: auto; gap: 0.25rem; padding-bottom: 0.25rem; }
  .pages button { flex: none; background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); }
  .pages button[aria-current=true] { background: var(--text); color: var(--bg); box-shadow: none; }
  .image-edit { grid-template-columns: 88px 1fr; }
  .thumb { width: 88px; height: 66px; }
  h1 { font-size: 1.3rem; }
}

/* Keep the header on one line on phones */
.top .row { flex-wrap: nowrap; }
.top .row > a.btn { min-width: 0; overflow: hidden; text-overflow: ellipsis; display: block; line-height: 32px; flex-shrink: 1; }
.tabs, .chips, .pages { scrollbar-width: none; }

/* chats */
.chat-log { display: flex; flex-direction: column; gap: .6rem; }
.chat-msg { max-width: 80%; padding: .6rem .8rem; border-radius: var(--radius); background: var(--sunken); white-space: pre-wrap; }
.chat-msg.user { align-self: flex-end; background: var(--accent-soft); }
.chat-event { align-self: center; font-size: .85rem; }

/* Editor: modes, sections, on-the-page editing */
.seg.two { grid-template-columns: repeat(2, auto); }
.seg.two button { padding: 0 0.9rem; }
main.wide { max-width: none; }
details.section { border-top: 1px solid var(--line); }
details.section:first-child { border-top: 0; }
details.section > summary { cursor: pointer; padding: 0.8rem 0; font-weight: 600; list-style-position: inside; }
details.section[open] > summary { padding-bottom: 0.4rem; }
details.section > .region:first-of-type { border-top: 0; }
details.section .region { padding-left: 0.25rem; }
.dot { color: var(--warn-text); }
.region.is-hidden { opacity: 0.55; }
.editor .head input[type=search] { flex: 1; min-width: 180px; }
.visual { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 1rem; align-items: start; }
.frame-wrap { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface); position: sticky; top: 6.5rem; }
.frame-bar { display: flex; gap: 0.5rem; align-items: center; padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--line); }
.frame-bar select { width: auto; max-width: 70%; min-height: 32px; padding-block: 0.2rem; }
.frame { display: block; width: 100%; height: calc(100vh - 13.5rem); min-height: 420px; border: 0; background: #fff; }
.side { display: grid; gap: 0.75rem; align-content: start; max-height: calc(100vh - 8rem); overflow: auto; position: sticky; top: 6.5rem; }
.side .region { padding: 0; border: 0; }
@media (max-width: 900px) {
  .visual { grid-template-columns: 1fr; }
  .frame-wrap, .side { position: static; max-height: none; }
  .frame { height: 52vh; min-height: 320px; }
}
