:root {
  --green-dark: #2f5233;
  --green: #4a7c59;
  --green-light: #e7f0e5;
  --orange: #e0793e;
  --bg: #faf8f4;
  --card-bg: #ffffff;
  --text: #23291f;
  --text-muted: #6b7568;
  --border: #e4e1d8;
  --danger: #c0453a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14170f;
    --card-bg: #1e2318;
    --text: #ecefe7;
    --text-muted: #9aa393;
    --border: #313828;
    --green-light: #22301f;
  }
}

* { box-sizing: border-box; }

/* An attribute selector and a class selector tie on specificity, so without
   this, any class that sets `display` (e.g. .backup-actions{display:flex})
   can beat the UA's [hidden]{display:none} and the element stays visible. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", Helvetica, Arial, sans-serif;
  height: 100%;
  overscroll-behavior-y: none;
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  padding: calc(14px + var(--safe-top)) 18px 14px;
  background: var(--green-dark);
  color: #fff;
  text-align: center;
  flex-shrink: 0;
}
.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

#views {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.view {
  display: none;
  position: absolute;
  inset: 0;
  overflow-y: auto;
  /* Without this, setting only overflow-y computes overflow-x to `auto` too
     (per spec), so any child rendering even 1px wider than the view (iOS's
     date input, most notably) silently turns into its own horizontal
     scroll container instead of just being clipped. */
  overflow-x: hidden;
  padding: 16px 16px calc(24px + var(--safe-bottom));
  -webkit-overflow-scrolling: touch;
}
.view.active { display: block; }

#view-map { padding: 0; }
#view-map.active { display: flex; flex-direction: column; }
#map { flex: 1; width: 100%; }

/* Form */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 600;
}
.field input, .field select, .field textarea {
  font-size: 1rem;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card-bg);
  color: var(--text);
  font-weight: 400;
  width: 100%;
  /* Without this, iOS Safari renders native pickers (date, select) in light
     mode regardless of our own colors, leaving a stray white box. */
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  .field input, .field select, .field textarea { color-scheme: dark; }
}
.date-select-row {
  display: flex;
  gap: 8px;
}
/* Plain <select> elements, unlike <input type="date">, have no internal
   shadow-DOM minimum width on iOS — these sizes are honored exactly. */
.date-select-row select#f-date-month { flex: 1.3; min-width: 0; }
.date-select-row select#f-date-day { flex: 0.8; min-width: 0; }
.date-select-row select#f-date-year { flex: 1; min-width: 0; }

.field-row {
  display: flex;
  gap: 12px;
}
.field-row .field { flex: 1; min-width: 0; }

.star-picker {
  display: flex;
  gap: 4px;
}
.star-picker .star {
  font-size: 2rem;
  line-height: 1;
  background: none;
  border: none;
  color: var(--border);
  cursor: pointer;
  padding: 2px;
}
.star-picker .star.filled { color: var(--orange); }

.photo-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.photo-preview .thumb {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--green-light);
}
.photo-preview .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-preview .thumb .remove {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 12px;
  line-height: 20px;
  text-align: center;
  padding: 0;
}

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}
.form-status {
  font-size: 0.85rem;
  color: var(--green);
  min-height: 1.2em;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 13px 20px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
}
.btn-primary { background: var(--green-dark); color: #fff; }
.btn-secondary { background: var(--green-light); color: var(--green-dark); }
.btn-ghost { background: transparent; color: var(--text-muted); flex: 0 0 auto; }
.btn-danger { background: #fbe9e7; color: var(--danger); }
@media (prefers-color-scheme: dark) {
  .btn-secondary { color: #8fc99b; }
  .btn-danger { background: #3a201d; color: #e59189; }
}

/* Filter bar */
.filter-bar {
  margin-bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.filter-bar input[type="search"] {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  font-size: 0.95rem;
}
.filter-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}
.filter-row select {
  flex: 0 0 auto;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  font-size: 0.85rem;
  max-width: 45vw;
}

/* Hike list */
.hike-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.hike-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  cursor: pointer;
}
.hike-card h3 {
  margin: 0 0 4px;
  font-size: 1.05rem;
  font-weight: 700;
}
.hike-card .meta {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.hike-card .stars {
  color: var(--orange);
  letter-spacing: 1px;
  font-size: 0.95rem;
}
.hike-card .thumbs {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  overflow-x: auto;
}
.hike-card .thumbs img {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  margin-top: 40px;
  font-size: 0.95rem;
  padding: 0 20px;
}

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 8px;
}
.stat-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  text-align: center;
}
.stat-card .value {
  font-size: 1.7rem;
  font-weight: 800;
  color: var(--green-dark);
}
@media (prefers-color-scheme: dark) {
  .stat-card .value { color: #8fc99b; }
}
.stat-card .label {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.section-title {
  font-size: 1rem;
  margin: 22px 0 10px;
}

.rating-breakdown {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.rating-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
}
.rating-row .stars { color: var(--orange); width: 90px; flex-shrink: 0; }
.rating-row .bar-track {
  flex: 1;
  background: var(--border);
  border-radius: 6px;
  height: 10px;
  overflow: hidden;
}
.rating-row .bar-fill {
  background: var(--orange);
  height: 100%;
}
.rating-row .count { width: 24px; text-align: right; color: var(--text-muted); }

.highlight-breakdown {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.highlight-chip {
  background: var(--green-light);
  color: var(--green-dark);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.82rem;
  font-weight: 600;
}
@media (prefers-color-scheme: dark) {
  .highlight-chip { color: #8fc99b; }
}

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

.sync-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--green-light);
  color: var(--green-dark);
  margin-bottom: 12px;
}
@media (prefers-color-scheme: dark) { .sync-status { color: #8fc99b; } }
.sync-status.error { background: #fbe9e7; color: var(--danger); }
@media (prefers-color-scheme: dark) {
  .sync-status.error { background: #3a201d; color: #e59189; }
}
#sync-connect-form { margin-top: 4px; }
.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 10px;
  line-height: 1.4;
}

/* Tab bar */
.tab-bar {
  display: flex;
  border-top: 1px solid var(--border);
  background: var(--card-bg);
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  color: var(--text-muted);
  cursor: pointer;
}
.tab-btn.active { color: var(--green-dark); }
@media (prefers-color-scheme: dark) {
  .tab-btn.active { color: #8fc99b; }
}
.tab-icon { font-size: 1.4rem; line-height: 1; }
.tab-label { font-size: 0.68rem; font-weight: 600; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
}
.modal[hidden] { display: none; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
}
.modal-sheet {
  position: relative;
  background: var(--bg);
  width: 100%;
  max-height: 86vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 20px 18px calc(20px + var(--safe-bottom));
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--border);
  border: none;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  font-size: 14px;
  cursor: pointer;
}
.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}
#modal-body h2 { margin: 4px 30px 6px 0; }
#modal-body .meta-line { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 4px; }
#modal-body .stars { color: var(--orange); font-size: 1.1rem; margin: 6px 0; }
#modal-body .photos {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
#modal-body .photos img {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 10px;
}

/* Map popup */
.leaflet-popup-content h3 { margin: 0 0 4px; font-size: 0.95rem; }
.leaflet-popup-content .meta { font-size: 0.8rem; color: #666; margin-bottom: 6px; }
.leaflet-popup-content button {
  background: var(--green-dark);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.8rem;
  cursor: pointer;
}

.pushpin-marker {
  background: transparent;
  border: none;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(84px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.85rem;
  z-index: 2000;
  max-width: 90vw;
  text-align: center;
}
.toast[hidden] { display: none; }
