/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #273549;
  --border: #334155;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --accent: #38bdf8;
  --accent-hover: #7dd3fc;
  --good: #22c55e;
  --suspect: #f97316;
  --bad: #ef4444;
  --na: #94a3b8;
  --radius: 8px;
}

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* ── Layout ── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem 2rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent);
}

header p {
  font-size: 0.8rem;
  color: var(--text-muted);
}

main {
  max-width: 1600px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ── Control panel ── */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.panel h2 {
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  align-items: end;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}

select, input[type="date"] {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.55rem 0.75rem;
  font-size: 0.9rem;
  width: 100%;
  appearance: none;
  cursor: pointer;
  transition: border-color 0.15s;
}

select:focus, input[type="date"]:focus {
  outline: none;
  border-color: var(--accent);
}

select:disabled, input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Data-type checkboxes ── */
.data-type-group {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  padding-top: 0.2rem;
}

.check-row-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.check-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  line-height: 1.3;
}

.check-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  cursor: pointer;
  accent-color: var(--accent);
}

.check-label input[type="checkbox"]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ▶ About toggle button */
.about-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  padding: 0 0.15rem;
  position: relative;
  white-space: nowrap;
  transition: color 0.15s;
}

.about-toggle:hover { color: var(--accent); }

/* Tooltip on the About button */
.about-toggle::after {
  content: attr(data-tip);
  position: absolute;
  top: calc(100% + 7px);
  left: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.7rem;
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--text);
  white-space: normal;
  width: 260px;
  z-index: 200;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  line-height: 1.5;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  text-align: left;
}

.about-toggle:hover::after { opacity: 1; }

/* ── About modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  backdrop-filter: blur(3px);
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-width: 660px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
}

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

.modal-header h3 {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  line-height: 1;
  transition: color 0.15s, background 0.15s;
}
.modal-close:hover { color: var(--text); background: var(--surface2); }

.modal-body {
  padding: 1rem 1.25rem 1.25rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.65;
}

.about-source {
  font-size: 0.78rem;
  margin-bottom: 0.85rem;
  color: var(--text-muted);
}
.about-source strong { color: var(--text); }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: background 0.15s, opacity 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--accent);
  color: #0f172a;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-outline:hover { border-color: var(--accent); color: var(--accent); }

/* ── Status & alerts ── */
.alert {
  padding: 0.85rem 1.1rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
}
.alert-error { background: rgba(239,68,68,0.15); border: 1px solid rgba(239,68,68,0.4); color: #fca5a5; }
.alert-info  { background: rgba(56,189,248,0.1);  border: 1px solid rgba(56,189,248,0.3); color: var(--accent); }

.hidden { display: none !important; }

/* ── Loading spinner ── */
.spinner-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 3rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Summary cards ── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}

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

.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--accent);
}

.stat-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 0.2rem;
}

/* ── Quality badge strip ── */
.quality-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}

.q-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 500;
}
.q-badge .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ── Info icon with tooltip ── */
.info-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.6rem;
  font-weight: 700;
  font-style: normal;
  cursor: help;
  position: relative;
  vertical-align: middle;
  margin-left: 0.2rem;
  flex-shrink: 0;
  line-height: 1;
  transition: border-color 0.15s, color 0.15s;
}

.info-icon:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Tooltip bubble — appears above by default */
.info-icon::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 7px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.75rem;
  font-size: 0.775rem;
  font-weight: 400;
  color: var(--text);
  white-space: normal;
  width: 270px;
  z-index: 200;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  line-height: 1.55;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  text-align: left;
}

.info-icon:hover::after { opacity: 1; }

/* Variant: tooltip appears below (use for icons near the top of the page) */
.info-icon.tip-below::after {
  bottom: auto;
  top: calc(100% + 7px);
}

/* ── Collapsible dataset info (Option 2) ── */
.dataset-info {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.02);
  margin-bottom: 1.25rem;
}

.dataset-info > summary {
  padding: 0.6rem 0.85rem;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  user-select: none;
  transition: color 0.15s;
}

/* Hide default browser disclosure triangle */
.dataset-info > summary::-webkit-details-marker { display: none; }
.dataset-info > summary::marker               { display: none; }

.dataset-info > summary::before {
  content: '▶';
  font-size: 0.6rem;
  flex-shrink: 0;
  transition: transform 0.2s;
}

.dataset-info[open] > summary::before { transform: rotate(90deg); }
.dataset-info[open] > summary         { color: var(--text); border-bottom: 1px solid var(--border); }
.dataset-info > summary:hover         { color: var(--accent); }

.dataset-info-body {
  padding: 0.85rem 1rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.65;
}

.dataset-info-body > p { margin-bottom: 0.85rem; }

/* Column definition list inside the collapsible */
.col-defs {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.col-def {
  display: grid;
  grid-template-columns: 14rem 1fr;
  gap: 0.4rem 0.75rem;
  align-items: baseline;
}

.col-name {
  font-family: ui-monospace, 'Cascadia Code', 'Source Code Pro', monospace;
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
}

.col-desc { color: var(--text-muted); }

@media (max-width: 700px) {
  .col-def { grid-template-columns: 1fr; gap: 0.15rem; }
}

/* ── Results layout ── */

/* Space between the SCADA section and the Energy section.
   Because .hidden children are display:none they don't generate gap,
   so there is no unwanted whitespace when only one section is visible. */
#results-box {
  display: flex;
  flex-direction: column;
  gap: 3rem;
}

/* Panels within each section are stacked flush — no gap */
#scada-results,
#energy-results {
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* Card-group: square off inner edges, collapse duplicate borders */
#scada-results .panel,
#energy-results .panel {
  border-radius: 0;
}
#scada-results .panel:first-child,
#energy-results .panel:first-child {
  border-top-left-radius: var(--radius);
  border-top-right-radius: var(--radius);
}
#scada-results .panel:last-child,
#energy-results .panel:last-child {
  border-bottom-left-radius: var(--radius);
  border-bottom-right-radius: var(--radius);
}
#scada-results .panel + .panel,
#energy-results .panel + .panel {
  border-top: none;
}

/* ── Charts ── */
#scada-chart-container,
#energy-chart-container {
  width: 100%;
  height: clamp(400px, 50vh, 650px);
}

/* ── Download bar ── */
.download-bar {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

.download-bar span {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ── Footer ── */
footer {
  text-align: center;
  padding: 2rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

footer a,
.panel a { color: var(--accent); text-decoration: none; }
footer a:hover,
.panel a:hover { text-decoration: underline; }

@media (max-width: 600px) {
  header { flex-direction: column; align-items: flex-start; }
  .controls { grid-template-columns: 1fr; }
}
