/* ================================================
   Sensorio – Main Stylesheet
   Mobile-first, dark theme, PWA optimized
   ================================================ */

:root {
  --bg-primary:   #0f172a;
  --bg-secondary: #1e293b;
  --bg-card:      #1e293b;
  --bg-input:     #0f172a;
  --border:       #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted:   #64748b;
  --accent:       #3b82f6;
  --accent-hover: #2563eb;
  --success:      #22c55e;
  --warning:      #f59e0b;
  --danger:       #ef4444;
  --critical:     #dc2626;
  --info:         #06b6d4;
  --sidebar-w:    240px;
  --topbar-h:     56px;
  --radius:       10px;
  --shadow:       0 4px 16px rgba(0,0,0,0.4);
  --transition:   0.2s ease;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---- Utilities ---- */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
.text-muted { color: var(--text-muted); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.text-success { color: var(--success); }

/* ---- Login Screen ---- */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}

.login-logo {
  font-size: 48px;
  text-align: center;
  margin-bottom: 12px;
}

.login-card h1 {
  text-align: center;
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 4px;
  color: var(--accent);
}

.login-subtitle {
  text-align: center;
  color: var(--text-secondary);
  margin: 0 0 32px;
  font-size: 14px;
}

/* ---- Forms ---- */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="url"],
select,
textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  transition: border-color var(--transition);
  -webkit-appearance: none;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
}

select { cursor: pointer; }

.error-msg {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.3);
  border-radius: 6px;
  color: #fca5a5;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }
.btn-block { width: 100%; }

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: var(--accent); }

.btn-danger {
  background: rgba(239,68,68,0.15);
  color: var(--danger);
  border: 1px solid rgba(239,68,68,0.3);
}
.btn-danger:hover { background: rgba(239,68,68,0.25); }

.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-icon { width: 36px; height: 36px; padding: 0; border-radius: 8px; }

/* ---- App Shell ---- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  z-index: 200;
  padding-top: env(safe-area-inset-top);
}

.topbar-title {
  flex: 1;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.hamburger {
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex-shrink: 0;
}

.hamburger span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 2px;
  transition: all var(--transition);
}

.alert-badge {
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 20px;
  min-width: 22px;
  text-align: center;
}

/* ---- Sidebar ---- */
.sidebar {
  position: fixed;
  top: 0; left: 0;
  width: var(--sidebar-w);
  height: 100dvh;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 0 0 16px;
  z-index: 300;
  transform: translateX(-100%);
  transition: transform var(--transition);
  overflow-y: auto;
}

.sidebar.open {
  transform: translateX(0);
  box-shadow: var(--shadow);
}

.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 250;
  backdrop-filter: blur(2px);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}

.sidebar-icon { font-size: 24px; }

.sidebar-brand {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0 8px;
  flex: 1;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 8px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all var(--transition);
  margin-bottom: 2px;
}

.nav-link:hover {
  background: rgba(59,130,246,0.1);
  color: var(--text-primary);
}

.nav-link.active {
  background: rgba(59,130,246,0.15);
  color: var(--accent);
}

.nav-icon { font-size: 18px; flex-shrink: 0; }

.logout-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px;
  padding: 10px 14px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  transition: all var(--transition);
  width: calc(100% - 32px);
}
.logout-btn:hover { color: var(--danger); border-color: var(--danger); }

/* ---- Main Content ---- */
.main-content {
  margin-top: var(--topbar-h);
  padding: 20px 16px;
  min-height: calc(100dvh - var(--topbar-h));
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
}

@media (min-width: 768px) {
  .hamburger { display: none; }
  .sidebar {
    transform: translateX(0);
    box-shadow: none;
  }
  .sidebar-overlay { display: none !important; }
  .main-content {
    margin-left: var(--sidebar-w);
    padding: 24px 32px;
  }
  .topbar { left: var(--sidebar-w); }
}

/* ---- Loading State ---- */
.page-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  color: var(--text-muted);
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Cards ---- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
}

.card-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
}

/* ---- Meter Card ---- */
.meter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.meter-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  position: relative;
  overflow: hidden;
  transition: border-color var(--transition);
}

.meter-card:hover { border-color: var(--accent); }

.meter-card.status-ok   { border-left: 3px solid var(--success); }
.meter-card.status-warn { border-left: 3px solid var(--warning); }
.meter-card.status-crit { border-left: 3px solid var(--danger); }
.meter-card.status-offline { border-left: 3px solid var(--text-muted); opacity: 0.7; }

.meter-name {
  font-size: 14px;
  font-weight: 600;
  margin: 0 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meter-location {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 0 16px;
}

.meter-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat {
  display: flex;
  flex-direction: column;
}

.stat-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
}

.stat-unit {
  font-size: 11px;
  color: var(--text-secondary);
}

.meter-footer {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
}

.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 6px;
}
.dot-ok   { background: var(--success); box-shadow: 0 0 6px var(--success); }
.dot-warn { background: var(--warning); box-shadow: 0 0 6px var(--warning); animation: pulse 2s infinite; }
.dot-crit { background: var(--danger);  box-shadow: 0 0 8px var(--danger);  animation: pulse 1s infinite; }
.dot-off  { background: var(--text-muted); }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ---- Alert Items ---- */
.alert-list { display: flex; flex-direction: column; gap: 10px; }

.alert-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  border-left: 3px solid var(--border);
}

.alert-item.severity-warning  { border-left-color: var(--warning); }
.alert-item.severity-critical  { border-left-color: var(--danger); }
.alert-item.severity-info      { border-left-color: var(--info); }
.alert-item.status-acknowledged { opacity: 0.6; }
.alert-item.status-resolved    { opacity: 0.4; }

.alert-type {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.alert-message {
  font-size: 14px;
  margin: 0 0 8px;
  line-height: 1.4;
}

.alert-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-muted);
}

.alert-actions {
  margin-top: 10px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ---- Pipeline View ---- */
.pipeline-container {
  overflow-x: auto;
  padding-bottom: 16px;
}

.pipeline-row {
  display: flex;
  align-items: stretch;
  gap: 0;
  min-width: max-content;
  padding: 20px 0;
}

.pipeline-meter {
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 160px;
  text-align: center;
  position: relative;
}

.pipeline-meter.has-loss { border-color: var(--warning); }
.pipeline-meter.high-loss { border-color: var(--danger); }

.pipeline-pipe {
  display: flex;
  align-items: center;
  padding: 0 8px;
}

.pipe-line {
  height: 4px;
  width: 80px;
  background: var(--border);
  border-radius: 2px;
  position: relative;
}

.pipe-loss {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  font-size: 11px;
  font-weight: 600;
  color: var(--warning);
}

.pipe-loss.high { color: var(--danger); }

.pipe-arrow {
  border: solid var(--text-muted);
  border-width: 0 3px 3px 0;
  padding: 5px;
  transform: rotate(-45deg);
  margin-left: 4px;
}

/* ---- Charts ---- */
.chart-container {
  position: relative;
  width: 100%;
  height: 280px;
}

@media (min-width: 768px) {
  .chart-container { height: 340px; }
}

/* ---- Stats Summary ---- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

@media (min-width: 640px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
}

.stat-card .stat-icon { font-size: 24px; margin-bottom: 8px; }
.stat-card .stat-value { font-size: 24px; font-weight: 700; color: var(--text-primary); }
.stat-card .stat-label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* ---- Settings ---- */
.settings-section {
  margin-bottom: 32px;
}

.settings-section h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 0 0 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
  gap: 16px;
  flex-wrap: wrap;
}

.settings-row:last-child { border-bottom: none; }

.settings-label {
  flex: 1;
  min-width: 200px;
}

.settings-label strong {
  display: block;
  font-size: 14px;
  font-weight: 500;
}

.settings-label small {
  color: var(--text-muted);
  font-size: 12px;
}

/* ---- Page Header ---- */
.page-header {
  margin-bottom: 24px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.page-header h1 {
  font-size: 22px;
  font-weight: 700;
  margin: 0;
}

.page-header p {
  color: var(--text-secondary);
  font-size: 14px;
  margin: 4px 0 0;
}

/* ---- Temp indicator ---- */
.temp-critical { color: var(--danger); font-weight: 700; }
.temp-warning  { color: var(--warning); font-weight: 600; }
.temp-ok       { color: var(--success); }

/* ---- Badge ---- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}
.badge-warning  { background: rgba(245,158,11,0.15); color: var(--warning); }
.badge-danger   { background: rgba(239,68,11,0.15);  color: var(--danger); }
.badge-success  { background: rgba(34,197,94,0.15);  color: var(--success); }
.badge-info     { background: rgba(6,182,212,0.15);  color: var(--info); }
.badge-muted    { background: rgba(100,116,139,0.15); color: var(--text-muted); }

/* ---- Meter status badges ---- */
.meter-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 10px;
}

/* ---- Debug page ---- */
.debug-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}
@media (max-width: 640px) { .debug-grid-2 { grid-template-columns: 1fr; } }

.debug-kv { padding: 0 20px 16px; }
.debug-kv-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.debug-kv-row:last-child { border-bottom: none; }
.debug-kv-label { color: var(--text-muted); }
.debug-kv-value { font-weight: 600; font-family: monospace; }

/* Debug table */
.debug-scroll { overflow-x: auto; }
.debug-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  white-space: nowrap;
}
.debug-table th {
  background: var(--bg-primary);
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.05em;
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.debug-table td {
  padding: 7px 12px;
  border-bottom: 1px solid rgba(51,65,85,0.5);
  vertical-align: middle;
}
.debug-table tr:last-child td { border-bottom: none; }
.debug-table tr:hover td { background: rgba(255,255,255,0.02); }

/* Status classes */
.dbg-ok   { color: var(--success); }
.dbg-warn { color: var(--warning); }
.dbg-crit { color: var(--danger); font-weight: 600; }

/* Flag badges in table */
.dbg-flag {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 5px;
  border-radius: 4px;
  text-transform: uppercase;
  margin: 1px;
}
.dbg-flag.warn { background: rgba(245,158,11,0.15); color: var(--warning); }
.dbg-flag.crit { background: rgba(239,68,68,0.15);  color: var(--danger); }
.dbg-flag.ok   { background: rgba(34,197,94,0.15);  color: var(--success); }

/* Test result box */
.debug-result {
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
}
.debug-result pre {
  margin: 8px 0 0;
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 200px;
  overflow-y: auto;
}
.debug-result.ok   { background: rgba(34,197,94,0.1);  border: 1px solid rgba(34,197,94,0.3); }
.debug-result.crit { background: rgba(239,68,68,0.1);  border: 1px solid rgba(239,68,68,0.3); }

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ---- Toasts ---- */
.toast-container {
  position: fixed;
  bottom: calc(16px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}

.toast {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in 0.3s ease;
  pointer-events: auto;
  max-width: 90vw;
}

.toast.toast-success { border-color: var(--success); color: var(--success); }
.toast.toast-error   { border-color: var(--danger);  color: #fca5a5; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---- Table ---- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; padding: 10px 12px; color: var(--text-muted); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid var(--border); }
td { padding: 12px 12px; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,0.02); }

/* ---- Select device dropdown ---- */
.select-wrap { position: relative; }
.select-wrap select { padding-right: 32px; }
.select-wrap::after {
  content: '▼';
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  font-size: 10px;
  color: var(--text-muted);
}

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state h3 { font-size: 18px; margin: 0 0 8px; color: var(--text-secondary); }
.empty-state p  { font-size: 14px; margin: 0; }

/* ---- Period selector ---- */
.period-selector {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.period-btn {
  padding: 6px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
}

.period-btn.active,
.period-btn:hover {
  background: rgba(59,130,246,0.15);
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- Device detail period buttons ---- */
.det-period.active {
  background: rgba(59,130,246,0.2);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

/* ---- Clickable meter cards ---- */
.meter-card {
  transition: transform var(--transition), box-shadow var(--transition);
}
.meter-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
