:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #16191f;
  --muted: #5f6774;
  --accent: #0f766e;
  --accent-2: #14b8a6;
  --accent-soft: #e6f4f2;
  --danger: #c2410c;
  --danger-soft: #fdeee6;
  --warn: #a16207;
  --warn-soft: #fdf6e3;
  --ok: #15803d;
  --ok-soft: #e8f6ec;
  --info: #1d4ed8;
  --info-soft: #e8eefc;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 10px;
  --sidebar: 228px;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116; --surface: #161a21; --surface-2: #1d222b; --border: #2a303b; --text: #e7eaf0; --muted: #98a1b0;
    --accent: #2dd4bf; --accent-2: #14b8a6; --accent-soft: #10302d; --danger: #fb923c; --danger-soft: #3a2116;
    --warn: #facc15; --warn-soft: #332a10; --ok: #4ade80; --ok-soft: #12301d; --info: #93b4ff; --info-soft: #18233d;
    --shadow: none; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1116; --surface: #161a21; --surface-2: #1d222b; --border: #2a303b; --text: #e7eaf0; --muted: #98a1b0;
  --accent: #2dd4bf; --accent-2: #14b8a6; --accent-soft: #10302d; --danger: #fb923c; --danger-soft: #3a2116;
  --warn: #facc15; --warn-soft: #332a10; --ok: #4ade80; --ok-soft: #12301d; --info: #93b4ff; --info-soft: #18233d;
  --shadow: none; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 20px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.pos { color: var(--ok); } .neg { color: var(--danger); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack { display: grid; gap: 16px; }
.hidden { display: none !important; }

/* Layout */
.layout { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border); padding: 16px 10px;
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 16px; font-weight: 700; font-size: 16px; }
.brand img { width: 28px; height: 28px; }
.brand small { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text); font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav .badge { margin-left: auto; }
.sidebar .foot { margin-top: auto; padding: 10px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.main { min-width: 0; padding: 20px 24px 40px; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.menu-btn { display: none; }

/* Surfaces */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card-b { padding: 16px; }
.grid { display: grid; gap: 16px; }
.g-kpi { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
.g-2 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.g-half { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.kpi { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 500; }
.kpi .value { font-size: 22px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--muted); }
.chart-box { position: relative; height: 260px; }
.chart-box.sm { height: 220px; }

/* Controls */
button, .btn {
  font: inherit; font-weight: 550; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 12px; border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] button.primary { color: #06201d; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) button.primary { color: #06201d; } }
button.primary:hover { filter: brightness(1.05); }
button.danger { color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; }
button.sm { padding: 4px 8px; font-size: 12px; }
button svg { width: 16px; height: 16px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 1px; }
textarea { resize: vertical; }
label { display: grid; gap: 4px; font-size: 12px; font-weight: 550; color: var(--muted); }
.form { display: grid; gap: 12px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); font-weight: 400; }
.check input { width: auto; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 12.5px; font-weight: 500; }
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tr.clickable { cursor: pointer; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.accent { background: var(--accent); color: #fff; }

.callout { padding: 12px 14px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface-2); }
.callout.warn { background: var(--warn-soft); border-color: transparent; }
.callout.info { background: var(--info-soft); border-color: transparent; }

/* Products */
.products { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
.product { overflow: hidden; cursor: pointer; display: flex; flex-direction: column; }
.product:hover { border-color: var(--accent-2); }
.product .img { aspect-ratio: 1; background: var(--surface-2) center/cover no-repeat; display: grid; place-items: center; color: var(--muted); }
.product .body { padding: 10px 12px; display: grid; gap: 4px; }
.product .name { font-weight: 600; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.6em; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; }
.gallery img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }

/* Drawer & modal */
.overlay { position: fixed; inset: 0; background: rgba(10, 12, 16, .45); z-index: 40; display: flex; justify-content: flex-end; }
.overlay.center { justify-content: center; align-items: center; padding: 16px; }
.drawer { width: min(640px, 100%); height: 100%; background: var(--surface); overflow-y: auto; display: flex; flex-direction: column; }
.modal { width: min(560px, 100%); max-height: 90vh; overflow-y: auto; background: var(--surface); border-radius: var(--radius); }
.drawer-h { position: sticky; top: 0; background: var(--surface); padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; z-index: 1; }
.drawer-b { padding: 18px; display: grid; gap: 18px; }

/* Agent chat */
.chat { display: grid; grid-template-columns: 250px 1fr; gap: 16px; height: calc(100vh - 110px); min-height: 480px; }
.convs-card { overflow: hidden; display: flex; flex-direction: column; }
.convs { overflow-y: auto; display: flex; flex-direction: column; }
.convs .item { padding: 9px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.convs .item:hover { background: var(--surface-2); }
.convs .item.active { background: var(--accent-soft); }
.convs .title { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread { display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 860px; }
.msg.user { align-self: flex-end; background: var(--accent-soft); padding: 9px 13px; border-radius: 12px 12px 2px 12px; white-space: pre-wrap; }
.msg.assistant { align-self: stretch; }
.md p { margin: 0 0 8px; } .md p:last-child { margin: 0; }
.md ul, .md ol { margin: 0 0 8px; padding-left: 22px; }
.md table { margin: 8px 0; font-size: 13px; width: auto; }
.md th, .md td { padding: 5px 10px; border: 1px solid var(--border); }
.md pre { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md h1, .md h2, .md h3 { margin: 12px 0 6px; font-size: 15px; }
.tool { font-size: 12.5px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.tool summary { padding: 6px 10px; cursor: pointer; display: flex; gap: 8px; align-items: center; list-style: none; }
.tool summary::-webkit-details-marker { display: none; }
.tool pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--border); white-space: pre-wrap; word-break: break-word; max-height: 300px; overflow-y: auto; }
.thinking { font-size: 12.5px; color: var(--muted); border-left: 3px solid var(--border); padding: 2px 10px; white-space: pre-wrap; }
.composer { border-top: 1px solid var(--border); padding: 12px; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 200px; }
.suggestions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: 12px; }
.dots::after { content: '…'; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }

/* Auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(420px, 100%); }
.auth .brand { justify-content: center; padding: 8px 0 4px; font-size: 20px; }

#toasts { position: fixed; bottom: 16px; right: 16px; display: grid; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; max-width: 380px; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
.toast.error { background: var(--danger); color: #fff; }

/* Narrow: phone and Chrome side panel */
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; z-index: 50; left: 0; top: 0; width: 250px; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgba(0,0,0,.2); }
  .layout.nav-open .sidebar { transform: none; }
  .menu-btn { display: inline-flex; }
  .main { padding: 14px 16px 32px; }
  .g-2, .g-half { grid-template-columns: minmax(0, 1fr); }
  .topbar button, .topbar .btn { white-space: normal; }
  .chat { grid-template-columns: minmax(0, 1fr); height: calc(100vh - 90px); }
  .chat .convs-card { display: none; }
  .chat.show-convs .convs-card { display: block; position: fixed; inset: 60px 16px auto; max-height: 60vh; z-index: 30; }
  .kpi .value { font-size: 19px; }
}

/* HTML bar lists (status, expense categories) */
.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 140px) 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.bar-track { height: 10px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.bell { position: relative; padding: 6px 8px; }
.bell svg { width: 20px; height: 20px; }
.bell .badge { position: absolute; top: -2px; right: -4px; font-size: 10px; padding: 0 5px; }
a.btn { color: var(--text); text-decoration: none; }
.notif { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--border); align-items: start; }
.notif.unread { background: var(--accent-soft); }
.dot { width: 9px; height: 9px; border-radius: 50%; margin-top: 6px; background: var(--info); }
.dot.warn { background: var(--warn); } .dot.critical { background: var(--danger); }
.score { display: inline-grid; place-items: center; min-width: 38px; padding: 2px 6px; border-radius: 6px; font-weight: 700; font-variant-numeric: tabular-nums; }
.score.hi { background: var(--ok-soft); color: var(--ok); } .score.mid { background: var(--warn-soft); color: var(--warn); } .score.lo { background: var(--surface-2); color: var(--muted); }
.copybox { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.inline-input { padding: 3px 6px; font-size: 12.5px; max-width: 140px; }

/* Grid children default to min-width:auto and would grow to fit wide tables;
   allow them to shrink so .table-wrap scrolls inside the card instead of the page. */
.stack > *, .grid > *, .drawer-b > *, .card, .card-b { min-width: 0; }
