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

:root {
  --card-radius: 8px;
  --bg-primary: #f7f8fb;
  --bg-card: #fff;
  --border-color: #e0e0e0;
  --border-light: #f0f0f0;
  --text-primary: #263238;
  --text-secondary: #607d8b;
  --text-muted: #90a4ae;
  --bg-hover: #eceff1;
  --bg-alt: #fafafa;
  --input-bg: #fff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-primary: #1a1a1a;
    --bg-card: #2a2a2a;
    --border-color: #404040;
    --border-light: #353535;
    --text-primary: #e0e0e0;
    --text-secondary: #b0b0b0;
    --text-muted: #808080;
    --bg-hover: #353535;
    --bg-alt: #252525;
    --input-bg: #1f1f1f;
  }
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* Form elements reset */
button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  margin: 0;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
}

input[type="date"]::-webkit-calendar-picker-indicator {
  cursor: pointer;
  filter: var(--date-picker-filter, none);
}

@media (prefers-color-scheme: dark) {
  input[type="date"]::-webkit-calendar-picker-indicator {
    filter: invert(1);
  }
}

main.container-fluid {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0;
  min-height: 0;
}

#config-bar {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  padding: 0.3rem 0.6rem;
  box-shadow: 0 2px 4px rgba(0,0,0,0.04);
  margin: 0;
}

#workspace {
  flex: 1;
  display: flex;
  flex-direction: row;
  gap: 0.75rem;
  padding: 0.6rem;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

#charts {
  flex: 1.618;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 0;
  margin: 0;
}


.chart-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border-radius: var(--card-radius);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  min-height: 0;
}

.chart-card:first-child {
  flex: 1.618;
}

.chart-card:last-child {
  flex: 1;
}

.chart-card header {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.4rem 0.7rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border-light);
}

.chart-card .chart {
  flex: 1;
  min-height: 0;
}

#panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-width: 320px;
}

#stats {
  background: var(--bg-card);
  border-radius: var(--card-radius);
  padding: 0.6rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  gap: 0;
  font-size: 0.8rem;
  overflow: hidden;
}

#workspace.no-data::before {
  content: 'NO DATA';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--text-muted);
  font-size: 1.5rem;
  font-weight: 500;
  letter-spacing: 0.2em;
  z-index: 10;
}

#workspace.no-data #charts,
#workspace.no-data #panel {
  visibility: hidden;
}

#stats .stats-title {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-primary);
  padding: 0.5rem 0.6rem;
  text-align: center;
  border-bottom: 2px solid var(--border-color);
  background: var(--bg-alt);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

#stats .stats-city {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
}

#stats .stats-period {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
}

#stats .stats-header {
  border-bottom: 2px solid var(--border-color);
}

#stats .stats-header-row {
  display: grid;
  gap: 0.5rem;
  padding: 0.35rem 0.4rem;
  border-bottom: 1px solid var(--border-light);
}

#stats .stats-header-row:last-child {
  border-bottom: none;
}

#stats .stats-header-row:first-child {
  background: var(--bg-alt);
}

#stats .stats-header-row:first-child .stats-value {
  font-weight: 600;
  font-size: 0.85rem;
}

#stats .stats-header-row:last-child .stats-value {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

#stats .stats-table {
  display: flex;
  flex-direction: column;
}

#stats .stats-row {
  display: grid;
  gap: 0.5rem;
  padding: 0.35rem 0.4rem;
  align-items: center;
  border-bottom: 1px solid var(--border-light);
}

#stats .stats-row:last-child {
  border-bottom: none;
}

#stats .stats-row:nth-child(odd) {
  background: var(--bg-alt);
}

#stats .stats-label {
  font-weight: 500;
  color: var(--text-secondary);
  font-size: 0.8rem;
  text-align: left;
  padding-right: 0.5rem;
  cursor: help;
}

#stats .stats-value {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 0.8rem;
  text-align: right;
}

#stats .color-blue {
  color: #1E88E5;
}

#stats .color-red {
  color: #E53935;
}

#stats .color-green {
  color: #43A047;
}

.smoothing-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 0.5rem 0.6rem;
  text-align: center;
  font-style: italic;
  border-top: 1px solid var(--border-light);
  background: var(--bg-alt);
}

#controls {
  display: flex;
  flex-direction: row;
}

.controls-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.35rem;
  flex: 1;
  flex-wrap: nowrap;
}

#controls .spacer {
  flex: 1 1 auto;
  min-width: 0.5rem;
}

/* City Tags Container */
.city-tags-container {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex: 0 0 auto;
  min-width: 0;
}

.city-tags {
  display: flex;
  gap: 0.3rem;
  align-items: center;
  flex-wrap: nowrap;
}

.city-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
  color: #fff;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.city-tag.tag-blue {
  background: #1E88E5;
}

.city-tag.tag-red {
  background: #E53935;
}

.city-tag.tag-green {
  background: #43A047;
}

.city-tag:hover {
  filter: brightness(1.1);
}

.city-tag-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  cursor: pointer;
  font-size: 0.7rem;
  transition: background 0.15s ease;
}

.city-tag-remove:hover {
  background: rgba(255, 255, 255, 0.5);
}

.city-input-wrapper {
  position: relative;
  flex: 0 0 auto;
  display: flex;
  gap: 2px;
  transition: all 0.2s ease;
}

.city-input-wrapper.hidden {
  display: none;
}

.city-search-input {
  width: 90px;
  padding: 0.25rem 0.4rem;
  font-size: 0.8rem;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--input-bg);
  color: var(--text-primary);
  height: 28px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.auto-location-btn {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: var(--input-bg);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auto-location-btn img {
  transition: opacity 0.15s ease;
}

.auto-location-btn:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: #1e88e5;
  transform: scale(1.05);
}

.auto-location-btn:active:not(:disabled) {
  transform: scale(0.95);
}

.auto-location-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

@media (prefers-color-scheme: dark) {
  .auto-location-btn img {
    filter: invert(1) hue-rotate(180deg);
  }
}

.city-search-input:hover {
  border-color: #1e88e5;
}

.city-search-input:focus {
  outline: none;
  border-color: #1e88e5;
  box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
}

.city-search-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  border-color: var(--border-color);
}

.city-search-input:disabled:hover {
  border-color: var(--border-color);
}

#controls select {
  padding: 0.25rem 0.4rem;
  font-size: 0.8rem;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--input-bg);
  color: var(--text-primary);
  height: 28px;
  flex: 0 0 auto;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  cursor: pointer;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url('data:image/svg+xml;utf8,<svg fill="%23607d8b" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/></svg>');
  background-repeat: no-repeat;
  background-position: right 0.25rem center;
  background-size: 1rem;
  padding-right: 1.5rem;
}

#controls select:hover {
  border-color: #1e88e5;
}

#controls select:focus {
  outline: none;
  border-color: #1e88e5;
  box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
}

@media (prefers-color-scheme: dark) {
  #controls select {
    background-image: url('data:image/svg+xml;utf8,<svg fill="%23b0b0b0" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/></svg>');
  }
}

#controls input {
  padding: 0.25rem 0.4rem;
  font-size: 0.8rem;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--input-bg);
  color: var(--text-primary);
  width: 75px;
  flex: 0 0 auto;
  height: 28px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#controls input:hover {
  border-color: #1e88e5;
}

#controls input:focus {
  outline: none;
  border-color: #1e88e5;
  box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
}

input[type="date"]::-webkit-datetime-edit-fields-wrapper {
  padding: 0;
}

input[type="date"]::-webkit-datetime-edit-text {
  padding: 0 0.2em;
}

.city-input {
  width: 100%;
}

#start, #end {
  width: 132px !important;
  flex: 0 0 auto;
}

/* Mode controls */
.mode-controls {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex: 1 1 auto;
  min-width: 0;
  flex-wrap: nowrap;
}

.mode-controls.hidden {
  display: none;
}

/* Year tags */
.year-tags-container {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex: 0 0 auto;
  min-width: 0;
}

.year-tags {
  display: flex;
  gap: 0.3rem;
  align-items: center;
  flex-wrap: nowrap;
}

.year-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
  color: #fff;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.year-tag.tag-blue {
  background: #1E88E5;
}

.year-tag.tag-red {
  background: #E53935;
}

.year-tag.tag-green {
  background: #43A047;
}

.year-tag:hover {
  filter: brightness(1.1);
}

.year-tag-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  cursor: pointer;
  font-size: 0.7rem;
  transition: background 0.15s ease;
}

.year-tag-remove:hover {
  background: rgba(255, 255, 255, 0.5);
}

.year-input {
  width: 95px !important;
  flex: 0 0 auto;
}

#controls-periodic input[type="text"] {
  width: 70px !important;
  flex: 0 0 auto;
}

/* Progression period config container */
#period-config-container {
  display: contents;
}

#period-type {
  width: auto;
  flex: 0 0 auto;
}

#season-select,
#month-select,
#day-select {
  width: 85px !important;
  flex: 0 0 auto !important;
}

#year-from,
#year-to {
  width: 65px !important;
  flex: 0 0 auto !important;
}

.city-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 0.25rem;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  max-height: 280px;
  overflow-y: auto;
  z-index: 1000;
}

.city-dropdown.visible {
  display: block;
}

.city-option {
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border-light);
  transition: background-color 0.1s ease;
}

.city-option:last-child {
  border-bottom: none;
}

.city-option:hover,
.city-option.selected {
  background: var(--bg-hover);
}

.city-name {
  font-weight: 600;
  color: var(--text-primary);
  font-size: 0.9rem;
  margin-bottom: 0.15rem;
}

.city-meta {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 0.1rem;
}

.city-coords {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: 'Courier New', monospace;
}

#controls button {
  padding: 0;
  font-size: 0.8rem;
  border-radius: 4px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  height: 28px;
  min-width: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#controls .primary {
  background: #1e88e5;
  color: #fff;
  padding: 0 0.75rem;
  font-size: 0.8rem;
  min-width: auto;
}

#controls .primary:hover {
  background: #1976d2;
}

#controls button:not(.primary):not(.danger) {
  background: transparent;
  color: var(--text-secondary);
}

#controls button:not(.primary):not(.danger):hover {
  background: var(--bg-hover);
}

#controls .danger {
  background: transparent;
  color: #c62828;
}

#controls .danger:hover {
  background: #ffebee;
}

.action-buttons {
  display: flex;
  gap: 0.2rem;
  align-items: center;
}

.icon-btn {
  width: 24px !important;
  height: 24px !important;
  min-width: 24px !important;
  padding: 0 !important;
  font-size: 0.9rem !important;
  background: transparent !important;
  color: var(--text-secondary) !important;
  border-radius: 4px !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
}

.icon-btn img {
  width: 16px;
  height: 16px;
  filter: brightness(0) saturate(100%) invert(51%) sepia(11%) saturate(626%) hue-rotate(155deg) brightness(91%) contrast(87%);
}

.icon-btn:hover {
  background: var(--bg-hover) !important;
}

.icon-btn:hover img {
  filter: brightness(0) saturate(100%) invert(28%) sepia(10%) saturate(1150%) hue-rotate(155deg) brightness(96%) contrast(91%);
}

.icon-btn.danger img {
  filter: brightness(0) saturate(100%) invert(20%) sepia(83%) saturate(2683%) hue-rotate(347deg) brightness(87%) contrast(90%);
}

.icon-btn.danger:hover {
  background: #ffebee !important;
}

.icon-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

@media (prefers-color-scheme: dark) {
  .icon-btn img {
    filter: brightness(0) saturate(100%) invert(77%) sepia(0%) saturate(1114%) hue-rotate(155deg) brightness(91%) contrast(87%);
  }

  .icon-btn:hover img {
    filter: brightness(0) saturate(100%) invert(100%) sepia(0%) saturate(0%) hue-rotate(0deg) brightness(100%) contrast(100%);
  }

  .icon-btn.danger img {
    filter: brightness(0) saturate(100%) invert(37%) sepia(73%) saturate(2683%) hue-rotate(347deg) brightness(95%) contrast(90%);
  }

  .icon-btn.danger:hover {
    background: rgba(198, 40, 40, 0.15) !important;
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #263238;
  color: #fff;
  padding: 1rem 1.5rem;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  font-size: 0.85rem;
  line-height: 1.5;
  max-width: 500px;
  white-space: pre-wrap;
  z-index: 10000;
  opacity: 0;
  transition: all 0.3s ease;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

#toast-progress {
  bottom: 5.5rem;
  z-index: 10001;
}

.toast-cancel {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.85);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.15s ease;
  padding: 0;
  margin-left: auto;
  width: 20px;
  height: 20px;
}

.toast-cancel:hover {
  color: #fff;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.toast-success {
  background: #2e7d32;
}

.toast.toast-error {
  background: #546e7a;
  max-width: 600px;
  text-align: left;
  font-weight: 400;
}

.toast.toast-info {
  background: #1565c0;
}

@media (max-width: 1024px) {
  #workspace {
    flex-direction: column;
  }

  #panel {
    max-width: none;
  }

  #charts {
    flex: none;
    min-height: 400px;
  }

  .controls-row {
    flex-wrap: wrap;
    gap: 0.4rem;
  }

  #city-inputs {
    flex-basis: 100%;
    margin-right: 0;
    margin-bottom: 0.4rem;
    order: -1;
  }

  .action-buttons {
    margin-left: auto;
  }
}

@media (max-width: 768px) {
  #config-bar {
    padding: 0.4rem;
  }

  #workspace {
    padding: 0.4rem;
    gap: 0.4rem;
    overflow: visible;
  }

  #panel {
    overflow: visible;
  }

  #stats {
    overflow: visible;
  }

  .controls-row {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  /* Group 1: Mode selectors - full width row */
  #mode-selector,
  #smoothing-selector {
    flex: 1 1 calc(50% - 0.25rem);
  }

  /* Group 2: Cities - full width */
  .city-tags-container,
  .year-tags-container {
    flex: 1 1 100%;
  }

  .city-tags,
  .year-tags {
    flex-wrap: wrap;
  }

  .city-input-wrapper {
    flex: 1 1 100%;
  }

  .city-search-input {
    flex: 1;
    width: auto;
  }

  /* Group 3: Mode controls - full width */
  .mode-controls {
    flex: 1 1 100%;
    flex-wrap: wrap;
  }

  /* Group 4: Dates/parameters - adaptive */
  #start, #end {
    flex: 1 1 calc(50% - 0.25rem);
    width: auto;
  }

  #controls-periodic input[type="text"] {
    flex: 1 1 calc(50% - 0.25rem);
    width: auto;
  }

  .year-input {
    flex: 1 1 100%;
    width: auto;
  }

  #period-type {
    flex: 1 1 100%;
    width: auto;
  }

  #season-select,
  #month-select,
  #day-select {
    flex: 1 1 100% !important;
    width: auto !important;
  }

  #year-from,
  #year-to {
    flex: 1 1 calc(50% - 0.25rem) !important;
    width: auto !important;
  }

  /* Group 5: Control buttons */
  .spacer {
    flex: 1 1 100%;
  }

  .action-buttons {
    flex: 0 0 auto;
  }

  #apply {
    flex: 1 1 auto;
  }
}

/* Ko-fi Widget */
.kofi-floater {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  background: var(--bg-card);
  border-radius: 50%;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 9999;
}

.kofi-floater:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.kofi-floater img {
  width: 32px;
  height: 32px;
  filter: brightness(0) saturate(100%) invert(51%) sepia(11%) saturate(626%) hue-rotate(155deg) brightness(91%) contrast(87%);
  transition: filter 0.2s ease;
}

.kofi-floater:hover img {
  filter: brightness(0) saturate(100%) invert(28%) sepia(10%) saturate(1150%) hue-rotate(155deg) brightness(96%) contrast(91%);
}

.kofi-floater.hidden {
  display: none;
}

@media (prefers-color-scheme: dark) {
  .kofi-floater img {
    filter: brightness(0) saturate(100%) invert(77%) sepia(0%) saturate(1114%) hue-rotate(155deg) brightness(91%) contrast(87%);
  }

  .kofi-floater:hover img {
    filter: brightness(0) saturate(100%) invert(100%) sepia(0%) saturate(0%) hue-rotate(0deg) brightness(100%) contrast(100%);
  }
}

.kofi-panel {
  position: fixed;
  bottom: 52px;
  right: 52px;
  width: 400px;
  height: 660px;
  max-width: calc(100vw - 48px);
  max-height: calc(100vh - 48px);
  background: transparent;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  z-index: 9998;
  overflow: hidden;
  transition: all 0.3s ease;
}

.kofi-panel.hidden {
  display: none;
}
