:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #10242e;
  --ink-soft: #5b7080;
  --line: #dce4ea;
  --teal: #0f7b8a;
  --teal-dark: #0b5f6b;
  --teal-soft: #e3f3f5;
  --good: #17795e;
  --good-soft: #dff3ec;
  --warn: #a35c00;
  --warn-soft: #fdf0dc;
  --bad: #a32b2b;
  --bad-soft: #fbe6e6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 36, 46, .06), 0 4px 14px rgba(16, 36, 46, .05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1519;
    --panel: #142127;
    --ink: #e6eef2;
    --ink-soft: #8ea6b3;
    --line: #24363f;
    --teal: #3fb0c0;
    --teal-dark: #6fcdd9;
    --teal-soft: #16333a;
    --good: #5fd0aa;
    --good-soft: #143029;
    --warn: #e0a458;
    --warn-soft: #33271528;
    --bad: #e78585;
    --bad-soft: #33191c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 19px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }

/* --- top bar --- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 24px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.mark {
  display: grid; place-items: center; width: 38px; height: 38px; border-radius: 9px;
  background: var(--teal); color: #fff; font-weight: 700; font-size: 13px; letter-spacing: .04em;
}
.sub { margin: 1px 0 0; font-size: 12px; color: var(--ink-soft); }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.badge {
  font-size: 11px; font-weight: 700; letter-spacing: .07em; padding: 5px 9px; border-radius: 999px;
}
.badge-sim { background: var(--teal-soft); color: var(--teal-dark); }
.badge-live { background: var(--bad-soft); color: var(--bad); }

main { max-width: 1400px; margin: 0 auto; padding: 20px 24px 60px; }

/* --- panels --- */
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 18px; box-shadow: var(--shadow);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.panel-actions { display: flex; gap: 8px; }
.setup-grid { display: grid; grid-template-columns: 1.35fr 1fr; gap: 28px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid { display: grid; grid-template-columns: 1.15fr 1fr; gap: 18px; align-items: start; }

@media (max-width: 1080px) {
  .setup-grid, .grid { grid-template-columns: 1fr; }
}

.hint { margin: 0; font-size: 13px; color: var(--ink-soft); }
.hint code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.empty { margin: 6px 0; font-size: 13px; color: var(--ink-soft); }
.muted { color: var(--ink-soft); }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.note { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.note p { margin: 6px 0 0; font-size: 13px; color: var(--ink-soft); }
.note p.ok { color: var(--good); }

/* --- forms --- */
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; }
.field span em { font-weight: 400; color: var(--ink-soft); }
input[type=text], textarea, input[type=file] {
  font: inherit; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%;
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; resize: vertical; }
input[type=text]:focus, textarea:focus { outline: 2px solid var(--teal); outline-offset: -1px; border-color: transparent; }

.btn {
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 14px; transition: background .12s, border-color .12s;
}
.btn:hover { border-color: var(--teal); }
.btn-primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn-primary:hover { background: var(--teal-dark); border-color: var(--teal-dark); }
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-small { padding: 5px 10px; font-size: 12px; }
.btn-chip { padding: 5px 12px; font-size: 12px; border-radius: 999px; background: var(--bg); }
a.btn { text-decoration: none; display: inline-block; }

/* --- flash / banners --- */
.flash {
  background: var(--teal-soft); color: var(--teal-dark); border: 1px solid var(--teal);
  border-radius: var(--radius); padding: 11px 14px; margin-bottom: 16px; font-size: 13.5px; font-weight: 600;
}
.banner {
  background: var(--warn-soft); color: var(--warn); border: 1px solid var(--warn);
  border-radius: var(--radius); padding: 11px 14px; margin-bottom: 16px; font-size: 13.5px;
}
.rejects { border-color: var(--bad); }
.rejects h2 { color: var(--bad); }
.reason { font-size: 12.5px; color: var(--ink-soft); }

/* --- stats --- */
.stats { display: grid; grid-template-columns: repeat(8, 1fr); gap: 10px; margin-bottom: 18px; }
@media (max-width: 1080px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; display: flex; flex-direction: column; gap: 2px;
}
.stat .n { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { font-size: 11px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .05em; }
.stat-good .n { color: var(--good); }
.stat-warn .n { color: var(--warn); }
.stat-bad .n { color: var(--bad); }

/* --- tables --- */
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--ink-soft); font-weight: 700; padding: 6px 10px; border-bottom: 1px solid var(--line);
}
tbody td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.is-active { background: var(--teal-soft); }
.roster td strong { display: block; }
.roster td .muted { font-size: 11.5px; }
td form { margin: 0; }

/* --- state pills --- */
.pill {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px;
  background: var(--bg); color: var(--ink-soft); white-space: nowrap;
}
.pill-idle { background: var(--bg); color: var(--ink-soft); }
.pill-awaiting_name, .pill-awaiting_reorder, .pill-awaiting_info_change, .pill-pending {
  background: var(--teal-soft); color: var(--teal-dark);
}
.pill-order_placed { background: var(--good-soft); color: var(--good); }
.pill-needs_human { background: var(--warn-soft); color: var(--warn); }
.pill-failed_id { background: var(--bad-soft); color: var(--bad); }
.pill-declined, .pill-expired, .pill-opted_out { background: var(--bg); color: var(--ink-soft); }

/* --- dialogs --- */
dialog.dialog {
  padding: 0; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--ink);
  width: min(620px, calc(100vw - 32px)); max-height: calc(100vh - 64px);
  box-shadow: 0 12px 48px rgba(16, 36, 46, .28);
}
dialog.dialog-wide { width: min(820px, calc(100vw - 32px)); }
dialog.dialog::backdrop { background: rgba(10, 22, 28, .5); backdrop-filter: blur(2px); }
dialog.dialog > form { display: contents; }

.dialog-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 16px 18px; border-bottom: 1px solid var(--line);
}
.dialog-head-right { display: flex; align-items: center; gap: 10px; }
.dialog-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.dialog-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 14px 18px; border-top: 1px solid var(--line); background: var(--bg);
  border-bottom-left-radius: 14px; border-bottom-right-radius: 14px;
}
.dialog-foot-reply { flex-direction: column; align-items: stretch; gap: 9px; }
.btn-icon {
  padding: 2px 9px; font-size: 19px; line-height: 1.2; color: var(--ink-soft);
  background: transparent; border-color: transparent;
}
.btn:focus-visible { outline: 2px solid var(--teal); outline-offset: 1px; }
.btn:focus:not(:focus-visible) { outline: none; }

.dialog-error {
  background: var(--bad-soft); border: 1px solid var(--bad); border-radius: 8px;
  padding: 11px 13px; font-size: 13px; color: var(--bad);
}
.dialog-error table { margin-top: 8px; }
.dialog-error thead th, .dialog-error tbody td { border-color: color-mix(in srgb, var(--bad) 30%, transparent); }
.dialog-error .reason { color: var(--bad); }

.empty-state { text-align: center; padding: 34px 16px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.empty-state p { margin: 0; color: var(--ink-soft); }
.empty-state .hint { max-width: 380px; }

/* --- conversation --- */
.convo-meta { font-size: 12px; color: var(--ink-soft); }
.convo-meta .warn { color: var(--warn); font-weight: 600; }

.phone {
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px; padding: 12px;
  max-height: min(420px, 44vh); overflow-y: auto;
}
/* Bottom-anchored like a messages app: min-height keeps the screen a full
   panel when the transcript is one bubble long, and the min-height lives here
   (not on .phone) so .phone's height just follows its content either way. */
.phone-screen {
  display: flex; flex-direction: column; justify-content: flex-end; gap: 12px;
  min-height: min(300px, 38vh);
}
.bubble-row { display: flex; flex-direction: column; max-width: 82%; }
.bubble-row.from-us { align-self: flex-start; }
.bubble-row.from-them { align-self: flex-end; align-items: flex-end; }
.bubble {
  padding: 9px 13px; border-radius: 16px; font-size: 13.5px; line-height: 1.45;
  background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 5px;
  overflow-wrap: anywhere;
}
.from-them .bubble {
  background: var(--teal); color: #fff; border-color: var(--teal);
  border-bottom-left-radius: 16px; border-bottom-right-radius: 5px;
}
/* Autolinked URLs in a message, the way a phone renders them. Inbound bubbles
   are teal-on-white, so those links take the bubble's own color. */
.bubble a { color: var(--teal-dark); text-decoration: underline; }
.from-them .bubble a { color: inherit; }
.bubble-meta { font-size: 10.5px; color: var(--ink-soft); margin-top: 3px; font-variant-numeric: tabular-nums; }
.bubble-note { font-size: 11px; color: var(--ink-soft); font-style: italic; margin-top: 2px; }

/* Only bubbles appended by a reply carry .bubble-new -- the transcript the
   dialog opens with is already there and shouldn't replay itself. A turn's
   bubbles stagger via --bubble-delay so the client's reply lands before the
   machine's answer. `both` holds the from-state during the delay, but layout
   space is claimed immediately, so the autoscroll below still finds the end. */
@keyframes bubble-in {
  from { opacity: 0; transform: translateY(10px) scale(.96); }
  to   { opacity: 1; transform: none; }
}
.bubble-row.bubble-new {
  animation: bubble-in .32s cubic-bezier(.22, 1, .36, 1) both;
  animation-delay: var(--bubble-delay, 0ms);
  transform-origin: bottom left;
}
.bubble-row.bubble-new.from-them { transform-origin: bottom right; }

.closed-note {
  font-size: 12.5px; color: var(--ink-soft); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; margin: 0;
}
.quick { display: flex; flex-wrap: wrap; gap: 7px; }
.dialog-foot-reply .btn-chip { background: var(--panel); }
.reply-row { display: flex; gap: 8px; }

/* --- order details page --- */
.details-page { min-height: 100vh; display: grid; place-items: center; padding: 32px 20px; }
.details-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 32px; max-width: 520px; width: 100%; box-shadow: var(--shadow);
}
.details-brand { display: flex; align-items: center; gap: 10px; font-weight: 650; margin-bottom: 22px; }
.lede { color: var(--ink-soft); margin: 8px 0 22px; }
.details-list { margin: 0; display: flex; flex-direction: column; gap: 1px; background: var(--line); border-radius: 10px; overflow: hidden; }
.details-list > div { display: flex; justify-content: space-between; gap: 16px; background: var(--panel); padding: 11px 14px; }
.details-list dt { font-size: 12.5px; color: var(--ink-soft); margin: 0; }
.details-list dd { margin: 0; font-weight: 600; text-align: right; }
.fine { font-size: 12.5px; color: var(--ink-soft); margin: 20px 0 0; }
