:root {
  --bg: #0a0c10;
  --bg-elevated: #141820;
  --bg-card: #1a1f28;
  --border: #2a3140;
  --text: #eef0f3;
  --text-muted: #8b94a3;
  --accent: #3b82f6;
  --accent-pressed: #2563eb;
  --ok: #22c55e;
  --danger: #f87171;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --touch: 48px;
}

* {
  box-sizing: border-box;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 120% 80% at 50% -30%, rgba(59, 130, 246, 0.12), transparent 55%);
  color: var(--text);
  line-height: 1.45;
}

.hidden { display: none !important; }

.login-card {
  max-width: min(400px, calc(100% - 2rem));
  margin: max(8dvh, 2rem) auto;
  padding: 1.5rem 1.25rem 1.35rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.login-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.login-card::before {
  content: "Управление устройствами";
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.login-card input,
.login-card button { width: 100%; }

input,
button,
select {
  margin: 0.35rem 0;
  padding: 0.75rem 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: inherit;
  font-size: 16px;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
}

button {
  cursor: pointer;
  font-weight: 500;
}

button.primary,
#btnLogin {
  background: var(--accent);
  border: none;
  color: #fff;
  min-height: var(--touch);
}

button.primary:active { background: var(--accent-pressed); }

.err { color: var(--danger); font-size: 0.875rem; margin-top: 0.5rem; }

.app-shell { min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  padding-top: calc(0.5rem + env(safe-area-inset-top));
  background: rgba(20, 24, 32, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

.topbar .title {
  flex: 1;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.icon-btn {
  min-width: var(--touch);
  min-height: var(--touch);
  padding: 0 0.65rem;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  color: var(--text);
}

.main-view {
  flex: 1;
  padding: 0.75rem 1rem 1rem;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

.meta-pill {
  font-size: 0.8rem;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius-sm);
  margin-bottom: 0.75rem;
  line-height: 1.5;
}

.meta-pill.empty {
  border-style: dashed;
  text-align: center;
  padding: 1rem;
}

.screen-section { margin-bottom: 0.85rem; }

.screen-frame {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  max-width: 100%;
  touch-action: none;
  border: 1px solid var(--border);
  min-height: 160px;
  aspect-ratio: 9 / 16;
  max-height: min(52dvh, 520px);
  margin: 0 auto;
}

#screen {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.screen-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  pointer-events: none;
}

.screen-hint {
  position: absolute;
  left: 10px;
  bottom: 10px;
  right: 10px;
  font-size: 0.68rem;
  color: #cbd5e1;
  background: rgba(0, 0, 0, 0.65);
  padding: 6px 10px;
  border-radius: 8px;
  text-align: center;
}

.toolbar.scroll-x {
  display: flex;
  gap: 0.45rem;
  overflow-x: auto;
  padding: 0.75rem 0.15rem 0.25rem;
  margin: 0 -0.15rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.toolbar.scroll-x::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  min-height: 40px;
  padding: 0.5rem 0.95rem;
  font-size: 0.82rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
}

.chip:active { background: var(--bg-elevated); }

.tabs {
  display: flex;
  gap: 0.35rem;
  margin: 0.65rem 0;
  position: sticky;
  top: calc(52px + env(safe-area-inset-top));
  z-index: 10;
  background: var(--bg);
  padding: 0.35rem 0;
}

.tab {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--bg-card);
  padding: 0.65rem 0.35rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  min-height: 44px;
  color: var(--text-muted);
}

.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.tab-panel { display: none; padding-bottom: 0.5rem; }
.tab-panel.active { display: block; }

.row {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin: 0.65rem 0;
  align-items: stretch;
}

.row input { flex: 1; margin: 0; }

.row button {
  width: 100%;
  margin: 0;
  min-height: var(--touch);
}

pre {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  padding: 0.85rem;
  border-radius: var(--radius-sm);
  max-height: 42dvh;
  overflow: auto;
  font-size: 12px;
  margin: 0;
  -webkit-overflow-scrolling: touch;
}

.sheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 200;
  display: flex;
  align-items: flex-end;
}

.sheet:not(.hidden) {
  pointer-events: auto;
}

body.sheet-open {
  overflow: hidden;
}

body.sheet-open .global-nav {
  z-index: 10;
  opacity: 0;
  pointer-events: none;
}

.sheet-inner {
  width: 100%;
  max-height: 85dvh;
  overflow: auto;
  background: var(--bg-card);
  border-radius: 18px 18px 0 0;
  padding: 0.5rem 1rem calc(1.25rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
}

.sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 99px;
  margin: 0.35rem auto 0.75rem;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.sheet h2 { margin: 0; font-size: 1rem; font-weight: 600; }

.sheet-close {
  min-width: 44px;
  min-height: 44px;
  border: none;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.8rem;
}

.device {
  padding: 0.85rem 0.9rem;
  margin: 0.4rem 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: var(--bg-elevated);
  border: 1px solid transparent;
  font-size: 0.9rem;
  min-height: 56px;
}

.device.active {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.12);
}

.device .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 8px;
  vertical-align: middle;
}

.device .dot.on { background: var(--ok); box-shadow: 0 0 8px rgba(34, 197, 94, 0.5); }
.device .dot.off { background: #6b7280; }

.device small { display: block; margin-top: 0.2rem; color: var(--text-muted); font-size: 0.75rem; }

.muted-list {
  color: var(--text-muted);
  font-size: 0.88rem;
  line-height: 1.45;
  padding: 0.5rem 0.25rem 1rem;
  margin: 0;
}

@media (min-width: 900px) {
  .main-view { padding: 1.25rem; }
  .screen-frame { max-height: 72vh; aspect-ratio: auto; }
  .row { flex-direction: row; align-items: stretch; }
  .row button { width: auto; min-width: 120px; }
  .tabs { position: static; padding: 0; }
  .app-shell { flex-direction: column; }
  body.sheet-open { overflow: auto; }
  body.sheet-open .global-nav {
    opacity: 1;
    pointer-events: auto;
    z-index: 50;
  }
  body.panel-logged-in.has-global-nav #app {
    margin-left: 300px;
  }
  #deviceSheet {
    display: none !important;
  }
  body.panel-logged-in #deviceSheet:not(.hidden) {
    position: fixed;
    left: 108px;
    top: 0;
    bottom: 0;
    width: 300px;
    display: flex !important;
    align-items: stretch;
    background: transparent;
    z-index: 25;
    pointer-events: none;
  }
  #deviceSheet .sheet-inner {
    pointer-events: auto;
    height: 100%;
    max-height: none;
    border-radius: 0;
    border-right: 1px solid var(--border);
    padding-top: calc(4rem + env(safe-area-inset-top));
  }
  #deviceSheet.hidden {
    display: none !important;
  }
  .sheet-handle,
  .sheet-close { display: none; }
  #btnDevices { display: none; }
}