/* ============================================================
   Solar Monitor - Estilos
   Tema claro/escuro com CSS custom properties
   ============================================================ */

/* ---------- Variaveis de tema ---------- */
:root {
  --primary: #E8670A;
  --primary-light: #FF8C3A;
  --primary-dark: #C45500;
  --bg: #f0f2f5;
  --bg-card: #ffffff;
  --bg-header: #ffffff;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --text-light: #9ca3af;
  --border: #e5e7eb;
  --success: #22c55e;
  --success-bg: #f0fdf4;
  --warning: #f59e0b;
  --warning-bg: #fffbeb;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --info: #3b82f6;
  --info-bg: #eff6ff;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

[data-theme="dark"] {
  --bg: #0f1117;
  --bg-card: #1a1d2e;
  --bg-header: #141625;
  --text: #e5e7eb;
  --text-muted: #9ca3af;
  --text-light: #6b7280;
  --border: #2d3148;
  --success-bg: #052e16;
  --warning-bg: #451a03;
  --danger-bg: #450a0a;
  --info-bg: #172554;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
}

/* ---------- Reset + Base ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  transition: background 0.3s, color 0.3s;
}

/* ---------- Header ---------- */
.header {
  background: var(--bg-header);
  border-bottom: 3px solid var(--primary);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.header-subtitle {
  font-size: 14px;
  color: var(--primary);
  font-weight: 600;
  background: rgba(232, 103, 10, 0.1);
  padding: 2px 10px;
  border-radius: 20px;
}

.header-center {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.last-update-label { color: var(--text-muted); }
.last-update-value { font-weight: 600; color: var(--text); }

.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  display: inline-block;
}
.status-dot--ok { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot--warning { background: var(--warning); box-shadow: 0 0 6px var(--warning); }
.status-dot--danger { background: var(--danger); box-shadow: 0 0 6px var(--danger); animation: pulse 1.5s infinite; }

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-actions {
  display: flex;
  gap: 4px;
}

/* ---------- Botoes ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-collect {
  background: var(--primary);
  color: white;
}
.btn-collect:hover { background: var(--primary-dark); transform: translateY(-1px); }

.btn-icon {
  width: 36px; height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s;
}
.btn-icon:hover { background: var(--bg); color: var(--text); }

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

/* ---------- Summary Cards ---------- */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 16px;
  padding: 20px 24px;
}

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  transition: all 0.3s;
  position: relative;
  overflow: hidden;
}
.card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }

.card-icon {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(232, 103, 10, 0.1);
  color: var(--primary);
  margin-bottom: 12px;
}
.card-icon--green { background: var(--success-bg); color: var(--success); }
.card-icon--red { background: var(--danger-bg); color: var(--danger); }
.card-icon--orange { background: var(--warning-bg); color: var(--warning); }
.card-icon--yellow { background: var(--warning-bg); color: var(--warning); }

.card-value {
  font-size: 28px;
  font-weight: 700;
  line-height: 1.2;
  margin-bottom: 4px;
}

.card-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.card-pct {
  color: var(--success);
  font-weight: 600;
}

.card--danger .card-value { color: var(--danger); }
.card--warning .card-value { color: var(--warning); }

/* ---------- Economia Solar ---------- */
.economia-section {
  padding: 0 24px 24px;
}
.economia-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.economia-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  text-align: center;
}
.economia-icon {
  width: 48px; height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--success-bg);
  color: var(--success);
  margin: 0 auto 12px;
}
.economia-icon--blue { background: rgba(59,130,246,0.1); color: #3b82f6; }
.economia-icon--orange { background: var(--warning-bg); color: var(--warning); }
.economia-valor {
  font-size: 28px;
  font-weight: 700;
  color: var(--success);
  line-height: 1.2;
  margin-bottom: 4px;
}
.economia-kwh {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}
.economia-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
  font-weight: 500;
}
.economia-table {
  width: 100%;
  font-size: 13px;
}
.economia-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.economia-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.economia-table td:last-child,
.economia-table th:last-child {
  text-align: right;
}

/* ---------- Seletor de Periodo ---------- */
.period-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 24px 16px;
}

.period-label {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}

.btn-period {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: all 0.2s;
}
.btn-period:hover { border-color: var(--primary); color: var(--primary); }
.btn-period.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.period-right {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

/* ---------- Tabela de Usinas ---------- */
.plants-section {
  padding: 0 24px 24px;
}

.section-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.table-wrap {
  overflow-x: auto;
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.plants-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.plants-table thead th {
  background: var(--bg-card);
  border-bottom: 2px solid var(--border);
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
  position: sticky;
  top: 0;
}

.plants-table tbody tr {
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}
.plants-table tbody tr:last-child { border-bottom: none; }
.plants-table tbody tr:hover { background: rgba(232, 103, 10, 0.04); }

.plants-table td {
  padding: 12px 16px;
  white-space: nowrap;
}

.plant-name {
  font-weight: 600;
  color: var(--text);
}

.plant-location {
  font-size: 11px;
  color: var(--text-muted);
}

/* Status badges na tabela */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}
.status-badge--ok { background: var(--success-bg); color: var(--success); }
.status-badge--warning { background: var(--warning-bg); color: var(--warning); }
.status-badge--danger { background: var(--danger-bg); color: var(--danger); }

/* Health bar */
.health-bar {
  width: 80px;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  display: inline-block;
  vertical-align: middle;
  margin-left: 6px;
}
.health-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.5s;
}

/* Score badge */
.score-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 28px;
  border-radius: 6px;
  font-weight: 700;
  font-size: 13px;
  color: white;
}
.score-badge--green { background: var(--success); }
.score-badge--yellow { background: var(--warning); }
.score-badge--orange { background: #f97316; }
.score-badge--red { background: var(--danger); }

/* ---------- Charts ---------- */
.charts-section {
  padding: 0 24px 24px;
}

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.chart-card--full {
  grid-column: 1 / -1;
}

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

.chart-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text);
}

.chart-card canvas {
  max-height: 280px;
}

/* ---------- Alertas ---------- */
.alerts-section {
  padding: 0 24px 40px;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  font-size: 12px;
  font-weight: 700;
}
.badge--danger { background: var(--danger); color: white; }
.badge--warning { background: var(--warning); color: white; }

.alerts-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 400px;
  overflow-y: auto;
}

.alert-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  transition: all 0.2s;
}
.alert-item--critico { border-left-color: var(--danger); background: var(--danger-bg); }
.alert-item--alerta { border-left-color: var(--warning); background: var(--warning-bg); }
.alert-item--info { border-left-color: var(--info); background: var(--info-bg); }

.alert-icon {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}
.alert-icon--critico { background: var(--danger); color: white; }
.alert-icon--alerta { background: var(--warning); color: white; }
.alert-icon--info { background: var(--info); color: white; }

.alert-content { flex: 1; min-width: 0; }
.alert-usina { font-weight: 600; font-size: 13px; }
.alert-msg { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.alert-time { font-size: 11px; color: var(--text-light); white-space: nowrap; }

.alert-resolve {
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  white-space: nowrap;
}
.alert-resolve:hover { color: var(--success); border-color: var(--success); }

.alerts-history {
  margin-top: 16px;
}
.alerts-history summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  padding: 8px 0;
}

.alerts-empty {
  text-align: center;
  padding: 32px;
  color: var(--text-muted);
  font-size: 14px;
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 0.2s;
}

.modal {
  background: var(--bg-card);
  border-radius: var(--radius);
  width: 100%;
  max-width: 900px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
}

.modal-close {
  font-size: 24px;
  width: 36px;
  height: 36px;
  border: none;
}

.modal-body {
  padding: 24px;
}

/* Modal - Info grid */
.modal-info-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 24px;
}

.modal-info-item {
  padding: 12px;
  background: var(--bg);
  border-radius: var(--radius-sm);
}

.modal-info-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.modal-info-value {
  font-size: 15px;
  font-weight: 600;
}

/* Modal - ECU table */
.modal-ecu-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin-bottom: 24px;
}
.modal-ecu-table th {
  background: var(--bg);
  padding: 8px 12px;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-muted);
}
.modal-ecu-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

/* Modal - Grid de paineis */
.panel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(14px, 1fr));
  gap: 2px;
  margin-bottom: 24px;
}

.panel-cell {
  aspect-ratio: 1;
  border-radius: 2px;
  min-width: 10px;
  min-height: 10px;
}
.panel-cell--ok { background: var(--success); }
.panel-cell--baixo { background: var(--warning); }
.panel-cell--nc { background: var(--danger); }
.panel-cell--zero { background: #6b7280; }

.panel-legend {
  display: flex;
  gap: 16px;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 16px;
}
.panel-legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}
.panel-legend-dot {
  width: 10px; height: 10px;
  border-radius: 2px;
}

/* Modal - Popover historico de painel */
.panel-history-popover {
  width: 340px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  overflow: hidden;
}
.panel-history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}
.panel-cell[data-pos]:hover {
  outline: 2px solid var(--text);
  outline-offset: -1px;
  z-index: 1;
}

/* Modal - Problemas recorrentes */
.recurring-list {
  list-style: none;
}
.recurring-item {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  display: flex;
  justify-content: space-between;
}
.recurring-item:last-child { border-bottom: none; }

/* ---------- Toast ---------- */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  color: white;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease-out;
  max-width: 360px;
}
.toast--success { background: var(--success); }
.toast--warning { background: var(--warning); }
.toast--danger { background: var(--danger); }
.toast--info { background: var(--info); }

/* ---------- Animacoes ---------- */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideIn {
  from { opacity: 0; transform: translateX(100px); }
  to { opacity: 1; transform: translateX(0); }
}

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

/* ---------- Responsivo ---------- */
@media (max-width: 1200px) {
  .summary-cards { grid-template-columns: repeat(3, 1fr); }
  .charts-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 10px;
    padding: 12px 16px;
  }
  .header-center { order: 3; }

  .summary-cards {
    grid-template-columns: repeat(2, 1fr);
    padding: 12px 16px;
    gap: 10px;
  }

  .card { padding: 14px; }
  .card-value { font-size: 22px; }

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

  .economia-cards { grid-template-columns: 1fr; }

  .plants-section, .alerts-section, .charts-section, .period-selector, .economia-section {
    padding-left: 16px;
    padding-right: 16px;
  }

  .modal {
    max-width: 100%;
    max-height: 95vh;
    border-radius: var(--radius) var(--radius) 0 0;
  }

  .modal-info-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 480px) {
  .summary-cards { grid-template-columns: 1fr 1fr; }
  .modal-info-grid { grid-template-columns: 1fr; }
  .period-right { display: none; }
}

/* ---------- Print ---------- */
@media print {
  .header, .period-selector, .btn-collect, .btn-icon, .alerts-section { display: none; }
  body { background: white; }
  .card, .chart-card, .table-wrap { box-shadow: none; border: 1px solid #ddd; }
}

/* ---------- Scrollbar customizada ---------- */
::-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); }

/* ---------- Tooltip nos cabecalhos ---------- */
.th-tooltip {
  position: relative;
  cursor: help;
  text-decoration: underline dotted var(--text-muted);
  text-underline-offset: 3px;
}

.th-tooltip-text {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: var(--text);
  color: var(--bg-card);
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  white-space: pre-line;
  line-height: 1.6;
  width: 280px;
  box-shadow: var(--shadow-lg);
  z-index: 150;
  pointer-events: none;
}

.th-tooltip-text::after {
  content: '';
  position: absolute;
  bottom: 100%;
  right: 20px;
  border: 6px solid transparent;
  border-bottom-color: var(--text);
}

.th-tooltip:hover .th-tooltip-text {
  display: block;
}

/* ---------- Sound toggle ---------- */
.btn-icon.sound-off { opacity: 0.4; }
