:root {
  --bg: #f2f4f7; --panel: #ffffff; --panel2: #f7f9fb; --text: #18232e; --muted: #5b6977; --line: #dbe2e8;
  --brand: #14304a; --accent: #0d7a70; --accent-hover: #0a655d; --accent-ink: #ffffff; --link: #0a64c2;
  --p1: #c62828; --p2: #d9650b; --p3: #b38900; --p4: #2e7d32; --p5: #5f7a88;
  --ok: #2e7d32; --warn: #a86400; --bad: #c62828; --ok-bg: #e7f4e8; --warn-bg: #fff3dc; --bad-bg: #fde8e8;
  --nav: #1b2833; --nav-text: #d5dfe7; --nav-muted: #86a0b4; --nav-hover: #253746; --nav-active: #0d7a70;
  --code: #0f1820; --code-text: #d5e2ee; --input: #ffffff;
  --radius: 8px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 10px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f151b; --panel: #17212a; --panel2: #1c2833; --text: #e2e9ef; --muted: #98a9b8; --line: #2a3a47;
    --brand: #0b1823; --accent: #14a395; --accent-hover: #18b8a8; --accent-ink: #04201d; --link: #6cb4ff;
    --ok-bg: #16301b; --warn-bg: #352a12; --bad-bg: #3a1717; --ok: #6fcf77; --warn: #f0b350; --bad: #ff7b7b;
    --nav: #121c24; --nav-hover: #1c2a35; --input: #111a21; --code: #0a1015;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f151b; --panel: #17212a; --panel2: #1c2833; --text: #e2e9ef; --muted: #98a9b8; --line: #2a3a47;
  --brand: #0b1823; --accent: #14a395; --accent-hover: #18b8a8; --accent-ink: #04201d; --link: #6cb4ff;
  --ok-bg: #16301b; --warn-bg: #352a12; --bad-bg: #3a1717; --ok: #6fcf77; --warn: #f0b350; --bad: #ff7b7b;
  --nav: #121c24; --nav-hover: #1c2a35; --input: #111a21; --code: #0a1015;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; }
a { color: var(--link); }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 10px; }
h3 { font-size: 14px; margin: 14px 0 6px; }
code, pre, .mono { font-family: Consolas, "Cascadia Mono", ui-monospace, monospace; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 20; height: 52px; display: flex; align-items: center; gap: 12px; padding: 0 14px; background: var(--brand); color: #fff; }
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; white-space: nowrap; }
.brand-mark { width: 28px; height: 28px; border-radius: 6px; background: var(--accent); display: grid; place-items: center; font-weight: 700; color: #fff; }
.brand-text b { font-weight: 600; color: #9fe3db; }
.global-search { flex: 1; max-width: 520px; }
.global-search input { width: 100%; height: 32px; border-radius: 16px; border: 1px solid rgba(255,255,255,.2); background: rgba(255,255,255,.1); color: #fff; padding: 0 14px; }
.global-search input::placeholder { color: rgba(255,255,255,.6); }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.agent { font-size: 12px; opacity: .85; white-space: nowrap; }
.icon-btn { background: transparent; border: 0; color: #fff; font-size: 18px; cursor: pointer; padding: 4px 6px; border-radius: 6px; }
.icon-btn:hover { background: rgba(255,255,255,.12); }
.menu-btn { display: none; }

/* Shell */
.shell { display: flex; min-height: calc(100vh - 52px); }
.nav { width: 240px; flex: 0 0 240px; background: var(--nav); color: var(--nav-text); padding: 10px 0 30px; overflow-y: auto; position: sticky; top: 52px; height: calc(100vh - 52px); }
.nav-filter { width: calc(100% - 20px); margin: 0 10px 8px; height: 30px; border-radius: 6px; border: 1px solid #34495a; background: #0f1a22; color: #fff; padding: 0 10px; }
.nav-h { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--nav-muted); padding: 10px 16px 4px; }
.nav a { display: block; padding: 5px 16px 5px 24px; color: var(--nav-text); text-decoration: none; font-size: 13px; border-left: 3px solid transparent; }
.nav a:hover { background: var(--nav-hover); }
.nav a.active { background: var(--nav-hover); border-left-color: var(--nav-active); color: #fff; }
main { flex: 1; min-width: 0; padding: 18px 22px 60px; outline: none; }

/* Page header */
.page-h { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-h .spacer { flex: 1; }
.crumb { font-size: 12px; color: var(--muted); margin-bottom: 2px; }

/* Panels & grid */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.panel > h2:first-child { margin-top: 0; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.kpis { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; text-decoration: none; color: inherit; box-shadow: var(--shadow); display: block; }
.kpi:hover { border-color: var(--accent); }
.kpi .v { font-size: 26px; font-weight: 700; line-height: 1.2; }
.kpi .l { font-size: 12px; color: var(--muted); }
.kpi.bad .v { color: var(--bad); } .kpi.warn .v { color: var(--warn); }

/* Buttons & forms */
.btn { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--text); cursor: pointer; font: inherit; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--bad); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 13px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--input); border: 1px solid var(--line); border-radius: 6px; padding: 6px 9px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); border-color: var(--accent); }
input[type=checkbox] { width: auto; }
textarea { min-height: 80px; resize: vertical; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 18px; }
.form-grid .full { grid-column: 1 / -1; }
.field-inline { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.field-inline > div { flex: 1; min-width: 140px; }
.readonly { background: var(--panel2); }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--panel2); white-space: nowrap; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--panel2); }
td.num { white-space: nowrap; font-weight: 600; color: var(--link); }

/* Badges */
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--panel2); border: 1px solid var(--line); white-space: nowrap; }
.pri { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.pri::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--p5); }
.pri.p1::before { background: var(--p1); } .pri.p2::before { background: var(--p2); } .pri.p3::before { background: var(--p3); } .pri.p4::before { background: var(--p4); }
.sla { font-size: 12px; padding: 1px 8px; border-radius: 10px; white-space: nowrap; }
.sla.ok, .sla.met { background: var(--ok-bg); color: var(--ok); }
.sla.warn { background: var(--warn-bg); color: var(--warn); }
.sla.breached { background: var(--bad-bg); color: var(--bad); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 3px 12px; border-radius: 14px; border: 1px solid var(--line); background: var(--panel); text-decoration: none; color: var(--text); font-size: 13px; }
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Bars */
.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 150px 1fr 34px; gap: 10px; align-items: center; font-size: 13px; }
.bar-track { height: 10px; background: var(--panel2); border-radius: 5px; overflow: hidden; border: 1px solid var(--line); }
.bar-fill { height: 100%; background: var(--accent); border-radius: 5px; }

/* Record form */
.record-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.activity { display: grid; gap: 10px; }
.note { border: 1px solid var(--line); border-left: 3px solid var(--muted); border-radius: 6px; padding: 8px 12px; background: var(--panel2); }
.note.work { border-left-color: var(--p3); }
.note.comment { border-left-color: var(--link); }
.note.system { border-left-color: var(--line); font-size: 12px; color: var(--muted); }
.note .meta { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.note .body { white-space: pre-wrap; word-wrap: break-word; }
.side-actions { display: grid; gap: 8px; }
.side-actions .btn { justify-content: flex-start; }

/* Tools */
.tool-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.snip { border: 1px solid var(--line); border-radius: 6px; margin: 0 0 12px; overflow: hidden; background: var(--panel); }
.snip-h { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 10px; background: var(--panel2); border-bottom: 1px solid var(--line); font-size: 13px; }
.snip-h .tag { font-size: 11px; padding: 0 6px; border-radius: 8px; background: var(--warn-bg); color: var(--warn); margin-left: 6px; }
.snip-note { padding: 6px 10px 0; font-size: 12px; color: var(--muted); }
pre { margin: 0; padding: 10px 12px; background: var(--code); color: var(--code-text); overflow-x: auto; font-size: 12.5px; line-height: 1.45; white-space: pre; }
.out { margin-top: 10px; }
.out pre { white-space: pre-wrap; word-break: break-word; border-radius: 6px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; font-family: Consolas, ui-monospace, monospace; word-break: break-all; }
.link-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.link-card { display: block; padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; text-decoration: none; color: var(--text); background: var(--panel); }
.link-card:hover { border-color: var(--accent); }
.link-card b { color: var(--link); }
.link-card .d { font-size: 12px; color: var(--muted); margin-top: 2px; }
.link-card .badge { margin-top: 6px; font-size: 11px; }
.checklist { list-style: none; padding: 0; margin: 0 0 10px; }
.checklist li { display: flex; gap: 8px; align-items: flex-start; padding: 4px 0; border-bottom: 1px dashed var(--line); }
.checklist li input { margin-top: 4px; }
.checklist label { display: flex; gap: 8px; align-items: flex-start; color: var(--text); font-size: 14px; margin: 0; cursor: pointer; }
.panel label > input[type=checkbox] { margin-right: 4px; vertical-align: -1px; }
.flag { padding: 6px 10px; border-radius: 6px; margin: 4px 0; font-size: 13px; }
.flag.bad { background: var(--bad-bg); color: var(--bad); } .flag.warn { background: var(--warn-bg); color: var(--warn); } .flag.ok { background: var(--ok-bg); color: var(--ok); }
.kb-body { white-space: pre-wrap; line-height: 1.6; }
.empty { padding: 30px; text-align: center; color: var(--muted); }

/* Toasts */
#toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; }
.toast { background: var(--brand); color: #fff; padding: 10px 14px; border-radius: 6px; box-shadow: 0 4px 14px rgba(0,0,0,.25); font-size: 13px; max-width: 340px; border-left: 4px solid var(--accent); }
.toast.warn { border-left-color: #f0a020; }

@media (max-width: 1100px) {
  .record-layout { grid-template-columns: 1fr; }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
  .menu-btn { display: block; }
  .nav { position: fixed; left: 0; top: 52px; z-index: 30; transform: translateX(-100%); transition: transform .2s; }
  body.nav-open .nav { transform: none; }
  .brand-text, .agent { display: none; }
  main { padding: 14px 16px 60px; }
  .g2, .g3, .form-grid { grid-template-columns: 1fr; }
  .bar-row { grid-template-columns: 110px 1fr 30px; }
  .top-actions .btn { display: none; }
}
