:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f3f6;
  --ink: #17202a;
  --muted: #667085;
  --line: #d9e0e8;
  --brand: #0f766e;
  --brand-2: #2563eb;
  --danger: #b42318;
  --warn: #b54708;
  --ok: #067647;
  --shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
  --radius: 8px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 244px minmax(0, 1fr);
}
.sidebar {
  background: #0f172a;
  color: #e5edf6;
  padding: 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 14px;
  border-bottom: 1px solid rgba(255,255,255,.12);
}
.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #14b8a6;
  color: #052f2b;
  font-weight: 800;
}
.brand small { color: #9fb0c7; display: block; margin-top: 2px; }
.nav-section { margin: 16px 0; }
.nav-label {
  color: #8fa2bd;
  font-size: 11px;
  letter-spacing: 0;
  text-transform: uppercase;
  margin: 0 8px 8px;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  padding: 8px 10px;
  border-radius: 8px;
  color: #d8e2f0;
}
.nav-link:hover, .nav-link.active { background: rgba(255,255,255,.1); color: #fff; }
.nav-icon { width: 20px; text-align: center; color: #9fb0c7; }

.main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(246,247,249,.94);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
  padding: 10px 18px;
}
.topbar-left, .topbar-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.icon-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn:hover { border-color: #a8b5c5; }
.page {
  padding: 20px;
  width: 100%;
  max-width: 1500px;
}
.page-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}
.page-title h1 { margin: 0 0 4px; font-size: 24px; letter-spacing: 0; }
.page-title p { margin: 0; color: var(--muted); }
.muted { color: var(--muted); }
dt { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
dd { margin: 0; font-weight: 650; }

.flash {
  border: 1px solid var(--line);
  background: var(--surface);
  border-left: 4px solid var(--brand-2);
  padding: 12px;
  border-radius: var(--radius);
  margin-bottom: 14px;
}
.flash.error { border-left-color: var(--danger); }
.flash.success { border-left-color: var(--ok); }

.grid { display: grid; gap: 12px; }
.metrics { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.two-col { grid-template-columns: minmax(0, 1fr) minmax(320px, .45fr); align-items: start; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card.pad { padding: 14px; }
.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.section-heading h2, .section-heading h3 { margin-top: 0; }
.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  min-height: 104px;
}
.metric .label { color: var(--muted); font-size: 12px; }
.metric .value { font-size: 24px; font-weight: 750; margin: 8px 0 4px; }
.metric .meta { color: var(--muted); font-size: 12px; }

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  position: sticky;
  top: 58px;
  z-index: 10;
  margin-bottom: 12px;
}
.field { display: grid; gap: 4px; }
.field label { color: var(--muted); font-size: 12px; }
input, select, textarea {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  min-height: 36px;
}
textarea { min-height: 88px; }
.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  min-height: 36px;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--ink);
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: white; }
.btn.danger { background: #fff5f3; border-color: #f4b9b0; color: var(--danger); }
.btn:disabled, .is-disabled { opacity: .55; cursor: not-allowed; }

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
table { width: 100%; border-collapse: collapse; min-width: 960px; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th {
  background: #f3f6f9;
  color: #4b5563;
  font-size: 12px;
  position: sticky;
  top: 0;
  z-index: 2;
}
tbody tr:hover { background: #fafbfc; }
.sticky-actions {
  position: sticky;
  right: 0;
  background: inherit;
  box-shadow: -8px 0 12px rgba(15,23,42,.05);
}
.child-shell { padding: 0; background: #fbfcfd; }
.child-shell > div { padding: 12px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  border: 1px solid var(--line);
  background: #fff;
  white-space: nowrap;
}
.badge.ok { color: var(--ok); background: #ecfdf3; border-color: #abefc6; }
.badge.warn { color: var(--warn); background: #fffaeb; border-color: #fedf89; }
.badge.bad { color: var(--danger); background: #fef3f2; border-color: #fecdca; }
.badge.info { color: #175cd3; background: #eff8ff; border-color: #b2ddff; }
.badge.neutral { color: #475467; background: #f8fafc; }
.badges { display: flex; flex-wrap: wrap; gap: 5px; }

.empty-state, .error-state, .loading-row, .setup-panel {
  background: var(--surface);
  border: 1px dashed #b8c4d2;
  border-radius: var(--radius);
  padding: 16px;
  color: var(--muted);
}
.setup-panel h3 { margin: 0 0 12px; color: var(--ink); }
.setup-panel .field + .field { margin-top: 10px; }
.error-state { border-color: #f4b9b0; color: var(--danger); background: #fff8f7; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.inline-form {
  display: flex;
  gap: 6px;
  align-items: center;
  min-width: 260px;
}
.inline-form select { min-width: 190px; }
.network-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: 420px;
}
.panel-space { margin: 10px 0 14px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tab { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.tab.active { border-color: var(--brand); color: var(--brand); }

.pagination { display: flex; justify-content: space-between; align-items: center; padding: 10px; color: var(--muted); }
.kpi-strip { display: flex; flex-wrap: wrap; gap: 8px; }
.kpi-strip .badge { border-radius: 8px; }
.link { color: #2563eb; font-weight: 650; }
.num-good { color: #059669; font-weight: 700; }
.num-bad { color: #dc2626; font-weight: 700; }

.dashboard-filters {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) minmax(190px, 1fr) minmax(190px, 1fr) minmax(180px, 1fr) minmax(220px, 1.2fr) minmax(160px, .8fr) auto auto;
  gap: 10px;
  align-items: end;
  padding: 12px;
  margin-bottom: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.05);
}
.dashboard-filters input,
.dashboard-filters select,
.portfolio-toolbar select { min-width: 0; width: 100%; }

.dash-metrics {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.metric-card {
  min-height: 110px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06);
  padding: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.metric-card .label { color: #475467; font-size: 12px; font-weight: 650; }
.metric-card .value { font-size: 22px; font-weight: 800; margin: 6px 0 3px; white-space: nowrap; }
.metric-card .meta { color: var(--muted); font-size: 11px; line-height: 1.35; }
.metric-icon {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-weight: 800;
}
.tone-blue { color: #175cd3; background: #eff8ff; }
.tone-green { color: #087443; background: #edfcf2; }
.tone-purple { color: #6d28d9; background: #f4f0ff; }
.tone-orange { color: #c2410c; background: #fff4ed; }
.tone-red { color: #c01048; background: #fff1f3; }

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(320px, 1.15fr) minmax(360px, 1.2fr) minmax(250px, .75fr) minmax(250px, .75fr);
  gap: 14px;
  align-items: stretch;
}
.panel,
.portfolio-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.05);
  padding: 14px;
}
.panel h2,
.portfolio-panel h2 { margin: 0; font-size: 16px; }
.mini-table { min-width: 0; font-size: 13px; }
.mini-table th,
.mini-table td { padding: 9px 8px; }
.mini-table th { position: static; background: #f8fafc; }

.donut-compact {
  display: grid;
  justify-items: center;
  gap: 8px;
  padding-top: 6px;
}
.donut-svg {
  width: min(230px, 100%);
  height: auto;
  display: block;
}
.donut-svg text {
  fill: white;
  font-size: 8px;
  font-weight: 800;
  paint-order: stroke;
  stroke: rgba(0, 0, 0, .2);
  stroke-width: 2px;
}
.donut-center {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: white;
  display: grid;
  place-items: center;
  align-content: center;
  text-align: center;
  box-shadow: 0 1px 8px rgba(15, 23, 42, .06);
}
.donut-center span { display: block; color: var(--muted); font-size: 8px; line-height: 1.1; }
.donut-center strong { display: block; color: var(--ink); font-size: 12px; line-height: 1.1; margin-top: 2px; }
.donut-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 10px;
  font-size: 12px;
  color: #475467;
}
.donut-legend span { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.donut-legend i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.run-list { display: grid; gap: 10px; margin: 0; }
.run-list > div {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr);
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid #edf1f5;
}
.run-list > div:last-child { border-bottom: 0; padding-bottom: 0; }
.progress {
  display: inline-block;
  width: 130px;
  height: 7px;
  border-radius: 999px;
  background: #eef2f7;
  overflow: hidden;
  vertical-align: middle;
  margin-right: 8px;
}
.progress span { display: block; height: 100%; background: #2563eb; }

.portfolio-toolbar { align-items: end; }
.portfolio-toolbar .field { min-width: 160px; flex: 1; }
.portfolio-panel { padding: 0; overflow: visible; }
.portfolio-panel > .section-heading { padding: 14px 14px 0; }
.portfolio-table {
  border-left: 0;
  border-right: 0;
  border-bottom: 0;
  border-radius: 0;
}
.portfolio-table table { min-width: 1920px; font-size: 13px; }
.portfolio-table th,
.portfolio-table td { padding: 9px 10px; }
.portfolio-table th { background: #eef4ff; color: #26364f; white-space: nowrap; }
.portfolio-table th.ads-col,
.portfolio-table td.ads-col { background: #fff7d6; }
.portfolio-table th.gam-col,
.portfolio-table td.gam-col { background: #fff2bd; }
.portfolio-table tbody tr:hover td { background-color: #f8fbff; }
.portfolio-table tbody tr:hover td.ads-col { background-color: #fff2bd; }
.portfolio-table tbody tr:hover td.gam-col { background-color: #ffedac; }
.portfolio-table .sticky-actions { min-width: 88px; }
.portfolio-table tfoot td {
  background: #f8fafc;
  border-top: 2px solid #b8c4d2;
  font-weight: 800;
  position: sticky;
  bottom: 0;
  z-index: 4;
}
.portfolio-table tfoot td.ads-col { background: #fff1b8; }
.portfolio-table tfoot td.gam-col { background: #ffe99e; }
.total-row td:first-child { color: var(--ink); }

.floating-table-header {
  position: fixed;
  display: none;
  overflow: hidden;
  z-index: 35;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  box-shadow: 0 8px 14px rgba(15, 23, 42, .08);
  pointer-events: none;
}
.floating-table-header table {
  border-collapse: collapse;
  table-layout: fixed;
  min-width: 1920px;
  font-size: 13px;
}
.floating-table-header th {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  background: #eef4ff;
  color: #26364f;
  text-align: left;
  white-space: nowrap;
}
.floating-table-header th.ads-col { background: #fff7d6; }
.floating-table-header th.gam-col { background: #fff2bd; }

.mix-bar {
  margin-top: 6px;
  width: 160px;
  height: 16px;
  display: flex;
  overflow: hidden;
  border-radius: 999px;
  background: #e5e7eb;
  border: 1px solid rgba(15, 23, 42, .08);
}
.mix-bar span {
  min-width: 22px;
  display: grid;
  place-items: center;
  color: white;
  font-size: 10px;
  font-weight: 750;
  line-height: 1;
}
.mix-dgen { background: #2563eb; }
.mix-display { background: #16a34a; }
.mix-pmax { background: #7c3aed; }
.mix-unknown { background: #94a3b8; }

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.login-card { max-width: 420px; width: 100%; }

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    z-index: 40;
    transform: translateX(-105%);
    transition: transform .18s ease;
    width: 280px;
  }
  .mobile-nav-open .sidebar { transform: translateX(0); }
  .page { padding: 14px; }
  .topbar { padding: 10px 12px; }
  .two-col { grid-template-columns: 1fr; }
  .toolbar { position: static; }
  .dashboard-filters { grid-template-columns: 1fr; }
  .dash-metrics { grid-template-columns: 1fr; }
  .dashboard-grid { grid-template-columns: 1fr; }
  .inline-form { flex-wrap: wrap; min-width: 220px; }
  .inline-form select { min-width: 180px; }
  table { min-width: 760px; }
}

@media (max-width: 1280px) and (min-width: 901px) {
  .dashboard-filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .dash-metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .dashboard-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (min-width: 901px) {
  .mobile-only { display: none !important; }
  .sidebar-collapsed .app-shell { grid-template-columns: 76px minmax(0, 1fr); }
  .sidebar-collapsed .brand div:not(.brand-mark),
  .sidebar-collapsed .nav-text,
  .sidebar-collapsed .nav-label { display: none; }
  .sidebar-collapsed .sidebar { padding: 14px 10px; }
  .sidebar-collapsed .nav-link { justify-content: center; }
}
