:root {
  --bg:           #0f1117;
  --surface:      #1c1f2a;
  --border:       #2a2d3a;
  --border-input: #3a3d4a;
  --text:         #e0e0e0;
  --text-muted:   #888;
  --text-label:   #aaa;
  --text-heading: #fff;
  --text-card:    #ccc;
  --accent:       #4ade80;
  --accent-hover: #22c55e;
  --accent-t:     rgba(74,222,128,0.15);
  --blue:         #60a5fa;
  --red:          #f87171;
  --grid:         rgba(255,255,255,0.06);
  --tick:         #666;
  --hover:        rgba(255,255,255,0.08);
  --year2:        rgba(96,165,250,0.9);
  --year3:        rgba(249,115,22,0.9);
  --year4:        rgba(167,139,250,0.9);
  --year5:        rgba(251,113,133,0.9);
  --year6:        rgba(250,204,21,0.9);
  --bar-selected:   rgba(96,165,250,0.55);
  --bar-unselected:  rgba(96,165,250,0.2);
  --goal-line:    rgba(250,204,21,0.6);
  --radius-sm:    6px;
  --radius-md:    8px;
  --radius-lg:    12px;
  --radius-xl:    16px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg:           #f5f5f7;
    --surface:      #ffffff;
    --border:       #e2e4ea;
    --border-input: #d0d0d8;
    --text:         #1a1a1a;
    --text-muted:   #666;
    --text-label:   #555;
    --text-heading: #111;
    --text-card:    #333;
    --accent:       #16a34a;
    --accent-hover: #15803d;
    --accent-t:     rgba(22,163,74,0.12);
    --blue:         #2563eb;
    --red:          #dc2626;
    --grid:         rgba(0,0,0,0.06);
    --tick:         #999;
    --hover:        rgba(0,0,0,0.06);
    --year2:        rgba(37,99,235,0.9);
    --year3:        rgba(234,88,12,0.9);
    --year4:        rgba(109,40,217,0.9);
    --year5:        rgba(190,18,60,0.9);
    --year6:        rgba(161,98,7,0.9);
    --bar-selected:   rgba(37,99,235,0.55);
    --bar-unselected:  rgba(37,99,235,0.2);
    --goal-line:    rgba(161,98,7,0.6);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  padding: 32px 24px;
  min-height: 100vh;
}
@media (max-width: 540px) {
  html { font-size: 112%; }
  body { padding: 20px 16px; }
}
.topbar {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: 48px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  margin: 0 -16px 24px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.is-staging .topbar { background: repeating-linear-gradient(135deg, #7c3aed 0px, #7c3aed 10px, #6d28d9 10px, #6d28d9 20px); }
.is-staging .topbar::after { content: "TESTMILJÖ"; font-size: 0.65rem; font-weight: 700; letter-spacing: .1em; color: #fff; background: rgba(0,0,0,0.25); border-radius: 3px; padding: 2px 7px; margin-left: 8px; }
.topbar-back { font-size: 0.78rem; color: var(--accent); text-decoration: none; opacity: 0.7; margin-right: 16px; white-space: nowrap; }
.topbar-back:hover { opacity: 1; }
.topbar-title { font-size: 0.8rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); flex: 1; }
.fetch-status {
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%) translateY(-100%);
  background: var(--surface);
  border: 1px solid var(--accent);
  border-top: none;
  border-radius: 0 0 10px 10px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
  padding: 7px 18px;
  font-size: 0.82rem;
  color: var(--text);
  white-space: nowrap;
  pointer-events: none;
  z-index: 51;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
}
.fetch-status:not(:empty) {
  opacity: 1;
  transform: translateX(-50%) translateY(0%);
}
.fetch-status.error   { color: var(--red); }
.fetch-status.pulsing { animation: fetch-pulse 1.4s ease-in-out infinite; }
h1 { font-size: 1.6rem; font-weight: 600; margin-bottom: 4px; color: var(--text-heading); }
.subtitle { color: var(--text-muted); font-size: 0.95rem; margin-bottom: 28px; }

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: var(--radius-md);
  padding: 11px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  margin-top: 6px;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: default; }

/* Settings-knapp i topbar */
.topbar-icon-btn { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 1.5rem; padding: 4px 6px; line-height: 1; }
.topbar-icon-btn:hover { color: var(--accent); }

/* Settings-modal */
.modal-settings-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; font-size: 0.95rem; font-weight: 600; color: var(--text-card); }
.modal-close-btn { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 1.1rem; padding: 2px 4px; line-height: 1; }
.modal-close-btn:hover { color: var(--text-card); }
.settings-modal-body { overflow: hidden; flex: 1; display: flex; flex-direction: column; padding: 16px 16px 16px; margin-bottom: 0; border: none; border-radius: 0; background: none; }
.settings-tabs { display: flex; border-bottom: 1px solid var(--border); padding: 0 16px; flex-shrink: 0; }
.settings-tab { background: none; border: none; border-bottom: 2px solid transparent; padding: 10px 16px; margin-bottom: -1px; font-size: 0.9rem; color: var(--text-muted); cursor: pointer; }
.settings-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* Kontrollrad */
.controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
}
.controls label { color: var(--text-label); font-size: 0.88rem; white-space: nowrap; }
.ctrl-group { display: flex; align-items: center; gap: 8px; }
.ctrl-section { display: flex; flex-direction: column; gap: 8px; }
.ctrl-section-label { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .07em; color: var(--text-muted); }
.ctrl-section-fields { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.ctrl-section-divider { height: 1px; background: var(--border); margin: 10px 0 18px; }
.settings-tab-pane { width: 100%; display: flex; flex-direction: column; flex: 1; overflow-y: auto; scrollbar-gutter: stable; }
.controls input[type=date],
.controls input[type=number] {
  background: var(--bg);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-md);
  color: var(--text);
  padding: 7px 12px;
  font-size: 0.9rem;
}
.controls input[type=number] { width: 72px; -moz-appearance: textfield; }
.controls input[type=number]:focus { outline: none; border-color: var(--accent); }
.controls input[type=number]::-webkit-inner-spin-button,
.controls input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.type-exclusion-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 6px; }
.type-exclusion-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.82rem;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  cursor: pointer;
  user-select: none;
}
.type-exclusion-label:has(input:checked) {
  border-color: var(--accent);
  color: var(--accent);
}
.type-exclusion-label input { accent-color: var(--accent); }
.controls select {
  background: var(--bg);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-md);
  color: var(--text);
  padding: 7px 10px;
  font-size: 0.9rem;
  cursor: pointer;
}
.controls select:focus { outline: none; border-color: var(--accent); }
.start-day   { width: 58px; }
.start-month { width: 120px; }
.controls span { color: var(--text-muted); font-size: 0.88rem; }
.controls .divider { display: none; }
.prompt-hint { font-size: 0.75rem; color: var(--text-muted); margin-bottom: 8px; }
.prompt-hint code { background: var(--surface-raised, var(--border)); border-radius: 3px; padding: 1px 4px; font-size: 0.72rem; color: var(--accent); }
#coach-prompt-template { width: 100%; font-size: 0.82rem; font-family: inherit; color: var(--text-card); background: var(--bg); border: 1px solid var(--border-input); border-radius: var(--radius-sm); padding: 10px 12px; resize: none; line-height: 1.55; field-sizing: content; overflow: hidden; }
#coach-prompt-template:focus { outline: none; border-color: var(--accent); }
@keyframes spin  { to { transform: rotate(360deg); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.refresh-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
  transition: color 0.15s;
  line-height: 0;
  margin-right: 4px;
  display: flex;
  align-items: center;
}
.refresh-btn svg {
  width: 18px;
  height: 18px;
}
.refresh-btn:hover  { color: var(--accent); }
.refresh-btn.spinning { animation: spin 0.8s linear infinite; color: var(--accent); }
.refresh-btn:disabled   { opacity: 0.35; cursor: default; }
.refresh-btn.rate-limited { animation: shake 0.4s ease; color: var(--text-muted); }
.more-menu { position: relative; }
.more-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.more-btn span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--text-muted);
  border-radius: 2px;
  transition: background 0.15s;
}
.more-btn:hover span { background: var(--accent); }
.more-dropdown {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  min-width: 160px;
  z-index: 100;
  overflow: hidden;
}
.more-dropdown.active { display: block; }
.more-dropdown button {
  display: block;
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 10px 16px;
  text-align: left;
  font-size: 0.88rem;
  cursor: pointer;
}
.more-dropdown button:hover { background: var(--hover); }
.more-dropdown button.danger:hover { color: var(--red); }
.ctrl-btn {
  background: transparent;
  border: 1px solid var(--border-input);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  padding: 7px 16px;
  font-size: 0.88rem;
  cursor: pointer;
  white-space: nowrap;
}
.ctrl-btn:hover { border-color: var(--accent); color: var(--accent); }
a.ctrl-btn { display: inline-block; text-decoration: none; line-height: 1; }
.ai-action-link {
  display: inline-block;
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--text-muted);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.ctrl-btn:disabled { opacity: 0.4; cursor: default; }
.ctrl-btn.danger:hover { border-color: var(--red); color: var(--red); }
.year-select {
  background: var(--bg);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  padding: 3px 8px;
  font-size: 0.78rem;
  cursor: pointer;
}
.year-select:focus { outline: none; border-color: var(--accent); }
.year-select-label { font-size: 0.72rem; color: var(--text-muted); opacity: 0.6; white-space: nowrap; }

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 28px;
  width: 100%;
  max-width: 380px;
}
.modal-settings { max-width: 560px; max-height: 90vh; display: flex; flex-direction: column; padding: 0; }
.modal h2 { font-size: 1rem; color: var(--text-card); margin-bottom: 20px; }
.modal-info { font-size: 0.78rem; color: var(--text-muted); margin: 12px 0 4px; line-height: 1.5; }

.welcome-box { max-width: 480px; margin: 80px auto; text-align: center; padding: 0 24px; }
.welcome-text { color: var(--text-muted); font-size: 0.95rem; line-height: 1.6; margin-bottom: 16px; }
.modal-status { font-size: 0.85rem; margin-top: 10px; color: var(--text-muted); min-height: 18px; }
.export-url-box {
  font-size: 0.78rem;
  font-family: monospace;
  word-break: break-all;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--accent);
  margin-top: 12px;
  user-select: all;
}
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions button { flex: 1; }
.modal-actions button.danger { color: var(--red); border-color: var(--red); }

/* Senaste pass */
.card-latest {
  padding-bottom: 0;
  margin-bottom: 0;
}
.latest-label { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 6px; }
#latest-date  { font-size: 0.82rem; color: var(--text-muted); margin-bottom: 4px; }
.latest-row   { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.latest-name  { font-size: 1rem; font-weight: 600; color: var(--text-heading); }
.latest-chip  { font-size: 0.75rem; color: var(--accent); background: var(--accent-t); border-radius: var(--radius-sm); padding: 2px 8px; text-transform: capitalize; }
.ai-classification-chip { color: #fff; border-radius: var(--radius-sm); padding: 2px 8px; font-size: 0.75rem; }
.cls-distans      { background: #3a7d44; }
.cls-langpass     { background: #1a5fa8; }
.cls-intervall    { background: #9b2335; }
.cls-troskel      { background: #c05a1f; }
.cls-aterhamtning { background: #5a7a2e; }
.cls-styrka       { background: #5b3a8c; }
.cls-teknik       { background: #2a7a7a; }
.cls-test         { background: #8c3a3a; }
.cls-default      { background: #555; }
.latest-meta  { font-size: 0.85rem; color: var(--text-muted); display: flex; gap: 8px; align-items: center; }
.latest-dot   { color: var(--border-input); }
.latest-cls-row { display: inline-flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.latest-tag   { font-size: 0.72rem; color: var(--text-muted); background: var(--bg-input); border-radius: var(--radius-sm); padding: 2px 7px; }
.latest-zone-wrap { display: flex; align-items: flex-start; gap: 12px; margin-top: 10px; }
.latest-zone-bar { display: inline-flex; gap: 3px; align-items: flex-end; height: 36px; margin-top: 3px; }
.latest-zone-hr  { display: flex; flex-direction: column; gap: 2px; font-size: 0.8rem; color: var(--text-muted); }
.zone-col     { display: flex; flex-direction: column; align-items: center; gap: 3px; width: 14px; }
.zone-col-bar { width: 10px; border-radius: 2px 2px 0 0; min-height: 2px; }
.zone-col-lbl { font-size: 0.65rem; color: var(--text-muted); line-height: 1; }

.card-latest.clickable { cursor: pointer; transition: color 0.15s; }
.card-latest.clickable:hover .latest-name { text-decoration: underline; }

/* Diagram-kort */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 24px;
  margin-bottom: 24px;
}
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; }
.card-header h2 { font-size: 1rem; font-weight: 600; color: var(--text-card); margin: 0; }
.card-header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.chart-with-tooltip { display: flex; flex-direction: column; gap: 8px; }
.chart-wrap { height: 180px; }
#wrap-fitness-chart { height: 220px; }
#wrap-tsb-chart     { height: 180px; }
@media (max-width: 540px) {
  .chart-wrap         { height: 130px; }
  #wrap-fitness-chart { height: 160px; }
  #wrap-tsb-chart     { height: 140px; }
  .card               { padding: 14px; margin-bottom: 12px; }
  .card-header        { margin-bottom: 12px; }
.card-latest        { padding-bottom: 10px; margin-bottom: 10px; }
}
canvas { display: block; width: 100%; }
#chart-tooltip {
  font-size: 0.92rem;
  color: var(--text-card);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 16px;
}
.ct-title { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 0; flex: 0 0 4rem; }
.ct-row { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 0; flex: 0 0 auto; display: flex; flex-direction: row; align-items: baseline; gap: 6px; }
.ct-row-value { font-size: 0.95rem; font-weight: 700; color: var(--accent); letter-spacing: 0; text-transform: none; font-variant-numeric: tabular-nums; display: inline-block; width: 6ch; }
@media (max-width: 540px) {
  #chart-tooltip, #fitness-tooltip, #tsb-tooltip { padding: 8px 10px; }
  .topbar-icon-btn { font-size: 1.1rem; }
}

/* Aktivitetslista */
#activities-section { touch-action: pan-y; user-select: none; }
.swipe-viewport { position: relative; overflow: hidden; }
.swipe-side { width: 100%; }
#activities-list-next { position: absolute; top: 0; left: 0; width: 100%; }
.activity-row {
  display: block;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: inherit;
}
.activity-row:last-child { border-bottom: none; }
.activity-row:hover { background: var(--hover); border-radius: var(--radius-md); }
.activity-date { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 3px; }
.activity-content-row   { display: flex; align-items: center; gap: 8px; margin-bottom: 3px; flex-wrap: wrap; }
.activity-name  { font-size: 0.9rem; font-weight: 600; color: var(--text-heading); }
.activity-meta  { font-size: 0.82rem; color: var(--text-muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.activity-header { display: flex; align-items: baseline; gap: 10px; }
.activity-filter-title { font-size: 0.82rem; color: var(--text-muted); }
.activity-empty { font-size: 0.88rem; color: var(--text-muted); padding: 8px 0; }

.strava-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0 8px;
  gap: 12px;
}
.strava-footer a { color: var(--accent); font-size: 0.85rem; text-decoration: none; }
.strava-footer a:hover { text-decoration: underline; }
.strava-badge { height: 24px; display: block; }
.stat-value.form-negative { color: var(--red); }
#fitness-tooltip,
#tsb-tooltip {
  font-size: 0.92rem;
  color: var(--text-card);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 16px;
}

.hidden { display: none; }
.btn-icon { background: none; border: none; cursor: pointer; padding: 0; }
.strava-brand { color: #FC5200; font-weight: 700; }

/* Fetch spinner */
@keyframes fetch-pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.35; }
}

/* AI-analys */
.ai-output.ai-loading { animation: fetch-pulse 1.4s ease-in-out infinite; }
.ai-output {
  white-space: pre-wrap;
  font-size: 0.88rem;
  line-height: 1.65;
  padding: 8px 0;
  min-height: 1.5em;
  color: var(--text-card);
}
.ai-usage {
  font-size: 0.73rem;
  color: var(--text-muted);
  margin-top: 4px;
}
.ai-output-meta {
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-top: 6px;
}
.ai-coach-output {
  border-top: 1px solid var(--border);
  margin-top: 10px;
  padding-top: 10px;
  white-space: normal;
}
.ai-coach-output h1, .ai-coach-output h2, .ai-coach-output h3 {
  font-size: 0.95rem;
  font-weight: 700;
  margin: 10px 0 4px;
  color: var(--text-heading);
}
.ai-coach-output ul, .ai-coach-output ol {
  padding-left: 1.4em;
  margin: 4px 0;
}
.ai-coach-output li { margin: 2px 0; }
.ai-coach-output p  { margin: 4px 0; }
.ai-coach-output strong { font-weight: 700; }
.manifest-status { display: flex; flex-direction: column; gap: 8px; }
.manifest-filename {
  font-size: 0.85rem;
  color: var(--text-muted);
}
.settings-tab:focus { outline: none; }
