:root {
  color-scheme: light;
  --bg: #f3f3f1; --surface: #fcfcfb; --surface-2: #f0efec; --surface-3: #e6e5e1;
  --border: #dddcd7; --border-strong: #c9c8c2;
  --text: #0b0b0b; --text-2: #52514e; --text-3: #7a7974;
  --accent: #2a78d6; --accent-ink: #1c5cab; --accent-soft: #e3eefb;
  --top: #1f2a37; --top-text: #f3f4f6; --top-muted: #9aa5b4; --top-ctl: #2c3a4b;
  --good: #0a7d0a; --bad: #c43434; --warn: #b87900;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --surface-3: #2e2e2b;
    --border: #33332f; --border-strong: #45453f;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
    --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #1b2c42;
    --top: #0d1117; --top-text: #f0efec; --top-muted: #8b949e; --top-ctl: #1c2430;
    --good: #3cc13c; --bad: #e66767; --warn: #e3a52a;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --surface-3: #2e2e2b;
  --border: #33332f; --border-strong: #45453f;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
  --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #1b2c42;
  --top: #0d1117; --top-text: #f0efec; --top-muted: #8b949e; --top-ctl: #1c2430;
  --good: #3cc13c; --bad: #e66767; --warn: #e3a52a;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.4 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.err { color: var(--bad); min-height: 18px; font-size: 13px; }

/* brand */
.brand { display: flex; align-items: center; gap: 8px; font-size: 17px; letter-spacing: .2px; white-space: nowrap; }
.brand b { font-weight: 700; }
.brand-mark { width: 22px; height: 22px; border-radius: 5px; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect x='7' y='16' width='4' height='9' rx='1' fill='white'/%3E%3Crect x='14' y='10' width='4' height='15' rx='1' fill='white'/%3E%3Crect x='21' y='6' width='4' height='19' rx='1' fill='white'/%3E%3C/svg%3E") center/100% no-repeat; }
.brand.big { font-size: 26px; justify-content: center; }
.brand.big .brand-mark { width: 32px; height: 32px; }

/* buttons */
.btn { border: 1px solid var(--border-strong); background: var(--surface); border-radius: 7px; padding: 6px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--bad); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; cursor: pointer; width: 30px; height: 30px; border-radius: 6px; color: inherit; }
.icon-btn:hover { background: var(--surface-2); }
.badge { background: var(--accent); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; margin-left: 4px; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; background: radial-gradient(circle at 30% 20%, var(--accent-soft), var(--bg) 60%); }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 12px; }
.login-card p { text-align: center; margin: 0 0 6px; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); }
input, select { background: var(--surface); border: 1px solid var(--border-strong); border-radius: 7px; padding: 7px 9px; color: var(--text); min-width: 0; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* shell */
.shell { display: grid; grid-template-columns: 220px 1fr auto; grid-template-rows: auto 1fr; height: 100vh; }
.shell.nav-collapsed { grid-template-columns: 0 1fr auto; }
.shell.nav-collapsed .nav { visibility: hidden; }
.topbar { grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; padding: 8px 14px; background: var(--top); color: var(--top-text); flex-wrap: wrap; }
.topbar .icon-btn:hover, .topbar .btn.ghost:hover { background: rgba(255,255,255,.08); }
.topbar .btn.ghost { color: var(--top-text); }
.top-controls { display: flex; gap: 10px; flex-wrap: wrap; flex: 1; align-items: center; }
.ctl { display: flex; align-items: center; gap: 6px; color: var(--top-muted); font-size: 12px; }
.ctl select, .ctl input { background: var(--top-ctl); border-color: transparent; color: var(--top-text); padding: 5px 8px; color-scheme: dark; }
#companySel { max-width: 260px; }
.top-actions { display: flex; gap: 4px; align-items: center; }
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: calc(100% + 6px); background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); min-width: 230px; padding: 4px; z-index: 50; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--surface-2); }

.nav { background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; padding: 10px 8px; display: flex; flex-direction: column; gap: 4px; }
.nav h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--text-3); margin: 12px 8px 4px; }
.nav-item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 7px; cursor: pointer; color: var(--text); }
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.nav-item .ico { width: 18px; text-align: center; color: var(--text-3); }
.nav-item.active .ico { color: var(--accent); }
.nav-foot { margin-top: auto; font-size: 11px; padding: 8px; }

.main { overflow-y: auto; padding: 14px 18px 40px; min-width: 0; }
.pagebar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.pagebar h1 { font-size: 20px; margin: 0; font-weight: 600; }
#pageTitleInput { font-size: 18px; font-weight: 600; width: 320px; }
.page-actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.page-actions > span { display: flex; gap: 6px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent-ink); border-radius: 14px; padding: 3px 4px 3px 10px; font-size: 12px; max-width: 360px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { border: 0; background: none; color: inherit; cursor: pointer; border-radius: 50%; width: 20px; height: 20px; }
.chip button:hover { background: rgba(0,0,0,.08); }
.chip.cross { background: var(--surface-3); color: var(--text); }

/* canvas */
.canvas { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 12px; grid-auto-flow: row dense; }
.vis { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 0; position: relative; }
.vis.h1 { height: 128px; } .vis.h2 { height: 340px; } .vis.h3 { height: 540px; } .vis.h4 { height: 740px; }
.vis-head { display: flex; align-items: center; gap: 6px; padding: 8px 10px 0 14px; min-height: 34px; }
.vis-title { font-weight: 600; font-size: 13.5px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vis-sub { font-size: 11px; color: var(--text-3); white-space: nowrap; }
.vis-tools { display: flex; gap: 1px; opacity: 0; transition: opacity .12s; }
.vis:hover .vis-tools, .vis:focus-within .vis-tools, .editing .vis-tools { opacity: 1; }
.vis-tools button { border: 0; background: none; cursor: pointer; width: 26px; height: 26px; border-radius: 5px; color: var(--text-2); font-size: 13px; }
.vis-tools button:hover { background: var(--surface-2); color: var(--text); }
.vis-tools button.on { background: var(--accent-soft); color: var(--accent-ink); }
.vis-body { flex: 1; min-height: 0; position: relative; padding: 2px 8px 8px; }
.vis-body .chart { position: absolute; inset: 2px 8px 8px; }
.vis.selected { outline: 2px solid var(--accent); }
.editing .vis { cursor: pointer; }
.editing .vis:hover { border-color: var(--accent); }
.vis .loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--text-3); font-size: 12px; }
.vis .msg { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 16px; color: var(--text-2); font-size: 13px; }
.vis .msg.error { color: var(--bad); }
.drill-path { font-size: 11px; color: var(--accent-ink); padding: 0 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { padding: 60px 20px; text-align: center; color: var(--text-2); border: 2px dashed var(--border-strong); border-radius: var(--radius); }

/* KPI tiles */
.kpis { display: flex; height: 100%; gap: 0; }
.kpis.vertical { flex-direction: column; }
.kpi { flex: 1; min-width: 0; padding: 4px 14px; display: flex; flex-direction: column; justify-content: center; border-left: 1px solid var(--border); }
.kpis.vertical .kpi { border-left: 0; border-top: 1px solid var(--border); }
.kpi:first-child { border: 0 !important; }
.kpi .k-label { font-size: 12px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .k-val { font-size: 26px; font-weight: 600; letter-spacing: -.3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.kpi .k-cmp { font-size: 12px; color: var(--text-2); white-space: nowrap; }
.kpi .up { color: var(--good); } .kpi .down { color: var(--bad); }
.card-single { height: 100%; display: flex; flex-direction: column; justify-content: center; padding: 0 8px; }
.card-single .k-val { font-size: 38px; }

/* tables */
.tbl-wrap { position: absolute; inset: 2px 8px 8px; overflow: auto; }
table.tbl { border-collapse: collapse; width: 100%; font-size: 12.5px; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.tbl th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; cursor: pointer; z-index: 2; user-select: none; }
.tbl th.num, .tbl td.num { text-align: right; }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl tbody tr.sel { background: var(--accent-soft); }
.tbl tfoot td { position: sticky; bottom: 0; background: var(--surface-2); font-weight: 600; border-top: 1px solid var(--border-strong); z-index: 2; }
.tbl td.dim { max-width: 360px; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.tbl td.dim:hover { text-decoration: underline; }
.tbl .bar-cell { position: relative; }
.tbl .bar-cell i { position: absolute; left: 0; top: 4px; bottom: 4px; background: var(--accent-soft); border-radius: 0 3px 3px 0; z-index: 0; }
.tbl .bar-cell span { position: relative; z-index: 1; }
.tbl td.neg { color: var(--bad); }

/* slicer */
.slicer { position: absolute; inset: 2px 8px 8px; display: flex; flex-direction: column; gap: 6px; }
.slicer input[type=search] { width: 100%; }
.slicer ul { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; }
.slicer li label { display: flex; flex-direction: row; align-items: center; gap: 8px; padding: 3px 2px; color: var(--text); cursor: pointer; font-size: 13px; }

/* side panes */
.side { grid-column: 3; grid-row: 2; width: 300px; background: var(--surface); border-left: 1px solid var(--border); overflow-y: auto; padding: 12px 14px 30px; }
.wide-side { width: 340px; }
.side-head { display: flex; align-items: center; justify-content: space-between; }
.side h2 { font-size: 15px; margin: 0; }
.side h4 { margin: 16px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--text-3); }
.filter-card { border: 1px solid var(--border); border-radius: 8px; padding: 8px; margin: 10px 0; }
.filter-card .fc-head { display: flex; align-items: center; justify-content: space-between; font-weight: 600; font-size: 13px; }
.filter-card .fc-vals { max-height: 220px; overflow: auto; margin-top: 6px; }
.filter-card .fc-vals label { display: flex; flex-direction: row; gap: 8px; align-items: center; color: var(--text); padding: 2px 0; font-size: 12.5px; cursor: pointer; }
.filter-card input[type=search] { width: 100%; margin-top: 6px; padding: 5px 8px; }
.add-filter select { width: 100%; margin: 10px 0; }

/* designer */
.types { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; }
.types button { border: 1px solid var(--border); background: var(--surface); border-radius: 7px; padding: 6px 2px; cursor: pointer; font-size: 10.5px; display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--text-2); }
.types button b { font-size: 17px; font-weight: 400; color: var(--text); }
.types button.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-ink); }
.field-list { border: 1px solid var(--border); border-radius: 8px; max-height: 230px; overflow: auto; padding: 4px; }
.field-list .grp { font-size: 11px; color: var(--text-3); padding: 6px 6px 2px; text-transform: uppercase; letter-spacing: .5px; }
.field-list label { display: flex; flex-direction: row; gap: 8px; align-items: center; color: var(--text); padding: 3px 6px; cursor: pointer; font-size: 13px; border-radius: 5px; }
.field-list label:hover { background: var(--surface-2); }
.wells { display: grid; gap: 6px; }
.well { border: 1px dashed var(--border-strong); border-radius: 7px; padding: 6px; min-height: 34px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.well .wl { font-size: 11px; color: var(--text-3); width: 100%; }
.pill { display: inline-flex; align-items: center; gap: 4px; background: var(--surface-3); border-radius: 12px; padding: 2px 4px 2px 9px; font-size: 12px; }
.pill button { border: 0; background: none; cursor: pointer; color: var(--text-2); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row2 label, .side label { margin-bottom: 6px; }
.check { display: flex !important; flex-direction: row !important; align-items: center; gap: 8px; color: var(--text) !important; }
.hint { font-size: 12px; color: var(--text-3); margin: 4px 0; }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal-card { background: var(--surface); border-radius: 12px; width: min(1100px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal-card.narrow { width: min(460px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px; overflow: auto; position: relative; }
.modal-body.focus { height: 70vh; }
.form-grid { display: grid; gap: 10px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.users-tbl td, .users-tbl th { padding: 6px 8px; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--top); color: var(--top-text); padding: 9px 16px; border-radius: 8px; z-index: 200; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }
.toast.error { background: var(--bad); color: #fff; }

/* responsive */
@media (max-width: 1100px) {
  .vis { grid-column: span 6 !important; }
  .vis.full { grid-column: span 12 !important; }
}
@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto 1fr; height: auto; min-height: 100vh; }
  .top-actions { flex-wrap: wrap; margin-left: auto; }
  .top-actions .btn { padding: 6px 8px; }
  .brand { font-size: 15px; }
  .ctl { flex: 1 1 140px; }
  .ctl select, .ctl input { flex: 1; min-width: 0; }
  .nav { grid-column: 1; border-right: 0; border-bottom: 1px solid var(--border); flex-direction: row; flex-wrap: wrap; padding: 6px 16px; }
  .nav h3, .nav-foot { display: none; }
  .nav-item { width: auto; }
  .shell.nav-collapsed .nav { display: none; }
  .main { padding: 12px 16px 40px; overflow: visible; }
  .side { position: fixed; inset: 0 0 0 auto; width: min(360px, 100vw); z-index: 60; box-shadow: var(--shadow); }
  .vis { grid-column: span 12 !important; }
  .vis.h1 { height: auto; min-height: 110px; }
  .kpis { flex-wrap: wrap; }
  .kpi { flex: 1 1 45%; border-left: 0; padding: 8px 4px; }
  .kpi .k-val { font-size: 22px; }
  .topbar { padding: 8px 16px; }
  .top-controls { order: 3; width: 100%; }
  .ctl.date input { width: 135px; }
  #pageTitleInput { width: 100%; }
}

/* point menu & drill-through */
.point-menu { position: fixed; z-index: 150; min-width: 240px; max-width: 360px; }
.point-menu .pm-head { font-size: 11.5px; color: var(--text-2); padding: 6px 10px 4px; border-bottom: 1px solid var(--border); margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dt-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.dt-head .chips { flex: 1; }
.dt-sum { font-size: 13px; white-space: nowrap; }
.dt-body { position: relative; height: calc(70vh - 60px); min-height: 260px; }
.tbl td.cell { cursor: context-menu; }
.modal-card.wide { width: min(1440px, 100%); }
.dt-body .tbl td.dim { max-width: 240px; }
