:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-alt: #f0f1f4;
  --border: #e1e3e8;
  --text: #1c1f26;
  --text-dim: #6b7280;
  --accent: #ff3d00;
  --accent-dim: #ffe4d9;
  --success: #1a9e5c;
  --danger: #d63636;
  --shadow: 0 1px 2px rgba(16,20,30,.06), 0 1px 1px rgba(16,20,30,.04);
  --radius: 10px;
}

[data-theme="dark"] {
  --bg: #12141a;
  --surface: #1a1d25;
  --surface-alt: #20242e;
  --border: #2b2f3a;
  --text: #e7e9ee;
  --text-dim: #9aa0ac;
  --accent: #ff5722;
  --accent-dim: #3a2018;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px;
  background: var(--accent); color: #fff; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.brand-title { font-weight: 700; font-size: 14px; }
.brand-sub { font-size: 11px; color: var(--text-dim); }

.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav-item {
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
}
.nav-item:hover { background: var(--surface-alt); color: var(--text); }
.nav-item.active { background: var(--accent-dim); color: var(--accent); font-weight: 600; }

.sidebar-footer { padding-top: 16px; border-top: 1px solid var(--border); }

.main { flex: 1; padding: 24px 28px; max-width: 100%; overflow-x: hidden; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px; flex-wrap: wrap; gap: 10px;
}
.topbar-title { font-size: 20px; font-weight: 700; }
.topbar-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.tab { display: none; }
.tab.active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 18px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-head h3 { margin: 0; font-size: 15px; }

.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.kpi-label { font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.kpi-value { font-size: 24px; font-weight: 700; }
.kpi-sub { font-size: 11px; color: var(--text-dim); margin-top: 4px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table-wrap { overflow-x: auto; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { color: var(--text-dim); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
th.th-sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.th-sortable:hover { color: var(--text); }
tbody tr:hover { background: var(--surface-alt); }
tfoot td { font-weight: 700; border-top: 2px solid var(--border); }

input, select {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 13px;
}
select[multiple] { min-height: 34px; }

.btn {
  border: none; border-radius: 7px; padding: 9px 14px; font-size: 13px;
  cursor: pointer; font-weight: 600;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--text); color: var(--bg); }
.btn-ghost { background: var(--surface-alt); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.field { margin-bottom: 14px; }
.field-label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; cursor: pointer; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-top: 8px; }
.hidden { display: none !important; }

.check-list {
  max-height: 200px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: 8px; padding: 8px;
  background: var(--surface-alt);
}
.check-list .group-title { font-size: 11px; color: var(--text-dim); margin: 8px 0 4px; text-transform: uppercase; }
.check-list label { display: flex; align-items: center; gap: 6px; padding: 3px 0; font-size: 13px; cursor: pointer; }

.report-layout { display: grid; grid-template-columns: 340px 1fr; gap: 18px; align-items: start; }
.report-config { position: sticky; top: 24px; }
@media (max-width: 980px) { .report-layout { grid-template-columns: 1fr; } .report-config { position: static; } }

.grid-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 18px; margin-bottom: 18px; }
.grid-row > .card { margin-bottom: 0; }

.filter-row { display: grid; grid-template-columns: 1.3fr .8fr 1fr auto; gap: 6px; margin-bottom: 6px; }

.preset-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.preset-item {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface-alt); border: 1px solid var(--border); border-radius: 7px; padding: 6px 10px; font-size: 12px;
}

.pager { display: flex; align-items: center; gap: 12px; justify-content: center; }

.hint { font-size: 12px; color: var(--text-dim); margin: -4px 0 10px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.badge-enabled { background: #dcf5e5; color: #178a4e; }
.badge-archived { background: #ececec; color: #666; }
.badge-unknown { background: #fdecc8; color: #92650a; }
[data-theme="dark"] .badge-enabled { background: #113224; color: #4ade80; }
[data-theme="dark"] .badge-archived { background: #262a33; color: #9aa0ac; }
[data-theme="dark"] .badge-unknown { background: #3a2c10; color: #fbbf24; }

.toast-host { position: fixed; top: 16px; right: 16px; z-index: 999; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 8px; padding: 10px 14px; font-size: 13px; box-shadow: var(--shadow); min-width: 220px;
  animation: slidein .15s ease-out;
}
.toast.error { border-left-color: var(--danger); }
.toast.success { border-left-color: var(--success); }
@keyframes slidein { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10,12,16,.5);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  width: 460px; max-width: 92vw; max-height: 86vh; overflow-y: auto; padding: 18px;
  box-shadow: 0 12px 30px rgba(0,0,0,.25);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-body .field { margin-bottom: 12px; }

.actions { display: flex; gap: 8px; margin-top: 6px; }

.muted { color: var(--text-dim); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }

.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 12px; font-weight: 600; padding: 0; }

tr.dr-campaign-row { cursor: pointer; }
.dr-detail-row td { background: var(--surface-alt); padding: 14px 16px; white-space: normal; }
.dr-adgroup { margin-bottom: 16px; }
.dr-adgroup:last-child { margin-bottom: 0; }
.dr-adgroup-title { font-weight: 600; font-size: 13px; margin-bottom: 8px; }
.dr-bid-input { width: 90px; padding: 5px 7px; font-size: 12px; }
.dr-ads { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.dr-ad-card { border: 1px solid var(--border); border-radius: 7px; padding: 8px 12px; font-size: 12px; }
.dr-ad-title { font-weight: 600; margin-bottom: 2px; }
.dr-ad-text { color: var(--text-dim); }

.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); padding: 20px; }
.login-card { width: 360px; max-width: 100%; }
.login-card input { width: 100%; }
.login-card .btn-block { margin-top: 6px; }

/* ---------------- Responsive (tablet / phone) ---------------- */

@media (max-width: 900px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 0; z-index: 20;
    flex-direction: row; align-items: center; gap: 12px;
    padding: 10px 14px; overflow-x: auto;
  }
  .brand { padding: 0; flex-shrink: 0; }
  .brand-sub { display: none; }
  .nav { flex-direction: row; flex: 1 1 auto; overflow-x: auto; gap: 4px; }
  .nav-item { white-space: nowrap; flex-shrink: 0; }
  .sidebar-footer { border-top: none; padding-top: 0; flex-shrink: 0; }
  .sidebar-footer .field-label { display: none; }
  .sidebar-footer .btn-block { width: auto; }
  #theme-toggle { padding: 8px 10px; white-space: nowrap; }

  .main { padding: 16px; }
  .kpi-value { font-size: 20px; }
}

@media (max-width: 600px) {
  .main { padding: 12px; }
  .card { padding: 14px; }
  .topbar-title { font-size: 17px; }
  .toolbar > * { flex: 1 1 140px; }
  .toolbar .btn { flex: 0 0 auto; }
  .row-2, .row-3 { grid-template-columns: 1fr; }
  .filter-row { grid-template-columns: 1fr; }
  .filter-row .f-remove { justify-self: end; }
  .kpi-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
  .modal { padding: 14px; }
}
