/* ============================ Theme tokens ============================ */
:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --sidebar: #0f1729;
  --sidebar-2: #16203a;
  --sidebar-text: #c4cfe3;
  --sidebar-muted: #7d8bab;
  --sidebar-active: #1f6feb22;
  --border: #e4e8ef;
  --text: #18222f;
  --text-muted: #687385;
  --accent: #008060;
  --accent-soft: #e3f1ec;
  --pos: #1f9d55;
  --neg: #d64545;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 1px 3px rgba(16,24,40,.06);
  --chart-grid: #eef1f5;
  --radius: 12px;
}
[data-theme="dark"] {
  --bg: #0b111d;
  --surface: #141d2e;
  --surface-2: #1b2740;
  --sidebar: #0a101c;
  --sidebar-2: #121b2e;
  --sidebar-text: #c4cfe3;
  --sidebar-muted: #6b7795;
  --sidebar-active: #1f6feb33;
  --border: #25324a;
  --text: #e7ecf3;
  --text-muted: #91a0b6;
  --accent: #2fb88f;
  --accent-soft: #143a30;
  --pos: #4ade80;
  --neg: #f87171;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
  --chart-grid: #243248;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45;
}
a { color: inherit; }
h1 { font-size: 22px; margin: 0; }
h3 { margin: 0 0 14px; font-size: 14px; }
h4 { margin: 18px 0 8px; font-size: 13px; }
.muted { color: var(--text-muted); font-weight: 400; font-size: 12.5px; }
.err-text { color: var(--neg); }

/* ============================ Layout ============================ */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 256px; flex-shrink: 0; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* ---- Sidebar ---- */
.side-brand {
  display: flex; align-items: center; gap: 10px; padding: 18px 20px; font-weight: 700;
  font-size: 15px; color: #fff; border-bottom: 1px solid #ffffff14;
}
.side-brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.side-nav { flex: 1; overflow-y: auto; padding: 12px 10px; }
.side-nav::-webkit-scrollbar { width: 8px; }
.side-nav::-webkit-scrollbar-thumb { background: #ffffff1f; border-radius: 8px; }
.side-section {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--sidebar-muted);
  padding: 14px 12px 6px; display: flex; align-items: center; gap: 8px;
}
.side-section .count {
  background: #ffffff1a; color: var(--sidebar-text); border-radius: 20px; padding: 0 7px; font-size: 11px;
}
.side-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 8px;
  text-decoration: none; color: var(--sidebar-text); font-weight: 500; font-size: 13.5px; cursor: pointer;
}
.side-link .ic { width: 18px; text-align: center; opacity: .85; }
.side-link:hover { background: #ffffff12; }
.side-link.active { background: var(--sidebar-active); color: #fff; box-shadow: inset 2px 0 0 var(--accent); }
.side-link.sub { padding-left: 34px; font-size: 13px; color: var(--sidebar-muted); }
.side-link.sub:hover, .side-link.sub.active { color: #fff; }
.side-empty { color: var(--sidebar-muted); padding: 10px 12px; font-size: 12.5px; }

.acct-head {
  display: flex; align-items: center; gap: 8px; width: 100%; background: none; border: none;
  color: var(--sidebar-text); padding: 8px 12px; border-radius: 8px; cursor: pointer; font: inherit;
  font-weight: 600; font-size: 13.5px; text-align: left;
}
.acct-head:hover { background: #ffffff12; }
.acct-head .caret { transition: transform .15s; font-size: 10px; opacity: .7; }
.acct.open .acct-head .caret { transform: rotate(90deg); }
.acct-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct-sub { display: none; }
.acct.open .acct-sub { display: block; }

.side-foot { padding: 10px; border-top: 1px solid #ffffff14; }
.side-foot .side-link { font-weight: 600; }

/* ---- App bar ---- */
.appbar {
  display: flex; align-items: center; gap: 14px; padding: 0 22px; height: 60px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.crumbs { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.crumb { color: var(--text-muted); }
.crumb-cur { color: var(--text); }
.crumb-sep { color: var(--text-muted); opacity: .5; }
.appbar-controls { display: flex; align-items: center; gap: 10px; }
.spacer { flex: 1; }
.tz-note { color: var(--text-muted); font-size: 12px; white-space: nowrap; }
.arrow { color: var(--text-muted); }
#custom-range { display: inline-flex; align-items: center; gap: 6px; }

select, input, .btn {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 11px;
}
input[type="date"] { padding: 7px 9px; }
textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; width: 100%; resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px;
}
select { cursor: pointer; }
.btn { cursor: pointer; background: var(--accent); color: #fff; border: none; font-weight: 600; }
.btn:hover { filter: brightness(1.06); }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: transparent; color: var(--neg); border: 1px solid var(--border); padding: 5px 10px; }
.btn.danger:hover { background: var(--neg); color: #fff; border-color: var(--neg); }
.icon-btn {
  cursor: pointer; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  width: 38px; height: 38px; font-size: 15px; color: var(--text); display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn.ghost { background: transparent; }
#side-toggle { display: none; }

/* ---- Content ---- */
.content { flex: 1; padding: 22px; max-width: 1320px; width: 100%; margin: 0 auto; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 18px; }
.page-head .sub { margin: 4px 0 0; color: var(--text-muted); font-size: 13px; }

/* ---- KPI ---- */
.kpi-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 14px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 15px 16px; box-shadow: var(--shadow); }
.kpi .label { color: var(--text-muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 22px; font-weight: 700; margin: 7px 0 3px; }
.delta { font-size: 12px; font-weight: 600; }
.delta.pos { color: var(--pos); } .delta.neg { color: var(--neg); } .delta.flat { color: var(--text-muted); }

/* ---- Panels & grids ---- */
.grid-2 { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; margin-bottom: 16px; }
.grid-2.even { grid-template-columns: 1fr 1fr; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; margin-bottom: 16px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.panel canvas { max-height: 290px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: none; }
tfoot tr td, tr.total td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; }
td.muted { color: var(--text-muted); }
.linkish { color: var(--accent); cursor: pointer; font-weight: 600; }

/* ---- Profit & Loss statement ---- */
.plhero { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.plhero .pllabel { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); }
.plnet { font-size: 30px; font-weight: 800; line-height: 1.15; font-variant-numeric: tabular-nums; }
.plbadge { padding: 6px 13px; border-radius: 20px; font-weight: 700; font-size: 13px; white-space: nowrap; }
.plbar { display: flex; height: 12px; border-radius: 7px; overflow: hidden; background: var(--surface-2); margin-bottom: 8px; }
.plseg { height: 100%; transition: width .3s; }
.pllegend { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 12px; font-size: 12px; color: var(--text-muted); }
.pllegend i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 5px; vertical-align: middle; }
.plline { display: grid; grid-template-columns: 1fr auto 60px; gap: 12px; padding: 8px 0; border-bottom: 1px dashed var(--border); align-items: baseline; }
.plline .plamt { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.plline .plpct { text-align: right; color: var(--text-muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.plline.plsub > span:first-child { color: var(--text-muted); padding-left: 14px; }
.plline.pltotal { border-top: 2px solid var(--border); border-bottom: none; margin-top: 4px; padding-top: 11px; }
.plline.pltotal > span { font-weight: 800; }
.mpill { padding: 2px 9px; border-radius: 20px; font-weight: 700; font-size: 11.5px; }

.kv { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px dashed var(--border); }
.kv span:first-child { color: var(--text-muted); }
.net-row span { font-weight: 700; color: var(--accent); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 11px; font-weight: 600; background: var(--accent-soft); color: var(--accent); }

/* ---- Tiles (account overview quick links) ---- */
.tiles { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; margin-bottom: 16px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px;
  text-decoration: none; font-weight: 600; display: flex; justify-content: space-between; align-items: center; box-shadow: var(--shadow);
}
.tile:hover { border-color: var(--accent); color: var(--accent); }
.tile .chev { opacity: .6; }

/* ---- Order lookup ---- */
.lookup { display: flex; gap: 10px; margin-bottom: 14px; }
.lookup input { width: 220px; }
.order-head { display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.order-head h3 { margin: 0; font-size: 17px; }
.order-detail { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 12px; }
.order-detail .full { grid-column: 1 / -1; }

/* ---- Settings ---- */
.field { margin-bottom: 13px; }
.field label { display: block; font-weight: 600; margin-bottom: 5px; }
.field input { width: 100%; }
.msg { padding: 10px 13px; border-radius: 8px; margin-bottom: 13px; }
.msg.ok { background: var(--accent-soft); color: var(--accent); }
.msg.err { background: rgba(214,69,69,.12); color: var(--neg); }
.account-row { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.account-row:last-child { border-bottom: none; }
.account-row .meta { flex: 1; }
.account-row .name { font-weight: 600; }
.account-row .dom { color: var(--text-muted); font-size: 12.5px; }
.steps { line-height: 1.8; margin: 0; padding-left: 20px; }
.adspend-form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.adspend-form .field { margin-bottom: 0; }
.adspend-form input, .adspend-form select { min-width: 130px; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }

/* ---- States ---- */
.state { text-align: center; padding: 70px 24px; color: var(--text-muted); }
.state h2 { color: var(--text); }
.state.err { color: var(--neg); }
.state .btn { display: inline-block; text-decoration: none; margin-top: 10px; }
.spinner {
  width: 26px; height: 26px; border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; margin: 0 auto 12px; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }

/* ---- Footer ---- */
.appfoot {
  display: flex; align-items: center; gap: 8px; padding: 14px 22px; border-top: 1px solid var(--border);
  background: var(--surface); color: var(--text-muted); font-size: 12.5px;
}
.dot-sep { opacity: .5; }

/* ---- Scrim (mobile sidebar) ---- */
.scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 25; }

/* ============================ Responsive ============================ */
@media (max-width: 1200px) { .kpi-grid { grid-template-columns: repeat(3, 1fr); } .tiles { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 1024px) {
  .grid-2, .grid-2.even, .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; }
  body.side-open .sidebar { transform: translateX(0); }
  body.side-open .scrim { display: block; }
  #side-toggle { display: inline-flex; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .tiles { grid-template-columns: 1fr 1fr; }
  .order-detail { grid-template-columns: 1fr; }
  .appbar-controls .tz-note { display: none; }
}
