/* REA Insights - one stylesheet for the client app and the admin hub.
   Mobile first (390px is the primary target), sidebar layout from 960px. */

:root {
  --navy: #0B1120;
  --navy-2: #121A2E;
  --blue: #2563EB;
  --blue-dark: #1D4ED8;
  --blue-light: #60A5FA;
  --tint: #EEF4FF;
  --tint-2: #DCE7FF;
  --text: #0B1120;
  --muted: #475569;          /* never lighter than this for text */
  --border: #E2E8F0;
  --surface: #F8FAFC;
  --bg: #FFFFFF;
  --green: #15803D;
  --green-bg: #ECFDF5;
  --red: #B91C1C;
  --red-bg: #FEF2F2;
  --amber: #B45309;
  --amber-bg: #FFFBEB;
  --gray-bg: #F1F5F9;
  --radius: 16px;
  --radius-sm: 12px;
  --shadow: 0 1px 2px rgba(11, 17, 32, 0.04), 0 6px 20px rgba(11, 17, 32, 0.05);
  --tab-h: 72px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--blue); text-decoration: none; }
button { font: inherit; }
h1, h2, h3, p { margin: 0; }
svg.ic { width: 22px; height: 22px; flex: none; }

/* ---------- App frame ---------- */
.app { min-height: 100dvh; display: flex; flex-direction: column; }
.sidebar { display: none; }
.main {
  flex: 1;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 20px 16px calc(var(--tab-h) + 24px);
}
.topbar { display: none; }

/* Brand */
.brand { font-size: 22px; letter-spacing: -0.01em; color: var(--text); }
.brand b { font-weight: 800; }
.brand-sm { font-size: 18px; color: var(--text); margin-bottom: 10px; }
.brand-sm b { font-weight: 800; }

/* Page header */
.page-head { margin-bottom: 16px; }
.page-head h1 { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
.page-head .greet { font-size: 17px; color: var(--muted); margin-top: 4px; }
.page-head .meta { font-size: 14px; color: var(--muted); margin-top: 4px; }
.page-title { font-size: 34px; font-weight: 800; letter-spacing: -0.02em; margin-bottom: 16px; }
.page-sub { color: var(--muted); font-size: 16px; margin-top: -10px; margin-bottom: 18px; }

/* Headline strip */
.headline {
  display: flex; align-items: center; gap: 12px;
  background: var(--tint); color: var(--blue);
  border-radius: 14px; padding: 14px 16px;
  font-weight: 600; font-size: 17px; margin-bottom: 16px;
}
.headline .ic { width: 24px; height: 24px; }

/* Cards */
.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 16px;
  margin-bottom: 16px;
}
.card h2 { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.hint { font-size: 13px; color: var(--muted); white-space: nowrap; }
.note { font-size: 13px; color: var(--muted); margin-top: 10px; }

/* Stat cards */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.stat-card {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas: "icon badge" "num num" "label label" "note note";
  align-items: start;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; color: var(--text);
  min-width: 0;
}
.stat-card:active { background: var(--surface); }
.stat-icon { grid-area: icon; }
.stat-icon.icon-chip { width: 44px; height: 44px; }
.stat-badge { grid-area: badge; justify-self: end; min-width: 0; text-align: right; }
.stat-badge .badge { font-size: 11px; padding: 4px 8px; gap: 3px; white-space: normal; line-height: 1.2; justify-content: flex-end; }
.stat-badge .badge .ic { width: 12px; height: 12px; }
.stat-num { grid-area: num; font-size: 32px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; margin-top: 12px; }
.stat-label { grid-area: label; font-size: 15px; color: var(--muted); margin-top: 4px; line-height: 1.3; }
.stat-note { grid-area: note; font-size: 12px; color: var(--muted); margin-top: 8px; }

.icon-chip {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--tint); color: var(--blue);
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-chip.green { background: var(--green-bg); color: var(--green); }
.icon-chip.amber { background: var(--amber-bg); color: var(--amber); }
.icon-chip.lg { width: 64px; height: 64px; }
.icon-chip.lg .ic { width: 28px; height: 28px; }

/* Delta badges */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  border-radius: 999px; padding: 5px 10px;
  font-size: 12px; font-weight: 500; white-space: nowrap;
  background: var(--gray-bg); color: var(--muted);
}
.badge b { font-weight: 700; }
.badge .ic { width: 14px; height: 14px; stroke-width: 2.5; }
.badge.up, .badge.new { background: var(--green-bg); color: var(--green); }
.badge.down { background: var(--red-bg); color: var(--red); }
.badge.down-small, .badge.flat { background: var(--gray-bg); color: var(--muted); }
.badge.lg { font-size: 16px; padding: 10px 16px; }
.badge.lg .ic { width: 18px; height: 18px; }

/* Line chart */
.lc { position: relative; }
.lc-plot { position: relative; margin: 8px 14px 0; }
.lc-plot svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.lc-grid line { stroke: #CBD5E1; stroke-width: 1; stroke-dasharray: 3 4; vector-effect: non-scaling-stroke; }
.lc-line { fill: none; stroke: var(--blue); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.lc-dot {
  position: absolute; width: 10px; height: 10px; border-radius: 50%;
  background: var(--blue); border: 2px solid #fff; box-shadow: 0 0 0 1px var(--blue);
  transform: translate(-50%, -50%);
}
.lc-labels { position: relative; height: 20px; margin: 8px 14px 0; }
.lc-labels span {
  position: absolute; transform: translateX(-50%);
  font-size: 13px; color: var(--muted); white-space: nowrap;
}
.lc.sm .lc-labels span { font-size: 11px; }
.lc.sm .lc-dot { width: 7px; height: 7px; border-width: 1.5px; }

/* Bar chart */
.bc { display: flex; align-items: flex-end; gap: 10px; height: 190px; padding-top: 4px; }
.bc-col { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; min-width: 0; }
.bc-val { font-size: 13px; font-weight: 600; color: var(--text); margin-bottom: 4px; }
.bc-track { flex: 1; width: 100%; display: flex; align-items: flex-end; }
.bc-bar { width: 100%; max-width: 42px; margin: 0 auto; background: var(--blue); border-radius: 6px 6px 3px 3px; min-height: 0; transition: height .3s; }
.bc-label { font-size: 13px; color: var(--muted); margin-top: 8px; }

/* Horizontal bars */
.hb { display: grid; gap: 12px; }
.hb-row { display: grid; grid-template-columns: minmax(96px, 42%) 1fr auto; align-items: center; gap: 12px; }
.hb-label { font-size: 15px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hb-track { height: 14px; background: var(--tint); border-radius: 999px; overflow: hidden; }
.hb-fill { height: 100%; background: var(--blue); border-radius: 999px; }
.hb-val { font-size: 15px; font-weight: 500; color: var(--text); min-width: 36px; text-align: right; }

/* Sparkline */
.spark { width: 110px; height: 36px; display: block; }
.spark-line { fill: none; stroke: var(--blue); stroke-width: 2; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
.spark-area { fill: rgba(37, 99, 235, 0.14); }

/* Segmented control */
.seg { display: flex; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 4px; margin-bottom: 16px; }
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--muted);
  font-weight: 600; font-size: 16px; padding: 10px; border-radius: 10px; cursor: pointer;
}
.seg button.active { background: var(--tint); color: var(--blue); }

/* Trend rows */
.trend-row { display: grid; grid-template-columns: auto 1fr 1.5fr; gap: 12px; align-items: center; color: var(--text); }
.trend-row .t-label { font-size: 15px; color: var(--muted); line-height: 1.25; }
.trend-row .t-num { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; margin-top: 2px; }

/* Detail screen */
.back { display: inline-flex; align-items: center; gap: 8px; color: var(--text); font-size: 24px; font-weight: 700; letter-spacing: -0.01em; margin-bottom: 14px; }
.back .ic { width: 26px; height: 26px; color: var(--navy); }
.big-num { font-size: 56px; font-weight: 800; letter-spacing: -0.03em; line-height: 1; margin: 6px 0 12px; }
.explain { display: flex; gap: 14px; background: var(--tint); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.explain .icon-chip { background: #fff; }
.explain h3 { font-size: 17px; font-weight: 700; margin-bottom: 4px; }
.explain p { color: var(--muted); font-size: 15px; }
.fact { display: flex; align-items: center; gap: 14px; }
.fact .f-label { font-size: 14px; color: var(--muted); }
.fact .f-val { font-size: 22px; font-weight: 700; }

/* Settings / list rows */
.row-card { display: block; padding: 4px 16px; color: var(--text); }
.row { display: flex; align-items: center; gap: 14px; padding: 14px 0; border-bottom: 1px solid var(--border); color: var(--text); }
.row:last-child { border-bottom: 0; }
.row .r-text { flex: 1; min-width: 0; display: block; }
.row .r-title { display: block; font-size: 17px; font-weight: 600; line-height: 1.3; }
.row .r-sub { display: block; font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.row .chev { color: var(--muted); }
.avatars { display: flex; gap: 6px; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; background: var(--tint); color: var(--navy);
  font-weight: 700; font-size: 13px; display: inline-flex; align-items: center; justify-content: center;
}

/* Toggle */
.toggle { position: relative; width: 52px; height: 30px; flex: none; }
.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle span {
  position: absolute; inset: 0; background: #CBD5E1; border-radius: 999px; cursor: pointer; transition: background .2s;
}
.toggle span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s;
}
.toggle input:checked + span { background: var(--blue); }
.toggle input:checked + span::after { transform: translateX(22px); }
.toggle input:focus-visible + span { outline: 2px solid var(--blue); outline-offset: 2px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--blue); color: #fff; border: 0; border-radius: 12px;
  font-weight: 600; font-size: 16px; padding: 14px 20px; cursor: pointer; width: 100%;
}
.btn:hover { background: var(--blue-dark); }
.btn.secondary { background: var(--tint); color: var(--blue); }
.btn.ghost { background: transparent; color: var(--blue); border: 1px solid var(--border); }
.btn.sm { width: auto; padding: 9px 14px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .6; cursor: default; }

/* Reports */
.report-hero { background: var(--tint); border: 0; }
.report-hero .rh-top { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.report-hero h2 { font-size: 20px; }
.report-row { display: flex; align-items: center; gap: 14px; color: var(--text); }
.report-row .rr-main { flex: 1; min-width: 0; }
.report-row h3 { font-size: 19px; font-weight: 700; margin-bottom: 6px; }
.report-stats { display: flex; gap: 0; }
.report-stats div { padding-right: 16px; margin-right: 16px; border-right: 1px solid var(--border); }
.report-stats div:last-child { border-right: 0; }
.report-stats b { display: block; font-size: 18px; font-weight: 600; }
.report-stats span { font-size: 13px; color: var(--muted); }

/* Empty / notices */
.empty { text-align: center; color: var(--muted); padding: 24px 12px; font-size: 15px; }
.notice { background: var(--amber-bg); color: var(--amber); border-radius: 12px; padding: 12px 14px; font-size: 14px; margin-bottom: 16px; }
.notice.info { background: var(--tint); color: var(--blue); }
.powered { text-align: center; color: var(--muted); font-size: 14px; margin: 20px 0 8px; }

/* Skeleton */
.skel { background: linear-gradient(90deg, #F1F5F9 25%, #E9EEF5 50%, #F1F5F9 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 10px; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* Tab bar (mobile) */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: var(--navy);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  min-height: var(--tab-h);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px;
  color: #CBD5E1; font-size: 12px; font-weight: 500; padding: 4px 0; touch-action: manipulation;
}
.tabbar a .ic { width: 24px; height: 24px; }
.tabbar a.active { color: var(--blue-light); }
.tabbar a.active .ic { stroke: var(--blue-light); fill: rgba(96, 165, 250, 0.25); }

/* Client switcher chip (admin) */
.chip-btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: #fff;
  border-radius: 999px; padding: 4px 10px 4px 4px; cursor: pointer; color: var(--text);
}
.chip-btn .avatar { width: 32px; height: 32px; font-size: 12px; }
.chip-menu {
  position: absolute; right: 0; top: 44px; background: #fff; border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); min-width: 240px; padding: 6px; z-index: 30; display: none;
}
.chip-menu.open { display: block; }
.chip-menu a, .chip-menu button { display: block; width: 100%; text-align: left; padding: 10px 12px; border-radius: 8px; color: var(--text); background: none; border: 0; cursor: pointer; font-size: 15px; }
.chip-menu a:hover, .chip-menu button:hover { background: var(--surface); }
.chip-menu .sep { height: 1px; background: var(--border); margin: 6px 0; }
.chip-wrap { position: relative; display: inline-block; }
.mobile-chip { display: flex; justify-content: flex-end; margin-bottom: 8px; }

/* ---------- Admin ---------- */
.summary-grid { display: grid; grid-template-columns: 1fr; gap: 12px; margin-bottom: 20px; }
.summary-card { display: flex; align-items: center; gap: 16px; }
.summary-card .s-label { font-size: 15px; color: var(--muted); }
.summary-card .s-val { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.summary-card .s-val.amber { color: var(--amber); }
.summary-card .s-val.green { color: var(--green); }

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table.tbl { width: 100%; border-collapse: collapse; min-width: 760px; }
.tbl th { text-align: left; font-size: 14px; font-weight: 500; color: var(--muted); padding: 10px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.tbl td { padding: 14px 10px; border-bottom: 1px solid var(--border); font-size: 15px; vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl td.name { font-weight: 600; white-space: nowrap; }
.tbl td.name a { color: var(--text); }
.tbl td.name small { display: block; font-weight: 400; font-size: 13px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; vertical-align: middle; background: #94A3B8; }
.dot.ok { background: #16A34A; }
.dot.red { background: #DC2626; }
.status { white-space: nowrap; color: var(--muted); }
.status.ok { color: var(--green); }
.status.red { color: var(--red); }
.pill { display: inline-block; font-size: 12px; font-weight: 600; border-radius: 999px; padding: 3px 9px; background: var(--gray-bg); color: var(--muted); }
.pill.active { background: var(--green-bg); color: var(--green); }
.pill.paused { background: var(--amber-bg); color: var(--amber); }

/* Forms */
.form-grid { display: grid; gap: 14px; }
.field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.field .help { font-size: 13px; color: var(--muted); margin-top: 4px; }
.field input[type=text], .field input[type=email], .field input[type=url], .field select, .field textarea {
  width: 100%; font: inherit; font-size: 16px; padding: 11px 12px; border: 1px solid var(--border); border-radius: 10px; background: #fff; color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--blue); outline-offset: 1px; border-color: var(--blue); }
.field.check { display: flex; align-items: center; gap: 10px; }
.field.check label { margin: 0; }
.form-section { margin-top: 10px; }
.form-section h3 { font-size: 16px; font-weight: 700; margin-bottom: 8px; }
.link-row { display: grid; grid-template-columns: 1fr; gap: 8px; margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px dashed var(--border); }
.errors { background: var(--red-bg); color: var(--red); border-radius: 10px; padding: 12px 14px; font-size: 14px; margin-bottom: 14px; }
.errors ul { margin: 6px 0 0; padding-left: 18px; }
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 8px 14px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + 16px); transform: translateX(-50%); background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 50; opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }

/* ---------- Login / no-access (standalone pages) ---------- */
.auth { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 32px 24px; text-align: center;
  background: linear-gradient(180deg, #E8EFFB 0%, #FFFFFF 55%); }
.auth .brand { font-size: 40px; margin-bottom: 6px; }
.auth .tag { color: var(--muted); font-size: 18px; margin-bottom: 36px; }
.auth form, .auth .auth-body { width: 100%; max-width: 380px; }
.auth .btn { margin-top: 10px; }
.auth .fine { color: var(--muted); font-size: 14px; margin-top: 14px; }
.auth .powered { position: fixed; bottom: 24px; left: 0; right: 0; }
.auth .field-icon { position: relative; }
.auth .field-icon .ic { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.auth .field-icon input { width: 100%; font: inherit; font-size: 17px; padding: 15px 14px 15px 46px; border: 1px solid var(--border); border-radius: 14px; background: #fff; }

/* ---------- Desktop ---------- */
@media (min-width: 960px) {
  .app { flex-direction: row; }
  .sidebar {
    display: flex; flex-direction: column; gap: 28px;
    width: 236px; flex: none; background: var(--navy); color: #fff;
    padding: 28px 18px; position: sticky; top: 0; height: 100dvh;
  }
  .sidebar .brand { color: #fff; font-size: 22px; padding: 0 10px; }
  .sidebar nav { display: flex; flex-direction: column; gap: 4px; }
  .sidebar nav a {
    display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: 12px;
    color: #CBD5E1; font-weight: 500; font-size: 15px;
  }
  .sidebar nav a:hover { background: rgba(255,255,255,0.06); color: #fff; }
  .sidebar nav a.active { background: rgba(37, 99, 235, 0.28); color: #fff; }
  .sidebar nav a.active .ic { color: var(--blue-light); }
  .sidebar .side-foot { margin-top: auto; font-size: 13px; color: #94A3B8; padding: 0 10px; }
  .sidebar .side-foot a { color: #CBD5E1; }
  .main { max-width: 1160px; min-width: 0; margin: 0; padding: 36px 40px 48px; }
  .tabbar { display: none; }
  .topbar { display: flex; justify-content: flex-end; align-items: center; margin-bottom: -40px; position: relative; z-index: 5; }
  .mobile-chip { display: none; }
  .brand-sm { display: none; }
  .page-head h1 { font-size: 34px; }
  .stat-grid { grid-template-columns: repeat(4, 1fr); gap: 16px; }
  .stat-card {
    grid-template-columns: 1fr;
    grid-template-areas: "icon" "label" "num" "badge" "note";
    padding: 20px;
  }
  .stat-badge { justify-self: start; margin-top: 10px; text-align: left; }
  .stat-badge .badge { font-size: 13px; padding: 6px 12px; gap: 5px; white-space: nowrap; }
  .stat-badge .badge .ic { width: 14px; height: 14px; }
  .stat-icon.icon-chip { width: 52px; height: 52px; }
  .stat-label { margin-top: 14px; }
  .stat-num { margin-top: 4px; }
  .two-col { display: grid; grid-template-columns: 1.15fr 1fr; gap: 16px; align-items: start; }
  .summary-grid { grid-template-columns: repeat(3, 1fr); gap: 16px; }
  .table-wrap { margin: 0; padding: 0; }
  .link-row { grid-template-columns: 1fr 2fr 1fr; }
  .form-grid.two { grid-template-columns: 1fr 1fr; }
  .toast { bottom: 24px; }
  .big-num { font-size: 64px; }
}
