@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap");

:root {
  --bg: #f7f9fc;
  --bg-soft: #eef2f8;
  --panel: rgba(255, 255, 255, 0.96);
  --panel-2: rgba(255, 255, 255, 0.92);
  --border: #e5e7eb;
  --line: #d8e1ef;
  --line-soft: #e8edf5;
  --text: #0f172a;
  --muted: #5b6b85;
  --primary: #0ea5e9;
  --primary-2: #2563eb;
  --normal: #16a34a;
  --warning: #f59e0b;
  --low: #f97316;
  --critical: #ef4444;
  --offline: #94a3b8;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -20%, #dfe7f6 0%, transparent 60%),
    radial-gradient(900px 500px at 110% -10%, #d7e7ff 0%, transparent 60%),
    var(--bg);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
}

.app-splash {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(800px 400px at 20% 0%, rgba(150, 190, 255, 0.45), transparent 60%),
    radial-gradient(900px 500px at 100% 100%, rgba(120, 190, 255, 0.35), transparent 62%),
    rgba(245, 248, 253, 0.98);
  transition: opacity 280ms ease, visibility 280ms ease;
}
.app-splash.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
.splash-card {
  width: min(360px, 88vw);
  padding: 24px 18px;
  border-radius: 18px;
  border: 1px solid rgba(155, 175, 205, 0.6);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(244, 248, 255, 0.95));
  box-shadow: 0 18px 44px rgba(15, 23, 42, 0.18);
  text-align: center;
  position: relative;
  overflow: hidden;
}
.splash-orbit {
  width: 94px;
  height: 94px;
  border-radius: 50%;
  border: 2px solid rgba(99, 141, 203, 0.3);
  border-top-color: rgba(37, 99, 235, 0.8);
  margin: 0 auto 12px;
  animation: splashSpin 1.15s linear infinite;
}
.splash-logo {
  width: 56px;
  height: 56px;
  object-fit: contain;
  border-radius: 12px;
  position: absolute;
  top: 42px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid rgba(203, 213, 225, 0.9);
  padding: 6px;
}
.splash-card h2 {
  margin: 2px 0 4px;
  font-size: 18px;
}
.splash-card p {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 12px;
}
.splash-bar {
  width: 100%;
  height: 6px;
  border-radius: 99px;
  background: rgba(26, 45, 79, 0.9);
  overflow: hidden;
}
.splash-bar span {
  display: block;
  width: 45%;
  height: 100%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--primary-2), var(--primary));
  animation: splashBar 1.3s ease-in-out infinite;
}
@keyframes splashSpin {
  to { transform: rotate(360deg); }
}
@keyframes splashBar {
  0% { transform: translateX(-120%); }
  50% { transform: translateX(80%); }
  100% { transform: translateX(230%); }
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 120;
  min-height: 72px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 12px 20px;
  background: #ffffff;
  gap: 16px;
  flex-wrap: wrap;
  box-shadow: var(--shadow);
}
.menu-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.menu-btn svg {
  width: 18px;
  height: 18px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  object-fit: contain;
  background: rgba(6, 12, 24, 0.7);
  border: 1px solid rgba(36, 56, 95, 0.8);
  padding: 6px;
}
.brand-wrap h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.2px;
  line-height: 1.15;
  font-weight: 600;
}
.brand-wrap p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
}
.top-actions {
  margin-left: auto;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.icon-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
}
.icon-btn svg {
  width: 18px;
  height: 18px;
  color: var(--text);
}
.status-pill,
.live-pill,
.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 12px;
  color: var(--muted);
}
.status-badge {
  display: none;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 11px;
  color: var(--muted);
}
.status-badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--normal);
}
.status-badge[data-status="warning"] .dot { background: var(--warning); }
.status-badge[data-status="critical"] .dot { background: var(--critical); }
.status-badge[data-status="offline"] .dot { background: var(--offline); }
.status-pill .dot,
.live-pill .dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--normal);
}
.status-pill[data-status="warning"] .dot { background: var(--warning); }
.status-pill[data-status="critical"] .dot { background: var(--critical); }
.status-pill[data-status="offline"] .dot { background: var(--offline); }
.live-pill .dot.live { background: var(--primary); }
.user-pill svg {
  width: 16px;
  height: 16px;
}

.layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: calc(100vh - 72px);
  min-width: 0;
}
.sidebar {
  background: #ffffff;
  border-right: 1px solid var(--line);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: sticky;
  top: 72px;
  height: calc(100vh - 72px);
}
.sidebar-title {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin: 6px 10px 12px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text);
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s ease;
}
.nav-item svg {
  width: 18px;
  height: 18px;
  color: var(--muted);
}
.nav-item:hover {
  background: var(--bg-soft);
}
.nav-item.active {
  background: rgba(37, 99, 235, 0.1);
  color: var(--primary-2);
  border-left: 3px solid var(--primary-2);
}
.nav-item.active svg { color: var(--primary-2); }
.main {
  width: 100%;
  min-width: 0;
  padding: 12px;
  padding-bottom: 84px;
  overflow-x: hidden;
}
.view { display: none; }
.view.show { display: block; }

.panel {
  background: #ffffff;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  min-width: 0;
  overflow: hidden;
  position: relative;
  z-index: 1;
}
.panel h2 {
  margin: 0 0 10px;
  font-size: 20px;
  font-weight: 600;
}
.panel-head {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 8px;
}
.tab-row {
  display: inline-flex;
  gap: 8px;
}
.tab-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.tab-btn.active {
  background: rgba(37, 99, 235, 0.12);
  border-color: rgba(37, 99, 235, 0.4);
  color: var(--primary-2);
}
.tab-view { display: none; }
.tab-view.show { display: block; }
.form-grid {
  display: grid;
  gap: 10px;
}
.panel-head select,
.panel-head button,
.panel-top-actions select,
.panel-top-actions button,
.panel-top-actions input,
.panel-top-actions label {
  width: 100%;
}
.panel-top-actions {
  display: grid;
  gap: 8px;
}
.panel-top-actions label {
  color: var(--muted);
  font-size: 12px;
}
.panel-top-actions select,
.panel-head select,
.onboard-form input,
.onboard-form textarea,
#chartSensorSelect,
#locationFilter {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 10px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}
.stat {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(242, 247, 255, 0.96));
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  padding: 10px;
}
.stat .k {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.45px;
}
.stat .v {
  margin-top: 5px;
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
}

#realtimeChart {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 12px;
  border: 1px solid var(--line-soft);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(241, 246, 255, 0.96));
}
.chart-wrap {
  height: 260px;
  min-height: 260px;
  max-height: 260px;
}

.sensor-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.sensor-panel {
  max-height: 520px;
  overflow: auto;
}
.mini-chart {
  width: 100%;
  height: 60px;
  margin-top: 10px;
}
.sensor-card {
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  padding: 10px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(242, 247, 255, 0.96));
  transition: all 0.2s ease;
}
.sensor-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.12);
}
.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid currentColor;
}
.normal { color: var(--normal); }
.warning { color: var(--warning); }
.low { color: var(--low); }
.critical { color: var(--critical); }
.offline { color: var(--offline); }

.table {
  width: 100%;
  border-collapse: collapse;
}
.table th,
.table td {
  border-bottom: 1px solid var(--line-soft);
  padding: 8px;
  text-align: left;
  font-size: 13px;
}
.table th {
  color: #475569;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.35px;
}

.toolbar {
  display: flex;
  gap: 8px;
  margin: 8px 0;
}
.toolbar button,
.btn-primary,
.pairing-tools button {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  color: #0f172a;
  cursor: pointer;
}
.btn-open-dashboard {
  display: inline-block;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  text-decoration: none;
  color: #1e3a8a;
  background: linear-gradient(90deg, rgba(37, 99, 235, 0.12), rgba(14, 165, 233, 0.12));
  font-size: 12px;
  font-weight: 600;
}
.btn-calibrate {
  display: inline-block;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(241, 245, 249, 0.9);
  color: #0f172a;
  font-size: 12px;
  font-weight: 600;
  margin-right: 6px;
  cursor: pointer;
}
.toolbar button,
.pairing-tools button {
  background: #eef2f8;
}
.btn-primary {
  font-weight: 700;
  background: linear-gradient(90deg, var(--primary-2), var(--primary));
  border-color: transparent;
  color: #fff;
}

.map {
  height: 260px;
  min-height: 260px;
  max-height: 260px;
  border-radius: 12px;
  overflow: hidden;
  background: #e9eef6;
  border: 1px solid var(--line-soft);
  position: relative;
  z-index: 1;
}
.leaflet-container {
  height: 260px !important;
  max-height: 260px !important;
}

.alert {
  border: 1px solid var(--line-soft);
  border-left-width: 6px;
  border-radius: 11px;
  padding: 10px;
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.96);
}
.alert.normal { border-left-color: var(--normal); }
.alert.warning { border-left-color: var(--warning); }
.alert.low { border-left-color: var(--low); }
.alert.critical { border-left-color: var(--critical); }
.alert.offline { border-left-color: var(--offline); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(226, 232, 240, 0.8);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  z-index: 100;
}
.hidden { display: none !important; }
.onboard-card {
  width: min(680px, 100%);
  max-height: 92vh;
  overflow: auto;
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: 14px;
}
.onboard-card p {
  color: var(--muted);
  margin: 0 0 10px;
}
.onboard-form {
  display: grid;
  gap: 8px;
}
.onboard-form label {
  font-size: 12px;
  color: var(--muted);
}
.row-2 {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}
.setup-msg {
  min-height: 18px;
  font-size: 12px;
  color: var(--muted);
}
.setup-msg.ok { color: var(--normal); }
.setup-msg.err { color: var(--critical); }
.pairing-tools {
  display: grid;
  gap: 8px;
  margin-bottom: 4px;
}
#selectedDeviceInfo {
  font-size: 12px;
  color: var(--muted);
}

.bottom-nav {
  display: grid;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  grid-template-columns: repeat(4, 1fr);
  border-top: 1px solid var(--line);
  background: #ffffff;
  backdrop-filter: blur(8px);
}
.bottom-nav a {
  color: var(--muted);
  text-decoration: none;
  text-align: center;
  padding: 10px 4px 11px;
  font-size: 11px;
  border-top: 2px solid transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.bottom-nav a svg {
  width: 18px;
  height: 18px;
}
.bottom-nav a.active {
  color: var(--primary-2);
  border-top-color: var(--primary-2);
  background: rgba(37, 99, 235, 0.08);
}

.install-banner {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 78px;
  z-index: 60;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #ffffff;
  padding: 10px;
  box-shadow: var(--shadow);
}
.install-text {
  display: grid;
  gap: 2px;
}
.install-text strong {
  font-size: 13px;
}
.install-text span {
  font-size: 11px;
  color: var(--muted);
}
.install-actions {
  display: flex;
  gap: 6px;
}
.install-actions button {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  background: #f3f4f6;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.15);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 15;
}

body.sidebar-open .sidebar-backdrop {
  opacity: 1;
  pointer-events: auto;
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: 240px 1fr;
  }
  .menu-btn { display: none; }
  .sidebar {
    position: sticky;
    transform: translateX(0);
  }
  .main {
    padding: 14px;
    padding-bottom: 14px;
  }
  .stats-grid {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
  .sensor-grid {
    grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
  }
  .panel-head {
    grid-template-columns: 1fr 260px;
    align-items: center;
  }
  .panel-top-actions {
    grid-template-columns: 150px 240px 180px;
    align-items: center;
    justify-content: end;
  }
  .row-2 {
    grid-template-columns: 1fr 1fr;
  }
  .bottom-nav { display: none; }
  .install-banner {
    right: 18px;
    left: auto;
    bottom: 16px;
    max-width: 410px;
  }
  .topbar {
    padding: 12px 18px;
    flex-wrap: nowrap;
  }
  .brand-wrap h1 {
    font-size: 28px;
  }
}

@media (max-width: 899px) {
  .sensor-panel {
    max-height: 60vh;
  }
  .menu-btn { display: inline-flex; }
  .layout {
    grid-template-columns: 1fr;
  }
  .map {
    height: 220px;
    min-height: 220px;
    max-height: 220px;
  }
  .leaflet-container {
    height: 220px !important;
    max-height: 220px !important;
  }
  .top-actions {
    width: 100%;
    justify-content: space-between;
  }
  .status-pill { display: none; }
  .status-badge { display: inline-flex; }
  .sidebar {
    position: fixed;
    top: 72px;
    left: 0;
    bottom: 0;
    width: 240px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 30;
  }
  body.sidebar-open .sidebar {
    transform: translateX(0);
  }
  .table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}
