/* ================================
   CSS VARIABLES FOR THEMING
================================ */
:root {
  --bg-primary: #f5f5f5;
  --bg-secondary: #fff;
  --bg-tertiary: #fafafa;
  --text-primary: #333;
  --text-secondary: #666;
  --text-heading: #222;
  --border-color: #ddd;
  --border-light: #eee;
  --shadow: rgba(0,0,0,0.1);
  --input-bg: #fff;
  --input-disabled: #f5f5f5;
  --input-disabled-text: #999;
}

body.dark-mode {
  --bg-primary: #1a1a1a;
  --bg-secondary: #2d2d2d;
  --bg-tertiary: #252525;
  --text-primary: #e0e0e0;
  --text-secondary: #b0b0b0;
  --text-heading: #f0f0f0;
  --border-color: #404040;
  --border-light: #353535;
  --shadow: rgba(0,0,0,0.3);
  --input-bg: #333;
  --input-disabled: #2a2a2a;
  --input-disabled-text: #666;
}

/* ================================
   RESET & BASE STYLES
================================ */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica', 'Arial', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.6;
  transition: background 0.3s, color 0.3s;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* ================================
   DARK MODE TOGGLE
================================ */
.dark-mode-toggle {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 6px;
  transition: opacity 0.3s, transform 0.2s;
  z-index: 10;
  opacity: 0.5;
  line-height: 1;
  color: #666;
}

body.dark-mode .dark-mode-toggle {
  color: #d0d0d0;
}

.dark-mode-toggle:hover {
  opacity: 1;
  transform: scale(1.1);
}

/* Hide dark mode toggle in print */
@media print {
  .dark-mode-toggle {
    display: none !important;
  }

  body.dark-mode {
    --bg-primary: #fff;
    --bg-secondary: #fff;
    --bg-tertiary: #f5f5f5;
    --text-primary: #333;
    --text-secondary: #666;
    --text-heading: #222;
    --border-color: #ddd;
    --border-light: #eee;
    --shadow: rgba(0,0,0,0.1);
    --input-bg: #fff;
    --input-disabled: #f5f5f5;
    --input-disabled-text: #999;
  }
}

/* ================================
   FILTER PAGE
================================ */
header {
  position: relative;
}

header h1 {
  font-size: 28px;
  margin-bottom: 8px;
  color: var(--text-heading);
}

header p {
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.latest-sample-info {
  color: var(--text-secondary);
  font-size: 13px;
  margin-bottom: 30px;
  opacity: 0.7;
}

.filter-form {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 24px;
  max-width: 500px;
  box-shadow: 0 1px 3px var(--shadow);
  transition: background 0.3s, border-color 0.3s;
}

.form-group {
  margin-bottom: 20px;
}

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

.required {
  color: #f85149;
}

.form-group select {
  width: 100%;
  padding: 8px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 14px;
  transition: background 0.3s, border-color 0.3s, color 0.3s;
}

.helper-text {
  display: block;
  margin-top: 4px;
  color: var(--text-secondary);
  font-size: 12px;
}

.field-error {
  display: block;
  margin-top: 6px;
  color: #d73a49;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 12px;
  background: #fff5f5;
  border: 1px solid #ffc0c0;
  border-radius: 4px;
  animation: slideIn 0.3s ease-out;
}

body.dark-mode .field-error {
  background: #3a1a1a;
  border-color: #6a3a3a;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.checkbox-group {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 10px;
  background: var(--bg-tertiary);
  transition: background 0.3s, border-color 0.3s;
}

/* Vineyard group expands to show all items without scrolling */
#vineyardCheckboxes {
  max-height: none;
  overflow-y: visible;
}

.checkbox-item {
  display: flex;
  align-items: center;
  padding: 6px 0;
}

.checkbox-item input[type="checkbox"] {
  margin: 0;
  margin-right: 8px;
  cursor: pointer;
}

.checkbox-item label {
  margin: 0;
  cursor: pointer;
  font-weight: normal;
  color: var(--text-primary);
  user-select: none;
}

.checkbox-item:hover {
  background: var(--border-light);
  margin: 0 -10px;
  padding: 6px 10px;
  border-radius: 4px;
}

.checkbox-item input[type="checkbox"]:disabled {
  cursor: not-allowed;
}

.checkbox-item input[type="checkbox"]:disabled + label {
  cursor: not-allowed;
  color: var(--input-disabled-text);
}

.block-select-wrapper {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.block-select-wrapper select {
  flex: 1;
}

.btn-select-all {
  background: #28a745;
  color: #fff;
  border: none;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s;
  height: 38px;
}

.btn-select-all:hover {
  background: #218838;
}

.btn-select-all:active {
  background: #1e7e34;
}

.form-actions {
  margin-top: 24px;
  display: flex;
  gap: 12px;
}

.btn-primary:disabled {
  background: #ccc;
  cursor: not-allowed;
  opacity: 0.6;
}

select:disabled {
  background: var(--input-disabled);
  color: var(--input-disabled-text);
  cursor: not-allowed;
}

.btn-primary {
  background: #007bff;
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: #0056b3;
}

.btn-primary:active {
  background: #004494;
}

.btn-secondary {
  background: #6c757d;
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
  transition: background 0.2s;
}

.btn-secondary:hover {
  background: #5a6268;
}

.btn-secondary:active {
  background: #545b62;
}

/* ================================
   RESULTS PAGE
================================ */
.results-page {
  max-width: 100%;
  padding: 20px 40px;
}

.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border-color);
}

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

.header-left h1 {
  font-size: 24px;
  color: var(--text-heading);
}

.btn-back {
  color: #007bff;
  text-decoration: none;
  font-size: 14px;
  padding: 6px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  transition: background 0.2s, border-color 0.3s;
}

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

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

.year-selector {
  display: flex;
  align-items: center;
  gap: 8px;
}

.year-selector label {
  font-size: 14px;
  color: var(--text-primary);
  font-weight: 500;
}

.year-dropdown {
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.3s, color 0.3s;
}

.year-dropdown:hover {
  background: var(--bg-tertiary);
}

.year-dropdown:focus {
  outline: none;
  border-color: #4A90E2;
}

.results-page .btn-secondary,
.yearly-comparison-page .btn-secondary {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.3s, color 0.3s;
}

.results-page .btn-secondary:hover,
.yearly-comparison-page .btn-secondary:hover {
  background: var(--bg-tertiary);
}

/* ================================
   BLOCK PANELS
================================ */
.block-panel {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 20px;
  margin-bottom: 30px;
  box-shadow: 0 1px 3px var(--shadow);
  transition: background 0.3s, border-color 0.3s;
}

.block-header {
  border-left: 4px solid #00BCD4;
  padding-left: 16px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.block-header h2 {
  font-size: 18px;
  color: var(--text-heading);
  font-weight: 600;
}

.block-header h3 {
  font-size: 16px;
  color: var(--text-heading);
  font-weight: 600;
  margin: 0;
}

.block-header-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.btn-yearly-comparison {
  color: #007bff;
  text-decoration: none;
  font-size: 13px;
  padding: 4px 10px;
  border: 1px solid #007bff;
  border-radius: 4px;
  transition: all 0.2s;
  white-space: nowrap;
}

.btn-yearly-comparison:hover {
  background: #007bff;
  color: #fff;
}

.sample-count {
  color: var(--text-secondary);
  font-size: 13px;
}

/* ================================
   CHARTS
================================ */
.combined-charts-section {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 40px;
}

.chart-section {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 12px;
  min-width: 0; /* Allow flex/grid items to shrink below content size */
  position: relative;
  transition: background 0.3s, border-color 0.3s;
}

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

.chart-section canvas {
  height: 250px !important;
  width: 100% !important;
  display: block;
}

.tables-section {
  margin-top: 40px;
}

.block-table-panel {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 1px 3px var(--shadow);
  transition: background 0.3s, border-color 0.3s;
}

.block-table-panel h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

/* ================================
   TABLES
================================ */
.table-container {
  overflow-x: auto;
  margin-top: 20px;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  overflow: hidden;
  transition: background 0.3s, border-color 0.3s;
}

thead {
  background: var(--bg-tertiary);
  transition: background 0.3s;
}

th {
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border-color);
  font-size: 13px;
}

td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-light);
  color: var(--text-primary);
  font-size: 13px;
}

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

tbody tr:hover {
  background: var(--bg-tertiary);
}

/* ================================
   LOADING & ERROR STATES
================================ */
.loading,
.error {
  text-align: center;
  padding: 40px;
  font-size: 16px;
  color: #666;
}

.error {
  color: #d73a49;
}

/* ================================
   MOBILE RESPONSIVE
================================ */
@media (max-width: 1024px) {
  .combined-charts-section {
    grid-template-columns: 1fr;
  }

  .results-page,
  .container {
    padding: 15px;
  }

  .chart-section canvas {
    height: 200px !important;
  }
}

@media (max-width: 768px) {
  .filter-form {
    max-width: 100%;
  }

  .checkbox-group {
    max-height: 150px;
  }

  .block-select-wrapper {
    flex-direction: column;
    gap: 8px;
  }

  .btn-select-all {
    width: 100%;
  }

  .form-actions {
    flex-direction: column;
  }

  .results-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .header-actions {
    width: 100%;
    flex-direction: column;
  }

  .header-actions button {
    width: 100%;
  }

  .block-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  table {
    font-size: 12px;
  }

  table th,
  table td {
    padding: 6px;
  }
}

/* ================================
   PRINT STYLES
================================ */
@media print {
  body {
    background: #fff;
    color: #000;
  }

  .results-header .header-actions,
  .btn-back,
  button,
  .btn-yearly-comparison,
  #yearSelectionPanel {
    display: none !important;
  }

  .combined-charts-section {
    display: none !important;
  }

  .block-panel {
    page-break-inside: avoid;
    border: 1px solid #ccc;
    background: #fff;
  }

  .block-header {
    border-left-color: #333;
    color: #000;
  }

  .block-table-panel {
    page-break-inside: avoid;
  }

  table {
    background: #fff;
    color: #000;
    border: 1px solid #ccc;
  }

  th {
    background: #f0f0f0;
    color: #000;
  }

  td {
    color: #000;
  }
}

/* ================================
   SELECT2 OVERRIDES
================================ */
/* Light mode colors */
.select2-container--default .select2-selection--single,
.select2-container--default .select2-selection--multiple {
  background: #fff;
  border: 1px solid #ccc;
  color: #333;
}

.select2-container--default .select2-selection--single .select2-selection__rendered {
  color: #333;
}

.select2-dropdown {
  background: #fff;
  border: 1px solid #ccc;
}

.select2-container--default .select2-results__option {
  color: #333;
}

.select2-container--default .select2-results__option--highlighted[aria-selected] {
  background: #007bff;
  color: #fff;
}

.select2-container--default .select2-search--dropdown .select2-search__field {
  background: #fff;
  border: 1px solid #ccc;
  color: #333;
}

/* Selected block styling */
.select2-container--default .select2-selection--multiple .select2-selection__choice {
  background: #2c5282;
  border: 1px solid #1a365d;
  color: #fff;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
  color: #fff;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove:hover {
  color: #fff;
}
