/* 廣告成效總覽 — 版面與圖表樣式
   色彩一律走 token；深色模式是另一組選定的值，不是自動反轉 */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #74726c;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --wash: rgba(11, 11, 11, 0.05);
  --error-bg: #fbecec;
  --error-ink: #8f2323;
  --note-bg: #f3f2ee;

  /* 平台顏色固定跟著平台走，不因篩選改色 */
  --pub-facebook: #2a78d6;
  --pub-instagram: #eb6834;
  --pub-google: #1baf7a;
  --pub-audience_network: #eda100;
  --pub-messenger: #e87ba4;

  --radius: 12px;
  --font: system-ui, -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #222221;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #9a988f;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-ink: #0b0b0b;
    --wash: rgba(255, 255, 255, 0.06);
    --error-bg: #3a1c1c;
    --error-ink: #f2b8b8;
    --note-bg: #222221;
    --pub-facebook: #3987e5;
    --pub-instagram: #d95926;
    --pub-google: #199e70;
    --pub-audience_network: #c98500;
    --pub-messenger: #d55181;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #222221;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #9a988f;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-ink: #0b0b0b;
  --wash: rgba(255, 255, 255, 0.06);
  --error-bg: #3a1c1c;
  --error-ink: #f2b8b8;
  --note-bg: #222221;
  --pub-facebook: #3987e5;
  --pub-instagram: #d95926;
  --pub-google: #199e70;
  --pub-audience_network: #c98500;
  --pub-messenger: #d55181;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
}
.page {
  max-width: 1240px;
  margin: 0 auto;
  padding: 20px 16px calc(32px + env(safe-area-inset-bottom));
}
h1, h2 { margin: 0; font-weight: 650; letter-spacing: 0.01em; }
h1 { font-size: 22px; }
h1 .brand { display: block; font-size: 13px; font-weight: 500; color: var(--ink-2); letter-spacing: 0.08em; margin-bottom: 2px; }
h2 { font-size: 16px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.num { font-variant-numeric: tabular-nums; }

/* ---------- 頂部 ---------- */
.top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.updated { margin: 4px 0 0; color: var(--ink-2); font-size: 13px; }

/* ---------- 按鈕與分段控制 ---------- */
.btn {
  font: inherit; font-size: 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  border-radius: 8px; padding: 7px 14px; cursor: pointer; min-height: 36px;
}
.btn:hover { background: var(--wash); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.5; cursor: progress; }
button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.seg {
  display: inline-flex; flex-wrap: wrap; gap: 2px; padding: 3px;
  background: var(--surface-2); border-radius: 10px; border: 1px solid var(--border);
}
.seg button {
  font: inherit; font-size: 14px; border: 0; background: transparent; color: var(--ink-2);
  padding: 6px 12px; border-radius: 7px; cursor: pointer; min-height: 34px;
}
.seg button:hover { background: var(--wash); color: var(--ink); }
.seg button[aria-pressed="true"] {
  background: var(--surface); color: var(--ink); font-weight: 600;
  box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgba(0, 0, 0, 0.06);
}
.seg.small button { font-size: 13px; padding: 4px 10px; min-height: 30px; }

/* ---------- 篩選列 ---------- */
.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  margin: 18px 0 14px;
}
.custom-range { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 14px; color: var(--ink-2); }
.custom-range input {
  font: inherit; font-size: 14px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; min-height: 34px;
}
.range-label { margin: 0; flex-basis: 100%; color: var(--muted); font-size: 13px; }
@media (max-width: 640px) {
  .filters .seg { width: 100%; }
  .filters .seg button { flex: 1 1 auto; padding: 6px 8px; }
}

/* ---------- 提示 ---------- */
.notice { border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; font-size: 14px; }
.notice.error { background: var(--error-bg); color: var(--error-ink); }
.notes { display: grid; gap: 8px; margin-bottom: 14px; }
.notes:empty { display: none; }
.notes p {
  margin: 0; padding: 10px 14px; border-radius: 10px; background: var(--note-bg);
  color: var(--ink-2); font-size: 13.5px;
}

/* 重新讀取時保留畫面、降低透明度，不閃爍 */
#content { transition: opacity 0.15s; }
#content.loading { opacity: 0.55; pointer-events: none; }

/* ---------- KPI 卡片：手機兩欄、桌機一列 ---------- */
.kpis {
  display: grid; gap: 10px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 720px) { .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (min-width: 1100px) { .kpis { grid-template-columns: repeat(7, minmax(0, 1fr)); } }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; min-width: 0;
}
.kpi-label { color: var(--ink-2); font-size: 13px; }
.kpi-value { font-size: 22px; font-weight: 650; margin-top: 2px; overflow-wrap: anywhere; line-height: 1.25; }
.kpi-delta { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.kpi-delta b { color: var(--ink-2); font-weight: 600; }
.kpi:first-child .kpi-value { font-size: 24px; }

/* ---------- 卡片 ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-top: 14px;
}
.card-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 8px 12px; margin-bottom: 12px;
}
.card-sub { margin: 0; color: var(--muted); font-size: 13px; }
.empty { color: var(--muted); font-size: 14px; padding: 18px 0; text-align: center; }

/* ---------- 平台比較 ---------- */
.pubs { display: grid; gap: 10px; }
@media (min-width: 720px) { .pubs { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); } }
.pub {
  border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; background: var(--surface);
}
.pub-name { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.pub-share { height: 6px; background: var(--surface-2); border-radius: 3px; margin: 10px 0 8px; overflow: hidden; }
.pub-share i { display: block; height: 100%; border-radius: 3px; }
.pub dl { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px 10px; margin: 0; }
.pub dt { color: var(--muted); font-size: 12px; }
.pub dd { margin: 0; font-weight: 600; font-size: 15px; }

/* ---------- 趨勢圖 ---------- */
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-bottom: 6px; font-size: 13px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 2px; border-radius: 1px; display: inline-block; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; touch-action: pan-y; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .series { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .endlabel { font-size: 11px; fill: var(--ink-2); }
.chart .crosshair { stroke: var(--ink-2); stroke-width: 1; stroke-dasharray: 2 3; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }

.tooltip {
  position: fixed; z-index: 20; pointer-events: none;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 13px; min-width: 150px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
}
.tooltip .tt-date { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 8px; }
.tooltip .tt-row i { width: 12px; height: 2px; border-radius: 1px; flex: none; }
.tooltip .tt-row b { font-variant-numeric: tabular-nums; }
.tooltip .tt-row span { color: var(--ink-2); }

/* ---------- 廣告活動：桌機表格、手機卡片 ---------- */
.table-wrap { overflow: visible; }
table.camps { width: 100%; border-collapse: collapse; font-size: 14px; }
.camps th {
  text-align: right; font-weight: 500; color: var(--muted); font-size: 12.5px;
  padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap;
}
.camps th:first-child, .camps td:first-child { text-align: left; }
.camps td { text-align: right; padding: 10px; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
.camps tr:last-child td { border-bottom: 0; }
.camps .c-name { font-weight: 600; overflow-wrap: anywhere; }
.camps .c-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
/* 方案二：每列操作區（調預算 / 開關），本期隱藏 */
.camps .c-actions { display: none; }

.tag {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--ink-2);
  background: var(--surface-2); border-radius: 6px; padding: 1px 7px; font-weight: 500;
}
.tag .swatch { width: 8px; height: 8px; border-radius: 2px; }
.tag.status-off { color: var(--muted); }

@media (max-width: 760px) {
  table.camps, .camps tbody, .camps tr, .camps td { display: block; width: 100%; }
  .camps thead { display: none; }
  .camps tr {
    border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px 10px;
  }
  .camps td { border: 0; padding: 0; text-align: left; }
  .camps td:first-child { grid-column: 1 / -1; }
  .camps td[data-label]::before {
    content: attr(data-label); display: block; color: var(--muted); font-size: 12px; font-weight: 400;
  }
  .camps td[data-label] { font-weight: 600; }
}

/* ---------- 頁尾 ---------- */
.foot { margin-top: 22px; color: var(--muted); font-size: 13px; }
.foot p { margin: 4px 0; }

/* ---------- 設定視窗 ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--ink);
  padding: 20px; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 48px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog h2 { margin-bottom: 14px; font-size: 18px; }
.field { display: grid; gap: 4px; margin: 0 0 14px; border: 0; padding: 0; }
.field > span, .field legend { font-size: 14px; color: var(--ink-2); padding: 0; }
.field input[type="number"] {
  font: inherit; color: var(--ink); background: var(--page);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-height: 40px;
}
.hint { color: var(--muted); font-size: 12.5px; margin: 2px 0 6px; }
.action-types { display: grid; gap: 4px; max-height: 220px; overflow: auto; }
.action-types label { display: flex; align-items: center; gap: 8px; font-size: 13.5px; overflow-wrap: anywhere; }
.action-types .n { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 6px; }
.dialog-actions .spacer { flex: 1; }
.alert-log { margin-top: 12px; font-size: 13px; color: var(--ink-2); }
.alert-log ol { padding-left: 18px; }
.alert-log li { margin: 6px 0; white-space: pre-wrap; overflow-wrap: anywhere; }

.sort { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); }
.sort select {
  font: inherit; font-size: 14px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; min-height: 34px;
}
