:root {
  color-scheme: light;
  --bg: #f4f4f2; --surface: #fcfcfb; --surface-2: #efeeea; --line: #e2e1dc;
  --text: #0b0b0b; --text-2: #52514e; --muted: #7a7974;
  --accent: #2a78d6; --accent-ink: #fff; --series: #2a78d6;
  --good: #0f7b3e; --warn: #a86400; --bad: #c62828; --good-bg: #e3f3e8; --warn-bg: #fdf0d9; --bad-bg: #fbe4e4;
  --grid: #e8e7e2;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --line: #2f2f2c;
    --text: #f0efec; --text-2: #c3c2b7; --muted: #8f8e86;
    --accent: #3987e5; --series: #3987e5;
    --good: #5fcf8a; --warn: #f0b44c; --bad: #f07a7a; --good-bg: #16301f; --warn-bg: #33270f; --bad-bg: #3a1818;
    --grid: #2a2a27;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 10px; }
code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; font-size: 12px; }
.muted { color: var(--muted); } .small { font-size: 12px; font-weight: 400; }
.hidden { display: none !important; }

.top { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; padding: 12px 20px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--warn-bg); color: var(--warn); font-weight: 600; }
.badge.live { background: var(--good-bg); color: var(--good); }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button, .seg button { border: 0; background: none; color: var(--text-2); padding: 6px 12px; border-radius: 8px; cursor: pointer; font: inherit; }
.tabs button.on, .seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }
.sync { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 12px; }

.btn { border: 1px solid var(--line); background: var(--surface); color: var(--text); padding: 6px 12px; border-radius: 8px; cursor: pointer; font: inherit; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: wait; }

.range { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 20px; }
.seg { display: flex; gap: 2px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 3px; flex-wrap: wrap; }
.custom { display: flex; gap: 6px; align-items: center; }
input, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
.banner { margin: 0 20px 10px; padding: 10px 14px; border-radius: 10px; background: var(--warn-bg); color: var(--warn); }
.banner.bad { background: var(--bad-bg); color: var(--bad); }

main { padding: 0 20px 40px; max-width: 1400px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 16px; min-width: 0; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 16px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 18px 0 10px; }
.section-head h2 { margin: 0; }
.card .section-head { margin-top: 0; }

.acct { display: flex; gap: 24px; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.acct div b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.acct div span { color: var(--muted); font-size: 12px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.kpi .l { color: var(--text-2); font-size: 12px; }
.kpi .v { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; margin: 2px 0; }
.kpi .d { font-size: 12px; color: var(--muted); }

.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 420px), 1fr)); gap: 12px; }
.chart { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.chart .l { font-size: 13px; font-weight: 600; } .chart .s { font-size: 12px; color: var(--muted); }
.chart .box { position: relative; height: 180px; margin-top: 6px; }

.picker { position: relative; }
.picker summary { cursor: pointer; color: var(--accent); font-size: 13px; list-style: none; }
.picker[open] > div { position: absolute; right: 0; z-index: 4; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px; min-width: 220px; max-height: 340px; overflow: auto; box-shadow: 0 8px 24px rgba(0,0,0,.12); }
.picker label { display: flex; gap: 8px; align-items: center; padding: 3px 0; white-space: nowrap; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 6px 0 12px; }
.toolbar input { flex: 1; min-width: 160px; max-width: 320px; }
.viewpick { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.viewpick select { min-width: 240px; max-width: 100%; font-weight: 400; }
.acct .btn { align-self: center; margin-left: auto; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 12px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 12px; color: var(--text-2); font-weight: 600; cursor: pointer; user-select: none; position: sticky; top: 0; background: var(--surface); }
th:first-child, td:first-child { text-align: left; }
tbody tr { cursor: pointer; } tbody tr:hover { background: var(--surface-2); }
td .sub { display: block; font-size: 11px; color: var(--muted); }
.st { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); }
.st.active { background: var(--good-bg); color: var(--good); }
.st.declined { background: var(--bad-bg); color: var(--bad); }
.st.on_hold, .st.in_review, .st.ready_for_review { background: var(--warn-bg); color: var(--warn); }

.form label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; color: var(--text-2); }
.form label.check { flex-direction: row; align-items: center; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > label { flex: 1; min-width: 120px; }

.list-item { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item .t { font-weight: 600; } .list-item .x { color: var(--muted); font-size: 12px; }
.list-item .acts { display: flex; gap: 6px; flex-shrink: 0; }
.ev { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.ev .when { color: var(--muted); font-size: 12px; }
.ok { color: var(--good); } .err { color: var(--bad); }

dialog { width: min(1000px, 96vw); max-height: 92vh; border: 1px solid var(--line); border-radius: 14px; background: var(--bg); color: var(--text); padding: 18px; }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.dlg-head h2 { margin: 0; font-size: 18px; }
.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }
.facts div { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
.facts span { font-size: 12px; color: var(--muted); display: block; } .facts b { font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.actions { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin-bottom: 12px; }
.actions .card { margin: 0; padding: 12px; }
.actions .row input { width: 100%; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 20; max-width: 90vw; }
.empty { color: var(--muted); padding: 12px 0; }

@media (max-width: 800px) {
  .grid2 { grid-template-columns: 1fr; }
  .top, .range { padding-left: 16px; padding-right: 16px; }
  main { padding: 0 16px 40px; }
  .sync { margin-left: 0; }
}

#tab-alerts, #tab-metrics, #tab-settings { padding-top: 16px; }
#thrUnit { display: inline; }
.sub.auto { color: var(--good); }
.topup .check { margin-bottom: 6px; }

/* live alert pop-ups and save messages; popover keeps them above the ad controls window */
.toast[popover] { inset: auto auto 20px 50%; margin: 0; border: 0; }
.toast.ok { background: var(--good); color: #fff; } .toast.err { background: var(--bad); color: #fff; }
.notes[popover] { inset: 64px 16px auto auto; margin: 0; border: 0; padding: 0; background: transparent; overflow: visible;
  width: min(380px, calc(100vw - 32px)); }
.notes { display: none; flex-direction: column; gap: 8px; }
.notes:popover-open { display: flex; }
.note { background: var(--surface); color: var(--text); border: 1px solid var(--line); border-left: 4px solid var(--accent);
  border-radius: 10px; padding: 10px 12px; box-shadow: 0 6px 20px rgba(0,0,0,.25); white-space: pre-line; font-size: 13px;
  animation: noteIn .2s ease-out; }
.note.good { border-left-color: var(--good); } .note.bad { border-left-color: var(--bad); } .note.warn { border-left-color: var(--warn); }
.note .x { float: right; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; padding: 0 0 0 8px; }
.note .when { color: var(--muted); font-size: 11px; margin-top: 4px; }
@keyframes noteIn { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }
#bellCount { background: var(--bad); color: #fff; border-radius: 99px; padding: 0 6px; font-size: 11px; margin-left: 4px; }

/* scheduled pause / resume */
.sched .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.sched .row label { flex: 1 1 130px; }
.sched .row .btn { flex: 0 0 auto; }
.sched-list { margin-top: 10px; }
.sched-item { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); align-items: center; }
.sched-item .x { color: var(--muted); font-size: 12px; }
.pill { font-size: 11px; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); font-weight: 600; }
.pill.pause { background: var(--warn-bg); color: var(--warn); } .pill.resume { background: var(--good-bg); color: var(--good); }
#tab-ads > #schedAll { margin-bottom: 12px; }
.sched .row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); min-width: 0; }
.sched .row label select, .sched .row label input { width: 100%; box-sizing: border-box; }
.sched .row label:has(.sAd) { flex: 2 1 240px; }

/* bulk select on the Ads table */
.bulk { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 8px 12px; margin-bottom: 10px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; }
.bulk b { margin-right: 6px; }
#adsTable th.ck, #adsTable td.ck { width: 28px; padding-right: 0; text-align: left; cursor: default; }
#adsTable th.ck + th, #adsTable td.ck + td { text-align: left; }
#adsTable tr.sel td { background: var(--surface-2); }
#adsTable input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }

/* deleted ads and the delete / copy controls */
.st.deleted { background: var(--surface-2); color: var(--muted); text-decoration: line-through; }
#adsTable tr.gone td { opacity: .6; }
.life { margin: 12px 0; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; }
.life .msg { flex: 1 1 300px; }
.life.warn { border-left: 4px solid var(--warn); } .life.gone { border-left: 4px solid var(--muted); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.dup .row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
.dup label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
.dup textarea { width: 100%; box-sizing: border-box; min-height: 64px; font: inherit; padding: 8px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); }
.dup .tgt { font-size: 12px; color: var(--text-2); background: var(--surface-2); border-radius: 8px; padding: 8px 10px; margin: 8px 0; }
.dup img { max-width: 160px; border-radius: 8px; display: block; margin: 6px 0; }
.dup label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 13px; color: var(--text); }

/* auto replace */
.rot .row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.rot label:not(.check) { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
.rot .steps { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; }
.rot .job { padding: 6px 0; border-bottom: 1px solid var(--line); }
.rot .job:last-child { border-bottom: 0; }
.logline { font-size: 12px; padding: 3px 0; display: grid; grid-template-columns: 170px 1fr; gap: 10px; }
.logline .when { color: var(--muted); }
.pill.state-done { background: var(--good-bg); color: var(--good); } .pill.state-failed, .pill.state-cancelled { background: var(--bad-bg); color: var(--bad); }
@media (max-width: 600px) { .logline { grid-template-columns: 1fr; gap: 0; } }

/* safety stop banner and self-healing log */
.banner.bad { background: var(--bad-bg); color: var(--bad); border: 1px solid var(--bad); font-weight: 600; }
.banner.bad .btn { margin-left: 10px; }
.hl { font-size: 12px; display: grid; grid-template-columns: 170px 110px 1fr; gap: 10px; padding: 3px 0; border-bottom: 1px solid var(--line); }
.hl .lv { font-weight: 600; } .hl.problem .lv, .hl.fail .lv, .hl.error .lv, .hl.escalate .lv { color: var(--bad); }
.hl.heal .lv { color: var(--warn); } .hl.recovered .lv, .hl.ok .lv { color: var(--good); }
.loops { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
@media (max-width: 600px) { .hl { grid-template-columns: 1fr; gap: 0; } }

/* ---------- scheduled pause / resume: small accordion */
.sched-acc > summary { list-style: none; cursor: pointer; display: flex; flex-direction: column; gap: 2px; position: relative; padding-right: 28px; }
.sched-acc > summary::-webkit-details-marker { display: none; }
.sched-acc > summary::after { content: "▾"; position: absolute; right: 2px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 16px; transition: transform .15s; }
.sched-acc[open] > summary::after { transform: translateY(-50%) rotate(180deg); }
.sched-acc .acc-t { font-weight: 600; font-size: 15px; }
.sched-acc .acc-s { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sched-acc .acc-body { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
#schedAll { padding: 12px 16px; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- phone header: one compact row of icons, scrollable tabs, one-line status */
@media (max-width: 800px) {
  .top { display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto; gap: 4px 6px; align-items: center; padding: 10px 16px 0; }
  .brand { grid-row: 1; grid-column: 1; min-width: 0; gap: 6px; }
  #bellBtn { grid-column: 2; } #syncBtn { grid-column: 3; } #logoutBtn { grid-column: 4; }
  .brand h1 { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .brand .badge { font-size: 10px; padding: 1px 6px; white-space: nowrap; }
  .sync { display: contents; }
  #bellBtn, #syncBtn, #logoutBtn { grid-row: 1; width: 38px; height: 38px; padding: 0; display: grid; place-items: center; border-radius: 10px; position: relative; font-size: 16px; }
  #syncBtn, #logoutBtn { font-size: 0; }
  #syncBtn::before { content: "↻"; font-size: 20px; line-height: 1; }
  #logoutBtn::before { content: "⏻"; font-size: 17px; line-height: 1; color: var(--text-2); }
  #syncBtn:disabled::before, #syncBtn.busy::before { animation: spin 1s linear infinite; }
  #syncBtn:disabled { opacity: 1; }
  #bellCount { position: absolute; top: -5px; right: -5px; margin: 0; min-width: 16px; line-height: 16px; padding: 0 4px; font-size: 10px; box-sizing: border-box; text-align: center; }
  #lastSync { grid-row: 2; grid-column: 1 / -1; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .tabs { grid-row: 3; grid-column: 1 / -1; flex-wrap: nowrap; gap: 0; overflow-x: auto; scrollbar-width: none; margin: 2px -16px 0; padding: 0 8px; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { flex: 1 0 auto; padding: 10px 10px 9px; border-radius: 0; white-space: nowrap; border-bottom: 2px solid transparent; }
  .tabs button.on { background: none; color: var(--text); border-bottom-color: var(--accent); }

  .range { flex-wrap: nowrap; flex-direction: column; align-items: stretch; gap: 4px; padding-top: 10px; padding-bottom: 6px; }
  .seg { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .seg::-webkit-scrollbar { display: none; }
  .seg button { flex: 1 0 auto; padding: 6px 10px; white-space: nowrap; font-size: 13px; }
  #rangeLabel { font-size: 11px; }
  .custom { flex-wrap: wrap; }
  .banner { margin: 0 16px 10px; }
}
