:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-card: #1e293b;
  --bg-card-hover: #273548;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --border: #334155;
  --shadow: 0 4px 6px -1px rgba(0,0,0,0.3);
  --radius: 0px;
  --radius-sm: 0px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  margin: 0;
}

/* Layout */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: 260px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  transition: transform 0.3s;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  font-size: 1.2rem;
  color: var(--accent);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-header .subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.sidebar-nav {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  margin-bottom: 2px;
  text-decoration: none;
}

.nav-item:hover {
  background: var(--bg-card-hover);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--accent);
  color: white;
}

.nav-item .icon {
  font-size: 1.2rem;
  width: 24px;
  text-align: center;
}

.nav-item .badge {
  margin-left: auto;
  background: var(--danger);
  color: white;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 0;
  font-weight: 600;
}

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
}

.mqtt-status {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
}

.status-dot.connected {
  background: var(--success);
}

/* Main Content */
.main-content {
  flex: 1;
  margin-left: 260px;
  padding: 24px;
  min-height: 100vh;
  min-width: 0;
}

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

.page-header h2 {
  font-size: 1.5rem;
  font-weight: 600;
}

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

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

.card-header h3 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-secondary);
}

/* Stats Grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 20px;
  box-shadow: var(--shadow);
}

.stat-card .label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}

.stat-card .value {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1;
}

.stat-card .unit {
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 400;
}

.stat-card .change {
  font-size: 0.8rem;
  margin-top: 6px;
}

.stat-card .change.positive { color: var(--success); }
.stat-card .change.negative { color: var(--danger); }

/* Charts */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

/* Responsiivinen 2-sarake — yksisarake mobiilissa */
.col-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.chart-container {
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 20px;
  box-shadow: var(--shadow);
}

.chart-container canvas {
  width: 100% !important;
  max-height: 300px;
}

/* Tables */
.table-container {
  overflow-x: auto;
}

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

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

td {
  font-size: 0.9rem;
}

tr:hover td {
  background: var(--bg-card-hover);
}

/* Buttons */
.btn {
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text-primary);
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn:hover { background: var(--bg-card-hover); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

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

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: white;
}

.btn-sm { padding: 4px 10px; font-size: 0.8rem; }

.btn-group {
  display: flex;
  gap: 8px;
}

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

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

.form-control {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.9rem;
}

.form-control:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.2);
}

select.form-control {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

/* Toggle */
.toggle {
  position: relative;
  width: 44px;
  height: 24px;
  display: inline-block;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle .slider {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: 0.3s;
}

.toggle .slider::before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: 0.3s;
}

.toggle input:checked + .slider {
  background: var(--accent);
}

.toggle input:checked + .slider::before {
  transform: translateX(20px);
}

.toggle .slider.locked {
  background: var(--warning, #f59e0b);
  cursor: not-allowed;
  opacity: 0.7;
}

.toggle input:disabled + .slider {
  cursor: not-allowed;
  opacity: 0.6;
}

/* Severity badges */
.severity {
  padding: 3px 10px;
  border-radius: 0;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.severity.info { background: rgba(59,130,246,0.15); color: var(--accent); }
.severity.warning { background: rgba(245,158,11,0.15); color: var(--warning); }
.severity.error, .severity.critical { background: rgba(239,68,68,0.15); color: var(--danger); }

/* Device status */
.device-status {
  display: flex;
  align-items: center;
  gap: 6px;
}

.device-status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.device-status .dot.online { background: var(--success); }
.device-status .dot.offline { background: var(--danger); }

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 24px;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
}

.modal h3 {
  margin-bottom: 16px;
}

/* Price bar chart */
.price-bar {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 120px;
  padding: 8px 0;
}

.price-bar .bar {
  flex: 1;
  min-width: 8px;
  background: var(--accent);
  border-radius: 0;
  transition: all 0.3s;
  position: relative;
}

.price-bar .bar.current {
  background: var(--warning);
}

.price-bar .bar.expensive {
  background: var(--danger);
}

.price-bar .bar.cheap {
  background: var(--success);
}

/* ========================= STAT BOXES ========================= */
.stat-box {
  padding: 8px 10px;
  background: var(--bg-primary);
  border-radius: 8px;
  border: 1px solid var(--border);
  text-align: center;
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 1px;
  white-space: nowrap;
}

/* ========================= DEVICE CARDS ========================= */
.device-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 14px;
  background: var(--bg-secondary);
  border-radius: 8px;
  transition: background 0.3s ease;
  border: 1px solid var(--border);
}

.device-card.device-locked {
  border-color: var(--warning, #f59e0b);
  opacity: 0.85;
}

.device-lock-badge {
  font-size: 0.9rem;
  line-height: 1;
}

.device-card.state-flash {
  animation: stateFlash 0.6s ease;
}

@keyframes stateFlash {
  0%   { background: rgba(59, 130, 246, 0.15); }
  100% { background: var(--bg-secondary); }
}

.device-toggle-left {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 52px;
}

.device-info { min-width: 0; }

.device-header {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.device-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.device-type-badge {
  font-size: 0.7rem;
  padding: 2px 6px;
  background: var(--bg-primary);
  border-radius: 6px;
  white-space: nowrap;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot.online { background: var(--success); }
.dot.offline { background: var(--danger); }

.device-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.device-state {
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.device-address {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

/* ========================= BOTTOM NAV (Mobile) ========================= */
.bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  z-index: 200;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.bottom-nav .bnav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 4px 6px;
  color: var(--text-muted);
  text-decoration: none;
  cursor: pointer;
  transition: color 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.bottom-nav .bnav-item.active {
  color: var(--accent);
}

.bottom-nav .bnav-icon {
  font-size: 1.3rem;
  line-height: 1;
  margin-bottom: 2px;
}

.bottom-nav .bnav-label {
  font-size: 0.65rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

/* ========================= RESPONSIVE ========================= */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main-content {
    margin-left: 0;
    padding: 12px;
    padding-top: 60px;
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0));
  }

  .bottom-nav {
    display: flex;
  }

  /* 2-sarake → yksisarake */
  .col-2 {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  /* Hintataulukot pinotaan mobiilissa */
  #price-tables-grid {
    grid-template-columns: 1fr;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .stat-card {
    padding: 12px;
  }

  .stat-card .value {
    font-size: 1.4rem;
  }

  .charts-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .chart-container {
    padding: 12px;
  }

  .chart-container canvas {
    max-height: 200px;
  }

  .card {
    padding: 12px;
  }

  .page-header {
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;
    align-items: flex-start;
  }

  .page-header h2 {
    font-size: 1.15rem;
  }

  .btn-group {
    flex-wrap: wrap;
    gap: 6px;
  }

  .mobile-toggle {
    display: flex !important;
  }

  /* Device cards */
  .device-card {
    grid-template-columns: auto 1fr auto;
    gap: 8px;
  }

  .device-toggle-left {
    min-width: 44px;
  }

  .device-actions {
    flex-direction: column;
    gap: 4px;
  }

  .device-address {
    display: none;
  }

  /* Taulukot scrollattavat */
  table {
    font-size: 0.8rem;
  }

  th, td {
    padding: 8px 10px;
  }

  /* Modaalit täysleveät mobiilissa */
  .modal-content {
    width: 95% !important;
    max-width: 95% !important;
    margin: 10px auto;
    max-height: 90vh;
    overflow-y: auto;
  }

  /* Toggle isompi touchille */
  .toggle {
    width: 52px;
    height: 28px;
  }

  .toggle .slider::before {
    width: 22px;
    height: 22px;
  }

  .toggle input:checked + .slider::before {
    transform: translateX(24px);
  }

  /* Napit: ei liian massiivisia page-headerissa */
  .btn {
    padding: 8px 14px;
    font-size: 0.88rem;
    min-height: 38px;
  }

  .btn-sm {
    padding: 6px 12px;
    font-size: 0.82rem;
    min-height: 32px;
  }

  /* Form-elementit kosketusystävälliset */
  .form-control {
    padding: 10px 12px;
    font-size: 1rem;
    min-height: 42px;
  }
}

/* Pienet puhelimet */
@media (max-width: 380px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .stat-card .value {
    font-size: 1.3rem;
  }

  .main-content {
    padding: 8px;
    padding-top: 58px;
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0));
  }
}


.mobile-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 201;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-height: 44px;
  min-width: 44px;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
}

/* Loading */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: var(--text-muted);
}

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

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

/* Empty state */
.empty-state {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

.empty-state .icon {
  font-size: 3rem;
  margin-bottom: 12px;
}
