:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f3f4f6;
  --surface-3: #e9ebef;
  --border: #dfe2e7;
  --border-strong: #c7ccd4;
  --text: #15181d;
  --muted: #5d6573;
  --faint: #8a919c;
  --accent: #2457d6;
  --accent-hover: #1d48b5;
  --accent-soft: #e8eefc;
  --accent-contrast: #ffffff;
  --ok: #17794a;
  --ok-soft: #e4f4ec;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --err: #c0302b;
  --err-soft: #fcebea;
  --focus: 0 0 0 3px rgba(36, 87, 214, 0.28);
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1e2229;
    --surface-3: #262b33;
    --border: #2b3039;
    --border-strong: #3a414c;
    --text: #e8eaee;
    --muted: #a0a7b3;
    --faint: #737b88;
    --accent: #5b8cff;
    --accent-hover: #7aa2ff;
    --accent-soft: #1c2840;
    --accent-contrast: #0b1020;
    --ok: #4cc38a;
    --ok-soft: #14291f;
    --warn: #e5a54b;
    --warn-soft: #2d2313;
    --err: #ff6b63;
    --err-soft: #2f1716;
    --focus: 0 0 0 3px rgba(91, 140, 255, 0.35);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a20;
  --surface-2: #1e2229;
  --surface-3: #262b33;
  --border: #2b3039;
  --border-strong: #3a414c;
  --text: #e8eaee;
  --muted: #a0a7b3;
  --faint: #737b88;
  --accent: #5b8cff;
  --accent-hover: #7aa2ff;
  --accent-soft: #1c2840;
  --accent-contrast: #0b1020;
  --ok: #4cc38a;
  --ok-soft: #14291f;
  --warn: #e5a54b;
  --warn-soft: #2d2313;
  --err: #ff6b63;
  --err-soft: #2f1716;
  --focus: 0 0 0 3px rgba(91, 140, 255, 0.35);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }
p { margin: 0; }
code, pre { font-family: var(--mono); font-size: 12px; }
a { color: var(--accent); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; white-space: nowrap; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: var(--accent);
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='8'/%3E%3C/svg%3E");
  position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 5px 4px 6px;
  border-left: 3px solid var(--accent-contrast); border-bottom: 3px solid var(--accent-contrast);
  border-radius: 0 0 0 2px; opacity: .9;
}
.topbar-spacer { flex: 1; }
.conn { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.up { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.dot.down { background: var(--err); box-shadow: 0 0 0 3px var(--err-soft); }
.dot.auth { background: var(--warn); box-shadow: 0 0 0 3px var(--warn-soft); }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 24px 64px;
  align-items: start;
}
.builder { display: grid; gap: 16px; min-width: 0; }
.side { display: grid; gap: 16px; position: sticky; top: 72px; max-height: calc(100vh - 88px); overflow: auto; padding-bottom: 4px; }

@media (max-width: 1000px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .side { position: static; max-height: none; overflow: visible; }
}
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; gap: 8px; }
  .brand { font-size: 14px; }
  .topbar .btn { padding: 6px 10px; }
  .conn #connText { display: none; }
  .layout { padding: 16px 16px 48px; gap: 16px; }
}

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}
.card-head { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 18px; }
.step {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
  font-weight: 700; font-size: 13px;
}
.sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.side-title { margin-bottom: 12px; }
.side-title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.side-title-row .side-title { margin: 0; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 18px; }
@media (max-width: 640px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }
.stack { display: grid; gap: 14px; margin-top: 14px; }
.stack:first-child { margin-top: 0; }

/* ---------- Fields ---------- */
.field { display: grid; gap: 6px; min-width: 0; margin: 0; padding: 0; border: 0; }
fieldset.field { min-width: 0; }
.field + .field, .grid-2 + .field, .field + .disclosure { margin-top: 0; }
.card > .field, .card > fieldset.field { margin-bottom: 16px; }
.card > .field:last-child, .card > fieldset.field:last-child { margin-bottom: 0; }
label, legend, .label { font-weight: 550; font-size: 13px; color: var(--text); padding: 0; }
.req, .opt { font-weight: 450; font-size: 12px; margin-left: 4px; }
.req { color: var(--muted); }
.opt { color: var(--faint); }

input:not([type="radio"]):not([type="checkbox"]):not([type="file"]),
select,
textarea {
  width: 100%;
  min-width: 0;
  padding: 9px 11px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; min-height: 92px; }
input::placeholder, textarea::placeholder { color: var(--faint); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: var(--focus); }
.invalid, input.invalid, select.invalid, textarea.invalid { border-color: var(--err) !important; box-shadow: 0 0 0 3px var(--err-soft) !important; }

.input-row { display: flex; gap: 8px; align-items: center; min-width: 0; }
.input-row > input { flex: 1; }
/* !important: the generic input rule (width: 100%) has higher specificity. */
.input-row.tight > input { width: 84px !important; flex: none; }
.dash { color: var(--muted); font-size: 13px; }
.grow { flex: 1; }

.input-affix { position: relative; }
.input-affix input { padding-right: 56px !important; }
.affix { position: absolute; right: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 12px; font-weight: 600; pointer-events: none; }

.field-msg { font-size: 12px; color: var(--muted); min-height: 0; }
.field-msg:empty { display: none; }
.field-msg.ok { color: var(--ok); }
.field-msg.err { color: var(--err); }
.field-msg.warn { color: var(--warn); }
.field-msg.center { text-align: center; margin-top: 8px; }

/* ---------- Choice cards ---------- */
.choice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(136px, 1fr)); gap: 8px; }
.choice { position: relative; cursor: pointer; font-weight: 400; }
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.choice-body {
  display: grid; gap: 2px; height: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color .15s, background .15s;
}
.choice-body strong { font-size: 13px; }
.choice-body small { color: var(--muted); font-size: 12px; line-height: 1.35; }
.choice:hover .choice-body { border-color: var(--accent); }
.choice input:checked + .choice-body { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.choice input:focus-visible + .choice-body { box-shadow: var(--focus); }

/* ---------- Segmented control ---------- */
.segmented {
  display: inline-flex; flex-wrap: wrap; gap: 2px;
  padding: 3px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border);
  justify-self: start; max-width: 100%;
}
.segmented.wide { display: flex; justify-self: stretch; }
.segmented.wide label { flex: 1; }
.segmented label { position: relative; cursor: pointer; font-weight: 500; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block; text-align: center;
  padding: 6px 14px; border-radius: 7px;
  color: var(--muted); font-size: 13px;
  transition: background .15s, color .15s;
}
.segmented.small span { padding: 4px 10px; font-size: 12px; }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.segmented input:focus-visible + span { box-shadow: var(--focus); }

.check-list { display: grid; gap: 8px; margin-top: 10px; }
.check-list label, .inline-check { display: flex; gap: 8px; align-items: center; font-weight: 400; cursor: pointer; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }

.disclosure { border-top: 1px solid var(--border); padding-top: 14px; }
.disclosure summary { cursor: pointer; font-weight: 550; font-size: 13px; color: var(--muted); }
.disclosure[open] summary { color: var(--text); margin-bottom: 6px; }

.media-box { padding: 14px; border: 1px dashed var(--border-strong); border-radius: var(--radius-sm); background: var(--surface-2); }

.dropzone {
  display: grid; place-items: center; text-align: center;
  min-height: 96px; padding: 16px;
  border: 1.5px dashed var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--muted); font-weight: 400; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.dropzone.has-file { border-style: solid; border-color: var(--ok); color: var(--text); }

/* ---------- Chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:empty { display: none; }
.chip { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.chip b { color: var(--text); font-weight: 600; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; min-height: 36px;
  font: inherit; font-weight: 600; font-size: 13px; white-space: nowrap;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  cursor: pointer; transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:focus-visible { outline: none; box-shadow: var(--focus); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn.loading { position: relative; color: transparent !important; }
.btn.loading::after {
  content: ""; position: absolute; width: 14px; height: 14px;
  border: 2px solid currentColor; border-color: var(--muted) transparent var(--muted) transparent;
  border-radius: 50%; animation: spin .8s linear infinite;
}
.btn-primary.loading::after { border-color: var(--accent-contrast) transparent var(--accent-contrast) transparent; }
.btn-sm { padding: 3px 8px; min-height: 0; font-size: 12px; }
.link-btn { background: none; border: 0; padding: 0; font: inherit; font-size: 12px; color: var(--muted); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.link-btn:hover { color: var(--text); }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 16px; }
.actions .btn-primary { grid-column: 1 / -1; min-height: 40px; }
.actions.end { display: flex; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
.actions.end .btn-primary { min-height: 36px; }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; white-space: nowrap;
  padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge:empty { display: none; }
.badge-ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-err { background: var(--err-soft); color: var(--err); border-color: transparent; }
.badge-accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* ---------- Post grid ---------- */
.post-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.post-grid:empty { display: none; }
.post {
  display: grid; grid-template-rows: auto 1fr; text-align: left;
  padding: 0; overflow: hidden; cursor: pointer;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit;
  transition: border-color .15s, box-shadow .15s;
}
.post:hover { border-color: var(--accent); }
.post.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.post:focus-visible { outline: none; box-shadow: var(--focus); }
.post-thumb { aspect-ratio: 1 / 1; background: var(--surface-3); display: grid; place-items: center; color: var(--faint); font-size: 12px; overflow: hidden; }
.post-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.post-body { padding: 8px 10px; display: grid; gap: 4px; }
.post-msg { font-size: 12px; line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.8em; }
.post-meta { font-size: 11px; color: var(--faint); }

/* ---------- Ad preview ---------- */
.ad { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface); }
.ad-head { display: flex; gap: 10px; align-items: center; padding: 12px 12px 8px; }
.ad-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; overflow: hidden; flex: none; }
.ad-avatar img { width: 100%; height: 100%; object-fit: cover; }
.ad-page { font-weight: 650; font-size: 13px; line-height: 1.2; }
.ad-sponsored { font-size: 12px; color: var(--muted); }
.ad-text { padding: 0 12px 10px; font-size: 13px; white-space: pre-wrap; word-break: break-word; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.ad-text.empty { color: var(--faint); font-style: italic; }
.ad-media { aspect-ratio: 1.91 / 1; background: var(--surface-3); display: grid; place-items: center; color: var(--faint); font-size: 12px; overflow: hidden; position: relative; }
.ad-media.square { aspect-ratio: 1 / 1; }
.ad-media img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ad-media .play { position: absolute; width: 48px; height: 48px; border-radius: 50%; background: rgba(0, 0, 0, .55); display: grid; place-items: center; }
.ad-media .play::after { content: ""; border-left: 14px solid #fff; border-top: 9px solid transparent; border-bottom: 9px solid transparent; margin-left: 4px; }
.ad-foot { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--surface-2); }
.ad-foot-text { flex: 1; min-width: 0; }
.ad-domain { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-headline { font-weight: 650; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-desc { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-cta { flex: none; padding: 6px 12px; border-radius: 6px; background: var(--surface-3); font-weight: 650; font-size: 12px; }
.ad-caption { margin-top: 8px; font-size: 12px; color: var(--faint); text-align: center; }

/* ---------- Checks ---------- */
.checks { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.checks li { display: grid; grid-template-columns: 20px 1fr; gap: 8px; padding: 6px 0; font-size: 13px; border-bottom: 1px solid var(--border); }
.checks li:last-child { border-bottom: 0; }
.checks .icon { width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center; font-size: 11px; font-weight: 800; margin-top: 1px; }
.checks .ok .icon { background: var(--ok-soft); color: var(--ok); }
.checks .fail .icon { background: var(--err-soft); color: var(--err); }
.checks .detail { display: block; color: var(--muted); font-size: 12px; }
.checks .fail .detail { color: var(--err); }

/* ---------- Result ---------- */
.result-head { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 12px; }
.result-icon { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; flex: none; }
.result-icon.ok { background: var(--ok-soft); color: var(--ok); }
.result-icon.err { background: var(--err-soft); color: var(--err); }
.result-icon.info { background: var(--accent-soft); color: var(--accent); }
.result-title { font-weight: 700; font-size: 14px; }
.result-sub { color: var(--muted); font-size: 12px; }
.result-list { margin: 0 0 12px; padding-left: 18px; display: grid; gap: 4px; font-size: 13px; }
.result-list.err li::marker { color: var(--err); }
.result-list.warn li::marker { color: var(--warn); }
.note { font-size: 12px; color: var(--muted); padding: 10px 12px; border-radius: var(--radius-sm); background: var(--surface-2); }
.note.warn { background: var(--warn-soft); color: var(--warn); }
.note.err { background: var(--err-soft); color: var(--err); }
.note + .note, .note + .kv, .kv + .note, .result-list + .note { margin-top: 10px; }

.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 12px; margin: 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; display: flex; gap: 6px; align-items: center; word-break: break-all; }
.kv code { background: var(--surface-2); padding: 1px 6px; border-radius: 4px; }

.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.steps li { border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.steps details > summary, .steps .step-line { display: flex; gap: 8px; align-items: center; padding: 7px 10px; font-size: 12px; cursor: pointer; list-style: none; }
.steps .step-line { cursor: default; }
.steps summary::-webkit-details-marker { display: none; }
.steps summary::after { content: "▸"; margin-left: auto; color: var(--faint); transition: transform .15s; }
.steps details[open] summary::after { transform: rotate(90deg); }
.method { font-family: var(--mono); font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; background: var(--surface-3); color: var(--muted); flex: none; }
.method.post { background: var(--accent-soft); color: var(--accent); }
.path { font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
pre.json { margin: 0; padding: 10px; background: var(--surface-2); border-top: 1px solid var(--border); overflow: auto; max-height: 280px; font-size: 11.5px; line-height: 1.45; }

.rollback { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 8px; }
.rollback th, .rollback td { text-align: left; padding: 5px 6px; border-bottom: 1px solid var(--border); }
.rollback th { color: var(--muted); font-weight: 600; }
.rollback td code { word-break: break-all; }

.busy { display: flex; gap: 12px; align-items: center; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2.5px solid var(--surface-3); border-top-color: var(--accent); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.result-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

/* ---------- History ---------- */
.history { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 2px; }
.history .h-empty { display: block; color: var(--faint); font-size: 12px; border: 0; padding: 0; }
.history li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; padding: 7px 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.history li:last-child { border-bottom: 0; }
.history .h-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history .h-meta { color: var(--faint); grid-column: 1; }
.history .badge { grid-row: 1; grid-column: 2; justify-self: end; }
.history a { grid-column: 2; justify-self: end; font-size: 12px; }

/* ---------- Dialogs ---------- */
.dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 22px; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(10, 14, 20, .45); backdrop-filter: blur(2px); }
.dialog h2 { font-size: 17px; }
.dialog .stack { margin-top: 0; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; width: min(420px, calc(100vw - 32px)); pointer-events: none; }
.toast {
  padding: 10px 14px; border-radius: 10px; font-size: 13px;
  background: var(--text); color: var(--bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
  animation: toast-in .2s ease-out;
}
.toast.err { background: var(--err); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---------- Language switch ---------- */
.lang-switch { display: inline-flex; padding: 2px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); flex: none; }
.lang-switch button {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  font: inherit; font-size: 11px; font-weight: 700; letter-spacing: .04em;
  padding: 4px 8px; border-radius: 6px;
}
.lang-switch button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.lang-switch button:focus-visible { outline: none; box-shadow: var(--focus); }

/* ---------- Account menu ---------- */
.account { position: relative; flex: none; }
.account-btn {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 4px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; font-weight: 600; font-size: 13px; cursor: pointer;
}
.account-btn:hover { background: var(--surface-2); }
.account-btn:focus-visible { outline: none; box-shadow: var(--focus); }
.avatar {
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 12px;
}
.account-name { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
  min-width: 220px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18);
  display: grid;
}
.menu-head { display: grid; gap: 2px; padding: 8px 10px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.menu-head span:not(.badge) { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
.menu-head .badge { justify-self: start; margin-top: 4px; }
.menu button[role="menuitem"] {
  text-align: left; padding: 8px 10px; border: 0; border-radius: 6px;
  background: transparent; color: var(--text); font: inherit; font-size: 13px; cursor: pointer;
}
.menu button[role="menuitem"]:hover, .menu button[role="menuitem"]:focus-visible { background: var(--surface-2); outline: none; }
.menu button.danger { color: var(--err); }

@media (max-width: 640px) {
  .account-name { display: none; }
  .account-btn { padding: 3px; }
}

/* ---------- Auth view ---------- */
.auth-view { min-height: calc(100vh - 60px); display: grid; place-items: center; padding: 32px 16px; }
.auth-card { width: min(400px, 100%); padding: 28px; }
.auth-card h1 { font-size: 22px; font-weight: 700; }
.auth-card .stack { margin-top: 0; }
.auth-foot { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.btn-block { width: 100%; min-height: 40px; }

/* ---------- Users dialog ---------- */
.dialog-wide { width: min(900px, calc(100vw - 32px)); }
.dialog-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 16px; }
.users-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
@media (max-width: 760px) { .users-layout { grid-template-columns: minmax(0, 1fr); } }
.user-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; max-height: 60vh; overflow: auto; }
.user-row { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; display: grid; gap: 6px; }
.user-row.is-disabled { opacity: .6; }
.user-top { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.user-top strong { font-size: 13px; }
.user-email { color: var(--muted); font-size: 12px; word-break: break-all; }
.user-meta { font-size: 12px; color: var(--faint); }
.user-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.user-form { padding: 16px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); }
.user-form input, .user-form select { background: var(--surface); }
.field-msg.issued { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding: 10px; border-radius: var(--radius-sm); background: var(--ok-soft); }
.field-msg.issued code { font-size: 13px; padding: 2px 6px; border-radius: 4px; background: var(--surface); color: var(--text); user-select: all; }

/* ---------- Combobox (suggestions) ---------- */
.combo { position: relative; min-width: 0; }
.combo.grow, .input-row > .combo { flex: 1; }
.combo-list {
  position: absolute; z-index: 40; left: 0; right: 0; top: calc(100% + 4px);
  max-height: 300px; overflow: auto; margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18);
}
.combo-item {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 9px; border-radius: 6px; cursor: pointer;
}
.combo-item:hover, .combo-item.active { background: var(--accent-soft); }
.combo-item.selected .combo-title::after { content: " \2713"; color: var(--accent); }
.combo-item.muted { opacity: .6; }
.combo-main { display: grid; min-width: 0; flex: 1; }
.combo-title { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.combo-sub { font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.combo-empty { padding: 8px 10px; font-size: 12px; color: var(--muted); }
.combo-empty.error { color: var(--err); }
.combo-empty.loading::before {
  content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 6px; vertical-align: -1px;
  border-radius: 50%; border: 2px solid var(--surface-3); border-top-color: var(--accent); animation: spin .8s linear infinite;
}

.chips-input {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  min-height: 40px; padding: 5px 8px;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  cursor: text; transition: border-color .15s, box-shadow .15s;
}
.chips-input:focus-within { border-color: var(--accent); box-shadow: var(--focus); }
.chips-input:has(.invalid) { border-color: var(--err); box-shadow: 0 0 0 3px var(--err-soft); }
.chips-input input:not([type="radio"]):not([type="checkbox"]):not([type="file"]) {
  flex: 1; min-width: 120px; width: auto; border: 0; padding: 4px 2px; box-shadow: none !important; background: transparent;
}
.chip-tag {
  display: inline-flex; align-items: center; gap: 4px; max-width: 100%;
  padding: 2px 4px 2px 9px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 600;
}
.chip-tag > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-x {
  width: 18px; height: 18px; border: 0; border-radius: 50%; padding: 0;
  background: transparent; color: inherit; cursor: pointer; font-size: 14px; line-height: 1;
}
.chip-x:hover { background: rgba(0, 0, 0, .08); }

.assets-foot { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }

/* ---------- View tabs ---------- */
.view-tabs { display: flex; gap: 2px; padding: 3px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); }
.view-tabs button {
  border: 0; background: transparent; color: var(--muted); cursor: pointer; white-space: nowrap;
  font: inherit; font-size: 13px; font-weight: 600; padding: 5px 12px; border-radius: 7px;
}
.view-tabs button[aria-current="page"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.view-tabs button:focus-visible { outline: none; box-shadow: var(--focus); }
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; }
  .view-tabs { order: 10; width: 100%; }
  .view-tabs button { flex: 1; }
}

/* ---------- Manage view ---------- */
.manage {
  display: grid; grid-template-columns: minmax(0, 1fr) 420px; gap: 20px; align-items: start;
  max-width: 1280px; margin: 0 auto; padding: 20px 24px 64px;
}
.manage:has(#mgDetail[hidden]) { grid-template-columns: minmax(0, 1fr); }
@media (max-width: 1000px) { .manage { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 640px) { .manage { padding: 16px 16px 48px; } }
.manage-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
.manage-controls { display: flex; gap: 8px; align-items: center; flex: 1; min-width: 260px; max-width: 520px; }
.manage-filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
input.manage-search { max-width: 260px; }
#mgMsg { margin-bottom: 8px; }
.camp-list { display: grid; gap: 8px; }
.empty-state { color: var(--muted); font-size: 13px; padding: 24px 0; text-align: center; }

select.manage-range { width: auto; }
/* All ad accounts overview (full width above the list and the detail panel). */
.mg-overview { grid-column: 1 / -1; display: grid; gap: 10px; }
.mg-overview:empty { display: none; }
.ov-head { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.ov-head h2 { margin: 0; font-size: 16px; }
.ov-head .sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.ov-actions { display: flex; gap: 6px; align-items: center; }
.ov-cur { display: grid; gap: 6px; }
.ov-cur h3 { margin: 4px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.ov-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.ov-table th { text-align: left; font-weight: 600; color: var(--muted); font-size: 11px; padding: 4px 6px; border-bottom: 1px solid var(--border); }
.ov-table td { padding: 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
.ov-table td.num, .ov-table th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ov-table tbody tr { cursor: pointer; }
.ov-table tbody tr:hover { background: var(--surface-2); }
.ov-table tr.is-current { background: var(--accent-soft); }
.ov-table .ov-name { max-width: 0; width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ov-table small { display: block; color: var(--muted); font-size: 11px; }
.ov-scroll { max-height: 320px; overflow: auto; }
/* Totals of the shown campaigns, above the list. */
.mg-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin: 0 0 4px; }
.mg-summary div { padding: 8px 10px; border-radius: var(--radius-sm); background: var(--surface-2); min-width: 0; }
.mg-summary dt { font-size: 11px; color: var(--muted); }
.mg-summary dd { margin: 0; font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums; }
.mg-summary small { display: block; font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.camp-row {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 8px 12px; align-items: center;
  padding: 12px 14px; border: 1px solid var(--border); border-left: 3px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); transition: border-color .15s, background .15s;
}
.camp-row.health-error { border-left-color: var(--err); }
.camp-row.health-review { border-left-color: var(--accent); }
.camp-row.selected { background: var(--accent-soft); border-color: var(--accent); }
.camp-row { cursor: pointer; }
.camp-row:hover:not(.selected) { border-color: var(--border-strong); background: var(--surface-2, var(--surface)); }
.camp-main { display: grid; min-width: 0; }
.camp-name {
  border: 0; padding: 0; background: none; cursor: pointer; text-align: left;
  font: inherit; font-weight: 650; font-size: 14px; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.camp-name:hover { color: var(--accent); text-decoration: underline; }
.camp-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.camp-badges { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.camp-nums { grid-column: 2 / -1; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.camp-num { display: grid; min-width: 0; }
.camp-num strong { font-size: 13px; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.camp-num small { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 760px) {
  .camp-row { grid-template-columns: auto minmax(0, 1fr) auto; }
  .camp-badges { grid-column: 2 / -1; justify-content: flex-start; }
  .camp-nums { grid-column: 1 / -1; grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Switch */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex: none; }
.switch input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; z-index: 1; }
.switch input:disabled { cursor: not-allowed; }
.switch-track { position: absolute; inset: 0; border-radius: 999px; background: var(--surface-3); border: 1px solid var(--border-strong); transition: background .15s; }
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .3); transition: transform .15s;
}
.switch input:checked + .switch-track { background: var(--ok); border-color: var(--ok); }
.switch input:checked + .switch-track::after { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { box-shadow: var(--focus); }
.switch input:disabled + .switch-track { opacity: .5; }

/* Detail panel */
.camp-detail { position: sticky; top: 72px; max-height: calc(100vh - 88px); overflow: auto; display: grid; gap: 4px; }
@media (max-width: 1000px) { .camp-detail { position: static; max-height: none; } }
.detail-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.detail-head h3 { font-size: 16px; }
.detail-block { padding: 14px 0; border-top: 1px solid var(--border); display: grid; gap: 8px; }
.detail-head + .detail-block { border-top: 0; }
.detail-block h4 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.status-line { display: flex; gap: 10px; align-items: center; }
.metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 0; }
.metrics div { padding: 8px 10px; border-radius: var(--radius-sm); background: var(--surface-2); }
.metrics dt { font-size: 11px; color: var(--muted); }
.metrics dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.budget-editor { display: grid; gap: 4px; }
.budget-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
input.budget-input { width: 110px !important; flex: none; }
.adset-budget { padding: 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); display: grid; gap: 8px; }
.adset-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; font-size: 13px; }
.problem-list, .reason-list, .ad-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.problem { padding: 10px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); border-left: 3px solid var(--accent); display: grid; gap: 6px; }
.problem.error { border-left-color: var(--err); background: var(--err-soft); }
.problem-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; }
.problem-head .level { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.problem-fix { font-size: 12.5px; }
.reason-list { gap: 4px; font-size: 12.5px; padding-left: 10px; border-left: 2px solid var(--border-strong); }
.ad-list li { display: flex; flex-wrap: wrap; gap: 8px 10px; align-items: center; font-size: 13px; }
.ad-list .row-actions { margin-left: auto; }
.ad-thumb { width: 36px; height: 36px; border-radius: 6px; overflow: hidden; background: var(--surface-3); flex: none; }
.ad-thumb img { width: 100%; height: 100%; object-fit: cover; }
.ad-name { flex: 1 1 140px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn-danger { background: var(--err); border-color: var(--err); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(1.08); }
#actionBody { display: grid; gap: 10px; font-size: 13px; }

/* Keep the top bar on one line: the connection text yields first. */
.conn { white-space: nowrap; }
@media (max-width: 1180px) { #connText { display: none; } }

/* ---------- Media library browser ---------- */
.library-browser { display: grid; gap: 8px; }
.library-label { font-size: 12px; color: var(--muted); white-space: nowrap; align-self: center; }
.library-search { width: 100%; }
/* Rows keep their natural height (thumbnail + name) and the grid scrolls; without max-content the
   overflow:hidden tiles let the grid squeeze every row into the max-height. */
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); grid-auto-rows: max-content; align-content: start; gap: 8px; max-height: 520px; overflow: auto; padding: 2px; }
.media-grid:empty { display: none; }
.media-tile {
  display: grid; gap: 4px; padding: 0 0 6px; text-align: left; cursor: pointer; overflow: hidden;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); font: inherit;
}
.media-tile:hover { border-color: var(--accent); }
.media-tile.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.media-tile:focus-visible { outline: none; box-shadow: var(--focus); }
.media-thumb { position: relative; aspect-ratio: 1 / 1; display: grid; place-items: center; background: var(--surface-3); font-size: 22px; overflow: hidden; }
.media-thumb img { width: 100%; height: 100%; object-fit: cover; }
.media-badge { position: absolute; right: 4px; bottom: 4px; padding: 1px 5px; border-radius: 4px; background: rgba(0, 0, 0, .65); color: #fff; font-size: 10px; font-weight: 700; }
.media-name { padding: 0 6px; font-size: 11.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media-meta { padding: 0 6px; font-size: 10.5px; color: var(--muted); }
.library-picked { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.library-picked:empty { display: none; }

.row-actions { display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.up-panel { display: grid; gap: 8px; padding: 10px; border: 1px dashed var(--border-strong); border-radius: var(--radius-sm); }
.up-links { width: 100%; font-size: 12.5px; }
.up-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: 12.5px; max-height: 260px; overflow: auto; }
.up-list li { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); gap: 10px; align-items: start; }
.up-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.up-status { color: var(--muted); text-align: right; overflow-wrap: anywhere; }
.up-status.ok { color: var(--ok); }
.up-status.err { color: var(--err); }
.video-check { display: flex; gap: 10px; align-items: center; font-size: 13px; padding: 8px; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.video-check img, .video-check .media-thumb { width: 96px; height: 54px; object-fit: cover; border-radius: 6px; flex: none; display: grid; place-items: center; background: var(--surface-3); }
.dup-count { display: flex; align-items: center; gap: 10px; font-weight: 600; }

/* ---------- Builder sections (campaign → ad set → ads) ---------- */
.sub-head { margin: 18px 0 8px; padding-top: 14px; border-top: 1px solid var(--border); font-size: 13px; font-weight: 600; color: var(--text); }
.stack > .sub-head:first-child { margin-top: 4px; padding-top: 0; border-top: 0; }
.choice-grid.two { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.check-list.inline { display: flex; flex-wrap: wrap; gap: 8px 18px; }
.platforms { margin-top: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

.ad-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.ad-tab { display: inline-flex; align-items: center; gap: 6px; max-width: 220px; padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface-2); color: var(--text); font: inherit; font-size: 13px; cursor: pointer; }
.ad-tab span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-tab:hover { border-color: var(--border-strong); }
.ad-tab[aria-selected="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.ad-tab.has-error::after { content: ''; width: 7px; height: 7px; border-radius: 50%; background: var(--err); flex: none; }
.ad-tab-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
