/*
  TE Sequences admin UI.

  Breakpoints (checked by tools/layout-check.mjs at 1024x768, 1280x720, 390x844):
  - >= 1200px  Sequence editor is two columns: settings cards (380px, forms one-up) left, steps right.
               Below 1200 the settings cards stack above the steps.
  - >= 960px   Contact detail is two columns: email status + fields left, enrollments +
               timeline right. Below 960 they stack in that order.
  - <= 760px   Top bar wraps: brand + account on row 1, nav on row 2 (full width).
               Sequence header controls wrap below the title.
  - <= 560px   Enrollments table rows become stacked blocks (label: value, actions
               wrapped below), so row actions never need a sideways scroll.
               Two-up form grids become one column; page headers stack (title, then
               buttons); the account email is hidden (role pill + Sign out stay);
               modals go full width.
  Wide tables never widen the page: they scroll inside .table-scroll.
*/

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1d2330;
  --muted: #5d6676;
  --border: #d9dde4;
  --accent: #2456c7;
  --accent-ink: #ffffff;
  --green: #18794e;  --green-bg: #e3f4ea;
  --amber: #9a5b00;  --amber-bg: #fdf0d5;
  --red: #b42318;    --red-bg: #fde8e6;
  --blue: #1f4fa8;   --blue-bg: #e4ecfb;
  --grey: #4a5160;   --grey-bg: #e8eaee;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c; --surface: #1c2027; --surface-2: #232832; --text: #e6e9ef; --muted: #9aa3b2; --border: #343b47;
    --accent: #6b9bff; --accent-ink: #0b1020;
    --green: #5fd39a; --green-bg: #153526; --amber: #f2b552; --amber-bg: #3a2c12; --red: #ff8a80; --red-bg: #3d1c1a;
    --blue: #8fb3ff; --blue-bg: #1a2a4a; --grey: #b6bcc8; --grey-bg: #2b313b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); overflow-x: hidden; }
a { color: var(--accent); }
h1 { font-size: 1.45rem; margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: 0.95rem; margin: 0; }
p { margin: 0; }
ul { margin: 0.3rem 0 0; padding-left: 1.2rem; }
input, select, textarea, button { font: inherit; color: inherit; }
input[type="text"], input[type="email"], input[type="search"], input[type="number"], select, textarea {
  width: 100%; min-width: 0; padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface);
}
textarea { resize: vertical; font-family: var(--font); }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.red-text, .error-text { color: var(--red); }
.amber-text { color: var(--amber); }
.flag-line { color: var(--amber); font-weight: 600; font-size: .85rem; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 2px; }
.flag-line .flag-text::before { content: "\26A0  "; }
.ok-text { color: var(--green); }
.stack { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.stack > .btn { align-self: flex-start; }

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex; align-items: center; gap: 18px; padding: 10px 20px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 1.05rem; color: var(--text); text-decoration: none; display: flex; align-items: center; gap: 8px; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a { padding: 6px 10px; border-radius: 6px; color: var(--text); text-decoration: none; }
.nav a.active { background: var(--surface-2); font-weight: 600; box-shadow: inset 0 -2px 0 var(--accent); }
.who { margin-left: auto; display: flex; align-items: center; gap: 8px; min-width: 0; }
.who-email { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }

.banners { display: flex; flex-direction: column; }
.banner { padding: 9px 20px; border-bottom: 1px solid var(--border); display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.banner-red { background: var(--red-bg); color: var(--red); }
.banner-amber { background: var(--amber-bg); color: var(--amber); }
.banner-info { background: var(--blue-bg); color: var(--blue); }
.banner-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.banner-note { font-size: 0.85rem; }

.page { max-width: 1400px; margin: 0 auto; padding: 18px 20px 120px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px 16px; flex-wrap: wrap; }
.page-head > div:first-child { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.back { font-size: 0.85rem; text-decoration: none; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.section-title { margin-top: 2px; }

/* ------------------------------------------------------------ cards + pills */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.card-red { border-color: var(--red); }
.pills { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.pill {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.03em;
  white-space: nowrap; border: 1px solid transparent; vertical-align: middle;
}
.pill-grey { background: var(--grey-bg); color: var(--grey); }
.pill-green { background: var(--green-bg); color: var(--green); }
.pill-amber { background: var(--amber-bg); color: var(--amber); }
.pill-red { background: var(--red-bg); color: var(--red); }
.pill-blue { background: var(--blue-bg); color: var(--blue); }
.pill-outline { background: transparent; color: var(--muted); border-color: var(--border); }
.pill-live { background: var(--red); color: #fff; }

.callout { padding: 9px 12px; border-radius: 6px; border: 1px solid transparent; }
.callout-red { background: var(--red-bg); color: var(--red); border-color: var(--red); }
.g-row { display: flex; flex-direction: column; gap: 6px; padding-top: 10px; border-top: 1px solid var(--border); }
.g-row-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.g-row p { margin: 0; }
.callout-amber { background: var(--amber-bg); color: var(--amber); }
.callout-green { background: var(--green-bg); color: var(--green); }
.review-later summary { cursor: pointer; color: var(--muted); }
a.pill { text-decoration: none; }

/* ------------------------------------------------------------ buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 7px 13px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; text-decoration: none;
  white-space: nowrap; line-height: 1.2; font-weight: 500; min-height: 34px;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-sm { padding: 4px 9px; min-height: 28px; font-size: 0.85rem; }
.btn-xs { padding: 0 6px; min-height: 20px; font-size: 0.75rem; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; font-weight: 600; }
.btn-danger:hover:not(:disabled) { background: var(--red); filter: brightness(1.1); }
.btn-kill { font-size: 1rem; padding: 8px 18px; }
.btn-amber { background: var(--amber-bg); border-color: var(--amber); color: var(--amber); }
.btn-amber:hover:not(:disabled) { background: var(--amber-bg); filter: brightness(0.97); }
.btn-go { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn-go:hover:not(:disabled) { background: var(--green); filter: brightness(1.1); }
.btn-ghost { background: transparent; }
.btn-ghost-danger { background: transparent; color: var(--red); border-color: var(--red); }
.linklike { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-align: left; text-decoration: underline; word-break: break-all; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.ctl-group { display: inline-flex; gap: 8px; flex-wrap: wrap; }
.chip { padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); font-family: var(--mono); font-size: 0.8rem; cursor: pointer; }
.chip-picked { display: inline-flex; align-items: center; gap: 4px; font-family: var(--font); cursor: default; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }

/* ------------------------------------------------------------ forms */
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-weight: 600; font-size: 0.85rem; }
.field-hint { color: var(--muted); font-size: 0.8rem; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.check { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; }
.check input { margin-top: 3px; }
.check-big { font-weight: 500; }
.days { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.days .check { align-items: center; }
.days .check input { margin-top: 0; }
.inline { display: inline-flex; align-items: center; gap: 6px; }
.inline input { width: 5rem; }
.delay { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; }
.filters input { flex: 1 1 260px; }
.filters select { flex: 0 1 220px; width: auto; }
.problems:empty { display: none; }

/* ------------------------------------------------------------ sequences list */
.seq-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 440px), 1fr)); gap: 14px; }
.seq-card { display: flex; flex-direction: column; gap: 10px; }
.seq-card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; flex-wrap: wrap; }
.seq-name { font-weight: 700; font-size: 1.05rem; text-decoration: none; color: var(--text); overflow-wrap: anywhere; }
.seq-name:hover { text-decoration: underline; }
.seq-desc { font-size: 0.88rem; }
.stats { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.stat { background: var(--surface-2); border-radius: 6px; padding: 6px 8px; display: flex; flex-direction: column; min-width: 0; }
.stat-n { font-size: 1.15rem; font-weight: 700; }
.stat-l { font-size: 0.72rem; color: var(--muted); line-height: 1.2; }
.stat-red { background: var(--red-bg); }
.stat-red .stat-n, .stat-red .stat-l { color: var(--red); }
.seq-card-foot { display: flex; flex-direction: column; gap: 8px; margin-top: auto; }

/* ------------------------------------------------------------ sequence page */
.seq-title { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.seq-title h1 { overflow-wrap: anywhere; }
.seq-controls { justify-content: flex-end; }
.seq-meta { overflow-wrap: anywhere; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); overflow-x: auto; overflow-y: hidden; scrollbar-width: thin; }
.tabs a { padding: 8px 14px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a.active { color: var(--text); font-weight: 600; border-bottom-color: var(--accent); }

.editor-cols { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.editor-main, .editor-side { min-width: 0; }
@media (min-width: 1200px) {
  .editor-cols { grid-template-columns: 380px minmax(0, 1fr); align-items: start; }
  .editor-side .grid-2 { grid-template-columns: minmax(0, 1fr); }
}
.step { display: flex; flex-direction: column; gap: 10px; }
.step-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.savebar {
  position: sticky; bottom: 0; z-index: 10; margin-top: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; box-shadow: 0 -2px 8px rgba(16, 24, 40, 0.08); display: flex; flex-direction: column; gap: 8px;
}
.savebar .callout { max-height: 8.5rem; overflow: auto; }
.savebar-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ------------------------------------------------------------ tables */
.table-scroll { overflow-x: auto; max-width: 100%; border: 1px solid var(--border); border-radius: 6px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.tbl th, .tbl td { padding: 7px 10px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.tbl th { background: var(--surface-2); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); white-space: nowrap; }
.tbl tbody tr:last-child td { border-bottom: 0; }
.tbl-enr td:first-child { min-width: 200px; }
.row-actions { display: flex; gap: 6px; flex-wrap: nowrap; }
.detail-cell { min-width: 180px; }

.pick-results { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.pick-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; }
.pick-text { min-width: 0; overflow-wrap: anywhere; }
#enroll-panel .pick-results { max-height: 260px; overflow-y: auto; }
#enroll-panel .enroll-col { gap: 12px; }
#enroll-panel input[type=file] { max-width: 100%; }
.tbl-preview code { font-size: 0.8rem; }

/* ------------------------------------------------------------ preview */
.mail-frame { width: 100%; height: 300px; border: 1px solid var(--border); border-radius: 6px; background: #fff; resize: vertical; }
.mail-text { white-space: pre-wrap; font: 0.92rem/1.55 var(--font); background: var(--surface-2); padding: 10px; border-radius: 6px; margin: 0; overflow-wrap: anywhere; }

/* ------------------------------------------------------------ key/value, timeline */
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 5px 14px; margin: 0; }
.kv dt { color: var(--muted); font-size: 0.85rem; }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.kv-compact { grid-template-columns: repeat(2, max-content minmax(0, 1fr)); }
.detail-cols { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
@media (min-width: 960px) { .detail-cols { grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); align-items: start; } }
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.timeline li { border-left: 3px solid var(--border); padding-left: 10px; }
.tl-head { display: flex; gap: 8px; align-items: center; }
.tl-text { margin-top: 3px; white-space: pre-wrap; overflow-wrap: anywhere; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: 14px; }

/* ------------------------------------------------------------ modal, toast, login */
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 14, 22, 0.45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px 16px; z-index: 50; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 10px; width: min(560px, 100%); box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25); display: flex; flex-direction: column; max-height: 88vh; }
.modal-wide { width: min(820px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-head h2 { overflow-wrap: anywhere; }
.modal-body { padding: 14px 16px; overflow-y: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--border); }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: min(420px, calc(100vw - 32px)); }
.toast { padding: 10px 14px; border-radius: 6px; background: var(--text); color: var(--surface); box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2); overflow-wrap: anywhere; }
.toast-error { background: var(--red); color: #fff; }
.loading { padding: 30px; color: var(--muted); text-align: center; }
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: min(400px, 100%); display: flex; flex-direction: column; gap: 14px; }
.login-title { font-size: 1.3rem; }

/* ------------------------------------------------------------ small screens */
@media (max-width: 760px) {
  .topbar { padding: 8px 16px; gap: 6px 12px; }
  .nav { order: 3; width: 100%; }
  .page { padding: 14px 16px 120px; }
  .banner { padding: 8px 16px; }
  .seq-controls { justify-content: flex-start; }
}
@media (max-width: 560px) {
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .page-head { flex-direction: column; align-items: stretch; }
  .who-email { display: none; }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .kv-compact { grid-template-columns: max-content minmax(0, 1fr); }
  .modal-backdrop { padding: 8px; }
  .banner-actions { margin-left: 0; }
  h1 { font-size: 1.25rem; }
  .tabs a { padding: 8px 9px; }
  /* Enrollments table becomes one block per row so the actions stay on screen. */
  .tbl-stack thead { display: none; }
  .tbl-stack, .tbl-stack tbody, .tbl-stack tr, .tbl-stack td { display: block; width: 100%; }
  .tbl-stack tr { padding: 8px 10px; border-bottom: 1px solid var(--border); }
  .tbl-stack tbody tr:last-child { border-bottom: 0; }
  .tbl-stack td { border: 0; padding: 2px 0; min-width: 0 !important; white-space: normal; }
  .tbl-stack td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: 0.8rem; }
  .tbl-stack .row-actions { flex-wrap: wrap; padding-top: 4px; }
}
