:root {
  color-scheme: dark;
  --bg: #050a12;
  --bg-raised: #07111d;
  --panel: #0b1726;
  --panel-2: #101f31;
  --panel-3: #13283d;
  --border: #213a54;
  --border-bright: #315b7e;
  --text: #f4f8fc;
  --muted: #92a8bb;
  --cyan: #22d3ee;
  --blue: #2563eb;
  --blue-2: #0ea5e9;
  --green: #31d08b;
  --yellow: #f7c948;
  --red: #fb7185;
  --shadow: 0 18px 45px rgba(0, 0, 0, .34);
  --radius: 18px;
  --radius-sm: 11px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 0%, rgba(14, 165, 233, .14), transparent 30rem),
    radial-gradient(circle at 90% 20%, rgba(37, 99, 235, .12), transparent 30rem),
    linear-gradient(180deg, #07111d 0%, var(--bg) 60%);
}
button, input, select { font: inherit; }
a { color: var(--cyan); text-decoration: none; }
a:hover { color: #8beeff; }
img { max-width: 100%; }

.app-shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 22px;
  min-height: 72px;
  padding: 10px clamp(16px, 4vw, 54px);
  border-bottom: 1px solid rgba(49, 91, 126, .6);
  background: rgba(5, 10, 18, .89);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 11px; color: var(--text); white-space: nowrap; }
.brand:hover { color: var(--text); }
.brand img { filter: drop-shadow(0 7px 16px rgba(14, 165, 233, .22)); }
.brand span { display: grid; line-height: 1.1; }
.brand strong { font-size: 1rem; letter-spacing: .01em; }
.brand small { color: var(--muted); font-size: .72rem; margin-top: 4px; }
.topnav { display: flex; align-items: center; gap: 5px; margin-left: auto; }
.topnav a {
  color: #b8c7d5;
  padding: 10px 13px;
  border-radius: 10px;
  font-size: .91rem;
  font-weight: 700;
}
.topnav a:hover, .topnav a.active { color: white; background: var(--panel-2); }
.topnav a.active { box-shadow: inset 0 0 0 1px rgba(34, 211, 238, .24); }
.logout-form { margin: 0; }

.page-content { width: min(1480px, 100%); margin: 0 auto; padding: 30px clamp(16px, 4vw, 54px) 54px; flex: 1; }
.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 24px;
  padding: 20px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: .82rem;
}

.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.page-heading h1 { margin: 0 0 7px; font-size: clamp(1.65rem, 3vw, 2.4rem); letter-spacing: -.035em; }
.page-heading p { margin: 0; color: var(--muted); max-width: 780px; line-height: 1.55; }
.heading-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }

.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.layout-map { display: grid; grid-template-columns: minmax(300px, 390px) minmax(0, 1fr); gap: 18px; align-items: stretch; }
.layout-detail { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(300px, .6fr); gap: 18px; align-items: start; }

.card {
  background: linear-gradient(160deg, rgba(16, 31, 49, .96), rgba(8, 20, 34, .98));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-pad { padding: 22px; }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 15px; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.card-header h2, .card-header h3 { margin: 0; font-size: 1.02rem; }
.card-body { padding: 20px; }
.card-subtle { background: rgba(16, 31, 49, .58); box-shadow: none; }

.stat-card { padding: 20px; }
.stat-label { color: var(--muted); font-size: .8rem; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; }
.stat-value { margin-top: 8px; font-size: 1.7rem; font-weight: 850; letter-spacing: -.04em; }
.stat-note { margin-top: 7px; color: var(--muted); font-size: .83rem; }

.button {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 11px;
  min-height: 42px;
  padding: 10px 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: white;
  background: linear-gradient(135deg, var(--blue-2), var(--blue));
  box-shadow: 0 10px 24px rgba(37, 99, 235, .22);
  font-weight: 800;
  cursor: pointer;
  transition: transform .16s ease, border-color .16s ease, filter .16s ease;
}
.button:hover { color: white; filter: brightness(1.08); transform: translateY(-1px); }
.button:active { transform: translateY(0); }
.button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible { outline: 3px solid rgba(34, 211, 238, .45); outline-offset: 2px; }
.button-secondary { background: var(--panel-3); border-color: var(--border-bright); box-shadow: none; }
.button-ghost { background: transparent; border-color: var(--border); box-shadow: none; color: #c6d4df; }
.button-danger { background: rgba(190, 24, 55, .25); border-color: rgba(251, 113, 133, .55); box-shadow: none; }
.button-small { min-height: 36px; padding: 7px 11px; font-size: .84rem; }
.button[disabled] { opacity: .55; cursor: not-allowed; transform: none; }

.form-grid { display: grid; gap: 16px; }
.form-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.field { display: grid; gap: 7px; }
.field label { font-size: .85rem; color: #c6d4df; font-weight: 750; }
.field small { color: var(--muted); line-height: 1.45; }
input, select {
  width: 100%;
  min-height: 46px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: #07121f;
  color: var(--text);
  padding: 10px 12px;
}
input::placeholder { color: #63788c; }
.checkbox-row { display: flex; gap: 10px; align-items: flex-start; color: #c6d4df; line-height: 1.45; }
.checkbox-row input { width: 19px; height: 19px; min-height: 0; flex: 0 0 auto; margin-top: 2px; }
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 3px; }
.help-text { color: var(--muted); line-height: 1.55; font-size: .9rem; }
.error-box, .notice-box, .success-box {
  padding: 13px 15px;
  border-radius: 11px;
  border: 1px solid;
  line-height: 1.5;
  font-size: .9rem;
}
.error-box { background: rgba(190, 24, 55, .18); border-color: rgba(251, 113, 133, .5); color: #fecdd3; }
.notice-box { background: rgba(14, 165, 233, .12); border-color: rgba(34, 211, 238, .35); color: #c9f8ff; }
.success-box { background: rgba(22, 163, 74, .14); border-color: rgba(49, 208, 139, .38); color: #c8fbe6; }

.auth-wrap { min-height: calc(100vh - 180px); display: grid; place-items: center; padding: 30px 0; }
.auth-card { width: min(520px, 100%); }
.auth-logo { display: grid; place-items: center; text-align: center; gap: 10px; margin-bottom: 20px; }
.auth-logo img { width: 88px; height: 88px; }
.auth-logo h1 { margin: 0; font-size: 1.8rem; }
.auth-logo p { margin: 0; color: var(--muted); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 13px 14px; border-bottom: 1px solid rgba(33, 58, 84, .72); text-align: left; white-space: nowrap; }
th { color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .07em; background: rgba(7, 17, 29, .7); position: sticky; top: 0; }
td { font-size: .9rem; }
tbody tr { transition: background .15s ease; }
tbody tr:hover { background: rgba(34, 211, 238, .045); }
.table-link { color: white; font-weight: 800; }
.table-muted { color: var(--muted); }

.status { display: inline-flex; align-items: center; gap: 7px; padding: 5px 9px; border-radius: 999px; border: 1px solid currentColor; font-size: .75rem; font-weight: 850; }
.status::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: currentColor; box-shadow: 0 0 12px currentColor; }
.status-success { color: var(--green); background: rgba(49, 208, 139, .08); }
.status-warning { color: var(--yellow); background: rgba(247, 201, 72, .08); }
.status-danger { color: var(--red); background: rgba(251, 113, 133, .08); }

.device-list { max-height: 660px; overflow-y: auto; padding: 10px; }
.device-item { display: block; padding: 14px; border: 1px solid transparent; border-radius: 13px; color: var(--text); margin-bottom: 7px; }
.device-item:hover, .device-item.active { background: var(--panel-2); border-color: var(--border); color: white; }
.device-item-top { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.device-item strong { overflow: hidden; text-overflow: ellipsis; }
.device-item-meta { display: flex; justify-content: space-between; gap: 10px; margin-top: 9px; color: var(--muted); font-size: .78rem; }
.empty-state { text-align: center; padding: 48px 24px; color: var(--muted); }
.empty-state h2 { color: var(--text); margin: 0 0 10px; }

.map { width: 100%; min-height: 540px; background: #07111d; }
.map-large { min-height: 610px; }
.map-placeholder { min-height: 540px; display: grid; place-items: center; color: var(--muted); padding: 30px; text-align: center; }
.leaflet-container { font-family: inherit; background: #07111d; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel-2); color: var(--text); }

.detail-list { display: grid; gap: 0; margin: 0; }
.detail-row { display: grid; grid-template-columns: 140px minmax(0, 1fr); gap: 16px; padding: 12px 0; border-bottom: 1px solid rgba(33, 58, 84, .7); }
.detail-row:last-child { border-bottom: 0; }
.detail-row dt { color: var(--muted); }
.detail-row dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.coordinates { font-variant-numeric: tabular-nums; }

.pair-code { display: grid; place-items: center; padding: 26px; background: #050b13; border: 1px dashed var(--border-bright); border-radius: 15px; }
.pair-code strong { font-size: clamp(2rem, 8vw, 4.3rem); letter-spacing: .16em; font-variant-numeric: tabular-nums; text-indent: .16em; background: linear-gradient(90deg, var(--cyan), #b6f5ff, #60a5fa); -webkit-background-clip: text; background-clip: text; color: transparent; }
.pair-code span { color: var(--muted); margin-top: 6px; }
.steps { counter-reset: step; display: grid; gap: 13px; padding: 0; margin: 0; list-style: none; }
.steps li { counter-increment: step; display: grid; grid-template-columns: 32px 1fr; gap: 11px; align-items: start; color: #c5d2dd; line-height: 1.5; }
.steps li::before { content: counter(step); display: grid; place-items: center; width: 30px; height: 30px; border-radius: 50%; background: rgba(14, 165, 233, .14); border: 1px solid rgba(34, 211, 238, .35); color: var(--cyan); font-weight: 900; }

.log-list { display: grid; gap: 0; }
.log-item { display: grid; grid-template-columns: 170px 160px minmax(0, 1fr); gap: 16px; padding: 14px 0; border-bottom: 1px solid rgba(33, 58, 84, .7); align-items: start; }
.log-item:last-child { border-bottom: 0; }
.log-time, .log-type { color: var(--muted); font-size: .82rem; }
.log-message { line-height: 1.5; }

.toast-stack { position: fixed; right: 18px; top: 88px; z-index: 2000; display: grid; gap: 9px; width: min(420px, calc(100vw - 36px)); }
.toast { padding: 13px 15px; border-radius: 12px; background: var(--panel-2); border: 1px solid var(--border-bright); box-shadow: var(--shadow); transition: opacity .35s ease, transform .35s ease; }
.toast-success { border-color: rgba(49, 208, 139, .55); }
.toast-error { border-color: rgba(251, 113, 133, .6); }
.toast-warning { border-color: rgba(247, 201, 72, .6); }
.toast-hide { opacity: 0; transform: translateX(24px); pointer-events: none; }

.install-progress { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 22px; }
.install-step { text-align: center; padding: 9px; border-radius: 9px; color: var(--muted); background: rgba(16, 31, 49, .55); border: 1px solid var(--border); font-size: .78rem; font-weight: 800; }
.install-step.active, .install-step.done { color: white; border-color: rgba(34, 211, 238, .45); background: rgba(14, 165, 233, .11); }
.code { padding: 3px 7px; border-radius: 7px; background: #02060b; border: 1px solid var(--border); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85em; }

@media (max-width: 1050px) {
  .layout-map, .layout-detail { grid-template-columns: 1fr; }
  .map, .map-large { min-height: 480px; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .topbar { flex-wrap: wrap; gap: 8px 14px; }
  .topnav { order: 3; width: 100%; overflow-x: auto; margin-left: 0; padding-bottom: 2px; }
  .logout-form { margin-left: auto; }
}

@media (max-width: 720px) {
  .page-content { padding-top: 22px; }
  .page-heading { align-items: stretch; flex-direction: column; }
  .heading-actions { justify-content: stretch; }
  .heading-actions .button { flex: 1; }
  .grid-2, .grid-3, .grid-4, .form-row { grid-template-columns: 1fr; }
  .stat-value { font-size: 1.45rem; }
  .map, .map-large { min-height: 420px; }
  .detail-row { grid-template-columns: 1fr; gap: 5px; }
  .detail-row dd { text-align: left; }
  .log-item { grid-template-columns: 1fr; gap: 4px; }
  .brand small { display: none; }
  .topnav a { padding: 9px 11px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
