/* ── CSS Variables ──────────────────────────────────────────────────────── */
:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --surface2: #f0f0f0;
  --border: #e0e0e0;
  --text: #1a1a1a;
  --text-muted: #666666;
  --primary: #e84040;
  --primary-dark: #c72020;
  --accent: #f97316;
  --progress-track: #e0e0e0;
  --progress-fill: linear-gradient(90deg, #e84040, #f97316);
  --header-bg: #ffffff;
  --header-shadow: 0 2px 8px rgba(0,0,0,0.08);
  --nav-bg: #ffffff;
  --nav-shadow: 0 -2px 8px rgba(0,0,0,0.08);
  --modal-overlay: rgba(0,0,0,0.5);
  --card-shadow: 0 2px 12px rgba(0,0,0,0.06);
  --runner-shadow: drop-shadow(0 2px 4px rgba(0,0,0,0.4));
  --link: #2563eb;
}

[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface2: #2a2a2a;
  --border: #333333;
  --text: #f0f0f0;
  --text-muted: #999999;
  --primary: #ff5555;
  --primary-dark: #e03030;
  --accent: #fb923c;
  --progress-track: #333333;
  --header-bg: #1e1e1e;
  --header-shadow: 0 2px 8px rgba(0,0,0,0.4);
  --nav-bg: #1e1e1e;
  --nav-shadow: 0 -2px 8px rgba(0,0,0,0.4);
  --modal-overlay: rgba(0,0,0,0.75);
  --card-shadow: 0 2px 12px rgba(0,0,0,0.3);
  --link: #60a5fa;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 12px;
  background: var(--header-bg);
  box-shadow: var(--header-shadow);
  position: relative;
  z-index: 100;
  flex-shrink: 0;
}

.app-title {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.3px;
}

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

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 6px;
  border-radius: 8px;
  transition: background 0.15s;
  color: var(--text);
  line-height: 1;
}
.icon-btn:hover { background: var(--surface2); }

.weather-widget {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--surface2);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: default;
}
#weather-icon { font-size: 1rem; }

/* ── Progress Section ───────────────────────────────────────────────────── */
.progress-section {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px 10px;
  flex-shrink: 0;
  position: relative;
  z-index: 90;
}

.progress-bar-wrap {
  position: relative;
  margin-bottom: 6px;
  padding-bottom: 26px; /* space for next-city tick label */
}

.progress-flag {
  position: absolute;
  top: 5px; /* center of the 10px bar (not 50% of the taller wrap) */
  transform: translateY(-50%);
  font-size: 1rem;
  z-index: 2;
}
.start-flag { left: -4px; }
.end-flag { right: -4px; }

.progress-bar-track {
  height: 10px;
  background: var(--progress-track);
  border-radius: 8px;
  overflow: hidden;
  margin: 0 18px;
}

.progress-bar-fill {
  height: 100%;
  background: var(--progress-fill);
  border-radius: 8px;
  transition: width 0.8s cubic-bezier(0.4, 0, 0.2, 1);
}

.progress-stats-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0 0;
}
.progress-stats {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0;
}
.pstat {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 10px;
  gap: 1px;
}
.unit-toggle-btn {
  flex-shrink: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  line-height: 1;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.unit-toggle-btn:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.pstat-value {
  font-size: 1rem;
  font-weight: 800;
  color: var(--text);
  line-height: 1.2;
  letter-spacing: -0.3px;
}
.pstat-unit {
  font-size: 0.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
}
.pstat-accent .pstat-value { color: var(--primary); }
.pstat-sub {
  font-size: 0.6rem;
  color: var(--text-muted);
  font-weight: 500;
  margin-top: 1px;
}
.pstat-divider {
  width: 1px;
  height: 26px;
  background: var(--border);
  flex-shrink: 0;
}

/* ── City tick marks ────────────────────────────────────────────────────── */
.city-ticks-layer {
  position: absolute;
  left: 18px;
  right: 18px;
  top: -4px;
  height: 18px; /* 4px above + 10px bar + 4px below */
  pointer-events: none;
  overflow: visible;
}
.city-tick {
  position: absolute;
  top: 0;
  height: 100%;
  width: 2px;
  transform: translateX(-50%);
  background: var(--text-muted);
  border-radius: 1px;
  opacity: 0.3;
}
.city-tick-passed {
  background: #fff;
  opacity: 0.5;
}
.city-tick-next {
  background: var(--primary);
  opacity: 1;
  width: 2px;
}
.city-tick-label {
  position: absolute;
  top: calc(100% + 3px);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  pointer-events: none;
}
.city-tick-name {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}
.city-tick-dist {
  font-size: 0.64rem;
  font-weight: 500;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── Layout ─────────────────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

body {
  display: flex;
  flex-direction: column;
}

.view { display: none; height: 100%; }
.view.active { display: flex; flex-direction: column; }

/* ── Map ────────────────────────────────────────────────────────────────── */
#map {
  flex: 1;
  width: 100%;
  height: 100%;
  z-index: 1;
}

/* ── Details View ───────────────────────────────────────────────────────── */
.details-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

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

.section-title {
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 12px;
  color: var(--text);
}

/* City card */
.city-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.city-name { font-size: 1.3rem; font-weight: 800; }
.city-prefecture { font-size: 0.8rem; color: var(--text-muted); }
.city-wiki-link {
  display: inline-block;
  margin-top: 4px;
  font-size: 0.78rem;
  color: var(--link);
  text-decoration: none;
}
.city-wiki-link:hover { text-decoration: underline; }

.city-stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.city-stat {
  background: var(--surface2);
  border-radius: 10px;
  padding: 8px 10px;
}
.city-stat-label { font-size: 0.7rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.city-stat-value { font-size: 0.9rem; font-weight: 600; margin-top: 2px; }

.city-section { margin-top: 10px; }
.city-section h4 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); margin-bottom: 6px; }
.city-list { list-style: none; display: flex; flex-direction: column; gap: 4px; }
.city-list li { font-size: 0.88rem; padding: 4px 0; border-bottom: 1px solid var(--border); }
.city-list li:last-child { border-bottom: none; }
.city-list a { color: var(--link); text-decoration: none; }
.city-list a:hover { text-decoration: underline; }
.pill-tag { display: inline-block; background: var(--surface2); border-radius: 20px; padding: 2px 8px; font-size: 0.78rem; margin: 2px; }
a.pill-tag { color: var(--text); text-decoration: none; transition: background 0.15s; border: 1px solid transparent; }
a.pill-tag:hover { background: var(--border); text-decoration: underline; }

/* Food tags */
.food-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

/* Jogs list */
.jog-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.jog-item:last-child { border-bottom: none; }
.jog-date { font-size: 0.82rem; color: var(--text-muted); }
.jog-dist { font-size: 1rem; font-weight: 700; color: var(--primary); }
.jog-dist-mi { font-size: 0.75rem; color: var(--text-muted); margin-left: 4px; }
.jog-notes { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }

/* Comments list */
.comment-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.comment-item:last-child { border-bottom: none; }
.comment-author { font-size: 0.82rem; font-weight: 600; }
.comment-text { font-size: 0.9rem; margin-top: 2px; line-height: 1.4; }
.comment-time { font-size: 0.72rem; color: var(--text-muted); margin-top: 4px; }

/* Comment form */
.comment-form-card input,
.comment-form-card textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  color: var(--text);
  margin-bottom: 10px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}
.comment-form-card input:focus,
.comment-form-card textarea:focus { border-color: var(--primary); }
.comment-form-card textarea { resize: vertical; }

/* ── Bottom Nav ─────────────────────────────────────────────────────────── */
.bottom-nav {
  display: flex;
  background: var(--nav-bg);
  box-shadow: var(--nav-shadow);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 100;
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 0 10px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  transition: color 0.15s;
  gap: 2px;
}
.nav-btn:active { background: var(--surface2); }
.nav-btn.active { color: var(--primary); }
.nav-icon { font-size: 1.4rem; line-height: 1; }
.nav-label { font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn-primary {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 20px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  font-family: inherit;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:active { transform: scale(0.98); }

.btn-secondary {
  display: inline-block;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  font-family: inherit;
}
.btn-secondary:hover { background: var(--border); }
.btn-sm { padding: 7px 12px; font-size: 0.8rem; }

/* ── Modals ─────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  animation: fadeIn 0.15s ease;
}
.modal-overlay.hidden { display: none; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.modal-box {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  max-height: 92vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 24px 20px;
  animation: slideUp 0.2s ease;
  -webkit-overflow-scrolling: touch;
}
.modal-box-wide { max-width: 600px; }

.modal-box h2 { font-size: 1.2rem; margin-bottom: 16px; }
.modal-box h3 { font-size: 1rem; margin-bottom: 10px; margin-top: 4px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}
.modal-header h2 { margin-bottom: 0; }
.admin-header-btns { display: flex; gap: 8px; align-items: center; }

.btn-icon-close {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  color: var(--text-muted);
}
.btn-icon-close:hover { background: var(--surface2); }

.modal-box label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 5px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.modal-box input[type="text"],
.modal-box input[type="password"],
.modal-box input[type="number"],
.modal-box input[type="date"] {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.95rem;
  color: var(--text);
  margin-bottom: 14px;
  font-family: inherit;
  outline: none;
}
.modal-box input:focus { border-color: var(--primary); }

.form-row { display: flex; gap: 12px; }
.form-group { flex: 1; }
.form-group label { display: block; font-size: 0.82rem; font-weight: 600; color: var(--text-muted); margin-bottom: 5px; text-transform: uppercase; letter-spacing: 0.4px; }
.form-group input { width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font-size: 0.95rem; color: var(--text); margin-bottom: 14px; font-family: inherit; outline: none; }
.form-group input:focus { border-color: var(--primary); }

.modal-actions { display: flex; gap: 10px; margin-top: 4px; }
.modal-actions .btn-primary, .modal-actions .btn-secondary { flex: 1; text-align: center; }

.error-msg { color: #ef4444; font-size: 0.85rem; margin-bottom: 10px; }
.error-msg.hidden { display: none; }
.status-msg { font-size: 0.85rem; color: var(--text-muted); margin-left: 10px; }

/* ── Admin jog list ─────────────────────────────────────────────────────── */
.admin-jog-list { display: flex; flex-direction: column; gap: 8px; max-height: 300px; overflow-y: auto; }
.admin-jog-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface2);
  border-radius: 10px;
  padding: 10px 12px;
  gap: 10px;
}
.admin-jog-info { flex: 1; }
.admin-jog-info .jog-date { font-size: 0.78rem; color: var(--text-muted); }
.admin-jog-info .jog-dist { font-size: 0.95rem; font-weight: 700; color: var(--primary); }
.admin-jog-info .jog-notes { font-size: 0.78rem; color: var(--text-muted); margin-top: 2px; }
.admin-jog-actions { display: flex; gap: 6px; }
.btn-edit, .btn-delete {
  border: none;
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 0.78rem;
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
}
.btn-edit { background: var(--border); color: var(--text); }
.btn-edit:hover { background: #d0d0d0; }
.btn-delete { background: #fee2e2; color: #ef4444; }
.btn-delete:hover { background: #fecaca; }
[data-theme="dark"] .btn-edit { background: #3a3a3a; }
[data-theme="dark"] .btn-delete { background: #3a1010; color: #ff7070; }

/* ── Map custom markers ─────────────────────────────────────────────────── */
.runner-marker {
  filter: var(--runner-shadow);
  animation: bounce 1.5s infinite;
  display: block;
}
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

/* Pulsing comment circle marker */
.comment-pulse-wrap {
  position: relative;
  width: 22px;
  height: 22px;
  cursor: pointer;
}
.comment-pulse-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 10px;
  height: 10px;
  background: var(--accent);
  border-radius: 50%;
  border: 2px solid #fff;
  transform: translate(-50%, -50%);
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pulse-count {
  position: absolute;
  top: -8px;
  left: 6px;
  background: var(--primary);
  color: #fff;
  font-size: 0.55rem;
  font-weight: 800;
  border-radius: 8px;
  padding: 0 3px;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
}
.popup-group-count {
  font-weight: 700;
  font-size: 0.85rem;
  margin-bottom: 8px;
  color: var(--primary);
}
.popup-group-item { padding: 4px 0; }
.popup-divider { border: none; border-top: 1px solid var(--border); margin: 6px 0; }
.comment-pulse-ring {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  border: 2px solid var(--accent);
  border-radius: 50%;
  transform: translate(-50%, -50%) scale(0.3);
  opacity: 0;
  animation: comment-pulse 2.6s ease-out infinite;
  z-index: 1;
}
.comment-pulse-ring:nth-child(2) { animation-delay: 1.0s; }

@keyframes comment-pulse {
  0%   { transform: translate(-50%, -50%) scale(0.3); opacity: 0.9; }
  100% { transform: translate(-50%, -50%) scale(2.8); opacity: 0; }
}

/* ── City hero image ────────────────────────────────────────────────────── */
.city-card { padding-top: 0; }

.city-hero {
  margin: 0 -16px 14px;
  height: 160px;
  border-radius: 12px 12px 0 0;
  overflow: hidden;
  position: relative;
  background: var(--surface2);
}
.city-hero-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  opacity: 1;
}
.city-hero-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    to top,
    rgba(0,0,0,0.78) 0%,
    rgba(0,0,0,0.28) 55%,
    rgba(0,0,0,0.08) 100%
  );
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 14px 16px;
}
.city-hero-label {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: rgba(255,255,255,0.75);
  margin-bottom: 3px;
}
.city-hero-name {
  font-size: 1.65rem;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 1px 6px rgba(0,0,0,0.5);
  line-height: 1.1;
}
.city-hero-pref {
  font-size: 0.78rem;
  color: rgba(255,255,255,0.78);
  margin-top: 3px;
}
.city-body { padding-top: 2px; }

/* ── Leaflet popup custom style ─────────────────────────────────────────── */
.leaflet-popup-content-wrapper {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-radius: 12px !important;
  box-shadow: 0 4px 20px rgba(0,0,0,0.2) !important;
  border: 1px solid var(--border) !important;
}
.leaflet-popup-tip-container {
  height: 22px !important;
  width: 22px !important;
  position: relative;
}
.leaflet-popup-tip-container::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 11px;
  background: var(--surface);
}
.leaflet-popup-tip { background: var(--surface) !important; }
.leaflet-popup-content { font-size: 0.85rem !important; margin: 10px 14px !important; }
.popup-comment-name { font-weight: 700; font-size: 0.9rem; }
.popup-comment-text { margin-top: 4px; line-height: 1.4; }
.popup-comment-time { color: var(--text-muted); font-size: 0.72rem; margin-top: 6px; }
.popup-comment-km { color: var(--text-muted); font-size: 0.72rem; }

/* ── Utility ────────────────────────────────────────────────────────────── */
.loading-text { color: var(--text-muted); font-size: 0.85rem; }
.empty-text { color: var(--text-muted); font-size: 0.85rem; font-style: italic; }

a { color: var(--link); }

/* ── Scrollbar (desktop) ────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Desktop layout ─────────────────────────────────────────────────────── */
@media (min-width: 600px) {
  .modal-overlay { align-items: center; padding: 20px; }
  .modal-box { border-radius: 20px; max-height: 85vh; }
  .app-title { font-size: 1.1rem; }
}
