:root {
  --green: #1f7a4d;
  --green-dark: #155c39;
  --green-soft: #dcefe3;
  --amber: #e9a23b;
  --bg: #f6f4ee;
  --card: #ffffff;
  --text: #1d2420;
  --muted: #6b746f;
  --line: #e4e1d8;
  --danger: #c0392b;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--green); text-decoration: none; }
h1, h2, h3 { margin: 0; }
img { max-width: 100%; display: block; }

#app {
  max-width: 560px;
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  box-shadow: 0 0 0 1px var(--line);
}
.loading { margin: auto; color: var(--muted); }

/* ---------- top bar & bottom nav ---------- */
.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--green);
  color: #fff;
  min-height: 56px;
}
.topbar .title { flex: 1; min-width: 0; }
.topbar .title a { color: inherit; }
.topbar h1 { font-size: 19px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar small { display: block; opacity: .85; font-size: 12px; }
.topbar .actions { display: flex; gap: 6px; }
.topbar .btn.small { background: rgba(255,255,255,.18); color: #fff; border: 0; }
.topbar-profile { display: flex; flex: none; align-items: center; gap: 5px; padding: 2px 7px 2px 2px; border-radius: 999px; background: rgba(255,255,255,.18); color: #fff; font-size: 12px; font-weight: 600; white-space: nowrap; }
.topbar-profile:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
main.fixed { overflow: hidden; display: flex; flex-direction: column; }

.bottomnav {
  flex: none;
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 7px 0 6px;
  color: var(--muted);
  font-size: 12px;
}
.bottomnav a.active { color: var(--green); font-weight: 600; }
.bottomnav .ic { font-size: 22px; position: relative; line-height: 1.2; }
.bottomnav .badge { position: absolute; top: -4px; right: -14px; }

/* ---------- common ---------- */
.pad { padding: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0; font-size: 14px; }
.section { margin-top: 24px; }
.section h3 { font-size: 16px; margin-bottom: 10px; }
.dashboard-page { display: flex; flex-direction: column; gap: 16px; padding: 14px; }
.dashboard-welcome { padding: 18px; }
.dashboard-welcome h2 { margin-top: 4px; font-size: 22px; }
.dashboard-welcome p { margin: 6px 0 0; }
.dashboard-section-title { margin-bottom: 9px; font-size: 16px; }
.dashboard-stats, .dashboard-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.dashboard-stat { display: flex; flex-direction: column; gap: 4px; padding: 14px; }
.dashboard-stat strong { color: var(--green); font-size: 24px; }
.dashboard-stat span { color: var(--muted); font-size: 12px; }
.dashboard-action { display: flex; min-width: 0; align-items: center; gap: 8px; padding: 12px; color: var(--text); }
.dashboard-action-icon { font-size: 22px; }
.dashboard-action strong { flex: 1; font-size: 13px; }
.dashboard-action > span:last-child { color: var(--muted); font-size: 20px; }
.community-page { display: flex; flex-direction: column; gap: 12px; padding: 14px; }
.community-intro { padding: 15px; }
.community-intro p { margin: 0; }
.community-intro p + p { margin-top: 8px; }
.community-page-action { display: flex; justify-content: flex-end; }
.community-posts { display: grid; gap: 10px; }
.community-post { min-width: 0; padding: 15px; }
.community-post h3 { font-size: 17px; overflow-wrap: anywhere; }
.community-post-meta { margin-top: 5px; }
.community-post-body { margin: 12px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.community-post-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 18px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:active { background: var(--green-dark); }
.btn.danger { color: var(--danger); border-color: #f0c8c3; }
.btn.small { padding: 6px 12px; font-size: 14px; }
.btn.block { display: flex; width: 100%; margin-top: 28px; }
.btn:disabled { opacity: .6; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.btn-row .btn { flex: 1; }

.icon-btn {
  border: 0;
  background: transparent;
  color: inherit;
  font-size: 24px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  flex: none;
}
.topbar .icon-btn { font-size: 32px; line-height: 1; margin-left: -6px; }

.badge {
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 6px;
  min-width: 18px;
  text-align: center;
}

.avatar {
  flex: none;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

.card { background: var(--card); border-radius: var(--radius); border: 1px solid var(--line); }

.empty { text-align: center; padding: 48px 24px; color: var(--muted); grid-column: 1 / -1; }
.empty .big { font-size: 44px; }
.empty h3 { color: var(--text); margin: 8px 0 4px; }
.empty .btn { margin-top: 16px; }

/* ---------- forms ---------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
input, select, textarea {
  font: inherit;
  font-weight: 400;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }

.photo-pick {
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  background: var(--card);
  min-height: 160px;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
  overflow: hidden;
  padding: 0;
}
.photo-pick img { width: 100%; max-height: 280px; object-fit: cover; }

/* ---------- auth ---------- */
.auth { padding: 32px 20px; overflow-y: auto; flex: 1; }
.brand { text-align: center; margin-bottom: 24px; }
.brand img { margin: 0 auto 12px; border-radius: 18px; }
.brand h1 { font-size: 30px; color: var(--green); }
.auth-card { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.auth-card[hidden] { display: none; }
.forgot-link { text-align: right; margin: -4px 2px 0; font-size: 14px; }

/* ---------- lists ---------- */
.list { list-style: none; margin: 0; padding: 0; background: var(--card); }
.list li + li { border-top: 1px solid var(--line); }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; color: var(--text); }
.row-top, .row-bottom { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.row-bottom { margin-top: 3px; font-size: 14px; }
.time { font-size: 12px; color: var(--muted); flex: none; }
.checklist input[type=checkbox] { width: 22px; height: 22px; flex: none; accent-color: var(--green); }
.checklist label { cursor: pointer; }

.toolbar { padding: 12px; display: flex; flex-direction: column; gap: 10px; background: var(--bg); position: sticky; top: 0; z-index: 1; }
.search { border-radius: 999px; padding-left: 40px; background: var(--card) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' fill='none' stroke='%236b746f' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='8' cy='8' r='6'/%3E%3Cpath d='M13 13l4 4'/%3E%3C/svg%3E") no-repeat 14px center; }
.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 14px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
}
.chip.active { background: var(--green); border-color: var(--green); color: #fff; }

/* ---------- marketplace ---------- */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; padding: 0 12px 16px; }
.section .grid { padding: 0; }
.listing { overflow: hidden; color: var(--text); display: flex; flex-direction: column; }
.thumb { aspect-ratio: 1; background: var(--green-soft); position: relative; display: flex; align-items: center; justify-content: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb-icon { font-size: 48px; }
.sold { position: absolute; top: 8px; left: 8px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 6px; }
.listing-body { padding: 8px 10px 10px; }
.price { color: var(--green); font-weight: 700; }
.price.big { font-size: 24px; }

.detail .hero { width: 100%; max-height: 360px; object-fit: cover; background: var(--green-soft); cursor: zoom-in; }
.detail .hero.placeholder { height: 220px; display: flex; align-items: center; justify-content: center; font-size: 80px; cursor: default; }
.detail h2 { font-size: 20px; margin: 4px 0; }
.desc { white-space: pre-wrap; line-height: 1.5; }
.seller { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-top: 16px; }

/* ---------- rides ---------- */
.rides-map-surface { position: relative; display: flex; flex: 1; min-height: 0; overflow: hidden; }
.rides-map-surface[hidden] { display: none; }
.rides-list-view { display: flex; flex: 1; flex-direction: column; min-height: 0; }
.rides-list-view[hidden], .apam-map-view[hidden] { display: none; }
.ride-search-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; padding: 10px 12px; background: var(--card); border-bottom: 1px solid var(--line); }
.ride-search-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; color: var(--muted); font-size: 12px; }
.ride-search-field input, .ride-search-field select { width: 100%; min-width: 0; min-height: 40px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--card); color: var(--text); font: inherit; font-size: 14px; }
.ride-search-form > .btn { min-height: 40px; }
.rides-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0 12px 16px; display: flex; flex-direction: column; gap: 10px; }
.apam-map-view { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #d9edf4; touch-action: none; user-select: none; cursor: grab; }
.apam-map-view.is-dragging { cursor: grabbing; }
.apam-map-image { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.apam-map-location { position: absolute; top: 12px; left: 12px; z-index: 1; display: flex; flex-direction: column; gap: 2px; padding: 9px 12px; border-radius: 12px; background: rgba(255,255,255,.94); box-shadow: 0 2px 8px rgba(0,0,0,.12); }
.apam-map-location strong { font-size: 15px; }
.apam-map-location span { color: var(--muted); font-size: 12px; }
.apam-map-controls { position: absolute; top: 64px; right: 12px; z-index: 2; display: flex; flex-direction: column; gap: 8px; }
.apam-map-controls .map-control { width: 44px; height: 44px; padding: 0; border-radius: 12px; background: rgba(255,255,255,.96); font-size: 23px; line-height: 1; box-shadow: 0 2px 8px rgba(0,0,0,.12); }
.apam-map-controls .map-center { font-size: 20px; }
.apam-map-online { position: absolute; top: 12px; right: 12px; z-index: 2; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.16); }
.apam-map-attribution { position: absolute; bottom: 8px; left: 8px; z-index: 1; padding: 4px 7px; border-radius: 5px; background: rgba(255,255,255,.9); color: #34433b; font-size: 10px; }
.apam-map-attribution a { color: inherit; text-decoration: underline; }
.online-map-view { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #e7ece8; }
.online-map-view[hidden] { display: none; }
.online-map-canvas { position: absolute; inset: 0; z-index: 0; }
.online-map-search-panel { position: absolute; top: 12px; left: 12px; right: 102px; z-index: 1000; max-width: 380px; }
.online-map-search { display: flex; gap: 6px; padding: 6px; border-radius: 12px; background: rgba(255,255,255,.97); box-shadow: 0 2px 8px rgba(0,0,0,.16); }
.online-map-search input { flex: 1; min-width: 0; padding: 8px; border: 0; outline: 0; font: inherit; }
.online-map-search .btn { flex: none; padding: 7px 12px; }
.online-map-results { max-height: 38dvh; overflow-y: auto; margin-top: 6px; border-radius: 10px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.16); }
.online-map-results[hidden] { display: none; }
.online-map-result { display: block; width: 100%; padding: 10px 12px; border: 0; border-bottom: 1px solid var(--line); background: #fff; color: var(--text); text-align: left; font: inherit; cursor: pointer; }
.online-map-result:last-child { border-bottom: 0; }
.online-map-result small { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; }
.online-map-mode { position: absolute; top: 12px; right: 12px; z-index: 1000; padding: 8px 10px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.16); }
.online-map-view .leaflet-top.leaflet-right { top: 58px; }
.online-map-locate { position: absolute; bottom: 34px; left: 12px; z-index: 1000; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.16); }
.ride-card { color: var(--text); padding: 14px; display: block; }
.ride-kind { color: var(--green); font-size: 13px; font-weight: 700; margin-bottom: 10px; }
.ride-route { display: grid; grid-template-columns: 12px minmax(0, 1fr); align-items: center; gap: 5px 8px; }
.ride-route strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ride-route > span:first-child, .ride-route > span:nth-last-child(2) { color: var(--green); font-size: 10px; }
.route-line { grid-column: 1; width: 2px; height: 24px; background: var(--line); justify-self: center; }
.ride-meta { display: flex; align-items: center; gap: 8px; margin-top: 12px; color: var(--muted); font-size: 12px; }
.ride-meta span:last-child { margin-left: auto; }
.ride-status { border-radius: 999px; background: var(--green-soft); color: var(--green); padding: 4px 8px; font-weight: 700; }
.ride-status.accepted { background: #fff3d6; color: #946200; }
.ride-status.cancelled, .ride-status.completed { background: #eee; color: var(--muted); }
.ride-route-card { padding: 14px; }
.ride-owner { padding: 8px 0 14px; }
.detail-route { padding: 10px 0 2px; }
.ride-facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0; }
.ride-facts div { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; gap: 4px; }
.matched-box { background: var(--green-soft); border-radius: var(--radius); padding: 12px; display: flex; align-items: center; justify-content: space-between; margin-top: 12px; }

/* ---------- profile ---------- */
.profile { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px; margin-bottom: 20px; }
.profile h2 { margin-top: 6px; }
.profile .btn-row { width: 100%; }
.profile .section { width: 100%; text-align: left; }
.admin-account { padding: 14px; border-bottom: 1px solid var(--line); }
.admin-account:last-child { border-bottom: 0; }
.admin-pin-result { margin: 8px 0 0; color: var(--green-dark); font-size: 13px; font-weight: 600; overflow-wrap: anywhere; }
.avatar-edit { position: relative; border: 0; background: none; padding: 0; cursor: pointer; }
.avatar-badge { position: absolute; right: 0; bottom: 0; background: var(--card); border-radius: 50%; width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; box-shadow: 0 1px 4px rgba(0,0,0,.2); }

/* ---------- chat ---------- */
.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 4px; background: #ece8dd; }
.day { align-self: center; background: rgba(255,255,255,.75); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 999px; margin: 10px 0 6px; }
.msg { max-width: 78%; padding: 7px 10px 5px; border-radius: 14px; background: var(--card); align-self: flex-start; box-shadow: 0 1px 1px rgba(0,0,0,.06); }
.msg.mine { align-self: flex-end; background: #d4f0dc; border-bottom-right-radius: 4px; }
.msg.theirs { border-bottom-left-radius: 4px; }
.msg-name { font-size: 12px; font-weight: 700; color: var(--green); margin-bottom: 2px; }
.msg-text { white-space: pre-wrap; word-wrap: break-word; line-height: 1.35; }
.msg-img { border-radius: 10px; max-height: 300px; min-height: 80px; cursor: zoom-in; background: var(--line); }
.msg-time { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.load-earlier { align-self: center; margin-bottom: 8px; }

.composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
}
.composer textarea { resize: none; border-radius: 20px; padding: 9px 14px; max-height: 120px; line-height: 1.35; }
.send-btn { flex: none; width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--green); color: #fff; font-size: 18px; cursor: pointer; }

/* ---------- overlays ---------- */
.viewer { position: fixed; inset: 0; background: rgba(0,0,0,.92); display: flex; align-items: center; justify-content: center; z-index: 50; cursor: zoom-out; }
.viewer img { max-width: 100%; max-height: 100%; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: #222; color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 14px; z-index: 60; max-width: 90vw; text-align: center; }
