:root {
  --bg: #eceef2;
  --card: #ffffff;
  --card-2: #f7f8fa;
  --ink: #15181d;
  --ink-2: #4b5260;
  --muted: #8a909c;
  --line: #e4e7ec;
  --line-2: #eef0f3;
  --accent: #e4572e;
  --accent-ink: #ffffff;
  --accent-soft: #fdebe5;
  --good: #1f9d55;
  --good-soft: #e3f5ea;
  --warn: #c77d0a;
  --warn-soft: #fdf1dc;
  --bad: #d23b3b;
  --bad-soft: #fbe6e6;
  --info-soft: #e8eefb;
  --info: #3460c4;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 24px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 12px 40px rgba(16, 24, 40, 0.16);
  --r: 18px;
  --r-sm: 10px;
  --rail: 248px;
  --chart-a: #e4572e;
  --chart-b: #3460c4;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --card: #171a20; --card-2: #1d2129; --ink: #eef0f4; --ink-2: #b7bdc8; --muted: #7f8794;
    --line: #2a2f39; --line-2: #222731; --accent-soft: #3a2119; --good-soft: #15301f; --warn-soft: #33270f;
    --bad-soft: #3a1b1b; --info-soft: #1a2440; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --card: #171a20; --card-2: #1d2129; --ink: #eef0f4; --ink-2: #b7bdc8; --muted: #7f8794;
  --line: #2a2f39; --line-2: #222731; --accent-soft: #3a2119; --good-soft: #15301f; --warn-soft: #33270f;
  --bad-soft: #3a1b1b; --info-soft: #1a2440; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.45 "DM Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* Shell */
.shell { display: grid; grid-template-columns: var(--rail) 1fr; min-height: 100vh; gap: 16px; padding: 16px; }
.rail {
  position: sticky; top: 16px; height: calc(100vh - 32px); background: var(--card); border-radius: var(--r);
  box-shadow: var(--shadow); display: flex; flex-direction: column; padding: 18px 14px; gap: 4px; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 50%; background: var(--ink); color: var(--card);
  display: grid; place-items: center; font-weight: 700; font-size: 13px; letter-spacing: 0.02em;
}
.brand-name { font-weight: 600; line-height: 1.1; }
.brand-name small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); padding: 14px 10px 6px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 999px; text-decoration: none;
  color: var(--ink-2); font-weight: 500;
}
.nav a:hover { background: var(--card-2); color: var(--ink); }
.nav a.active { background: var(--card-2); color: var(--ink); box-shadow: inset 0 0 0 1px var(--line); }
.nav svg { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.rail-foot { margin-top: auto; display: flex; align-items: center; justify-content: space-between; padding: 12px 8px 0; border-top: 1px solid var(--line-2); }
.theme-toggle { display: inline-flex; background: var(--card-2); border-radius: 999px; padding: 3px; gap: 2px; }
.theme-toggle button { border: 0; background: transparent; border-radius: 999px; padding: 4px 9px; cursor: pointer; color: var(--muted); font-size: 12px; }
.theme-toggle button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }

.main { min-width: 0; display: flex; flex-direction: column; gap: 16px; }
.topbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.topbar .title { flex: 1; min-width: 200px; }
.topbar .title p { margin: 2px 0 0; color: var(--muted); }

/* Scope switcher (segmented) */
.seg { display: inline-flex; background: var(--card); border-radius: 999px; padding: 4px; box-shadow: var(--shadow); gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 7px 14px; border-radius: 999px; cursor: pointer; color: var(--ink-2); font-weight: 500; white-space: nowrap; }
.seg button.on { background: var(--ink); color: var(--card); }
.seg button:not(.on):hover { background: var(--card-2); }

/* Cards and KPIs */
.card { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow); padding: 20px; }
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2 { flex: 1; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); background: var(--card); border-radius: var(--r); box-shadow: var(--shadow); }
.kpi { padding: 16px 20px; border-right: 1px solid var(--line-2); }
.kpi:last-child { border-right: 0; }
.kpi .label { color: var(--ink-2); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .delta { font-size: 12px; margin-left: 6px; font-weight: 500; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }

/* Buttons and inputs */
.btn {
  display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--card);
  padding: 8px 14px; border-radius: 999px; cursor: pointer; font-weight: 500; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--card-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(0.95); }
.btn.soft { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--card-2); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; width: 32px; height: 32px; border-radius: 50%; cursor: pointer; display: inline-grid; place-items: center; color: var(--ink-2); }
.icon-btn:hover { background: var(--card-2); }
.icon-btn.menu-btn { display: none; }
.icon-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--ink-2); }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], input[type=tel], input[type=url], input[type=search], select, textarea {
  border: 1px solid var(--line); background: var(--card); border-radius: var(--r-sm); padding: 9px 12px; width: 100%; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 72px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); cursor: pointer; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.search { position: relative; }
.search input { padding-left: 34px; border-radius: 999px; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; stroke: var(--muted); fill: none; stroke-width: 1.8; }

/* Pills */
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 11px; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase; white-space: nowrap; }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.info { background: var(--info-soft); color: var(--info); }
.pill.neutral { background: var(--card-2); color: var(--ink-2); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.list td { padding: 11px 10px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: var(--card-2); }
table.list tr:last-child td { border-bottom: 0; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: transparent; padding: 10px 12px; cursor: pointer; color: var(--muted); font-weight: 500; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--ink); }

/* Schedule board */
.board-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px 4px; }
table.board { border-collapse: separate; border-spacing: 0; width: 100%; min-width: 840px; }
table.board th { font-size: 12px; color: var(--muted); font-weight: 500; padding: 6px 4px 10px; text-align: center; white-space: nowrap; }
table.board th.today { color: var(--accent); }
table.board th .d { display: block; color: var(--ink); font-weight: 600; font-size: 13px; }
table.board th.today .d { color: var(--accent); }
table.board td { padding: 3px; }
table.board td.row-head { padding: 3px 10px 3px 0; white-space: nowrap; width: 1%; }
.row-code { font-weight: 600; font-variant-numeric: tabular-nums; }
.row-meta { font-size: 11px; color: var(--muted); }
tr.group-row td { padding: 14px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
.cell {
  width: 100%; min-height: 38px; border: 1px dashed var(--line); background: transparent; border-radius: var(--r-sm);
  cursor: pointer; padding: 6px 8px; text-align: left; font-weight: 500; color: var(--muted); display: flex; align-items: center; gap: 6px;
}
.cell:hover { border-color: var(--ink-2); color: var(--ink); }
.cell.filled { border: 1px solid transparent; background: var(--card-2); color: var(--ink); }
.cell.filled:hover { border-color: var(--line); }
.cell.on-call.filled { background: var(--info-soft); }
.cell.flag-warn { background: var(--warn-soft); border-color: transparent; }
.cell.flag-bad { background: var(--bad-soft); border-color: transparent; }
.cell .dot { width: 6px; height: 6px; border-radius: 50%; flex: none; }
.cell.flag-warn .dot { background: var(--warn); }
.cell.flag-bad .dot { background: var(--bad); }
.cell .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell:disabled { cursor: default; }
.board-legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.board-legend span { display: inline-flex; align-items: center; gap: 6px; }
.board-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.week-nav { display: inline-flex; align-items: center; gap: 4px; background: var(--card); border-radius: 999px; padding: 4px; box-shadow: var(--shadow); }
.week-nav .lbl { padding: 0 10px; font-weight: 600; white-space: nowrap; }
.schedule-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.summary-list { display: flex; flex-direction: column; }
.summary-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line-2); }
.summary-row:last-child { border-bottom: 0; }
.summary-row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.daybar { display: flex; gap: 2px; }
.daybar i { width: 6px; height: 14px; border-radius: 2px; background: var(--line); }
.daybar i.on { background: var(--ink); }

/* Popover picker */
.popover {
  position: fixed; z-index: 60; width: 280px; max-height: 380px; display: flex; flex-direction: column; background: var(--card);
  border-radius: 14px; box-shadow: var(--shadow-lg); border: 1px solid var(--line); overflow: hidden;
}
.popover .pop-head { padding: 10px; border-bottom: 1px solid var(--line-2); }
.popover .pop-list { overflow-y: auto; padding: 6px; }
.pick { display: flex; align-items: center; gap: 8px; width: 100%; border: 0; background: transparent; text-align: left; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.pick:hover, .pick.kb { background: var(--card-2); }
.pick .nm { flex: 1; font-weight: 500; }
.pick .why { font-size: 11px; }
.pick.clear { color: var(--bad); }

/* Drawer + modal */
.scrim { position: fixed; inset: 0; background: rgba(15, 17, 21, 0.35); z-index: 70; }
.drawer {
  position: fixed; top: 12px; right: 12px; bottom: 12px; width: min(560px, calc(100vw - 24px)); background: var(--card); z-index: 71;
  border-radius: var(--r); box-shadow: var(--shadow-lg); display: flex; flex-direction: column; overflow: hidden;
}
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 18px 20px; border-bottom: 1px solid var(--line-2); }
.drawer-head h2 { flex: 1; }
.drawer-body { padding: 20px; overflow-y: auto; flex: 1; }
.drawer-foot { padding: 14px 20px; border-top: 1px solid var(--line-2); display: flex; gap: 8px; justify-content: flex-end; }
.modal {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(440px, calc(100vw - 32px)); z-index: 72;
  background: var(--card); border-radius: var(--r); box-shadow: var(--shadow-lg); padding: 22px;
}
.modal p { color: var(--ink-2); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* Profile sections */
.kv { display: grid; grid-template-columns: 190px 1fr auto; gap: 0; }
.kv > div { padding: 10px 0; border-bottom: 1px solid var(--line-2); }
.kv .k { color: var(--ink-2); padding-right: 12px; }
.kv .v { word-break: break-word; }
.kv .a { text-align: right; white-space: nowrap; }
.kv > div:nth-last-child(-n+3) { border-bottom: 0; }
.secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.04em; }
.doc-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line-2); }
.doc-row:last-child { border-bottom: 0; }
.doc-ico { width: 34px; height: 34px; border-radius: 10px; background: var(--card-2); display: grid; place-items: center; flex: none; font-size: 10px; font-weight: 700; color: var(--ink-2); }
.doc-row .t { flex: 1; min-width: 0; }

/* Contacts */
.contact { display: flex; flex-direction: column; gap: 6px; }
.contact .org { color: var(--ink-2); }
.contact .lines { display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
.contact a { text-decoration: none; color: var(--info); }

.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty h3 { color: var(--ink); margin-bottom: 4px; }
.banner { padding: 12px 14px; border-radius: var(--r-sm); background: var(--card-2); color: var(--ink-2); font-size: 13px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--card); padding: 10px 16px; border-radius: 999px; z-index: 90; box-shadow: var(--shadow-lg); font-weight: 500; }
.toast.bad { background: var(--bad); color: #fff; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.skeleton { height: 120px; border-radius: var(--r); background: linear-gradient(90deg, var(--card) 0%, var(--card-2) 50%, var(--card) 100%); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }

/* Auth screens */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(400px, 100%); padding: 28px; }
.auth .brand { padding: 0 0 20px; }
.auth form { display: flex; flex-direction: column; gap: 14px; }
.auth .err { color: var(--bad); font-size: 13px; min-height: 18px; }

/* Charts */
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i, .tip-r i { width: 16px; border-top: 2px solid; display: inline-block; }
.legend i.dash, .tip-r i.dash { border-top-style: dashed; }
i.sw-a, .dot.sw-a { border-color: var(--chart-a); }
i.sw-b, .dot.sw-b { border-color: var(--chart-b); }
.chart { display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto 22px; column-gap: 10px; }
.chart .yax { position: relative; min-width: 44px; }
.chart .yax span { position: absolute; right: 0; transform: translateY(-50%); font-size: 11px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.chart .plot { position: relative; min-width: 0; touch-action: pan-y; }
.chart svg { position: absolute; inset: 0; overflow: visible; }
.chart .grid line { stroke: var(--line-2); stroke-width: 1; }
.chart .grid line.zero { stroke: var(--muted); }
.chart .ln { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .ln.dash { stroke-dasharray: 5 4; }
.chart .ln.sw-a { stroke: var(--chart-a); }
.chart .ln.sw-b { stroke: var(--chart-b); }
.chart .pt.sw-a { fill: var(--chart-a); stroke: var(--card); stroke-width: 2; }
.chart .pt.sw-b { fill: var(--chart-b); stroke: var(--card); stroke-width: 2; }
.chart .xax { grid-column: 2; position: relative; }
.chart .xax span { position: absolute; top: 5px; font-size: 11px; color: var(--muted); white-space: nowrap; transform: translateX(-50%); }
.chart .xh { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--ink-2); opacity: 0.35; pointer-events: none; }
.chart .dot { position: absolute; width: 10px; height: 10px; border-radius: 50%; border: 3px solid; box-shadow: 0 0 0 2px var(--card); background: var(--card); transform: translate(-50%, -50%); pointer-events: none; }
.chart .tip { position: absolute; top: 0; background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow-lg); border-radius: 10px; padding: 8px 10px; font-size: 12px; pointer-events: none; white-space: nowrap; z-index: 2; }
.tip-h { font-weight: 600; margin-bottom: 4px; }
.tip-r { display: flex; align-items: center; gap: 6px; color: var(--ink-2); }
.tip-r b { margin-left: auto; padding-left: 12px; color: var(--ink); font-variant-numeric: tabular-nums; }
.chart-table { margin-top: 10px; font-size: 13px; }
.chart-table summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.chart-table .table-wrap { max-height: 280px; overflow-y: auto; margin-top: 6px; }

/* P&L statement */
.pl { width: 100%; border-collapse: collapse; }
.pl td { padding: 6px 0; border-bottom: 1px solid var(--line-2); }
.pl td.num { text-align: right; padding-left: 12px; }
.pl tr.sec td { padding-top: 16px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--line); }
.pl tr.sub td { color: var(--ink-2); padding-left: 12px; }
.pl tr.tot td { font-weight: 600; }
.pl tr.big td { font-weight: 700; font-size: 15px; border-bottom: 0; padding-top: 10px; }
.cmp td.num, .cmp th.num { text-align: right; }
.cmp td:first-child { white-space: nowrap; }
.dro-in { width: 92px; text-align: right; padding: 6px 8px; }

@media (max-width: 1100px) {
  .schedule-layout { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; padding: 12px 16px; }
  .rail { position: fixed; left: 12px; top: 12px; bottom: 12px; height: auto; width: var(--rail); z-index: 80; transform: translateX(-120%); transition: transform 0.2s; }
  body.nav-open .rail { transform: none; }
  .icon-btn.menu-btn { display: inline-grid; }
  .form-grid { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr auto; }
  .kv .k { grid-column: 1 / -1; padding-bottom: 0; border-bottom: 0 !important; color: var(--muted); font-size: 12px; }
  .kpi { border-right: 0; border-bottom: 1px solid var(--line-2); }
}
