:root {
  --bg: #0b0f14;
  --bg-2: #0e141c;
  --surface: #111a24;
  --surface-2: #162231;
  --line: #223247;
  --line-soft: #1a2838;
  --text: #e6edf5;
  --muted: #8fa3b8;
  --muted-2: #65788d;
  --accent: #4ade80;
  --accent-2: #38bdf8;
  --warn: #fbbf24;
  --crit: #f87171;
  --info: #a78bfa;
  --shadow: 0 12px 30px rgba(0, 0, 0, .28);
  --radius: 16px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(1100px 500px at 8% -12%, rgba(56, 189, 248, .10), transparent 60%),
    radial-gradient(800px 420px at 100% 0%, rgba(74, 222, 128, .07), transparent 55%),
    var(--bg);
}

button, input, select { font: inherit; }
button { cursor: pointer; }

.app-shell {
  max-width: 1180px;
  margin: 0 auto;
  min-height: 100vh;
  padding: 14px 14px calc(84px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 2px 12px;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 800; letter-spacing: .5px;
  color: #052e16;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 8px 22px rgba(74, 222, 128, .18);
}
.brand-title { font-size: 16px; font-weight: 750; letter-spacing: .2px; }
.brand-sub { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 9px; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted-2); box-shadow: 0 0 0 4px rgba(143,163,184,.08); }
.live-dot.on { background: var(--accent); box-shadow: 0 0 0 4px rgba(74,222,128,.12); }
.live-dot.err { background: var(--crit); box-shadow: 0 0 0 4px rgba(248,113,113,.12); }
.updated { color: var(--muted-2); font-size: 11px; white-space: nowrap; }
.icon-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  font-size: 20px;
  line-height: 1;
}
.icon-btn:hover { border-color: var(--accent-2); }

.range-wrap { margin: 0 0 12px; }
.range-bar {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  padding: 4px; border: 1px solid var(--line-soft); background: rgba(17,26,36,.7);
  border-radius: 12px;
}
.range-bar::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 7px 12px;
  border-radius: 9px;
  font-size: 12px;
  font-weight: 600;
}
.chip:hover { color: var(--text); background: rgba(56,189,248,.08); }
.chip.active { color: #06251a; background: linear-gradient(135deg, var(--accent), #86efac); border-color: transparent; }

.content { min-height: 55vh; }
.loading, .empty, .error, .skeleton {
  border: 1px dashed var(--line);
  background: rgba(17,26,36,.55);
  color: var(--muted);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
}
.error { border-color: rgba(248,113,113,.4); color: #fecaca; }
.error button { margin-top: 10px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; margin: 4px 2px 12px; }
.page-title { font-size: 22px; line-height: 1.15; margin: 0; letter-spacing: -.3px; }
.page-sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.readonly-pill {
  flex: 0 0 auto; border: 1px solid rgba(74,222,128,.25); color: #bbf7d0;
  background: rgba(74,222,128,.07); padding: 5px 9px; border-radius: 999px; font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .7px;
}

.kpi-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; margin-bottom: 12px; }
.kpi {
  position: relative; overflow: hidden;
  background: linear-gradient(160deg, rgba(22,34,49,.95), rgba(17,26,36,.92));
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  padding: 12px;
  min-height: 98px;
  box-shadow: 0 8px 24px rgba(0,0,0,.14);
}
.kpi::after { content: ""; position: absolute; inset: auto -30px -46px auto; width: 110px; height: 110px; border-radius: 50%; background: radial-gradient(circle, rgba(56,189,248,.13), transparent 65%); pointer-events: none; }
.kpi-label { color: var(--muted); font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: .45px; }
.kpi-value { margin-top: 8px; font-size: 22px; font-weight: 760; letter-spacing: -.5px; }
.kpi-value small { font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: 0; }
.kpi-foot { margin-top: 5px; color: var(--muted-2); font-size: 11px; display: flex; justify-content: space-between; gap: 8px; }
.value-good { color: var(--accent); }
.value-warn { color: var(--warn); }
.value-crit { color: var(--crit); }
.value-info { color: var(--accent-2); }

.panel {
  background: rgba(17,26,36,.88);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 13px;
  margin-bottom: 11px;
  box-shadow: var(--shadow);
}
.section-title { margin: 0 0 10px; font-size: 14px; font-weight: 720; letter-spacing: .1px; }
.section-title small { color: var(--muted-2); font-weight: 500; font-size: 11px; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 11px; }
.chart-card { padding: 11px 10px 6px; border: 1px solid var(--line-soft); background: rgba(11,15,20,.55); border-radius: 13px; margin-bottom: 10px; }
.chart-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 0 2px 3px; }
.chart-title { font-size: 12px; font-weight: 700; }
.chart-unit { color: var(--muted-2); font-size: 10px; }
.chart { width: 100%; height: 210px; position: relative; touch-action: pan-y; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart-tip {
  position: absolute; z-index: 4; pointer-events: none;
  background: #0a1119; border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 8px; color: var(--text); font-size: 10px; line-height: 1.45;
  box-shadow: 0 8px 18px rgba(0,0,0,.4); max-width: 180px; display: none;
}
.chart-tip .tip-time { color: var(--muted); margin-bottom: 3px; }
.chart-tip .tip-row { display: flex; justify-content: space-between; gap: 8px; }
.legend { display: flex; flex-wrap: wrap; gap: 8px; margin: 3px 2px 0; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 10px; }
.legend-dot { width: 7px; height: 7px; border-radius: 50%; }
.empty-chart { color: var(--muted); text-align: center; padding: 32px 8px; font-size: 12px; border: 1px dashed var(--line); border-radius: 10px; }

.stat-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; margin-top: 8px; }
.stat { background: rgba(11,15,20,.55); border: 1px solid var(--line-soft); border-radius: 10px; padding: 8px; }
.stat-label { color: var(--muted-2); font-size: 10px; }
.stat-value { font-size: 13px; font-weight: 700; margin-top: 2px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line-soft); border-radius: 11px; }
table { width: 100%; border-collapse: collapse; font-size: 11px; }
th, td { padding: 8px 9px; text-align: left; border-bottom: 1px solid var(--line-soft); white-space: nowrap; }
th { color: var(--muted); font-weight: 650; background: rgba(11,15,20,.45); }
tr:last-child td { border-bottom: 0; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }

.service-list { display: grid; gap: 7px; }
.service-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 10px; border: 1px solid var(--line-soft); background: rgba(11,15,20,.45); border-radius: 10px; }
.service-name { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11px; overflow: hidden; text-overflow: ellipsis; }
.service-meta { color: var(--muted-2); font-size: 10px; margin-top: 2px; }
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 4px 8px; border-radius: 999px; font-size: 10px; font-weight: 700; border: 1px solid transparent; }
.badge-good { color: #bbf7d0; background: rgba(74,222,128,.10); border-color: rgba(74,222,128,.25); }
.badge-warn { color: #fde68a; background: rgba(251,191,36,.10); border-color: rgba(251,191,36,.25); }
.badge-crit { color: #fecaca; background: rgba(248,113,113,.10); border-color: rgba(248,113,113,.25); }
.badge-info { color: #ddd6fe; background: rgba(167,139,250,.10); border-color: rgba(167,139,250,.25); }
.badge-muted { color: var(--muted); background: rgba(143,163,184,.07); border-color: var(--line); }

.timeline { display: grid; gap: 7px; }
.event { border-left: 2px solid var(--line); padding: 7px 0 7px 10px; }
.event.crit { border-color: var(--crit); }
.event.warn { border-color: var(--warn); }
.event.info { border-color: var(--accent-2); }
.event-top { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 10px; }
.event-title { font-size: 12px; font-weight: 650; margin-top: 2px; }
.event-detail { color: var(--muted-2); font-size: 10px; margin-top: 2px; }

.note { color: var(--muted-2); font-size: 10px; line-height: 1.5; margin-top: 8px; }
.split { display: grid; gap: 11px; }
.form-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 9px; }
select {
  background: var(--surface); color: var(--text); border: 1px solid var(--line);
  border-radius: 9px; padding: 7px 9px; font-size: 12px;
}
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  padding: 8px max(10px, calc((100vw - 1180px) / 2)) calc(8px + env(safe-area-inset-bottom));
  background: rgba(8,12,17,.92); border-top: 1px solid var(--line-soft);
  backdrop-filter: blur(14px);
}
.bottom-nav::-webkit-scrollbar { display: none; }
.nav-btn {
  flex: 0 0 auto; border: 1px solid transparent; background: transparent; color: var(--muted-2);
  border-radius: 10px; padding: 7px 9px; font-size: 10px; font-weight: 650; display: grid; gap: 2px; place-items: center; min-width: 54px;
}
.nav-btn .nav-ico { font-size: 16px; line-height: 1; }
.nav-btn.active { color: var(--text); background: rgba(56,189,248,.10); border-color: rgba(56,189,248,.22); }
.nav-btn:hover { color: var(--text); }

.toast {
  position: fixed; left: 50%; bottom: 88px; transform: translateX(-50%) translateY(12px);
  background: #111a24; border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 8px 12px; font-size: 11px; opacity: 0;
  pointer-events: none; transition: .2s ease; z-index: 40; max-width: 88vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.skeleton { height: 110px; margin-bottom: 10px; animation: pulse 1.3s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }

@media (min-width: 720px) {
  .app-shell { padding: 20px 22px 92px; }
  .kpi-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr 1fr; }
  .split { grid-template-columns: 1fr 1fr; }
  .chart { height: 240px; }
  .page-title { font-size: 26px; }
  .bottom-nav { justify-content: center; }
}
@media (max-width: 380px) {
  .kpi-grid { gap: 7px; }
  .kpi { padding: 10px; min-height: 92px; }
  .kpi-value { font-size: 19px; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
/* Stage 3.2 — theme, settings, dashboard polish */
:root {
  --panel-bg: rgba(17,26,36,.88);
  --panel-alt: rgba(11,15,20,.55);
  --nav-bg: rgba(8,12,17,.92);
  --tip-bg: #0a1119;
  --chart-axis: #65788d;
  --chart-grid: #1a2838;
  --chart-marker: #8fa3b8;
  --series-1: #38bdf8;
  --series-2: #4ade80;
  --series-3: #fbbf24;
  --series-4: #f87171;
  --series-5: #a78bfa;
  --series-6: #22d3ee;
}
html[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f7fb;
  --bg-2: #eef3f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --line: #d7e0ea;
  --line-soft: #e4ebf2;
  --text: #152234;
  --muted: #55677c;
  --muted-2: #718096;
  --accent: #16a34a;
  --accent-2: #0284c7;
  --warn: #b45309;
  --crit: #dc2626;
  --info: #7c3aed;
  --panel-bg: rgba(255,255,255,.94);
  --panel-alt: rgba(248,250,252,.9);
  --nav-bg: rgba(255,255,255,.94);
  --tip-bg: #ffffff;
  --chart-axis: #64748b;
  --chart-grid: #e2e8f0;
  --chart-marker: #94a3b8;
  --series-1: #0284c7;
  --series-2: #16a34a;
  --series-3: #d97706;
  --series-4: #dc2626;
  --series-5: #7c3aed;
  --series-6: #0891b2;
  --shadow: 0 10px 26px rgba(34, 51, 72, .10);
}
html[data-theme="light"] body {
  background:
    radial-gradient(1100px 500px at 8% -12%, rgba(2,132,199,.08), transparent 60%),
    radial-gradient(800px 420px at 100% 0%, rgba(22,163,74,.06), transparent 55%),
    var(--bg);
}
html[data-theme="light"] .kpi { background: linear-gradient(160deg, #ffffff, #f7fafc); box-shadow: 0 8px 24px rgba(34,51,72,.07); }
html[data-theme="light"] .kpi::after { background: radial-gradient(circle, rgba(2,132,199,.10), transparent 65%); }
html[data-theme="light"] .panel { background: var(--panel-bg); }
html[data-theme="light"] .chart-card,
html[data-theme="light"] .stat,
html[data-theme="light"] .service-row,
html[data-theme="light"] .loading,
html[data-theme="light"] .empty,
html[data-theme="light"] .error,
html[data-theme="light"] .skeleton { background: var(--panel-alt); }
html[data-theme="light"] .chart-tip { background: var(--tip-bg); box-shadow: 0 8px 18px rgba(34,51,72,.16); }
html[data-theme="light"] th { background: rgba(241,245,249,.9); }
html[data-theme="light"] .bottom-nav { background: var(--nav-bg); }
html[data-theme="light"] .toast { background: #fff; }
html[data-theme="light"] .range-bar { background: rgba(255,255,255,.8); }
html[data-theme="light"] .chip.active { color: #fff; }
html[data-theme="light"] .readonly-pill { color: #166534; background: rgba(22,163,74,.08); border-color: rgba(22,163,74,.25); }
html[data-theme="light"] .badge-good { color: #166534; }
html[data-theme="light"] .badge-warn { color: #92400e; }
html[data-theme="light"] .badge-crit { color: #991b1b; }
html[data-theme="light"] .badge-info { color: #5b21b6; }

html[data-accent="blue"] { --accent: #38bdf8; --accent-2: #818cf8; }
html[data-accent="violet"] { --accent: #a78bfa; --accent-2: #38bdf8; }
html[data-accent="amber"] { --accent: #fbbf24; --accent-2: #fb923c; }
html[data-density="compact"] .kpi { padding: 9px; min-height: 82px; }
html[data-density="compact"] .kpi-value { font-size: 19px; margin-top: 5px; }
html[data-density="compact"] .kpi-foot { margin-top: 3px; font-size: 10px; }
html[data-density="compact"] .panel { padding: 10px; margin-bottom: 8px; }
html[data-density="compact"] .chart { height: 180px; }
html[data-density="compact"] .chart-card { padding: 8px 8px 4px; margin-bottom: 7px; }
html[data-animations="off"] .chart svg { animation: none; }
html[data-animations="off"] * { transition: none !important; }

.page-head .readonly-pill { align-self: center; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 9px; }
.panel-head .section-title { margin: 0; }
.panel-hint { color: var(--muted-2); font-size: 10px; white-space: nowrap; }
.home-block { margin-bottom: 11px; }
.home-block .kpi-grid { margin-bottom: 0; }
.status-strip {
  display: flex; align-items: center; gap: 7px; flex-wrap: wrap;
  border: 1px solid var(--line-soft); background: var(--panel-alt);
  color: var(--muted); border-radius: 11px; padding: 8px 11px; margin: 0 0 11px;
  font-size: 11px;
}
.status-strip.is-ok .status-strip-dot { background: var(--accent); }
.status-strip.has-warning .status-strip-dot { background: var(--warn); }
.status-strip-dot { width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto; }
.strip-sep { color: var(--muted-2); }
.kpi-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.kpi-state-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted-2); opacity: .45; flex: 0 0 auto; }
.kpi-state-dot.value-good { background: var(--accent); opacity: 1; }
.kpi-state-dot.value-warn { background: var(--warn); opacity: 1; }
.kpi-state-dot.value-crit { background: var(--crit); opacity: 1; }
.kpi-state-dot.value-info { background: var(--accent-2); opacity: 1; }
.kpi-meter { height: 3px; border-radius: 99px; background: rgba(143,163,184,.14); overflow: hidden; margin-top: 7px; }
.kpi-meter span { display: block; height: 100%; border-radius: inherit; background: var(--accent); transition: width .3s ease; }
.kpi-value.value-warn + .kpi-meter span { background: var(--warn); }
.kpi-value.value-crit + .kpi-meter span { background: var(--crit); }
.kpi-value.value-info + .kpi-meter span { background: var(--accent-2); }
.delta-up { color: var(--warn); }
.delta-down { color: var(--accent); }
.chart-foot { display: flex; gap: 12px; color: var(--muted-2); font-size: 10px; padding: 0 2px 2px; }
.chart-foot b { color: var(--muted); font-weight: 650; }
.chart svg { animation: chartIn .28s ease-out; }
@keyframes chartIn { from { opacity: .35; } to { opacity: 1; } }

/* settings */
.settings-panel { padding: 13px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line-soft); }
.setting-row:last-child { border-bottom: 0; }
.setting-label { color: var(--muted); font-size: 12px; }
.seg { display: inline-flex; gap: 3px; padding: 3px; background: var(--panel-alt); border: 1px solid var(--line-soft); border-radius: 9px; }
.seg-btn { border: 0; background: transparent; color: var(--muted); padding: 5px 9px; border-radius: 6px; font-size: 11px; }
.seg-btn.active { color: var(--text); background: var(--surface-2); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.accent-row { display: flex; gap: 7px; }
.accent-btn { width: 24px; height: 24px; padding: 3px; border-radius: 50%; border: 2px solid transparent; background: transparent; }
.accent-btn span { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--accent); }
.accent-btn.accent-blue span { background: #38bdf8; }
.accent-btn.accent-violet span { background: #a78bfa; }
.accent-btn.accent-amber span { background: #fbbf24; }
.accent-btn.active { border-color: var(--text); }
.switch { position: relative; width: 38px; height: 22px; display: inline-block; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: .2s; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
#set-refresh, #set-range { min-width: 118px; }
.home-order { display: grid; gap: 2px; }
.home-order-row { padding: 7px 0; }
.check-label { display: inline-flex; align-items: center; gap: 8px; color: var(--text); font-size: 12px; }
.check-label input { accent-color: var(--accent); width: 15px; height: 15px; }
.move-buttons { display: flex; gap: 4px; }
.mini-btn { border: 1px solid var(--line); background: var(--panel-alt); color: var(--muted); border-radius: 7px; width: 28px; height: 26px; font-size: 12px; }
.mini-btn:hover:not(:disabled) { color: var(--text); border-color: var(--accent-2); }
.mini-btn:disabled { opacity: .35; cursor: default; }
.danger-btn { border: 1px solid rgba(248,113,113,.3); background: rgba(248,113,113,.07); color: var(--crit); border-radius: 9px; padding: 8px 12px; font-size: 12px; }
.danger-btn:hover { background: rgba(248,113,113,.13); }

@media (min-width: 720px) {
  .settings-panel { padding: 15px; }
}
@media (max-width: 480px) {
  .setting-row { align-items: flex-start; flex-direction: column; gap: 7px; }
  .seg { width: 100%; }
  .seg-btn { flex: 1; }
  .chart-foot { gap: 8px; flex-wrap: wrap; }
  .status-strip { gap: 5px; }
}
@media (prefers-reduced-motion: reduce) {
  html[data-animations="on"] .chart svg { animation: none; }
}

/* Stage 4B — Telegram auth gate */
.auth-gate {
  max-width: 420px;
  margin: 12vh auto 0;
  padding: 28px 22px;
  text-align: center;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel-bg);
  box-shadow: var(--shadow);
}
.auth-mark {
  width: 46px; height: 46px; border-radius: 14px;
  display: grid; place-items: center; margin: 0 auto 14px;
  font-weight: 800; color: #052e16;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.auth-gate h1 { font-size: 20px; margin: 0 0 8px; }
.auth-gate p { color: var(--muted); font-size: 12px; line-height: 1.55; }
.auth-gate .note { margin-top: 14px; color: var(--muted-2); font-size: 10px; }
html[data-theme="light"] .auth-gate { background: var(--panel-bg); }
