:root {
  --bg: #0a0c0f;
  --surface: #12151a;
  --surface-2: #181c22;
  --line: #232830;
  --text: #f2f4f7;
  --muted: #8b94a3;
  --faint: #5c6573;
  --good: #3ddc97;
  --bad: #ff6b6b;
  --warn: #f5c451;
  --na: #6b7280;
  --accent: #3ddc97;
  --radius: 18px;
  --gutter: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "tnum" 1, "cv11" 1;
  min-height: 100dvh;
  overflow-x: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
.num { font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }

.app { max-width: 560px; margin: 0 auto; padding: calc(var(--safe-top) + 14px) var(--gutter) calc(var(--safe-bottom) + 96px); }

/* header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.brand { font-weight: 650; font-size: 15px; letter-spacing: 0.01em; color: var(--text); display: flex; align-items: center; gap: 8px; }
.brand-mark { width: 22px; height: 22px; border-radius: 7px; background: linear-gradient(135deg, #1f2a24, #0f1512); display: grid; place-items: center; border: 1px solid #243a2f; }
.brand-mark i { width: 10px; height: 10px; border-radius: 3px; background: var(--good); display: block; }
.health { display: inline-flex; align-items: center; gap: 7px; padding: 7px 12px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 12.5px; color: var(--muted); max-width: 62vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 34px; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--na); }
.dot.ok { background: var(--good); box-shadow: 0 0 0 3px rgba(61, 220, 151, 0.14); }
.dot.attention { background: var(--warn); box-shadow: 0 0 0 3px rgba(245, 196, 81, 0.14); }
.dot.problem { background: var(--bad); box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.14); }

/* period switch */
.periods { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; margin-bottom: 16px; }
.periods button { padding: 8px 0; border-radius: 9px; font-size: 13px; color: var(--muted); min-height: 36px; }
.periods button[aria-pressed="true"] { background: var(--surface-2); color: var(--text); font-weight: 600; box-shadow: 0 1px 0 rgba(255,255,255,0.04) inset; }

/* KPI grid */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.kpi { min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 14px 13px; text-align: left; min-height: 96px; display: flex; flex-direction: column; justify-content: space-between; transition: background 0.15s; }
.kpi:active { background: var(--surface-2); }
.kpi-label { font-size: 12px; color: var(--muted); letter-spacing: 0.02em; display: flex; justify-content: space-between; gap: 6px; }
.kpi-value { font-size: 27px; font-weight: 650; line-height: 1.1; margin-top: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-sub { font-size: 11.5px; color: var(--faint); margin-top: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi.small .kpi-value { font-size: 22px; }
.v-good { color: var(--good); }
.v-bad { color: var(--bad); }
.v-na { color: var(--na); }
.flag { font-size: 10.5px; padding: 1px 6px; border-radius: 6px; background: rgba(245, 196, 81, 0.12); color: var(--warn); }
.flag.na { background: rgba(107, 114, 128, 0.16); color: var(--muted); }

.section { margin-top: 26px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; margin: 0 2px 10px; }
.section-head h2 { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; margin: 0; }
.section-head span { font-size: 12px; color: var(--faint); }

/* funnel */
.funnel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 14px; }
.step { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 4px 12px; padding: 11px 0; border-bottom: 1px solid var(--line); text-align: left; width: 100%; }
.step:last-child { border-bottom: 0; }
.step-name { font-size: 14px; }
.step-val { font-size: 17px; font-weight: 620; }
.bar { grid-column: 1 / -1; height: 5px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 3px; background: linear-gradient(90deg, #2bb67c, var(--good)); }
.bar.na i { background: var(--na); opacity: 0.35; }
.step-rate { grid-column: 1 / -1; font-size: 11.5px; color: var(--faint); }

/* campaigns */
.camp { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 10px; width: 100%; text-align: left; display: block; }
.camp:active { background: var(--surface-2); }
.camp-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.camp-name { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip { font-size: 11px; padding: 3px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); flex: none; }
.chip.paused { color: var(--muted); }
.chip.active { color: var(--good); border-color: rgba(61,220,151,0.35); }
.chip.data_issue { color: var(--warn); border-color: rgba(245,196,81,0.35); }
.camp-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 12px; }
.camp-grid div { min-width: 0; }
.camp-grid small { display: block; font-size: 11px; color: var(--faint); }
.camp-grid b { display: block; font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.camp-tag { font-size: 11px; color: var(--faint); margin-top: 3px; }

/* detail */
.back { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; padding: 8px 0; min-height: 44px; }
.detail-title { font-size: 20px; font-weight: 650; margin: 2px 0 6px; line-height: 1.25; }
.note { font-size: 12.5px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 10px 0 0; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 10px; }
.grid3 .kpi { min-height: 78px; padding: 12px; }
.grid3 .kpi-value { font-size: 18px; }
.budget { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 14px; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); width: 100%; text-align: left; min-height: 44px; }
.row:last-child { border-bottom: 0; }
.row span { color: var(--muted); font-size: 13.5px; }
.row b { font-weight: 600; }
.row em { font-style: normal; font-size: 11px; color: var(--faint); display: block; }
.tabs { display: flex; gap: 6px; margin: 4px 0 10px; overflow-x: auto; scrollbar-width: none; }
.tabs button { padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); font-size: 13px; white-space: nowrap; min-height: 36px; }
.tabs button[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }
.table { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.trow { padding: 11px 14px; border-bottom: 1px solid var(--line); }
.trow:last-child { border-bottom: 0; }
.trow-top { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; }
.trow-top b { font-weight: 560; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.trow-top small { color: var(--faint); flex: none; font-size: 11.5px; }
.trow-meta { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.trow-meta i { font-style: normal; display: block; font-size: 10.5px; color: var(--faint); }
.empty { padding: 22px 14px; color: var(--muted); font-size: 13.5px; text-align: center; }

/* sheet */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,0.55); opacity: 0; pointer-events: none; transition: opacity 0.18s; z-index: 20; }
.scrim.on { opacity: 1; pointer-events: auto; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 86dvh; overflow-y: auto; background: #111418; border-top: 1px solid var(--line); border-radius: 22px 22px 0 0; padding: 10px var(--gutter) calc(var(--safe-bottom) + 22px); transform: translateY(102%); transition: transform 0.22s ease; z-index: 21; max-width: 560px; margin: 0 auto; overscroll-behavior: contain; }
.sheet.on { transform: none; }
.grab { width: 38px; height: 5px; border-radius: 3px; background: #2a3039; margin: 2px auto 14px; }
.sheet h3 { margin: 0; font-size: 13px; color: var(--muted); font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.sheet .big { font-size: 34px; font-weight: 680; margin: 6px 0 4px; }
.sheet .why { font-size: 13px; color: var(--warn); margin: 0 0 10px; }
.kv { border-top: 1px solid var(--line); margin-top: 10px; }
.kv div { display: grid; grid-template-columns: 112px 1fr; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 11.5px; color: var(--muted); }
.close { position: sticky; top: 0; float: right; color: var(--muted); font-size: 14px; padding: 6px 2px 6px 12px; min-height: 36px; }

/* bottom nav */
.nav { position: fixed; left: 0; right: 0; bottom: 0; background: rgba(10,12,15,0.86); backdrop-filter: saturate(160%) blur(18px); -webkit-backdrop-filter: saturate(160%) blur(18px); border-top: 1px solid var(--line); padding: 6px 0 calc(var(--safe-bottom) + 6px); z-index: 10; }
.nav div { max-width: 560px; margin: 0 auto; display: grid; grid-template-columns: repeat(3, 1fr); }
.nav button { padding: 8px 0 4px; font-size: 11px; color: var(--faint); display: flex; flex-direction: column; align-items: center; gap: 3px; min-height: 48px; }
.nav button[aria-current="page"] { color: var(--text); }
.nav svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.foot { text-align: center; color: var(--faint); font-size: 11.5px; margin-top: 26px; line-height: 1.6; }
.state { text-align: center; color: var(--muted); padding: 60px 20px; }
.src { display: flex; align-items: center; gap: 10px; }
.skeleton { background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: var(--radius); height: 96px; }
@keyframes sk { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .sheet, .scrim { transition: none; } .skeleton { animation: none; } }
@media (min-width: 720px) { .app { padding-top: 32px; } }

/* login */
.login { max-width: 380px; margin: 0 auto; padding: calc(var(--safe-top) + 12vh) var(--gutter) 40px; }
.login h1 { font-size: 24px; margin: 18px 0 6px; font-weight: 680; }
.login p { color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.login input { width: 100%; font: 17px/1.3 inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px 14px; outline: none; }
.login input:focus { border-color: #3a4350; }
.login .go { width: 100%; margin-top: 12px; background: var(--text); color: var(--bg); font-weight: 650; border-radius: 14px; padding: 14px; font-size: 16px; min-height: 50px; }
.login .err { color: var(--bad); font-size: 13px; min-height: 20px; margin-top: 10px; }
