/* Twang Marketing Dashboard v2 | design system per 02-chemi-ui-spec.md
   RTL-first, logical properties only. Brand tokens injected on :root at runtime. */

:root {
  --brand: #C0392B;
  --brand-soft: rgba(192, 57, 43, 0.08);
  --ink: #1F2937;
  --font-brand: 'Assistant', 'Segoe UI', sans-serif;

  --bg: #F6F7F9;
  --card: #FFFFFF;
  --text: #1F2937;
  --muted: #6B7280;
  --line: #E5E7EB;
  --good: #0E7C42;
  --bad: #B42318;
  --neutral: #6B7280;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .07), 0 1px 2px rgba(16, 24, 40, .04);
  --shadow-lift: 0 8px 24px rgba(16, 24, 40, .14);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-brand); font-size: 15px; line-height: 1.55;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
img { max-width: 100%; display: block; }
a { color: var(--brand); }

.container { max-width: 1180px; margin-inline: auto; padding-inline: 20px; }
.container.wide { max-width: 1820px; padding-inline: clamp(20px, 2.5vw, 44px); }

/* ---------- header ---------- */
.page-head {
  background: var(--card); box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 20;
}
.page-head .container {
  display: flex; align-items: center; gap: 16px; padding-block: 14px;
}
.head-text { flex: 1; min-width: 0; }
.kicker {
  color: var(--brand); font-weight: 700; font-size: 12.5px;
  letter-spacing: .08em;
}
h1.title {
  margin: 0; font-size: 34px; font-weight: 800; color: var(--ink); line-height: 1.15;
  overflow-wrap: break-word;
  font-family: var(--font-head, var(--font-brand));
}
.section-head .t, .kpi .v { font-family: var(--font-head, var(--font-brand)); }
.meta-line { color: var(--muted); font-size: 14px; }
.head-logo img { max-height: 52px; max-width: 150px; object-fit: contain; }

select.range {
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px;
  background: var(--card); font-weight: 600; min-height: 44px; cursor: pointer;
}
select.range:focus-visible, button:focus-visible, input:focus-visible,
textarea:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--brand); outline-offset: 2px;
}

.demo-chip {
  display: inline-block; background: #F3F4F6; color: var(--muted);
  border-radius: 999px; padding: 3px 12px; font-size: 12.5px; font-weight: 600;
  margin-block-start: 10px;
}

/* ---------- widgets (client) ---------- */
/* 12-column report grid: widgets carry grid-column span from their layout */
.report {
  padding-block: 18px 30px; flex: 1 1 0; min-width: 0;
  display: grid; grid-template-columns: repeat(12, 1fr);
  gap: 34px 20px; align-items: start;
}
.widget { grid-column: 1 / -1; min-width: 0; }
.report > .empty-inline, .report > .page-state, .report > .skeleton { grid-column: 1 / -1; }
.section-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  border-block-end: 2px solid var(--brand);
  padding-block-end: 7px; margin-block-end: 16px;
}
.section-head .t { font-size: 20px; font-weight: 800; color: var(--ink); }
.section-head .s { font-size: 13px; color: var(--muted); }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(205px, 1fr)); gap: 16px; }
.kpi {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  border-block-start: 3px solid var(--brand);
  padding: 16px 18px 13px; min-height: 106px;
}
.kpi .v {
  font-size: 30px; font-weight: 800; color: #111827;
  direction: ltr; text-align: end; line-height: 1.2; white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.kpi .l { font-size: 13.5px; font-weight: 600; color: var(--muted); margin-block-start: 2px; }
.kpi .d { font-size: 13px; font-weight: 700; direction: ltr; text-align: end; }
.d.up { color: var(--good); } .d.down { color: var(--bad); } .d.flat { color: var(--neutral); }

.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 22px;
}
.chart-wrap { position: relative; }
.chart-tip {
  position: absolute; pointer-events: none; background: #111827; color: #fff;
  border-radius: 8px; padding: 6px 10px; font-size: 12.5px; white-space: nowrap;
  transform: translate(-50%, -115%); opacity: 0; transition: opacity .12s;
  direction: rtl; z-index: 5;
}
.chart-tip b { direction: ltr; unicode-bidi: isolate; }

/* tables
   Alignment contract: every column shares ONE edge between header and data.
   Text columns (name, audience) align to start (right in RTL); numeric columns
   are LTR and align to their right edge, header included. tabular-nums keeps
   digits on a fixed grid so stacked numbers line up perfectly. */
.table-scroll { overflow-x: auto; }
table.data {
  border-collapse: collapse; width: 100%; min-width: 560px;
  font-variant-numeric: tabular-nums;
}
table.data th {
  color: var(--muted); font-weight: 600; font-size: 13px;
  border-block-end: 1.5px solid var(--line);
  padding: 9px 12px; text-align: start; white-space: nowrap;
}
table.data th:first-child { width: 28%; }
table.data td {
  padding: 10px 12px; border-block-end: 1px solid #F1F2F4;
  text-align: start; white-space: nowrap;
}
table.data td.num { direction: ltr; text-align: end; }
table.data tr:hover td { background: #FAFBFC; }
table.data tr.totals td {
  background: var(--brand-soft); font-weight: 700; border-block-end: none;
}
td .name-bdi { display: inline-block; max-width: 440px; overflow: hidden;
  text-overflow: ellipsis; vertical-align: bottom; }

/* ad gallery */
.ads-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(235px, 1fr)); gap: 16px; }
.ad-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; transition: box-shadow .15s, transform .15s;
}
.ad-card:hover { box-shadow: var(--shadow-lift); transform: translateY(-2px); }
.ad-card .thumb { aspect-ratio: 4 / 5; background: #F3F4F6; }
.ad-card .thumb img { width: 100%; height: 100%; object-fit: contain; }
.ad-card .body { padding: 12px 14px 14px; }
.ad-card .name {
  font-weight: 700; font-size: 14.5px; line-height: 1.35; min-height: 2.7em;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.ad-card .stats { display: flex; gap: 14px; margin-block-start: 8px; flex-wrap: wrap; }
.ad-card .stat .sv { font-weight: 800; font-size: 15px; direction: ltr; }
.ad-card .stat .sl { font-size: 12px; color: var(--muted); }

/* note */
.note-box {
  background: var(--card); border-inline-start: 4px solid var(--brand);
  border-radius: 10px; box-shadow: var(--shadow);
  padding: 14px 18px; white-space: pre-wrap;
}

/* states */
.skeleton {
  background: linear-gradient(90deg, #EEF0F3 25%, #F6F7F9 50%, #EEF0F3 75%);
  background-size: 200% 100%; animation: shimmer 1.3s infinite; border-radius: var(--radius);
}
@keyframes shimmer { to { background-position: -200% 0; } }
.empty-inline { color: var(--muted); text-align: center; padding: 26px 10px; }
.page-state { text-align: center; padding: 80px 20px; }
.page-state .big { font-size: 26px; font-weight: 800; color: var(--ink); }

.footer {
  color: #9CA3AF; font-size: 12.5px; text-align: center;
  padding-block: 20px 26px; border-block-start: 1px solid var(--line); margin-block-start: 10px;
}

/* ---------- admin ---------- */
.admin-layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 260px; flex-shrink: 0; background: var(--card);
  border-inline-end: 1px solid var(--line);
  padding: 18px 14px; display: flex; flex-direction: column; gap: 6px;
}
.sidebar .brand { font-weight: 800; color: var(--ink); font-size: 17px; margin-block-end: 8px; }
.client-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  border: none; background: none; text-align: start; padding: 10px 12px;
  border-radius: 10px; cursor: pointer; font-weight: 600; min-height: 44px;
}
.client-item:hover { background: #F3F4F6; }
.client-item.active { background: var(--brand-soft); color: var(--brand); }
.client-item .dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.client-item.inactive { opacity: .55; }

.admin-main { flex: 1; min-width: 0; padding: 22px 26px 60px; }
.admin-topline { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.admin-topline h1 { font-size: 26px; margin: 0; color: var(--ink); font-weight: 800; flex: 1; }

.tabs { display: flex; gap: 4px; margin-block: 16px; border-block-end: 1px solid var(--line); }
.tab {
  border: none; background: none; padding: 10px 16px; font-weight: 700;
  color: var(--muted); cursor: pointer; border-block-end: 3px solid transparent;
  margin-block-end: -1px; min-height: 44px;
}
.tab.active { color: var(--brand); border-color: var(--brand); }

.btn {
  border: none; border-radius: 10px; padding: 10px 18px; font-weight: 700;
  cursor: pointer; min-height: 44px; display: inline-flex; align-items: center; gap: 7px;
  background: #EEF0F3; color: var(--ink); transition: filter .12s;
}
.btn:hover { filter: brightness(.96); }
.btn.primary { background: var(--brand); color: #fff; }
.btn.danger { background: #FEE4E2; color: var(--bad); }
.btn.ghost { background: none; border: 1px solid var(--line); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: 13.5px; }

.field { margin-block-end: 14px; }
.field label { display: block; font-weight: 600; font-size: 13.5px; margin-block-end: 5px; }
.field input[type="text"], .field input[type="password"], .field select, .field textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; background: var(--card); min-height: 44px;
}
.field input[type="color"] {
  width: 60px; height: 44px; border: 1px solid var(--line); border-radius: 10px;
  padding: 3px; background: var(--card); cursor: pointer;
}
.field-row { display: flex; gap: 14px; flex-wrap: wrap; }
.field .hint { font-size: 12.5px; color: var(--muted); margin-block-start: 4px; }
.field .error-text { font-size: 13px; color: var(--bad); margin-block-start: 4px; }

/* widget editor list */
.widget-list { display: flex; flex-direction: column; gap: 10px; margin-block: 14px; }
.widget-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 12px 14px; cursor: default;
}
.widget-row.dragging { opacity: .45; }
.widget-row.drop-above { box-shadow: 0 -3px 0 0 var(--brand), var(--shadow); }
.widget-row.drop-below { box-shadow: 0 3px 0 0 var(--brand), var(--shadow); }
.drag-handle {
  cursor: grab; color: #9CA3AF; font-size: 18px; user-select: none;
  padding: 6px 4px; touch-action: none;
}
.widget-row .w-icon {
  width: 38px; height: 38px; border-radius: 10px; background: var(--brand-soft);
  color: var(--brand); display: grid; place-items: center; font-size: 17px; flex-shrink: 0;
}
.widget-row .w-body { flex: 1; min-width: 0; }
.widget-row .w-title { font-weight: 700; }
.widget-row .w-sub { font-size: 13px; color: var(--muted); }
.widget-row .w-actions { display: flex; gap: 4px; opacity: 0; transition: opacity .12s; }
.widget-row:hover .w-actions, .widget-row:focus-within .w-actions { opacity: 1; }
.icon-btn {
  border: none; background: none; cursor: pointer; border-radius: 8px;
  width: 38px; height: 38px; display: grid; place-items: center; color: var(--muted);
}
.icon-btn:hover { background: #F3F4F6; color: var(--ink); }

/* drawer */
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(17, 24, 39, .45); z-index: 40;
  opacity: 0; pointer-events: none; transition: opacity .18s;
}
.drawer {
  position: fixed; inset-block: 0; inset-inline-end: 0; width: 440px; max-width: 100vw;
  background: var(--card); z-index: 41; box-shadow: var(--shadow-lift);
  transform: translateX(-14px); opacity: 0; pointer-events: none;
  transition: transform .18s, opacity .18s;
  display: flex; flex-direction: column;
}
html[dir="rtl"] .drawer { transform: translateX(14px); }
.drawer.open, .drawer-backdrop.open { opacity: 1; pointer-events: auto; transform: none; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-block-end: 1px solid var(--line);
}
.drawer-head .t { font-weight: 800; font-size: 18px; color: var(--ink); }
.drawer-body { flex: 1; overflow-y: auto; padding: 18px 20px; }
.drawer-foot { padding: 14px 20px; border-block-start: 1px solid var(--line); display: flex; gap: 10px; }

.type-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.type-card {
  border: 1.5px solid var(--line); border-radius: 12px; background: var(--card);
  padding: 14px; cursor: pointer; text-align: center; font-weight: 700;
}
.type-card .ic { font-size: 22px; }
.type-card:hover { border-color: var(--brand); }
.type-card.selected { border-color: var(--brand); background: var(--brand-soft); }

.metric-search { position: relative; margin-block-end: 10px; }
.metric-search input { width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 38px 10px 12px; min-height: 44px; }
.metric-search .ic { position: absolute; inset-inline-start: 12px; top: 50%;
  transform: translateY(-50%); color: var(--muted); }
html[dir="rtl"] .metric-search input { padding: 10px 12px 10px 38px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1.5px solid var(--line); border-radius: 999px; padding: 7px 14px;
  cursor: pointer; background: var(--card); font-weight: 600; font-size: 13.5px;
  min-height: 38px; display: inline-flex; align-items: center; gap: 6px;
}
.chip.on { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }
.chip.hidden { display: none; }

/* toast */
.toast {
  position: fixed; inset-block-end: 22px; inset-inline-start: 22px; z-index: 60;
  background: #111827; color: #fff; border-radius: 12px; padding: 12px 18px;
  box-shadow: var(--shadow-lift); display: flex; align-items: center; gap: 12px;
  opacity: 0; transform: translateY(8px); transition: all .18s; pointer-events: none;
}
.toast.show { opacity: 1; transform: none; pointer-events: auto; }
.toast button { background: none; border: none; color: #FCA5A5; font-weight: 700; cursor: pointer; }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-wrap[hidden] { display: none; }
.admin-layout[hidden] { display: none; }
.login-card { background: var(--card); border-radius: 16px; box-shadow: var(--shadow-lift);
  padding: 34px; width: 380px; max-width: 100%; }

.link-box {
  display: flex; align-items: center; gap: 10px; background: var(--brand-soft);
  border-radius: 10px; padding: 10px 14px; margin-block: 12px; flex-wrap: wrap;
}
.link-box code { direction: ltr; unicode-bidi: isolate; font-size: 13px; overflow-wrap: anywhere; }

/* ---------- responsive ---------- */
@media (max-width: 1024px) {
  .sidebar { width: 210px; }
}
@media (max-width: 900px) {
  /* narrow screens: every widget spans the full row, whatever its layout */
  .report > .widget { grid-column: 1 / -1 !important; }
}
@media (max-width: 640px) {
  h1.title { font-size: 26px; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .kpi .v { font-size: 24px; }
  .ads-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .report { gap: 26px 12px; }
  .card { padding: 14px; }
  .admin-layout { flex-direction: column; }
  .sidebar { width: 100%; border-inline-end: none; border-block-end: 1px solid var(--line); }
  .drawer { width: 100vw; }
  .admin-main { padding: 16px 14px 60px; }
}

/* ---------- v3: page tabs ---------- */
.tabs-bar {
  display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none;
  padding-block-start: 2px;
}
.tabs-bar::-webkit-scrollbar { display: none; }
.page-tab {
  border: none; background: none; cursor: pointer; white-space: nowrap;
  font-weight: 700; font-size: 15px; color: var(--muted);
  padding: 12px 16px; min-height: 46px;
  border-block-end: 3px solid transparent;
}
.page-tab.active { color: var(--brand); border-color: var(--brand); }
.page-tab:hover { color: var(--ink); }

/* ---------- v3: date range panel ---------- */
.range-wrap { position: relative; }
.range-panel {
  position: absolute; inset-inline-end: 0; top: calc(100% + 8px); z-index: 50;
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow-lift);
  width: 250px; max-height: 70vh; overflow-y: auto; padding: 8px;
}
.range-item {
  display: block; width: 100%; text-align: start; border: none; background: none;
  padding: 9px 12px; border-radius: 8px; cursor: pointer; font-weight: 600;
  min-height: 40px;
}
.range-item:hover { background: #F3F4F6; }
.range-item.on { color: var(--brand); background: var(--brand-soft); }
.range-custom { border-block-start: 1px solid var(--line); margin-block-start: 8px; padding: 10px 12px; }
.range-custom-title { font-weight: 700; font-size: 13.5px; margin-block-end: 8px; }
.range-custom-row { display: flex; flex-direction: column; gap: 8px; margin-block-end: 8px; }
.range-custom-row input {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-height: 40px;
}

/* ---------- v3: ads table (v4.5: natural aspect ratio, click-to-zoom) ----------
   Creatives arrive as 4:5, 9:16, 1:1 or 1.91:1 — the thumb keeps the ratio:
   fixed height, width follows, wide ones letterbox inside the cap. */
td.img-cell { width: 1%; min-width: 76px; }
td.img-cell img {
  height: 104px; width: auto; min-width: 56px; max-width: 132px; object-fit: contain;
  border-radius: 10px; display: block; background: #F3F4F6;
  cursor: zoom-in; box-shadow: 0 1px 2px rgba(16, 24, 40, .12);
}
td.img-cell img:hover { box-shadow: var(--shadow-lift); }
td.img-cell .img-ph {
  width: 60px; height: 104px; border-radius: 10px; background: var(--brand-soft);
}

/* lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 200; background: rgba(17, 24, 39, .82);
  display: grid; place-items: center; cursor: zoom-out; padding: 30px;
  animation: lb-in .14s ease;
}
@keyframes lb-in { from { opacity: 0; } to { opacity: 1; } }
.lightbox img {
  max-width: min(88vw, 900px); max-height: 82vh; border-radius: 14px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, .5); object-fit: contain; background: #fff;
}
.lightbox .lb-cap {
  position: fixed; inset-inline: 0; bottom: 26px; text-align: center;
  color: #fff; font-weight: 700; font-size: 15px; text-shadow: 0 1px 4px rgba(0,0,0,.6);
  padding-inline: 40px;
}
.aud-chip {
  display: inline-block; border-radius: 999px; padding: 3px 10px;
  font-size: 12.5px; font-weight: 700; background: #EEF2FF; color: #3730A3;
  white-space: nowrap;
}
.post-link {
  display: inline-grid; place-items: center; width: 36px; height: 36px;
  border-radius: 8px; text-decoration: none; font-size: 16px; font-weight: 700;
}
.post-link:hover { background: var(--brand-soft); }
.table-search { margin-block-end: 10px; }
.table-search input {
  width: 100%; max-width: 320px; border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 12px; min-height: 42px;
}
.pager {
  display: flex; align-items: center; gap: 10px; justify-content: flex-end;
  margin-block-start: 10px;
}
.pager-info { color: var(--muted); font-size: 13px; direction: ltr; }

/* ---------- v3: admin additions ---------- */
.page-tabs-admin { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-block-end: 12px; }
.page-chip {
  border: 1.5px solid var(--line); border-radius: 999px; background: var(--card);
  padding: 8px 16px; font-weight: 700; cursor: pointer; min-height: 40px;
  display: inline-flex; align-items: center; gap: 8px;
}
.page-chip.active { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }
.page-chip .gear { opacity: .5; }
.seg-row {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border-radius: 12px; box-shadow: var(--shadow); padding: 12px 14px; margin-block-end: 10px;
}
.seg-row .s-body { flex: 1; min-width: 0; }
.seg-row .s-name { font-weight: 700; }
.seg-row .s-sub { font-size: 13px; color: var(--muted); }
.warn-chip {
  display: inline-block; background: #FEF3C7; color: #92400E; border-radius: 8px;
  padding: 4px 10px; font-size: 13px; font-weight: 600;
}

/* ---------- v3.2: table controls (viewer campaign picker) ---------- */
.table-controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-block-end: 10px; }
.picker-wrap { position: relative; }
.picker-panel {
  position: absolute; inset-inline-start: 0; top: calc(100% + 6px); z-index: 40;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow-lift);
  min-width: 260px; max-height: 320px; overflow-y: auto; padding: 10px 12px;
}
.picker-item {
  display: flex; align-items: center; gap: 9px; padding-block: 7px;
  font-weight: 600; cursor: pointer; font-size: 14px;
}
.picker-item input { width: 17px; height: 17px; }
.picker-item bdi { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }

/* ---------- v3.3: polish (modern, interactive, fast) ---------- */
.kpi { transition: transform .16s ease, box-shadow .16s ease; }
.kpi:hover { transform: translateY(-2px); box-shadow: var(--shadow-lift); }
.page-tab { transition: color .15s ease, border-color .15s ease; }
.widget { animation: rise .28s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.widget:nth-child(2) { animation-delay: .04s; }
.widget:nth-child(3) { animation-delay: .08s; }
.widget:nth-child(4) { animation-delay: .12s; }

/* sticky table headers inside their own scroll area */
.table-scroll { max-height: min(72vh, 740px); overflow: auto; }
table.data thead th {
  position: sticky; top: 0; background: var(--card); z-index: 2;
  box-shadow: 0 1.5px 0 var(--line);
  border-block-end: none;
}

/* top loading bar for refreshes (no skeleton flash after first load) */
.loadbar {
  position: fixed; top: 0; inset-inline: 0; height: 3px; z-index: 100;
  background: linear-gradient(90deg, transparent, var(--brand), transparent);
  background-size: 40% 100%; background-repeat: no-repeat;
  animation: slidebar 1s linear infinite; opacity: 0; pointer-events: none;
  transition: opacity .15s;
}
body.loading .loadbar { opacity: 1; }
@keyframes slidebar {
  from { background-position: 130% 0; }
  to { background-position: -30% 0; }
}
body.loading .report { opacity: .55; transition: opacity .2s; }

/* ---------- v3.4: collapsible side nav + table alignment + heatmap ---------- */
.body-flex { display: flex; align-items: flex-start; gap: 24px; }

.nav-toggle {
  border: 1px solid var(--line); background: var(--card); cursor: pointer;
  border-radius: 10px; width: 44px; height: 44px; font-size: 18px; color: var(--ink);
  flex-shrink: 0;
}
.nav-toggle:hover { background: var(--brand-soft); color: var(--brand); }

.side-nav {
  position: sticky; top: 96px;
  width: 228px; flex-shrink: 0;
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 10px; margin-block-start: 18px;
  overflow: hidden;
  transition: width .22s ease, padding .22s ease, opacity .22s ease;
}
body.nav-collapsed .side-nav { width: 0; padding: 10px 0; opacity: 0; pointer-events: none; }
.side-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  border: none; background: none; cursor: pointer; text-align: start;
  padding: 11px 14px; border-radius: 10px; font-weight: 700; font-size: 14.5px;
  color: var(--muted); min-height: 44px; white-space: nowrap;
  border-inline-start: 3px solid transparent;
  transition: background .12s, color .12s;
}
.side-item:hover { background: #F3F4F6; color: var(--ink); }
.side-item.active {
  background: var(--brand-soft); color: var(--brand);
  border-inline-start-color: var(--brand);
}
.side-item bdi { overflow: hidden; text-overflow: ellipsis; }

/* mobile: overlay drawer */
.nav-backdrop { display: none; }
@media (max-width: 1024px) {
  .side-nav {
    position: fixed; inset-block: 0; inset-inline-start: 0; top: 0;
    width: 250px; margin: 0; border-radius: 0; z-index: 60;
    transform: translateX(100%); opacity: 1; transition: transform .22s ease;
    padding-block-start: 24px;
  }
  html[dir="rtl"] .side-nav { transform: translateX(100%); }
  body:not(.nav-collapsed) .side-nav { transform: none; }
  body.nav-collapsed .side-nav { width: 250px; padding: 24px 10px 10px; transform: translateX(100%); }
  body:not(.nav-collapsed) .nav-backdrop {
    display: block; position: fixed; inset: 0; z-index: 55;
    background: rgba(17, 24, 39, .4);
  }
}

/* numeric header cells align exactly like their data cells */
table.data th.num { direction: ltr; text-align: end; }

/* heatmap + active-only toggles */
.heat-toggle.on, .act-toggle.on {
  border-color: var(--brand); background: var(--brand-soft); color: var(--brand);
}

/* viewer header-click sorting (client view) */
table.viewer-sort thead th.num[data-key] { cursor: pointer; user-select: none; }
table.viewer-sort thead th.num[data-key]:hover { color: var(--brand); }

/* custom viewer filters: quick chips + dropdown */
.vf-chips { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.vf-chip.on {
  border-color: var(--brand); background: var(--brand); color: #fff; font-weight: 700;
}
.vf-select {
  border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px;
  min-height: 34px; font: inherit; font-size: 12.5px; font-weight: 600;
  color: var(--text); background: var(--card); cursor: pointer;
}
td.heat { transition: background .2s; }
td.heat-strong { font-weight: 700; }

/* ---------- v4.2: two-level tables (group rows) + sort indicator ---------- */
table.data tr.group-row td {
  background: var(--brand-soft); font-weight: 800; cursor: pointer;
  border-block-end: none; padding-block: 9px;
}
table.data tr.group-row:hover td { filter: brightness(.97); }
table.data tr.group-row .caret {
  display: inline-block; margin-inline-end: 8px; color: var(--brand); width: 14px;
}
table.data tr.group-row .g-count {
  display: inline-block; margin-inline-start: 8px; font-size: 11.5px; font-weight: 700;
  background: var(--card); color: var(--muted); border-radius: 999px; padding: 1px 8px;
  vertical-align: 1px;
}
table.data tr.in-group > td:first-child { padding-inline-start: 26px; }
table.data th.sorted { color: var(--brand); }
table.data th.num[data-key] { cursor: default; }
table.data th.col-target { outline: 2px dashed var(--brand); outline-offset: -2px; }

/* ---------- v4: pie / donut ---------- */
.pie-wrap { display: flex; align-items: center; gap: 26px; flex-wrap: wrap; }
.pie-wrap svg { flex-shrink: 0; }
.pie-legend { flex: 1; min-width: 190px; display: flex; flex-direction: column; gap: 9px; }
.pie-item { display: flex; align-items: center; gap: 9px; font-weight: 600; font-size: 13.5px; }
.pie-dot { width: 13px; height: 13px; border-radius: 4px; flex-shrink: 0; }
.pie-lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.pie-val {
  color: var(--muted); direction: ltr; font-variant-numeric: tabular-nums;
  margin-inline-start: auto; white-space: nowrap; font-size: 13px;
}

/* ---------- v4: visual layout editor (admin) ---------- */
.editor-head {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  margin-block-end: 4px;
}
.editor-head .hint { flex: 1; min-width: 200px; }
.lay-canvas {
  display: grid; grid-template-columns: repeat(12, 1fr); gap: 30px 20px;
  align-items: start; padding-block: 14px 30px;
  border-radius: var(--radius);
}
/* v4.6: widgets are FULLY interactive in the editor (search, pagination,
   collapse, lightbox). Dragging moved to a dedicated grip pill. */
.lay-item { position: relative; grid-column: 1 / -1; min-width: 0; border-radius: 12px; }
.lay-item > .widget { grid-column: auto; }
.lay-item:hover { outline: 2px dashed var(--brand); outline-offset: 8px; border-radius: 4px; }
.lay-grip {
  position: absolute; top: -13px; inset-inline-start: 50%; transform: translateX(50%);
  z-index: 6; min-width: 74px; height: 24px; border-radius: 999px;
  background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow);
  display: grid; place-items: center; cursor: grab; color: var(--muted);
  font-size: 13px; letter-spacing: 2px; opacity: 0; transition: opacity .12s;
  user-select: none;
}
html[dir="rtl"] .lay-grip { transform: translateX(50%); }
.lay-item:hover .lay-grip, .lay-item.dragging .lay-grip { opacity: 1; }
.lay-grip:hover { color: var(--brand); border-color: var(--brand); }
.lay-item.dragging { opacity: .45; }
.lay-item.drop-before { box-shadow: -8px 0 0 -4px var(--brand); }
.lay-item.drop-after { box-shadow: 8px 0 0 -4px var(--brand); }
.lay-item table.data thead th.num[data-key] { cursor: pointer; }
.lay-item table.data thead th.num[data-key]:hover { color: var(--brand); }
.lay-item .kpi { cursor: pointer; }
.lay-tools {
  position: absolute; top: -16px; inset-inline-end: 10px; z-index: 6;
  display: flex; align-items: center; gap: 2px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  padding: 3px 6px; box-shadow: var(--shadow-lift);
  opacity: 0; transition: opacity .12s;
}
.lay-item:hover .lay-tools, .lay-item:focus-within .lay-tools { opacity: 1; }
.lay-tools .w-badge {
  font-size: 12px; font-weight: 800; color: var(--muted); padding-inline: 6px;
  direction: ltr;
}
.lay-resize {
  position: absolute; inset-inline-start: -8px; top: 10%; height: 80%; width: 16px;
  cursor: ew-resize; z-index: 6; display: grid; place-items: center;
  opacity: 0; transition: opacity .12s;
}
.lay-item:hover .lay-resize { opacity: 1; }
.lay-resize::after {
  content: ""; width: 5px; height: 44px; border-radius: 4px;
  background: var(--brand); box-shadow: 0 0 0 2px #fff;
}
.lay-hresize {
  position: absolute; inset-inline: 20%; bottom: -8px; height: 16px;
  cursor: ns-resize; z-index: 6; display: grid; place-items: center;
  opacity: 0; transition: opacity .12s;
}
.lay-item:hover .lay-hresize { opacity: 1; }
.lay-hresize::after {
  content: ""; height: 5px; width: 44px; border-radius: 4px;
  background: var(--brand); box-shadow: 0 0 0 2px #fff;
}
.add-tile {
  grid-column: 1 / -1; border: 2px dashed var(--line); border-radius: 14px;
  padding: 24px; text-align: center; color: var(--muted); cursor: pointer;
  font-weight: 700; background: none; font-size: 15px;
}
.add-tile:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }

/* ---------- v4.6: Data-Studio column builder ---------- */
.col-row {
  display: flex; align-items: center; gap: 8px; margin-block-end: 8px;
}
.col-row .ord {
  color: var(--muted); font-weight: 800; font-size: 12.5px; width: 18px;
  text-align: center; flex-shrink: 0;
}
.metric-pick { position: relative; flex: 1; min-width: 0; }
.metric-btn {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  padding: 9px 12px; min-height: 42px; font-weight: 600; cursor: pointer;
  text-align: start; display: flex; align-items: center; gap: 8px;
}
.metric-btn .cap { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.metric-btn .car { color: var(--muted); }
.metric-btn.empty { color: var(--muted); border-style: dashed; }
.metric-panel {
  position: absolute; inset-inline: 0; top: calc(100% + 4px); z-index: 70;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow-lift);
  border: 1px solid var(--line); max-height: 280px; display: flex; flex-direction: column;
}
.metric-panel input {
  margin: 8px; border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; min-height: 38px;
}
.metric-panel .opts { overflow-y: auto; padding: 0 6px 8px; }
.metric-opt {
  display: block; width: 100%; text-align: start; border: none; background: none;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 13.5px;
}
.metric-opt:hover { background: var(--brand-soft); color: var(--brand); }
.metric-opt .k { color: var(--muted); font-size: 11px; direction: ltr; float: inline-end; }
.col-row .mv {
  border: 1px solid var(--line); background: var(--card); border-radius: 8px;
  width: 26px; height: 26px; cursor: pointer; color: var(--muted); font-size: 12px;
  display: grid; place-items: center; flex-shrink: 0;
}
.col-row .mv:hover:not(:disabled) { color: var(--brand); border-color: var(--brand); }
.col-row .mv:disabled { opacity: .3; cursor: default; }
.add-col-btn {
  border: 2px dashed var(--line); border-radius: 10px; background: none;
  padding: 10px; width: 100%; color: var(--muted); font-weight: 700; cursor: pointer;
}
.add-col-btn:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }

/* ---------- v4: metrics studio ---------- */
.plat-block { margin-block-end: 30px; }
.plat-block > .t {
  font-weight: 800; font-size: 17px; color: var(--ink);
  border-block-end: 2px solid var(--brand); padding-block-end: 6px; margin-block-end: 10px;
}
.metric-line {
  display: flex; align-items: center; gap: 10px; padding: 7px 4px;
  border-block-end: 1px solid #F1F2F4;
}
.metric-line code { direction: ltr; font-size: 12px; color: var(--muted); min-width: 110px; }
.metric-line input.m-label {
  border: 1px solid transparent; border-radius: 8px; padding: 6px 10px;
  width: 210px; background: none; font-weight: 600;
}
.metric-line input.m-label:hover { border-color: var(--line); background: var(--card); }
.metric-line input.m-label:focus { border-color: var(--brand); background: var(--card); outline: none; }
.metric-line .m-kind { color: var(--muted); font-size: 12.5px; min-width: 90px; }
.metric-line .m-flag {
  font-size: 11.5px; font-weight: 700; border-radius: 999px; padding: 2px 8px;
  background: #EEF2FF; color: #3730A3;
}
.metric-line .m-del { margin-inline-start: auto; }
.raw-box {
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 14px 16px; margin-block-start: 10px;
}

/* ---------- v4.4: global page filter ---------- */
.pgfilter { position: relative; }
.pgfilter input {
  border: 1.5px solid var(--line); border-radius: 999px; background: var(--card);
  padding: 8px 34px 8px 14px; min-height: 38px; font-size: 13.5px; font-weight: 600;
  width: 190px; transition: width .15s ease, border-color .15s ease;
}
.pgfilter input:focus { width: 250px; border-color: var(--brand); outline: none; }
.pgfilter input.on { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }
.pgfilter .pg-ic {
  position: absolute; inset-inline-start: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted); pointer-events: none; font-size: 13px;
}
.pgfilter .pg-clear {
  position: absolute; inset-inline-end: 10px; top: 50%; transform: translateY(-50%);
  border: none; background: none; cursor: pointer; color: var(--brand);
  font-weight: 800; font-size: 13px; padding: 2px;
}
@media (max-width: 900px) { .pgfilter input { width: 130px; } }

/* filter widget: same engine, placeable anywhere on the page */
.filter-card { padding: 14px 16px; }
.filter-card .wfilter input { width: 100%; }
.filter-card .wfilter input:focus { width: 100%; }

/* ---------- v4.1: comparison toggle ---------- */
.cmp-chip {
  border: 1.5px solid var(--line); border-radius: 999px; background: var(--card);
  padding: 8px 14px; font-weight: 700; font-size: 13px; cursor: pointer;
  min-height: 38px; color: var(--muted); white-space: nowrap;
}
.cmp-chip.on { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }
body.no-compare .d { display: none; }

/* ---------- v4.1: KPI quick-edit popover (admin editor) ---------- */
.kpi-pop {
  position: fixed; z-index: 80; width: 300px;
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow-lift);
  border: 1px solid var(--line); padding: 14px;
}
.kpi-pop .t { font-weight: 800; margin-block-end: 10px; color: var(--ink); }
.kpi-pop select, .kpi-pop input[type="text"] {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 10px; min-height: 42px; margin-block-end: 10px; background: var(--card);
}
.kpi-pop .row { display: flex; gap: 6px; flex-wrap: wrap; }
.kpi-pop .row .btn { flex: 1; }
.lay-item .kpi { cursor: pointer; }

/* page chips: draggable */
.page-chip.dragging { opacity: .45; }
.page-chip.drop-before { box-shadow: 3px 0 0 0 var(--brand); }
html[dir="rtl"] .page-chip.drop-before { box-shadow: -3px 0 0 0 var(--brand); }
.page-chip.drop-after { box-shadow: -3px 0 0 0 var(--brand); }
html[dir="rtl"] .page-chip.drop-after { box-shadow: 3px 0 0 0 var(--brand); }
.page-chip input.rename {
  border: none; background: none; font: inherit; font-weight: 700;
  width: 110px; outline: none; color: var(--brand);
}

/* ---------- v4: brand palette ---------- */
.palette-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.palette-row input[type="color"] {
  width: 44px; height: 38px; border: 1px solid var(--line); border-radius: 8px;
  padding: 2px; background: var(--card); cursor: pointer;
}
