/* ==================== CSS RESET ==================== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
}

body {
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
}

input, button, textarea, select {
  font: inherit;
}

p, h1, h2, h3, h4, h5, h6 {
  overflow-wrap: break-word;
}

/* ==================== CSS VARIABLES ==================== */
:root {
  /* Color Scheme */
  --color-primary: #2563eb;
  --color-primary-dark: #1e40af;
  --color-primary-light: #3b82f6;

  --color-secondary: #10b981;
  --color-secondary-dark: #059669;
  --color-secondary-light: #34d399;

  --color-accent: #8b5cf6;
  --color-warning: #f59e0b;
  --color-danger: #ef4444;
  --color-success: #10b981;

  /* Neutrals */
  --color-bg: #f8fafc;
  --color-bg-secondary: #f1f5f9;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-border-light: #f1f5f9;

  /* Text Colors */
  --color-text-primary: #1e293b;
  --color-text-secondary: #64748b;
  --color-text-tertiary: #94a3b8;

  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);

  /* Spacing */
  --space-xs: 0.5rem;
  --space-sm: 0.75rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;

  /* Border Radius */
  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;

  /* Typography */
  --font-primary: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
}

/* ==================== BASE STYLES ==================== */
body {
  font-family: var(--font-primary);
  background-color: var(--color-bg);
  color: var(--color-text-primary);
  font-size: 16px;
}

/* ==================== HEADER ==================== */
#header {
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  color: white;
  padding: var(--space-lg) var(--space-xl);
  box-shadow: var(--shadow-md);
  margin-bottom: var(--space-2xl);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
}

.header-title {
  font-size: 1.75rem;
  font-weight: 700;
  margin-bottom: var(--space-sm);
  letter-spacing: -0.02em;
}

#modelLoadStatus {
  font-size: 0.875rem;
  opacity: 0.9;
  margin-bottom: var(--space-xs);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

#modelLoadStatus::before {
  content: '';
  width: 8px;
  height: 8px;
  background: var(--color-success);
  border-radius: 50%;
  display: inline-block;
  animation: pulse 2s infinite;
}

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

#resultStats {
  font-size: 1rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--space-md);
  flex-wrap: wrap;
}

/* ==================== CONTAINERS ==================== */
#predictionContainer,
#resultsContainer {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 var(--space-xl);
  width: 100%;
}

#predictionContainer {
  margin-bottom: var(--space-2xl);
}

#predictionContainer h2,
#resultsContainer h2 {
  font-size: 1.875rem;
  font-weight: 700;
  color: var(--color-text-primary);
  margin-bottom: var(--space-xl);
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

#predictionContainer h2::before {
  content: '📊';
  font-size: 2rem;
}

#resultsContainer h2::before {
  content: '🎯';
  font-size: 2rem;
}

/* ==================== PREDICTION CANVAS ==================== */
.chart-wrapper {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  box-shadow: var(--shadow-lg);
  margin-bottom: var(--space-xl);
  border: 1px solid var(--color-border);
}

#predictionCanvas {
  width: 100% !important;
  height: auto !important;
}

/* ==================== RESULT BLOCKS ==================== */
.result-block {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  margin-bottom: var(--space-xl);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--color-border);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.result-block:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-xl);
}

.result-block-header {
  display: flex;
  flex-direction: row;
  gap: var(--space-md);
  margin-bottom: var(--space-xl);
  flex-wrap: wrap;
}

.result-block-header > div {
  flex: 1;
  min-width: 200px;
}

.result-block-header select {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  border: 2px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-text-primary);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.result-block-header select:hover {
  border-color: var(--color-primary);
}

.result-block-header select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

/* ==================== STATISTICS ==================== */
.stats-container {
  margin-bottom: var(--space-xl);
}

.model-strength-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-bottom: var(--space-xl);
  padding: var(--space-lg);
  background: linear-gradient(135deg, var(--color-secondary-light) 0%, var(--color-secondary) 100%);
  border-radius: var(--radius-lg);
  color: white;
}

.model-strength-field b {
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.9;
}

.model-strength-field > div:last-child {
  font-size: 2.5rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.model-strength-field > div:last-child::after {
  content: '%';
  font-size: 1.5rem;
  opacity: 0.8;
}

#outputContainer {
  margin-bottom: var(--space-lg);
}

#outputContainer > div:first-child {
  margin-bottom: var(--space-sm);
}

#toggleOutputBtn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-sm) var(--space-md);
  background: var(--color-primary);
  color: white;
  text-decoration: none;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

#toggleOutputBtn:hover {
  background: var(--color-primary-dark);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

#outputList {
  display: none;
  background: var(--color-bg-secondary);
  border-radius: var(--radius-md);
  padding: var(--space-md);
  margin-top: var(--space-sm);
  max-height: 300px;
  overflow-y: auto;
}

#outputList li {
  padding: var(--space-xs) 0;
  font-size: 0.875rem;
  color: var(--color-text-secondary);
  font-family: var(--font-mono);
}

.stats-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--color-surface);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.stats-table tr th {
  padding: var(--space-md);
  text-align: left;
  background: var(--color-bg-secondary);
  color: var(--color-text-secondary);
  font-weight: 600;
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 2px solid var(--color-border);
}

.stats-table tr td {
  padding: var(--space-md);
  color: var(--color-text-primary);
  font-size: 0.9375rem;
  font-weight: 500;
  font-family: var(--font-mono);
}

.stats-table tr:not(:last-child) td {
  border-bottom: 1px solid var(--color-border-light);
}

/* ==================== RESULTS TABLE ==================== */
#resultsTableContainer {
  max-width: 1400px;
  margin: var(--space-2xl) auto;
  padding: 0 var(--space-xl);
  margin-bottom: var(--space-2xl);
}

table.sortable {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
}

table.sortable thead {
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  color: white;
}

table.sortable th {
  padding: var(--space-md) var(--space-lg);
  text-align: left;
  font-weight: 600;
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
  user-select: none;
  border: none;
  transition: background 0.2s ease;
}

table.sortable th:hover {
  background: rgba(255, 255, 255, 0.1);
}

table.sortable td {
  padding: var(--space-md) var(--space-lg);
  border: none;
  border-bottom: 1px solid var(--color-border-light);
  font-size: 0.875rem;
  color: var(--color-text-primary);
}

table.sortable tbody tr {
  transition: background 0.2s ease;
}

table.sortable tbody tr:hover {
  background: var(--color-bg-secondary);
}

table.sortable tbody tr:last-child td {
  border-bottom: none;
}

/* ==================== CANVAS ELEMENTS ==================== */
canvas {
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

/* ==================== RESPONSIVE DESIGN ==================== */
@media (max-width: 768px) {
  #header {
    padding: var(--space-md);
  }

  .header-title {
    font-size: 1.25rem;
  }

  #header nav {
    margin-top: var(--space-xs) !important;
  }

  #header nav a {
    font-size: 0.8rem;
    padding: 6px 12px !important;
  }

  #predictionContainer,
  #resultsContainer,
  #resultsTableContainer {
    padding: 0 var(--space-sm);
  }

  .result-block {
    padding: var(--space-md);
  }

  .result-block-header {
    flex-direction: column;
  }

  .result-block-header > div {
    width: 100%;
    min-width: 0;
  }

  /* Wrap tables in scroll containers */
  .stats-table,
  table.sortable {
    font-size: 0.75rem;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .stats-table tr th,
  .stats-table tr td,
  table.sortable th,
  table.sortable td {
    padding: var(--space-xs) var(--space-sm);
    white-space: nowrap;
  }

  h2 {
    font-size: 1.25rem !important;
  }

  #predictionContainer h2,
  #resultsContainer h2 {
    font-size: 1.25rem;
    gap: var(--space-sm);
  }

  #predictionContainer h2::before,
  #resultsContainer h2::before {
    font-size: 1.5rem;
  }

  .model-strength-field > div:last-child {
    font-size: 1.75rem;
  }

  .chart-wrapper {
    padding: var(--space-md);
  }

  /* Chart.js canvases — force a usable height on mobile */
  .chart-wrapper canvas {
    max-height: 280px;
  }

  /* Accuracy panel: stack to single column */
  .accuracy-grid {
    grid-template-columns: 1fr !important;
  }

  /* Forecast info bar: stack vertically */
  .forecast-info-bar {
    flex-direction: column !important;
    gap: 0.5rem !important;
    align-items: flex-start !important;
  }

  /* Button selectors: smaller touch targets */
  .days-btn, .week-btn, .timeframe-btn {
    padding: 0.4rem 0.75rem !important;
    font-size: 0.8rem !important;
  }

  /* Result stats display */
  #resultStats {
    font-size: 0.85rem;
    gap: var(--space-sm);
  }

  #resultsTableContainer {
    margin: var(--space-lg) auto;
    padding: 0 var(--space-sm);
  }
}

/* ==================== UTILITIES ==================== */
.text-center {
  text-align: center;
}

.mt-xl {
  margin-top: var(--space-xl);
}

.mb-xl {
  margin-bottom: var(--space-xl);
}

/* ==================== LOADING STATES ==================== */
.loading {
  opacity: 0.6;
  pointer-events: none;
}

.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 4px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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