:root {
  color-scheme: light dark;
  --bg: #f4f3ef;
  --surface: #fcfcfb;
  --surface-2: #efede7;
  --line: #dedbd2;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7873;
  --accent: #2a78d6;
  --series-in: #2a78d6;
  --series-out: #eb6834;
  --good: #0ca30c;
  --warn: #c98500;
  --serious: #ec835a;
  --critical: #d03b3b;
  --danger-zone: rgba(208, 59, 59, 0.08);
  --lamp-glow: rgba(42, 120, 214, 0.18);
  --radius: 14px;
  --font: "SF Pro Text", -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #232321;
    --line: #34332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8d8b84;
    --accent: #3987e5;
    --series-in: #3987e5;
    --series-out: #d95926;
    --danger-zone: rgba(230, 103, 103, 0.10);
    --lamp-glow: rgba(57, 135, 229, 0.22);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 30px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0; }
a { color: inherit; }
.muted { color: var(--text-3); font-size: 13px; }

.page { max-width: 1180px; margin: 0 auto; padding: 28px 20px 64px; display: grid; gap: 18px; }
.topbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-3); text-decoration: none; }
.eyebrow.link:hover { color: var(--accent); }

.btn {
  font: inherit; font-size: 14px; border-radius: 10px; padding: 9px 16px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); text-decoration: none;
}
.btn:hover { border-color: var(--text-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn:focus-visible, input:focus-visible, .room:focus-visible, .seg:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Вход */
.login { max-width: 340px; margin: 18vh auto 0; padding: 32px 28px; display: grid; gap: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.login input { font: inherit; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.login-error { color: var(--critical); font-size: 13px; min-height: 18px; }
.login-mark { width: 36px; height: 36px; border-radius: 9px; background: linear-gradient(135deg, var(--series-in), var(--series-out)); }

/* План этажа */
.floor {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px;
  background: var(--line); border: 2px solid var(--line); border-radius: calc(var(--radius) + 2px); overflow: hidden;
}
.room { min-height: 190px; padding: 20px; display: grid; align-content: start; gap: 6px; background: var(--surface); text-decoration: none; position: relative; }
.room-live { transition: background 0.2s ease; }
.room-live::after { content: ""; position: absolute; inset: 0; pointer-events: none; background: radial-gradient(circle at 24px 26px, var(--lamp-glow), transparent 55%); }
.room-live:hover { background: var(--surface-2); }
.room-planned {
  color: var(--text-3);
  background-color: var(--surface);
  background-image: linear-gradient(var(--surface-2) 1px, transparent 1px), linear-gradient(90deg, var(--surface-2) 1px, transparent 1px);
  background-size: 22px 22px;
}
.room-head { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-3); }
.room-title { font-size: 20px; margin-top: 6px; }
.room-sub { color: var(--text-2); font-size: 13px; }
.room-planned .room-sub { color: var(--text-3); }
.room-lines { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 4px; font-size: 14px; }
.room-stage { margin-top: auto; font-size: 12px; border: 1px dashed var(--line); border-radius: 999px; padding: 2px 10px; justify-self: start; }
.lamp { width: 9px; height: 9px; border-radius: 50%; background: var(--text-3); }
.lamp-ok { background: var(--good); box-shadow: 0 0 0 4px rgba(12, 163, 12, 0.15); }
.lamp-alert { background: var(--serious); box-shadow: 0 0 0 4px rgba(236, 131, 90, 0.2); animation: blink 2s ease-in-out infinite; }
.lamp-error { background: var(--critical); }
.lamp-planned { background: transparent; border: 1px dashed var(--text-3); }

/* Агенты */
.agents { list-style: none; margin: 0; padding: 0; display: grid; }
.agent { display: grid; grid-template-columns: 14px 1fr auto; gap: 12px; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.agent:first-child { border-top: 0; }
.agent-name { font-weight: 600; display: flex; gap: 8px; align-items: baseline; }
.agent-room { font-size: 12px; color: var(--text-3); font-weight: 400; }
.agent-role { font-size: 13px; color: var(--text-2); }
.agent-meta { text-align: right; font-size: 13px; }
.pulse { width: 10px; height: 10px; border-radius: 50%; background: var(--text-3); }
.pulse-ok { background: var(--good); }
.pulse-error { background: var(--critical); }
.pulse-running { background: var(--accent); animation: blink 1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .lamp-alert, .pulse-running { animation: none; } }

/* Панели */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; display: grid; gap: 14px; min-width: 0; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; align-items: start; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; display: grid; gap: 4px; }
.kpi-label { font-size: 13px; color: var(--text-2); }
.kpi-value { font-size: 26px; font-weight: 650; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.kpi-note { font-size: 12px; color: var(--text-3); }
.kpi.bad .kpi-value { color: var(--critical); }

.advice { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.advice-item { display: grid; grid-template-columns: 104px 1fr; gap: 12px; padding: 12px; border-radius: 10px; background: var(--surface-2); }
.advice-item p { color: var(--text-2); font-size: 14px; margin-top: 2px; }
.badge { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; align-self: start; padding: 3px 8px; border-radius: 999px; border: 1px solid currentColor; justify-self: start; white-space: nowrap; }
.level-urgent .badge { color: var(--critical); }
.level-warn .badge { color: var(--warn); }
.level-info .badge { color: var(--accent); }
.level-idea .badge { color: var(--good); }

.rows { list-style: none; margin: 0; padding: 0; }
.row { display: grid; grid-template-columns: 64px 1fr auto auto; gap: 10px; align-items: baseline; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; }
.row:first-child { border-top: 0; }
.row-date { color: var(--text-3); font-variant-numeric: tabular-nums; }
.row-title { display: flex; flex-wrap: wrap; gap: 6px; align-items: baseline; min-width: 0; }
.row-amount { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.kind-income .row-amount { color: var(--good); }
.kind-memo .row-amount { color: var(--text-3); font-weight: 400; }
.tag { font-size: 11px; color: var(--text-3); border: 1px solid var(--line); border-radius: 999px; padding: 0 7px; }
.tag.ok { color: var(--good); border-color: currentColor; }
.icon-btn { border: 0; background: none; color: var(--text-3); cursor: pointer; font-size: 18px; line-height: 1; padding: 0 4px; }
.icon-btn:hover { color: var(--critical); }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; padding: 2px; gap: 2px; }
.seg { font: inherit; font-size: 13px; border: 0; background: none; color: var(--text-2); padding: 6px 12px; border-radius: 8px; cursor: pointer; }
.seg.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* Графики */
.chart { margin: 0; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart-title { font-size: 13px; font-weight: 600; color: var(--text-2); }
.grid { stroke: var(--line); stroke-width: 1; }
.axis-label { fill: var(--text-3); font-size: 11px; font-family: var(--font); }
.bar-in { fill: var(--series-in); }
.bar-out { fill: var(--series-out); }
.col:hover .bar-in, .col:hover .bar-out { filter: brightness(1.12); }
.hit { fill: transparent; }
.zero { stroke: var(--text-3); stroke-width: 1; }
.danger-zone { fill: var(--danger-zone); }
.balance-line { fill: none; stroke: var(--series-in); stroke-width: 2; stroke-linejoin: round; }
.balance-area { fill: var(--series-in); opacity: 0.08; }
.crosshair { stroke: var(--text-3); stroke-dasharray: 3 3; }
.dot { fill: var(--series-in); stroke: var(--surface); stroke-width: 2; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-2); margin-bottom: 6px; }
.key::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.key-in::before { background: var(--series-in); }
.key-out::before { background: var(--series-out); }

.tooltip {
  position: fixed; left: 0; top: 0; z-index: 10; pointer-events: none; max-width: 320px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  font-size: 13px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}
.tip-title { font-weight: 600; margin-bottom: 2px; }
.tip-line { color: var(--text-2); font-variant-numeric: tabular-nums; }

/* Долги */
.payoff { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; align-items: center; background: var(--surface-2); border-radius: 10px; padding: 12px 14px; }
.payoff input[type="range"] { width: 100%; accent-color: var(--accent); }
.payoff-value { font-weight: 650; font-variant-numeric: tabular-nums; }
.payoff-result { grid-column: 1 / -1; font-size: 14px; color: var(--text-2); }
.table-wrap { overflow-x: auto; }
.debts { width: 100%; border-collapse: collapse; font-size: 14px; }
.debts th, .debts td { text-align: left; padding: 10px 8px; border-top: 1px solid var(--line); vertical-align: top; }
.debts thead th { border-top: 0; font-size: 12px; color: var(--text-3); font-weight: 500; }
.debts tbody th { font-weight: 600; }
.debts .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cell-note { font-weight: 400; font-size: 12px; color: var(--text-3); max-width: 360px; margin-top: 2px; }
.debts .tag { margin-left: 6px; }
.meter { height: 4px; background: var(--surface-2); border-radius: 2px; margin-top: 6px; min-width: 90px; }
.meter-fill { display: block; height: 100%; border-radius: 2px; background: var(--series-out); }

.quick { display: flex; gap: 8px; }
.quick input { flex: 1; min-width: 0; font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }

@media (max-width: 900px) {
  .floor { grid-template-columns: repeat(2, 1fr); }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .page { padding: 20px 16px 48px; }
  .floor { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr; }
  .advice-item { grid-template-columns: 1fr; gap: 6px; }
  .row { grid-template-columns: 52px 1fr auto; }
  .row .icon-btn { grid-column: 3; }
  .agent { grid-template-columns: 14px 1fr; }
  .agent-meta { grid-column: 2; text-align: left; }
}
