/* Minimalist design – CSS custom properties, mobile-first */
:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --text: #1a1a2e;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --price-line: #6366f1;
  --price-neg: #10b981;
  --success: #10b981;
  --warning: #f59e0b;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.08);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  color-scheme: light dark;
}

/* Dark theme */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f172a;
    --surface: #1e293b;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --border: #334155;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
  }
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #334155;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
}

/* Reset */
*, *::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;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-font-smoothing: antialiased;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.header-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.header-title h1 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-title .subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: none;
}

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

/* Status bar */
.status-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 16px;
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 0.7rem;
  font-weight: 500;
}

.status-badge.available {
  background: rgba(16,185,129,0.12);
  color: var(--success);
}

.status-badge.waiting {
  background: rgba(100,116,139,0.1);
  color: var(--text-muted);
}

.status-badge.pending {
  background: rgba(245,158,11,0.12);
  color: var(--warning);
}

.status-badge.overdue {
  background: rgba(239,68,68,0.12);
  color: #ef4444;
}

/* Inline reload button in status bar */
.status-reload-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  transition: all 0.15s;
}
.status-reload-btn:hover { color: var(--accent); background: var(--border); }
.status-reload-btn svg { width: 13px; height: 13px; }

/* Controls bar */
.controls {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.control-group label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
  margin-right: 2px;
}

.divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  flex-shrink: 0;
  margin: 0 4px;
}

/* Buttons & Selects */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  min-width: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:disabled:hover { border-color: var(--border); color: var(--text); }

select.btn {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 24px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px center;
}

/* Icon button */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover { background: var(--border); color: var(--text); }

.icon-btn svg { width: 20px; height: 20px; }

/* Theme toggle – segmented pill */
.theme-toggle {
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px;
  gap: 1px;
}

.theme-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: calc(var(--radius) - 3px);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s;
  padding: 0;
}

.theme-btn:hover { color: var(--text); background: var(--border); }
.theme-btn.active { background: var(--accent); color: #fff; box-shadow: 0 1px 4px rgba(99,102,241,0.3); }
.theme-btn svg { width: 15px; height: 15px; }

/* Language select – compact dropdown */
.lang-select {
  height: 32px;
  padding: 0 24px 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px center;
  transition: all 0.15s;
}
.lang-select:hover { border-color: var(--accent); }
.lang-select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Country select – with flag */
.country-select {
  padding-left: 6px;
  font-size: 0.8rem;
  min-width: 0;
}

/* Custom date picker button – now a toggle-btn variant */
.picker-toggle-btn {
  gap: 4px;
  font-size: 0.75rem;
}
.picker-toggle-btn svg:first-child { width: 14px; height: 14px; flex-shrink: 0; }
.picker-chevron {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
  transition: transform 0.2s;
}
.custom-picker-wrap.open .picker-chevron { transform: rotate(180deg); }

/* Custom picker wrapper – sits inside toggle-group, button flows as part of group */
.custom-picker-wrap {
  position: relative;
  display: contents;
}

/* Shared dropdown popup */
.picker-popup {
  display: none;
  z-index: 30;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 8px;
  position: fixed;
  animation: pickerDropIn 0.12s ease;
}

/* Picker dropdown shows when wrap has open class */
.custom-picker-wrap.picker-dropdown.open .picker-popup { display: block; }
@keyframes pickerDropIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }

/* Week/Month picker – inline selects */
.picker-selects {
  display: flex;
  align-items: center;
  gap: 4px;
}
.picker-selects select {
  height: 28px;
  padding: 0 20px 0 6px;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 2px);
  background: var(--bg);
  color: var(--text);
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 4px center;
}
.picker-year-input {
  height: 28px;
  width: 56px;
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 2px);
  background: var(--bg);
  color: var(--text);
  font-size: 0.75rem;
  font-family: inherit;
  text-align: center;
}
.picker-year-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.picker-kw-input {
  height: 28px;
  width: 56px;
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 2px);
  background: var(--bg);
  color: var(--text);
  font-size: 0.75rem;
  font-family: inherit;
  text-align: center;
}
.picker-kw-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.picker-kw-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-weight: 500;
}
.picker-selects .picker-ok {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--accent);
  border-radius: calc(var(--radius) - 2px);
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.picker-selects .picker-ok:hover { opacity: 0.85; }

/* Calendar (day picker) */
.cal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.cal-header span {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text);
}
.cal-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
}
.cal-nav:hover { background: var(--border); color: var(--text); }
.cal-nav svg { width: 14px; height: 14px; }
.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0;
  text-align: center;
  font-size: 0.65rem;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 2px;
}
.cal-weekdays span { padding: 2px 0; }
.cal-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
}
.cal-day {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text);
  font-size: 0.72rem;
  font-family: inherit;
  cursor: pointer;
  padding: 0;
}
.cal-day:hover { background: var(--border); }
.cal-day.today { font-weight: 700; color: var(--accent); }
.cal-day.selected { background: var(--accent); color: #fff; }
.cal-day.other-month { color: var(--text-muted); opacity: 0.4; }
.cal-day:disabled { opacity: 0.2; cursor: not-allowed; }
.cal-day:disabled:hover { background: transparent; }

/* Segment/resolution inline collapsible */
.segment-popover-wrap {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
}
.btn-segment-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0 8px;
  font-size: 0.75rem;
  cursor: pointer;
  user-select: none;
}
.details-chevron {
  width: 14px;
  height: 14px;
  transition: transform 0.2s;
  flex-shrink: 0;
  cursor: pointer;
}
.segment-popover-wrap.open .btn-segment-toggle { display: none; }
.segment-popover {
  display: none;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.segment-popover-wrap.open .segment-popover { display: inline-flex; }
.divider-h {
  width: 1px;
  height: 24px;
  background: var(--border);
  flex-shrink: 0;
}

/* Chart container */
.chart-container {
  position: relative;
  flex: 1;
  min-height: 0;
  padding: 8px 8px 0;
}

.chart-container canvas {
  width: 100% !important;
  height: 100% !important;
}

/* Main layout */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* Loading overlay */
.loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 5;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.loading.visible { opacity: 0.7; pointer-events: auto; }

.loading::after {
  content: '';
  width: 24px;
  height: 24px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

/* Settings panel – slide-in from right */
.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 100;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.settings-panel.open {
  transform: translateX(0);
}

.settings-panel h2 {
  font-size: 1rem;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.setting-row label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

.setting-row select,
.setting-row input[type="number"]:not(.prec-custom-input) {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  font-family: inherit;
  width: 100%;
}

.setting-row .checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.setting-row .checkbox-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

/* Weather location input + autocomplete */
.weather-loc-input {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  font-family: inherit;
  width: 100%;
}
.setting-row { position: relative; }
.weather-ac-list {
  display: none;
  position: absolute;
  left: 0; right: 0;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-height: 160px;
  overflow-y: auto;
}
.weather-ac-item {
  padding: 6px 10px;
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.weather-ac-item:hover { background: var(--border); }

/* Precision slider */
.precision-slider-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}
.precision-slider-wrap input[type="range"] {
  flex: 1;
  height: 4px;
  accent-color: var(--accent);
  cursor: pointer;
}
.precision-slider-wrap span {
  font-size: 0.85rem;
  min-width: 36px;
  text-align: center;
  font-weight: 500;
}

/* Chart type toggle */
.chart-type-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.chart-type-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}
.chart-type-btn:not(:last-child) { border-right: 1px solid var(--border); }
.chart-type-btn:hover { color: var(--accent); }
.chart-type-btn.active { background: var(--accent); color: #fff; }
.chart-type-btn svg { width: 16px; height: 16px; }

/* Shared toggle group (scope buttons, precision, segment/resolution) */
.toggle-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  padding: 0 10px;
  border: none;
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.toggle-btn:not(:last-child) { border-right: 1px solid var(--border); }
/* Picker toggle-btn is visually last (display:contents parent) — no right border */
.picker-toggle-btn { border-right: none !important; }
.toggle-btn:hover { color: var(--accent); }
.toggle-btn.active { background: var(--accent); color: #fff; }
.toggle-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.toggle-btn:disabled:hover { color: var(--text); }

/* Compact toggle group variant for settings panel */
.toggle-group.compact .toggle-btn {
  height: 28px;
  padding: 0 8px;
  font-size: 0.75rem;
}

/* Full-width toggle group – fills settings panel width */
.toggle-group.full-width {
  display: flex;
  width: 100%;
}
/* All toggle buttons in full-width groups share equal width */
.toggle-group.full-width .toggle-btn {
  flex: 1 1 0;
  min-width: 0;
}

/* Precision custom input should be wider than the regular toggle items */
#precision-toggle .num-input-wrap {
  flex: 2 1 0;
}

/* Number input disguised as toggle-btn */
.num-input-wrap {
  position: relative;
  padding: 0 2px !important;
  overflow: hidden;
}
.num-input {
  width: 100%;
  height: 100%;
  border: none;
  background: transparent;
  color: inherit;
  font-size: inherit;
  font-family: inherit;
  text-align: center;
  padding: 0;
  outline: none;
  box-sizing: border-box;
}

/* Setting sub-row for min/max inside a setting-row */
.setting-sub-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}
.setting-sub-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  min-width: 28px;
  flex-shrink: 0;
}

/* Backdrop for settings */
.settings-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.settings-panel.open ~ .settings-backdrop {
  opacity: 1;
  pointer-events: auto;
}

/* Footer */
.footer {
  padding: 6px 16px;
  font-size: 0.65rem;
  color: var(--text-muted);
  text-align: center;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* Tooltip for disabled controls */
[data-tooltip] {
  position: relative;
}

[data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  padding: 4px 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.7rem;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 50;
}

/* Desktop */
@media (min-width: 600px) {
  .header-title .subtitle { display: inline; }
  .header { padding: 12px 24px; }
  .status-bar { padding: 6px 24px; }
  .controls { padding: 10px 24px; gap: 8px; }
  .chart-container { padding: 12px 16px 4px; }
  .footer { padding: 8px 24px; }
}

@media (min-width: 900px) {
  .chart-container { padding: 16px 24px 8px; }
}

/* Touch targets for mobile */
@media (pointer: coarse) {
  .btn { height: 40px; padding: 0 14px; font-size: 0.85rem; }
  .icon-btn { width: 44px; height: 44px; }
  select.btn { height: 40px; }
}
