:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --panel: #ffffff;
  --border: #e4e2dd;
  --text: #2b2b28;
  --text-dim: #74716a;
  --accent: #4a6d5c;
  --accent-soft: color-mix(in srgb, var(--accent) 15%, white);
  --danger: #b6543c;
  --workout-soft: #eef0e5;
  --workout-accent: #6b7a3f;
  --radius: 10px;
  --font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #171917;
    --panel: #212420;
    --border: #363a34;
    --text: #eae7e0;
    --text-dim: #9a978d;
    --accent: #5c9179;
    --accent-soft: color-mix(in srgb, var(--accent) 25%, black);
    --danger: #e08767;
    --workout-soft: #2b2f22;
    --workout-accent: #a8bd7a;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171917;
  --panel: #212420;
  --border: #363a34;
  --text: #eae7e0;
  --text-dim: #9a978d;
  --accent: #5c9179;
  --accent-soft: color-mix(in srgb, var(--accent) 25%, black);
  --danger: #e08767;
  --workout-soft: #2b2f22;
  --workout-accent: #a8bd7a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  padding: 20px 16px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.tab {
  border: none;
  background: none;
  font-size: 15px;
  padding: 10px 14px;
  color: var(--text-dim);
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.view {
  display: none;
  padding: 0 16px 40px;
  max-width: 640px;
  margin: 0 auto;
}

.view.active { display: block; }

.add-btn {
  width: 100%;
  margin-bottom: 16px;
}

.session-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.empty {
  color: var(--text-dim);
  text-align: center;
  padding: 40px 0;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.template-card {
  display: flex;
  gap: 12px;
}

.template-card-main {
  flex: 1;
  min-width: 0;
}

.route-thumb {
  width: 126px;
  height: 126px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  overflow: hidden;
}

.route-thumb svg {
  width: 100%;
  height: 100%;
}

.route-thumb polyline {
  fill: none;
  stroke: var(--accent);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  width: 36px;
  height: 36px;
  min-width: 36px;
  padding: 0;
  border-radius: 50%;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-actions .icon-btn {
  margin-left: auto;
}

.icon-btn.saved {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.card h3 {
  margin: 0 0 6px;
  font-size: 17px;
}

.badge {
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

.badge.Workout {
  background: var(--workout-soft);
  color: var(--workout-accent);
}

.date {
  font-size: 13px;
  color: var(--text-dim);
}

.meta {
  display: flex;
  gap: 12px;
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 6px;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.plan-meta {
  color: var(--text);
  flex-direction: column;
  gap: 2px;
}

.notes {
  font-size: 14px;
  color: var(--text-dim);
  margin: 6px 0;
  white-space: pre-wrap;
}

.history-card-layout {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.history-card-main {
  flex: 1;
  min-width: 0;
}

.route-thumb.small {
  width: 72px;
  height: 72px;
  cursor: pointer;
}

.stat-columns {
  display: flex;
  gap: 20px;
  margin: 6px 0 2px;
}

.stat-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.stat-column-heading {
  font-size: 12px;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
  margin-bottom: 3px;
}

.stat-column-line {
  font-size: 14px;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.clock-icon {
  font-size: 0.8em;
}

.stat-column-line:last-child {
  margin-top: 6px;
}

.effort-row {
  margin: 4px 0;
}

.effort-pill {
  font-size: 12px;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
  color: white;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

.btn svg {
  display: block;
}

.btn-icon {
  padding: 8px 10px;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.btn.danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--panel);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
}

.modal h2 {
  margin-top: 0;
  font-size: 18px;
}

.cal-day-header,
.modal-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.cal-day-header h2,
.modal-header-row h2 {
  margin-bottom: 0;
}

.modal-header-row {
  flex-wrap: wrap;
}

.modal form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 600;
}

.modal input,
.modal select,
.modal textarea {
  font-size: 16px;
  font-weight: normal;
  color: var(--text);
  background: var(--panel);
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
}

.modal input::placeholder,
.modal textarea::placeholder {
  color: var(--text-dim);
  opacity: 0.65;
  font-style: italic;
}

.hms-inputs {
  display: flex;
  gap: 8px;
}

.hms-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 0;
}

.hms-field input {
  width: 100%;
  text-align: center;
  padding: 8px 4px;
  -moz-appearance: textfield;
}

.hms-field input::-webkit-outer-spin-button,
.hms-field input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.hms-unit {
  font-size: 11px;
  color: var(--text-dim);
  font-weight: normal;
}

.log-target {
  font-weight: 600;
  margin: 0;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.route-section { display: flex; }

.route-buttons {
  display: flex;
  gap: 8px;
  width: 100%;
}

.route-buttons .btn {
  flex: 1;
}

.route-summary {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  font-size: 14px;
}

.route-summary span {
  color: var(--text-dim);
}

.template-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 14px;
}

.template-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  flex-wrap: wrap;
}

.template-item-main {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.template-item-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.template-item-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.modal.map-modal {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  border-radius: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.route-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.route-status {
  font-size: 13px;
  color: var(--text-dim);
}

.route-search {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.route-search input {
  flex: 1;
  font-size: 16px;
  color: var(--text);
  background: var(--panel);
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
}

#route-map {
  flex: 1;
  min-height: 0;
}

.route-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.route-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.route-distance {
  font-size: 14px;
  font-weight: 600;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.user-location-marker {
  width: 16px;
  height: 16px;
}

.user-location-marker .dot {
  position: absolute;
  inset: 0;
  background: #4285f4;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.4);
}

.user-location-marker .pulse {
  position: absolute;
  inset: 0;
  background: #4285f4;
  border-radius: 50%;
  opacity: 0.6;
  animation: location-pulse 1.8s ease-out infinite;
}

@keyframes location-pulse {
  0% {
    transform: scale(1);
    opacity: 0.6;
  }
  100% {
    transform: scale(3);
    opacity: 0;
  }
}

/* ---------- calendar ---------- */

.cal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.cal-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.cal-toggle-btn {
  border: none;
  background: var(--panel);
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  padding: 7px 12px;
  cursor: pointer;
}

.cal-toggle-btn.active {
  background: var(--accent);
  color: white;
}

.cal-nav {
  display: flex;
  gap: 6px;
}

.cal-nav .btn {
  padding: 8px 12px;
}

.cal-label {
  font-size: 16px;
  margin: 0 0 12px;
}

.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.cal-weekday {
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  padding-bottom: 4px;
}

.cal-cell {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 4px;
  padding: 6px 2px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
}

.cal-cell.outside {
  opacity: 0.4;
}

.cal-cell.today .cal-cell-num {
  background: var(--accent);
  color: white;
}

.cal-cell-num {
  font-size: 13px;
  color: var(--text);
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.cal-cell-dots {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
  justify-content: center;
}

.cal-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.cal-dot.completed {
  background: var(--text-dim);
}

.cal-week {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.cal-day-section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}

.cal-day-section.today {
  border-color: var(--accent);
}

.cal-day-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.cal-day-add {
  border: none;
  background: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  padding: 2px 4px;
  cursor: pointer;
  font-family: inherit;
}

.cal-day-empty {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
}

.cal-agenda-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: none;
  background: none;
  padding: 6px 0;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
}

.cal-agenda-title {
  font-size: 14px;
  color: var(--text);
}

/* ---------- settings ---------- */

.settings-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 14px;
}

.settings-avatar {
  width: 48px;
  height: 48px;
  min-width: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 16px;
}

.settings-profile input[type="text"] {
  flex: 1;
  font-size: 16px;
  color: var(--text);
  background: var(--panel);
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
}

.settings-color-row {
  display: flex;
  gap: 8px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.settings-color-row .color-swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
}

.settings-color-row .color-swatch.active {
  border-color: var(--text);
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-of-type {
  border-bottom: none;
}

.settings-row-label {
  font-size: 15px;
}

.settings-color-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

input[type="color"] {
  width: 44px;
  height: 32px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  cursor: pointer;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s;
}

.switch-slider::before {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.15s;
}

.switch input:checked + .switch-slider {
  background: var(--accent);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(20px);
}

.settings-section-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 20px 0 4px;
}

.settings-hint {
  font-size: 13px;
  color: var(--text-dim);
  margin: 0 0 10px;
}

.settings-backup-actions {
  display: flex;
  gap: 8px;
}

.settings-backup-actions .btn {
  flex: 1;
}

@media (min-width: 640px) {
  .topbar, .tabs, .view { padding-left: 0; padding-right: 0; }
  .topbar, .tabs { max-width: 640px; margin-left: auto; margin-right: auto; }
}
