:root {
  --brand: #1309e6;
  --brand-ink: #0d06a8;
  --brand-soft: #eceafe;
  --grad: linear-gradient(135deg, #2b62eb 0%, #7a3beb 100%);
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f9fafc;
  --line: #e6e8f0;
  --line-strong: #d4d8e4;
  --text: #0f1222;
  --muted: #5d6478;
  --faint: #8c92a4;
  --up: #0f8a4f;
  --up-soft: #e5f6ed;
  --down: #c62f3a;
  --down-soft: #fdebec;
  --warn: #a35b00;
  --warn-soft: #fff4e2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 20, 50, 0.05), 0 4px 16px rgba(16, 20, 50, 0.05);
  --shadow-lg: 0 12px 48px rgba(16, 20, 50, 0.18);
  --font: Inter, ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --brand: #6b63ff;
    --brand-ink: #8f89ff;
    --brand-soft: #1f1d4a;
    --bg: #0d0f1a;
    --surface: #151826;
    --surface-2: #1a1e2f;
    --line: #262b3f;
    --line-strong: #343a52;
    --text: #eef0f8;
    --muted: #a3a9bd;
    --faint: #737a92;
    --up: #3ccf8a;
    --up-soft: #12301f;
    --down: #ff6b74;
    --down-soft: #3a1a1e;
    --warn: #f0b04f;
    --warn-soft: #3a2a12;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 12px 48px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 22px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }
p { margin: 0; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12.5px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack.tight { gap: 6px; }
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1320px; margin: 0 auto; padding: 0 16px;
  height: 58px; display: flex; align-items: center; gap: 20px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; background: var(--grad);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 15px;
}
.brand small { display: block; font-weight: 500; font-size: 11px; color: var(--faint); margin-top: -2px; }
.nav { display: flex; gap: 4px; }
.nav a {
  padding: 7px 12px; border-radius: 8px; color: var(--muted); font-weight: 550;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--brand-soft); color: var(--brand); }

.page { max-width: 1320px; margin: 0 auto; padding: 24px 16px 64px; }
.page-head { display: flex; align-items: flex-start; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .titles { flex: 1; min-width: 240px; }
.page-head .titles p { margin-top: 4px; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--text);
  font-weight: 600; font-size: 13.5px; cursor: pointer; white-space: nowrap;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: var(--surface-2); border-color: var(--faint); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-ink); border-color: var(--brand-ink); }
.btn.danger { color: var(--down); }
.btn.danger:hover { background: var(--down-soft); border-color: var(--down); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { height: 30px; padding: 0 10px; font-size: 12.5px; }
.btn svg { width: 16px; height: 16px; }

.input, .select, .textarea {
  width: 100%; height: 38px; padding: 0 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface);
  transition: border-color 0.12s, box-shadow 0.12s;
}
.textarea { height: auto; min-height: 120px; padding: 10px 12px; resize: vertical; line-height: 1.45; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .label { font-weight: 600; font-size: 13px; }
.field .hint { font-size: 12px; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
@media (max-width: 720px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 500; }
.check input { width: 16px; height: 16px; accent-color: var(--brand); margin: 0; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: transparent; padding: 0 12px; height: 32px; font-weight: 600; font-size: 12.5px; color: var(--muted); cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--brand-soft); color: var(--brand); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 12px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--surface); cursor: pointer; font-weight: 550; font-size: 12.5px;
}
.chip.on { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }

/* ---------- cards & kpis ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { padding: 14px 16px; }
.kpi .k-label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi .k-value { font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.kpi .k-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.project-card { padding: 18px; cursor: pointer; transition: border-color 0.12s, transform 0.12s; display: block; color: inherit; }
.project-card:hover { border-color: var(--brand); text-decoration: none; }
.project-card .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 16px; }
.project-card .stat-v { font-size: 18px; font-weight: 700; }
.project-card .stat-l { font-size: 11.5px; color: var(--muted); }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 18px; overflow-x: auto; }
.tabs button {
  border: 0; background: none; padding: 10px 14px; font-weight: 600; color: var(--muted); cursor: pointer;
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
table.data th {
  position: sticky; top: 0; background: var(--surface-2); text-align: left; font-weight: 600; color: var(--muted);
  font-size: 12px; padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; user-select: none; z-index: 1;
}
table.data th.sortable { cursor: pointer; }
table.data th.sortable:hover { color: var(--text); }
table.data th .arrow { font-size: 10px; margin-left: 3px; }
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr { cursor: pointer; }
table.data tbody tr:hover td { background: var(--surface-2); }
table.data tr.sel td { background: var(--brand-soft); }
table.data tr.paused td { opacity: 0.55; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.c, th.c { text-align: center; }
.kw-cell { min-width: 220px; max-width: 360px; }
.kw-text { font-weight: 600; }
.pos { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
.pos.none { color: var(--faint); font-weight: 600; font-size: 13px; }
.delta { font-weight: 650; font-size: 12.5px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }
.delta.flat { color: var(--faint); }
.spark { display: block; }
.spark path { fill: none; stroke: var(--brand); stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.spark circle { fill: var(--brand); }

/* ---------- badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 7px; border-radius: 999px;
  font-size: 11px; font-weight: 650; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap;
}
.badge.brand { background: var(--brand-soft); border-color: transparent; color: var(--brand); }
.badge.up { background: var(--up-soft); border-color: transparent; color: var(--up); }
.badge.down { background: var(--down-soft); border-color: transparent; color: var(--down); }
.badge.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.tagline { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

/* ---------- toolbar ---------- */
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.toolbar .input, .toolbar .select { height: 34px; width: auto; }
.toolbar .search { min-width: 220px; flex: 1; max-width: 340px; }
.bulkbar { display: flex; gap: 8px; align-items: center; padding: 8px 14px; background: var(--brand-soft); border-bottom: 1px solid var(--line); flex-wrap: wrap; }

/* ---------- modal & drawer ---------- */
.overlay { position: fixed; inset: 0; background: rgba(10, 12, 30, 0.42); z-index: 50; display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; overflow-y: auto; animation: fade 0.12s ease-out; }
.modal { background: var(--surface); border-radius: 16px; width: 100%; max-width: 620px; box-shadow: var(--shadow-lg); }
.modal.wide { max-width: 820px; }
.modal-head { display: flex; align-items: center; padding: 18px 20px 0; gap: 12px; }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 16px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 14px 20px; border-top: 1px solid var(--line); align-items: center; }
.drawer-overlay { position: fixed; inset: 0; background: rgba(10, 12, 30, 0.32); z-index: 40; animation: fade 0.12s ease-out; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(760px, 100vw); background: var(--surface); z-index: 41;
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column; animation: slide 0.16s ease-out;
}
.drawer-head { padding: 18px 20px; border-bottom: 1px solid var(--line); display: flex; gap: 12px; align-items: flex-start; }
.drawer-body { padding: 18px 20px 40px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 18px; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide { from { transform: translateX(24px); opacity: 0.6; } to { transform: none; opacity: 1; } }
.x { border: 0; background: transparent; cursor: pointer; padding: 6px; border-radius: 8px; color: var(--muted); line-height: 0; }
.x:hover { background: var(--surface-2); color: var(--text); }

/* ---------- misc ---------- */
.chart-box { position: relative; height: 260px; padding: 8px 14px 14px; }
.chart-box.sm { height: 200px; }
.empty { padding: 48px 20px; text-align: center; color: var(--muted); }
.empty h3 { color: var(--text); margin-bottom: 6px; font-size: 16px; }
.empty .btn { margin-top: 14px; }
.notice { padding: 12px 14px; border-radius: var(--radius-sm); background: var(--warn-soft); color: var(--warn); font-size: 13px; }
.notice.info { background: var(--brand-soft); color: var(--brand-ink); }
.notice.error { background: var(--down-soft); color: var(--down); }
.toast-wrap { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow-lg); font-weight: 550; max-width: 90vw; }
.toast.error { background: var(--down); color: #fff; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2.5px solid var(--line-strong); border-top-color: var(--brand); animation: spin 0.8s linear infinite; }
.loading { display: grid; place-items: center; padding: 60px; }
@keyframes spin { to { transform: rotate(360deg); } }
.list-box { border: 1px solid var(--line); border-radius: var(--radius-sm); max-height: 240px; overflow-y: auto; }
.list-item { padding: 8px 12px; cursor: pointer; display: flex; gap: 8px; align-items: center; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item:hover, .list-item.on { background: var(--brand-soft); }
.answer { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 14px; font-size: 13.5px; line-height: 1.6; max-height: 360px; overflow-y: auto; white-space: normal; word-wrap: break-word; }
.answer mark { background: #fff2a8; color: #111; padding: 0 2px; border-radius: 3px; }
.answer mark.comp { background: #e3e6ef; }
.engine-cell { min-width: 112px; }
.engine-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 16px; }
.meter { height: 6px; background: var(--line); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.meter > i { display: block; height: 100%; background: var(--grad); border-radius: 999px; }
.sov-row { display: grid; grid-template-columns: 180px 1fr 48px; gap: 10px; align-items: center; font-size: 13px; }
.sov-bar { height: 10px; background: var(--line); border-radius: 999px; overflow: hidden; }
.sov-bar > i { display: block; height: 100%; border-radius: 999px; }
.serp-list { display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; }
.serp-list > div { display: flex; gap: 10px; padding: 7px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
.serp-list > div:last-child { border-bottom: 0; }
.serp-list .me { background: var(--brand-soft); font-weight: 650; }
.serp-list .rival { background: var(--surface-2); }
.foot-note { text-align: center; color: var(--faint); font-size: 12px; margin-top: 28px; }
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; padding: 28px; }
.kv { display: grid; grid-template-columns: 180px 1fr; gap: 8px 16px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
@media (max-width: 720px) {
  .topbar-inner { gap: 10px; }
  .nav a { padding: 7px 9px; }
  .brand small { display: none; }
  .page { padding-top: 16px; }
  .sov-row { grid-template-columns: 110px 1fr 40px; }
  .kv { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .brand > span:not(.brand-mark) { display: none; }
}

/* ---------- print (client reports saved as PDF) ---------- */
@page { margin: 14mm 12mm; }
@media print {
  body { background: #fff; font-size: 12px; }
  .chart-box canvas { max-width: 100% !important; height: auto !important; }
  .topbar { position: static; border-bottom: 1px solid #ddd; }
  .nav, .topbar .btn, .page-head .btn, .page-head a.btn, .toolbar, .bulkbar, .seg, .x, .toast-wrap, input[type=checkbox] { display: none !important; }
  .page { padding: 12px 0; max-width: none; }
  .card { box-shadow: none; }
  .kpi, .chart-box, .engine-grid .card { break-inside: avoid; }
  .kpis { grid-template-columns: repeat(3, 1fr); }
  table.data th { position: static; }
  .table-wrap { overflow: visible; }
  table.data { font-size: 10.5px; }
  table.data th, table.data td { padding: 5px 6px; }
  .kw-cell { min-width: 0; }
  .pos { font-size: 12px; }
  .col-trend, .col-checked { display: none; }
  table.data tbody tr { break-inside: avoid; }
  .drawer, .drawer-overlay, .overlay { display: none !important; }
}
