/* ============ Theme tokens ============ */
:root,
[data-theme="dark"] {
  --bg: #0b0f17;
  --surface: #131826;
  --surface-2: #1a2030;
  --border: #232a3b;
  --text: #e7ebf3;
  --muted: #8a93a6;
  --primary: #7c5cff;
  --primary-2: #9b7bff;
  --primary-contrast: #ffffff;
  --sidebar-bg: #0d1220;
  --sidebar-text: #aeb6c7;
  --topbar-grad: linear-gradient(90deg, #4c1d95 0%, #7c3aed 55%, #8b5cf6 100%);
  --ok: #34d399;
  --danger: #f87171;
  --shadow: 0 8px 24px rgba(0,0,0,.35);
}
[data-theme="light"] {
  --bg: #f3f4f8;
  --surface: #ffffff;
  --surface-2: #f7f8fc;
  --border: #e6e8ef;
  --text: #1c2230;
  --muted: #6b7280;
  --primary: #6d4dff;
  --primary-2: #8b6dff;
  --primary-contrast: #ffffff;
  --sidebar-bg: #14111f;
  --sidebar-text: #c3c8d6;
  --topbar-grad: linear-gradient(90deg, #6d28d9 0%, #7c3aed 55%, #8b5cf6 100%);
  --ok: #0a9d64;
  --danger: #dc2626;
  --shadow: 0 8px 24px rgba(20,20,40,.10);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--primary-2); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ============ App shell ============ */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: 248px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  position: relative;
  transition: width .18s ease;
  border-right: 1px solid var(--border);
}
.side-top {
  position: relative;
  padding: 1.1rem 1rem 1.25rem;
  text-align: center;
  border-bottom: 1px solid rgba(255,255,255,.06);
}
.org-name { font-size: .72rem; letter-spacing: .08em; color: var(--muted); font-weight: 700; }
.brand-logo { width: 42px; height: 42px; border-radius: 10px; display: block; margin: 0 auto .55rem; }
.nav-collapsed .brand-logo { width: 30px; height: 30px; border-radius: 8px; margin-bottom: 0; }
.auth-logo { width: 64px; height: 64px; border-radius: 15px; display: block; margin: 0 auto 1rem; }
.avatar {
  width: 84px; height: 84px; border-radius: 50%;
  margin: .9rem auto .6rem;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  color: #fff; font-size: 2.2rem; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 18px rgba(124,92,255,.45);
}
.user-name { font-weight: 700; color: #fff; font-size: .95rem; }
.user-role { font-size: .68rem; letter-spacing: .08em; color: var(--muted); text-transform: uppercase; }
.collapse-btn {
  position: absolute; right: -14px; top: 96px;
  width: 28px; height: 28px; padding: 0; border-radius: 50%;
  background: var(--primary); color: #fff; border: 3px solid var(--bg);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.collapse-btn:hover { filter: brightness(1.05); }

.side-nav { padding: .75rem .6rem; display: flex; flex-direction: column; gap: .15rem; }
.nav-section {
  font-size: .68rem; letter-spacing: .09em; color: var(--muted);
  font-weight: 700; padding: .9rem .65rem .4rem; text-transform: uppercase;
}
.nav-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .65rem .7rem; border-radius: 10px;
  color: var(--sidebar-text); font-size: .92rem; font-weight: 500;
  white-space: nowrap; cursor: pointer;
}
.nav-item:hover { background: rgba(255,255,255,.05); text-decoration: none; color: #fff; }
.nav-item.active {
  background: linear-gradient(90deg, rgba(124,92,255,.35), rgba(124,92,255,.12));
  color: #fff; box-shadow: inset 3px 0 0 var(--primary);
}
.nav-item.disabled { opacity: .45; cursor: default; }
.nav-item.disabled:hover { background: none; color: var(--sidebar-text); }
.nav-item svg { flex-shrink: 0; }
.soon { font-size: .6rem; background: rgba(255,255,255,.1); padding: .05rem .35rem; border-radius: 6px; font-style: normal; margin-left: .25rem; }

/* Collapsed rail */
.nav-collapsed .sidebar { width: 72px; }
.nav-collapsed .org-name,
.nav-collapsed .user-name,
.nav-collapsed .user-role,
.nav-collapsed .nav-section,
.nav-collapsed .nav-item span { display: none; }
.nav-collapsed .avatar { width: 40px; height: 40px; font-size: 1.1rem; }
.nav-collapsed .nav-item { justify-content: center; padding: .65rem; }
.nav-collapsed .collapse-btn svg { transform: rotate(180deg); }

/* ============ Main + topbar ============ */
.main-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 56px; background: var(--topbar-grad); color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1.25rem; position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-weight: 700; font-size: 1.05rem; }
.topbar-actions { display: flex; align-items: center; gap: .35rem; }
.icon-btn {
  width: 38px; height: 38px; padding: 0; border-radius: 10px; border: 0;
  background: rgba(255,255,255,.18); color: #fff; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.icon-btn:hover { background: rgba(255,255,255,.32); }
.icon-btn svg { display: block; }

.content { padding: 1.4rem 1.6rem; flex: 1; }
.content-head { display: flex; align-items: center; gap: .9rem; margin-bottom: 1.2rem; }
.home-btn {
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  display: inline-flex; align-items: center; justify-content: center;
}
.home-btn:hover { color: var(--primary-2); text-decoration: none; }
.secondary-nav { display: flex; gap: .35rem; flex-wrap: wrap; }
.secondary-nav .badge-num { margin-left: .25rem; }
.secondary-nav a {
  padding: .4rem .8rem; border-radius: 8px; color: var(--muted); font-size: .9rem; font-weight: 600;
}
.secondary-nav a:hover { background: var(--surface-2); text-decoration: none; }
.secondary-nav a.on { background: var(--surface); color: var(--text); border: 1px solid var(--border); }

/* ============ Auth (logged out) ============ */
.auth-content main { max-width: 440px; margin: 2.5rem auto; }

/* ============ Components ============ */
main { max-width: 1080px; }
h1, h2, h3, h4 { color: var(--text); }
section {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 1.1rem 1.3rem; margin-bottom: 1.25rem; box-shadow: var(--shadow);
}
section.narrow { max-width: 420px; margin: 1.5rem auto; }

form.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
form.stack { display: flex; flex-direction: column; gap: .75rem; }
form.stack label { display: flex; flex-direction: column; gap: .25rem; font-size: .9rem; color: var(--muted); }
form.inline { display: inline; }
label.check { display: flex; flex-direction: row; align-items: center; gap: .4rem; font-size: .9rem; }
label.check input { width: auto; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.grid2 label { display: flex; flex-direction: column; gap: .25rem; font-size: .9rem; color: var(--muted); }

input, select {
  padding: .55rem .6rem; border: 1px solid var(--border); border-radius: 9px;
  font-size: 1rem; background: var(--surface-2); color: var(--text);
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 0; }

button, .btn {
  padding: .55rem 1.05rem; border: 0; border-radius: 9px;
  background: var(--primary); color: var(--primary-contrast); cursor: pointer;
  text-decoration: none; display: inline-block; font-size: .95rem; font-weight: 600;
}
button:hover, .btn:hover { filter: brightness(1.08); text-decoration: none; }
button.danger { background: var(--danger); }
button.link { background: none; color: var(--primary-2); padding: 0; text-decoration: underline; font-weight: 600; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .5rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-size: .82rem; text-transform: uppercase; letter-spacing: .03em; }
table.kv th { width: 200px; text-transform: none; letter-spacing: 0; font-weight: 600; }

.muted { color: var(--muted); font-size: .9rem; }
.warn { color: #f59e0b; font-weight: 600; }
.error { color: var(--danger); background: rgba(248,113,113,.12); padding: .55rem .8rem; border-radius: 9px; }
.ok { color: var(--ok); }
.badge { font-size: .72rem; padding: .15rem .5rem; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.ok-badge { background: rgba(52,211,153,.16); color: var(--ok); }
.danger-badge { background: rgba(248,113,113,.16); color: var(--danger); }
.over-limit { color: var(--danger); }
.stale-badge { background: rgba(245,158,11,.16); color: #f59e0b; }
.row-stale td { opacity: .6; }
.badge-num { background: var(--danger); color: #fff; border-radius: 999px; padding: 0 .4rem; font-size: .68rem; font-style: normal; margin-left: .3rem; }
.review-banner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; background: rgba(245,158,11,.12); border: 1px solid rgba(245,158,11,.4); border-radius: 10px; padding: .8rem 1rem; margin-top: .6rem; }

.cat-name { display: inline-flex; align-items: center; gap: .45rem; }
.cat-ico { color: var(--primary-2); flex-shrink: 0; }

.presets { display: flex; flex-wrap: wrap; gap: .6rem; margin-bottom: 1.1rem; }
.preset {
  display: inline-flex; align-items: center; gap: .6rem; text-align: left;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 12px; padding: .6rem .9rem; cursor: pointer; font-weight: 600;
}
.preset .cat-ico { width: 26px; height: 26px; color: var(--primary-2); }
.preset span { display: flex; flex-direction: column; line-height: 1.15; }
.preset strong { font-size: .9rem; }
.preset em { font-style: normal; font-size: .75rem; color: var(--muted); }
.preset:hover { border-color: var(--primary); background: rgba(124,92,255,.10); }

.icon-picker { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .4rem; }
.icon-opt {
  width: 48px; height: 48px; border: 1.5px solid var(--border); border-radius: 12px;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  color: var(--muted); background: var(--surface-2); transition: all .12s ease;
}
.icon-opt:hover { color: var(--text); border-color: var(--primary-2); }
.icon-opt input { position: absolute; opacity: 0; width: 0; height: 0; }
.icon-opt:has(input:checked) {
  border-color: var(--primary); color: var(--primary-2);
  background: rgba(124,92,255,.16); box-shadow: 0 0 0 3px rgba(124,92,255,.18);
}
.danger-link { color: var(--danger); text-decoration: none; font-weight: 700; }
.filters-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; align-items: start; }
.filters-grid > label, .filters-col > label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); margin-bottom: .5rem; }
.filters-col { display: flex; flex-direction: column; }
.filters-actions { display: flex; align-items: flex-end; gap: .5rem; }
.btn-secondary { padding: .55rem 1rem; border-radius: 9px; background: var(--surface-2); color: var(--text); border: 1px solid var(--border); text-decoration: none; font-weight: 600; }
.builder-bar { display: flex; align-items: center; gap: .5rem; margin-bottom: .9rem; }
.builder-bar .grow { flex: 1; }
.add-filter { min-width: 200px; font-weight: 600; }
.active-filters { display: flex; flex-direction: column; gap: .5rem; }
.filter-row {
  display: flex; align-items: center; gap: .6rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: .5rem .7rem;
}
.filter-row .filter-label { min-width: 160px; font-weight: 600; font-size: .9rem; }
.filter-row .filter-control { flex: 1; }
.filter-row .filter-control select, .filter-row .filter-control input { width: 100%; }
.filter-remove {
  background: none; border: 0; color: var(--danger); font-weight: 700; cursor: pointer;
  padding: .2rem .5rem; border-radius: 6px;
}
.filter-remove:hover { background: rgba(248,113,113,.14); }
@media (max-width: 700px) { .filter-row { flex-wrap: wrap; } .filter-row .filter-label { min-width: 0; } }

.report-toolbar { display: flex; align-items: center; gap: .5rem; margin-bottom: .75rem; }
.report-toolbar .grow { flex: 1; }
.totals-row td { border-top: 2px solid var(--border); }
@media print { .report-filters, .report-toolbar { display: none !important; } }

.tt-filter-row th { padding: .25rem .4rem .5rem; border-bottom: 2px solid var(--border); }
.tt-filter-row .tt-colf { width: 100%; padding: .3rem .4rem; font-size: .82rem; font-weight: 400; border-radius: 7px; }
@media print { .tt-filter-row { display: none !important; } }

.table-controls { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .75rem; }
.table-controls input[type="search"] { min-width: 220px; }

.acct-toolbar { display: flex; gap: .5rem; align-items: center; margin-bottom: .75rem; }
.acct-toolbar input[type="search"] { min-width: 240px; }
.acct-toolbar .grow { flex: 1; }
.tool-btn {
  width: 38px; height: 38px; padding: 0; border-radius: 9px;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.tool-btn:hover { border-color: var(--primary); color: var(--primary-2); }
.dropdown { position: relative; }
.dropdown-menu {
  display: none; position: absolute; right: 0; top: 44px; z-index: 30; min-width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: .6rem .7rem;
}
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); margin-bottom: .5rem; }
.dropdown-menu label.check { flex-direction: row; align-items: center; gap: .4rem; color: var(--text); }
.dropdown-menu a.export-link, .dropdown-menu a[data-tt-print], .dropdown-menu a.print-link {
  display: flex; align-items: center; gap: .55rem;
  padding: .45rem .55rem; border-radius: 7px; color: var(--text); font-weight: 600;
}
.dropdown-menu a.export-link:hover, .dropdown-menu a[data-tt-print]:hover,
.dropdown-menu a.print-link:hover { background: var(--surface-2); text-decoration: none; }
.dropdown-menu a[data-tt-print], .dropdown-menu a.print-link { border-top: 1px solid var(--border); margin-top: .25rem; }

@media print {
  .sidebar, .topbar, .content-head { display: none !important; }
  body.tt-printing .content > main > section:not(.tt-print-area) { display: none !important; }
  body.tt-printing .tt-print-area .acct-toolbar,
  body.tt-printing .tt-print-area .table-controls,
  body.tt-printing .tt-print-area .col-actions { display: none !important; }
  .tt-print-area { box-shadow: none !important; border: none !important; }
  .content { padding: 0 !important; }
}

/* ============ Stat cards ============ */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 1.1rem 1.2rem; display: flex; align-items: flex-start; justify-content: space-between;
  box-shadow: var(--shadow);
}
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 2rem; font-weight: 800; margin-top: .35rem; color: var(--text); }
.stat .ico {
  width: 44px; height: 44px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center; font-size: 1.2rem;
  background: rgba(124,92,255,.18); color: var(--primary-2);
}
.stat.green .value { color: var(--ok); } .stat.green .ico { background: rgba(52,211,153,.16); color: var(--ok); }
.stat.blue .value { color: #60a5fa; } .stat.blue .ico { background: rgba(96,165,250,.16); color: #60a5fa; }

/* ============ Responsive ============ */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    transform: translateX(-100%); width: 248px;
  }
  .nav-collapsed .sidebar { transform: translateX(0); width: 248px; }
  .nav-collapsed .org-name, .nav-collapsed .user-name, .nav-collapsed .user-role,
  .nav-collapsed .nav-section, .nav-collapsed .nav-item span { display: revert; }
  .nav-collapsed .avatar { width: 84px; height: 84px; font-size: 2.2rem; }
  .main-wrap { width: 100%; }
  .grid2 { grid-template-columns: 1fr; }
}

/* ---- Topbar company switcher ---- */
.company-switch .switch-btn {
  display: flex; align-items: center; gap: .45rem; height: 34px; padding: 0 .7rem;
  border-radius: 9px; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font-weight: 600; font-size: .88rem; cursor: pointer; max-width: 260px;
}
.company-switch .switch-btn:hover { border-color: var(--muted); }
.company-switch .switch-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 160px; }
.switch-menu { min-width: 240px; max-height: 60vh; overflow-y: auto; }
.switch-menu .switch-item {
  display: flex; align-items: center; gap: .55rem; padding: .45rem .55rem;
  border-radius: 7px; color: var(--text); font-weight: 600; text-decoration: none;
}
.switch-menu .switch-item:hover { background: var(--surface-2); text-decoration: none; }
.switch-menu .switch-item.on { background: var(--surface-2); }
.switch-menu .switch-all {
  display: block; padding: .45rem .55rem; margin-top: .25rem; border-top: 1px solid var(--border);
  color: var(--muted); font-weight: 600; font-size: .85rem; text-decoration: none;
}
.switch-menu .switch-all:hover { color: var(--text); }

/* ---- Company chooser (QBO-style) ---- */
/* On bare pages (chooser / create) center the card both axes, with scroll if tall. */
.app.bare .content {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 2rem 1.6rem; overflow-y: auto;
}
.app.bare main { width: 100%; display: flex; flex-direction: column; align-items: center; }
.chooser { width: 100%; max-width: 560px; margin: 0 auto; }
.chooser-head { text-align: center; margin-bottom: 1rem; }
.chooser-head h2 { margin-bottom: .15rem; }
.chooser-user { font-weight: 600; color: var(--text); margin-bottom: .1rem; }
.chooser-search { position: relative; display: flex; align-items: center; margin-bottom: 1rem; }
.chooser-search svg { position: absolute; left: .75rem; color: var(--muted); }
.chooser-search input { width: 100%; padding: .7rem .8rem .7rem 2.4rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 1rem; }
.company-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .6rem; }
.company-card {
  display: flex; align-items: center; gap: .5rem; padding: .35rem .6rem .35rem .35rem;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
  transition: border-color .12s ease, box-shadow .12s ease;
}
.company-card:hover { border-color: var(--muted); box-shadow: var(--shadow); }
.company-card.is-default { border-color: var(--accent, #6d28d9); }
.company-open { display: flex; align-items: center; gap: .7rem; flex: 1; padding: .5rem; text-decoration: none; }
.company-open:hover { text-decoration: none; }
.company-avatar {
  width: 40px; height: 40px; border-radius: 10px; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 800; font-size: 1.05rem;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
}
.company-avatar.sm { width: 26px; height: 26px; font-size: .8rem; border-radius: 7px; }
.company-meta { display: flex; flex-direction: column; gap: .1rem; min-width: 0; }
.company-name { font-weight: 700; color: var(--text); }
.company-role { font-size: .76rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.star-btn { display: grid; place-items: center; width: 38px; height: 38px; flex-shrink: 0;
  border: none; background: none; padding: 0; cursor: pointer; color: var(--muted); border-radius: 9px; }
.star-btn:hover { background: var(--surface-2); color: var(--text); }
.star-btn.on { color: #f5a623; }
.chooser .hint { margin-top: 1rem; text-align: center; }
.chooser-create { margin-top: 1.1rem; padding-top: 1.1rem; border-top: 1px solid var(--border); text-align: center; }
.btn-create {
  display: inline-flex; align-items: center; gap: .45rem; padding: .55rem 1rem;
  border-radius: 10px; border: 1px dashed var(--border); background: var(--surface-2);
  color: var(--text); font-weight: 600; text-decoration: none;
}
.btn-create:hover { border-color: var(--muted); text-decoration: none; }

/* Topbar link (bare layout) — matches .icon-btn so it reads on the gradient */
.topbar-link {
  display: inline-flex; align-items: center; gap: .4rem; height: 38px; padding: 0 .8rem;
  border-radius: 10px; border: 0; background: rgba(255,255,255,.18);
  color: #fff; font-weight: 600; font-size: .85rem; text-decoration: none;
}
.topbar-link svg { display: block; }
.topbar-link:hover { background: rgba(255,255,255,.32); text-decoration: none; }
