:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #1c2330;
  --border: #2a323d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --accent-2: #7ee787;
  --danger: #ff7b72;
  --warn: #d29922;
  --radius: 12px;
  --shadow: 0 8px 24px rgba(0,0,0,.35);
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex-shrink: 0; background: var(--surface);
  border-right: 1px solid var(--border); padding: 22px 14px;
  position: sticky; top: 0; height: 100vh;
}
.brand { font-size: 18px; font-weight: 700; padding: 0 8px 20px; letter-spacing: -.02em; }
.brand small { display:block; color: var(--muted); font-weight: 400; font-size: 12px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: 10px; color: var(--muted); font-weight: 500; margin-bottom: 2px;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--surface-2); color: var(--text); box-shadow: inset 3px 0 0 var(--accent); }
.main { flex: 1; padding: 28px 32px; max-width: 1200px; }
h1 { font-size: 22px; margin: 0 0 20px; letter-spacing: -.02em; }
h2 { font-size: 16px; margin: 26px 0 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(150px,1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.stat { display: flex; flex-direction: column; gap: 4px; }
.stat .v { font-size: 30px; font-weight: 700; letter-spacing: -.03em; }
.stat .l { color: var(--muted); font-size: 13px; }
.stat.accent .v { color: var(--accent-2); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 12px; border-bottom: 1px solid var(--border); font-size: 14px; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
tr:hover td { background: var(--surface-2); }
.badge { display:inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge.on { background: rgba(126,231,135,.15); color: var(--accent-2); }
.badge.off { background: rgba(255,123,114,.15); color: var(--danger); }
.badge.q { background: rgba(210,153,34,.15); color: var(--warn); }
.btn {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  background: var(--accent); color: #06122a; border: none; padding: 9px 15px;
  border-radius: 9px; font-weight: 600; font-size: 14px; font-family: inherit;
}
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--danger); color: #2a0d0a; }
.btn.sm { padding: 5px 10px; font-size: 12px; border-radius: 7px; }
input, textarea, select {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); padding: 10px 12px; border-radius: 9px; font: inherit;
}
input:focus, textarea:focus { outline: none; border-color: var(--accent); }
textarea { min-height: 120px; resize: vertical; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: end; }
.row > * { flex: 1; }
.muted { color: var(--muted); }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.chart { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 10px; }
.chart .bar { flex: 1; background: linear-gradient(var(--accent), #2b5b9e); border-radius: 5px 5px 0 0; min-height: 3px; position: relative; }
.chart .bar span { position: absolute; top: -18px; left: 0; right: 0; text-align: center; font-size: 11px; color: var(--muted); }
.chart-x { display: flex; gap: 6px; margin-top: 6px; }
.chart-x span { flex: 1; text-align: center; font-size: 10px; color: var(--muted); }
/* login */
.login-wrap { display: grid; place-items: center; min-height: 100vh; width: 100%; padding: 20px; }
.login-card { width: 340px; max-width: 100%; }
.err { background: rgba(255,123,114,.12); color: var(--danger); padding: 10px 12px; border-radius: 9px; margin-bottom: 14px; font-size: 14px; }

/* таблицы никогда не ломают вёрстку — скроллятся внутри карточки */
.card { max-width: 100%; }
table { min-width: 520px; }

/* ---------- iPhone / планшет / Telegram Mini App ---------- */
@media (max-width: 820px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 0; z-index: 20;
    border-right: none; border-bottom: 1px solid var(--border);
    padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
    display: flex; align-items: center; gap: 8px;
  }
  .brand { padding: 0 6px 0 4px; font-size: 15px; white-space: nowrap; }
  .brand small { display: none; }
  .nav {
    display: flex; flex-direction: row; gap: 4px; overflow-x: auto;
    -webkit-overflow-scrolling: touch; flex: 1; scrollbar-width: none;
  }
  .nav::-webkit-scrollbar { display: none; }
  .nav a { padding: 8px 12px; margin: 0; white-space: nowrap; border-radius: 20px; font-size: 14px; }
  .nav a.active { box-shadow: none; background: var(--accent); color: #06122a; }
  .nav a[href="/logout"] { margin-top: 0; }
  .main {
    padding: 16px 14px calc(16px + env(safe-area-inset-bottom));
    padding-left: calc(14px + env(safe-area-inset-left));
    padding-right: calc(14px + env(safe-area-inset-right));
  }
  h1 { font-size: 20px; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .stat .v { font-size: 26px; }
  /* карточки с таблицами скроллим по горизонтали */
  .card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  /* формы в строку складываются в столбик */
  .row { flex-direction: column; align-items: stretch; }
  .row > * { width: 100%; }
  .toolbar { gap: 8px; }
  .toolbar input { max-width: 100% !important; }
  .btn { padding: 10px 14px; }
}

@media (max-width: 400px) {
  .grid { grid-template-columns: 1fr; }
}
