:root {
  --bg: #070b12;
  --surface: #131923;
  --surface-2: #0d121a;
  --line: #253044;
  --text: #f4f8ff;
  --muted: #98a6bd;
  --cyan: #18d3e8;
  --green: #32d179;
  --amber: #ffbd4a;
  --red: #ff5b6b;
  --blue: #2f6cf0;
  --nav-text: #b7c2d4;
  --chart-bg: #0d121a;
  --chart-grid: #222c3d;
  --button-text: #ffffff;
}

body.light-theme {
  --bg: #eef3f8;
  --surface: #ffffff;
  --surface-2: #f6f9fc;
  --line: #c6d2e3;
  --text: #071429;
  --muted: #435671;
  --nav-text: #5f6f89;
  --green: #14874d;
  --amber: #9b6500;
  --red: #c83349;
  --blue: #235fd6;
  --chart-bg: #ffffff;
  --chart-grid: #d6dfec;
  --button-text: #071429;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 280px 1fr;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Segoe UI, Arial, sans-serif;
}

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

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 22px;
  background:
    radial-gradient(circle at 20% 30%, rgba(24, 211, 232, .16), transparent 34%),
    radial-gradient(circle at 80% 70%, rgba(255, 189, 74, .11), transparent 32%),
    var(--bg);
}

body.authenticated .login-screen {
  display: none;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  padding: 30px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(19, 25, 35, .96);
  box-shadow: 0 22px 90px rgba(0, 0, 0, .38);
}

.login-card img {
  width: 86px;
  height: 86px;
  object-fit: cover;
  justify-self: center;
  border-radius: 8px;
  background: white;
}

.login-card h1,
.login-card p {
  margin: 0;
  text-align: center;
}

.login-card p,
.login-card small {
  color: var(--muted);
}

.login-card button,
.logout-btn,
.danger-btn,
.small-btn {
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  color: var(--button-text);
  font-weight: 900;
}

.login-card button {
  background: var(--cyan);
  color: #041018;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 22px 16px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}

.brand img {
  width: 54px;
  height: 54px;
  border-radius: 8px;
  object-fit: cover;
  background: white;
}

.brand strong,
.brand span {
  display: block;
}

.brand span,
.topbar p,
.panel-head p,
.hint {
  color: var(--muted);
}

nav {
  display: grid;
  gap: 10px;
  margin-top: 26px;
}

nav button {
  min-height: 46px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--nav-text);
  text-align: left;
  padding: 0 16px;
  font-weight: 900;
}

nav button.active {
  color: var(--text);
  background: rgba(24, 211, 232, .13);
  box-shadow: inset 4px 0 0 var(--cyan);
}

.side-status {
  margin-top: auto;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.logout-btn {
  margin-top: 12px;
  background: rgba(255, 91, 107, .13);
  color: var(--red);
}

.theme-toggle {
  min-height: 42px;
  margin-top: 12px;
  border: 1px solid rgba(24, 211, 232, .35);
  border-radius: 8px;
  background: rgba(24, 211, 232, .12);
  color: var(--cyan);
  font-weight: 900;
}

.side-status span {
  display: block;
  color: var(--muted);
  margin-bottom: 8px;
}

.content {
  min-width: 0;
  padding: 34px;
}

.topbar,
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.topbar h1,
.panel h2,
.chart-card h3 {
  margin: 0;
}

.topbar h1 { font-size: 36px; }

.device-select {
  display: flex;
  align-items: center;
  gap: 12px;
}

select,
input {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #090e16;
  color: var(--text);
  padding: 0 12px;
}

body.light-theme select,
body.light-theme input {
  background: #ffffff;
  color: #172033;
}

body.light-theme .sidebar {
  background: #ffffff;
}

body.light-theme .brand img {
  border: 1px solid var(--line);
}

.page { display: none; margin-top: 28px; }
.page.active { display: block; }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
}

.metric,
.panel,
.filter-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.metric {
  display: grid;
  gap: 8px;
  padding: 20px;
}

.metric span,
.metric small {
  color: var(--muted);
}

.metric strong {
  font-size: 34px;
}

.device-page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
}

.device-page-head h2 {
  margin: 0;
  font-size: 30px;
}

.device-page-head p {
  color: var(--muted);
  margin: 8px 0 0;
}

.device-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

.device-actions input {
  width: 280px;
}

.device-filter-tabs {
  display: flex;
  gap: 12px;
  margin-top: 26px;
}

.device-filter-tabs button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--muted);
  padding: 0 16px;
  font-weight: 900;
}

.device-filter-tabs button.active {
  color: var(--text);
  border-color: rgba(24, 211, 232, .6);
  background: rgba(24, 211, 232, .11);
}

.device-filter-tabs span {
  color: var(--text);
}

.device-cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 22px;
  margin-top: 32px;
}

.pju-card {
  display: grid;
  gap: 22px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 18px 40px rgba(0,0,0,.18);
}

.pju-card.normal { border-color: rgba(50, 209, 121, .24); }
.pju-card.watch { border-color: rgba(255, 189, 74, .34); }
.pju-card.bad { border-color: rgba(255, 91, 107, .34); }

.pju-card-top {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding-bottom: 18px;
  border-bottom: 1px solid #202938;
}

.pju-card h3 {
  margin: 0 0 8px;
  font-size: 22px;
}

.pju-card p,
.pju-card span {
  margin: 0;
  color: var(--muted);
}

.pju-card-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 28px;
}

.pju-card-metrics div {
  display: grid;
  gap: 10px;
}

.pju-card-metrics span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.pju-card-metrics strong {
  font-size: 22px;
}

.pju-card-metrics i {
  display: block;
  height: 6px;
  border-radius: 999px;
  background: #253044;
  overflow: hidden;
}

.pju-card-metrics b {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--cyan);
}

.pju-card-metrics div:nth-child(2) b {
  background: var(--green);
}

.pju-card-actions {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
}

.pju-card-actions button:first-child {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-weight: 900;
}

.detail-metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-top: 18px;
}

.detail-metric-grid div {
  display: grid;
  gap: 6px;
  padding: 14px;
  border: 1px solid #202938;
  border-radius: 8px;
  background: var(--surface-2);
}

.detail-metric-grid span,
.detail-metric-grid small {
  color: var(--muted);
}

.detail-metric-grid strong {
  font-size: 24px;
}

.main-grid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(340px, .8fr);
  gap: 18px;
  margin-top: 18px;
}

.panel {
  padding: 24px;
}

.chart-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 18px;
}

.chart-card {
  min-width: 0;
  padding: 14px;
  border: 1px solid #202938;
  border-radius: 8px;
  background: var(--surface-2);
}

.chart-card canvas {
  border-radius: 6px;
  background: var(--chart-bg);
}

.chart-card h3 {
  font-size: 14px;
}

canvas {
  width: 100%;
  margin-top: 10px;
}

.system-card {
  display: grid;
  place-items: center;
  margin-top: 18px;
  padding: 22px;
  border: 1px solid rgba(50, 209, 121, .28);
  border-radius: 8px;
  background: rgba(50, 209, 121, .08);
  text-align: center;
}

.system-card strong {
  display: block;
  font-size: 44px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.badge.normal { color: var(--green); background: rgba(50, 209, 121, .13); }
.badge.watch { color: var(--amber); background: rgba(255, 189, 74, .13); }
.badge.bad { color: var(--red); background: rgba(255, 91, 107, .13); }

body.light-theme .badge.normal { background: rgba(20, 135, 77, .13); }
body.light-theme .badge.watch { background: rgba(155, 101, 0, .13); }
body.light-theme .badge.bad { background: rgba(200, 51, 73, .12); }

.summary {
  display: grid;
  gap: 12px;
  margin-top: 18px;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.summary-row span { color: var(--muted); }
.summary-row strong { text-align: right; }

.location-panel {
  margin-top: 18px;
}

.location-panel iframe {
  width: 100%;
  height: 360px;
  margin-top: 18px;
  border: 0;
  border-radius: 8px;
  background: #e6efe4;
}

.location-panel a {
  color: var(--cyan);
  text-decoration: none;
  font-weight: 900;
}

.filter-panel {
  display: grid;
  grid-template-columns: 1fr 1fr auto auto;
  gap: 16px;
  align-items: end;
  padding: 18px;
  margin-bottom: 18px;
}

label {
  display: grid;
  gap: 8px;
  color: #aab8ce;
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.filter-panel button,
.settings-form button,
.secondary-btn,
.primary-btn {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--blue);
  color: #ffffff;
  padding: 0 18px;
  font-weight: 900;
}

.secondary-btn {
  background: rgba(24, 211, 232, .14);
  color: var(--cyan);
  border: 1px solid rgba(24, 211, 232, .35);
}

.primary-btn {
  background: linear-gradient(135deg, var(--cyan), #2f6cf0);
  color: #031018;
  box-shadow: 0 10px 28px rgba(24, 211, 232, .18);
}

.table-wrap {
  overflow: visible;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
}

th {
  color: #aab8ce;
  font-size: 12px;
  text-transform: uppercase;
}

.log-head {
  margin-bottom: 18px;
}

.log-counter {
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
}

.log-table {
  table-layout: fixed;
  font-size: 13px;
}

.log-table th,
.log-table td {
  white-space: normal;
  vertical-align: top;
}

.log-table td span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.log-table th,
.log-table td {
  padding: 9px 6px;
}

.log-table th:nth-child(1) { width: 15%; }
.log-table th:nth-child(2),
.log-table th:nth-child(3),
.log-table th:nth-child(4),
.log-table th:nth-child(5),
.log-table th:nth-child(6),
.log-table th:nth-child(7),
.log-table th:nth-child(8),
.log-table th:nth-child(9),
.log-table th:nth-child(10) { width: 7.2%; }
.log-table th:nth-child(11) { width: 6%; }
.log-table th:nth-child(12) { width: 7%; }
.log-table th:nth-child(13) { width: 10%; }

.device-monitor-panel {
  margin-top: 18px;
}

.alert-list {
  display: grid;
  gap: 10px;
  margin-top: 18px;
}

.alert-row {
  display: grid;
  grid-template-columns: auto minmax(160px, .7fr) minmax(220px, 1fr);
  align-items: center;
  gap: 14px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  text-align: left;
}

.alert-row.normal { border-color: rgba(50, 209, 121, .32); }
.alert-row.watch { border-color: rgba(255, 189, 74, .42); }
.alert-row.bad { border-color: rgba(255, 91, 107, .42); }
.alert-row span:last-child,
.empty-alert { color: var(--muted); }

.empty-alert {
  padding: 16px;
  border: 1px solid rgba(50, 209, 121, .25);
  border-radius: 8px;
  background: rgba(50, 209, 121, .08);
}

.settings-form {
  display: grid;
  max-width: 620px;
  gap: 16px;
  margin-top: 18px;
}

.admin-grid {
  display: grid;
  grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr);
  gap: 18px;
}

.admin-config {
  margin-bottom: 18px;
}

.inline-form {
  display: flex;
  align-items: end;
  gap: 14px;
  margin-top: 14px;
}

.inline-form label {
  max-width: 260px;
}

.admin-note {
  display: grid;
  gap: 6px;
  margin-top: 12px;
  padding: 14px;
  border: 1px solid rgba(24, 211, 232, .24);
  border-radius: 8px;
  background: rgba(24, 211, 232, .08);
}

.admin-note span {
  color: var(--muted);
}

.compact-table {
  margin-top: 22px;
  overflow: auto;
}

.compact-table table {
  min-width: 720px;
}

.danger-btn,
.small-btn {
  min-height: 34px;
  padding: 0 12px;
}

.compact-input {
  width: 160px;
  min-height: 34px;
}

.danger-btn {
  background: rgba(255, 91, 107, .14);
  color: var(--red);
}

.small-btn {
  background: rgba(47, 108, 240, .18);
  color: var(--blue);
}

body.light-theme .secondary-btn,
body.light-theme .theme-toggle {
  background: rgba(24, 211, 232, .16);
  color: #006d7d;
}

body.light-theme .danger-btn {
  background: rgba(200, 51, 73, .12);
}

body.light-theme .small-btn {
  background: rgba(35, 95, 214, .12);
}

code {
  color: var(--cyan);
}

@media (max-width: 1000px) {
  body { grid-template-columns: 1fr; }
  .sidebar { position: relative; height: auto; }
  .metric-grid,
  .detail-metric-grid,
  .main-grid,
  .chart-grid,
  .filter-panel,
  .admin-grid { grid-template-columns: 1fr; }
  .device-page-head,
  .device-actions { flex-direction: column; align-items: stretch; }
  .device-actions input { width: 100%; }
  .alert-row { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: stretch; }
}
