:root {
  --blue: #0b4f8a;
  --bg: #f1f5f9;
  --card: #fff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --red: #dc2626;
  --orange: #ea580c;
  --green: #16a34a;
  --errand: #2563eb;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: 'Sarabun', system-ui, sans-serif; color: var(--text); background: var(--bg); font-size: 16px; overflow: hidden; }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; }
.hidden { display: none !important; }

.topbar { position: fixed; inset: 0 0 auto 0; height: 52px; z-index: 1000; background: var(--blue); color: #fff; display: flex; align-items: center; justify-content: space-between; padding: 0 12px; gap: 8px; }
.brand { font-weight: 700; font-size: 18px; white-space: nowrap; }
.demo-banner { position: fixed; top: 52px; inset-inline: 0; z-index: 999; background: #fef3c7; color: #92400e; font-size: 12px; text-align: center; padding: 2px 8px; }
.user-chip { display: flex; align-items: center; gap: 6px; background: rgba(255,255,255,.15); border: 0; color: #fff; border-radius: 999px; padding: 4px 10px 4px 4px; max-width: 60vw; }
.user-chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: #fff; color: var(--blue); display: grid; place-items: center; font-weight: 700; flex: none; }
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; color: #fff; background: #64748b; white-space: nowrap; }
.badge.banned { background: var(--red); }
.badge.admin { background: #111827; }

#map { position: fixed; top: 74px; bottom: 96px; inset-inline: 0; }
.filters { position: fixed; top: 80px; left: 8px; right: 8px; z-index: 900; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 4px 12px; white-space: nowrap; font-size: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.chip.off { opacity: .45; text-decoration: line-through; }
.legend-btn { position: fixed; right: 8px; bottom: 106px; z-index: 900; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.2); }

.actions { position: fixed; bottom: 0; inset-inline: 0; height: 96px; z-index: 1000; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); background: #fff; border-top: 1px solid var(--line); }
.big { border: 0; border-radius: 14px; color: #fff; font-weight: 700; font-size: 14px; line-height: 1.2; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.big span { font-size: 22px; }
.big.water { background: #0284c7; }
.big.help { background: var(--red); }
.big.errand { background: var(--errand); }

.btn { border: 0; border-radius: 10px; padding: 10px 14px; font-weight: 600; }
.btn.primary { background: var(--blue); color: #fff; }
.btn.danger { background: var(--red); color: #fff; }
.btn.ghost { background: #e2e8f0; color: var(--text); }
.btn.green { background: var(--green); color: #fff; }
.btn.sm { padding: 5px 10px; font-size: 14px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row > .grow { flex: 1; }

/* โหมดเลือกตำแหน่ง */
.crosshair { position: fixed; z-index: 950; left: 50%; top: calc(74px + (100vh - 170px) / 2); transform: translate(-50%, -100%); font-size: 36px; pointer-events: none; filter: drop-shadow(0 2px 2px rgba(0,0,0,.4)); }
.pickbar { position: fixed; bottom: 0; inset-inline: 0; z-index: 1100; background: #fff; padding: 12px 12px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.pickbar .row { justify-content: flex-end; }
.pick-hint { font-weight: 600; margin-bottom: 8px; }

/* แผ่นเลื่อนขึ้น */
.sheet-wrap { position: fixed; inset: 0; z-index: 2000; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(15,23,42,.45); }
.sheet { position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); width: min(640px, 100%); max-height: 90vh; overflow-y: auto; background: var(--card); border-radius: 18px 18px 0 0; padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); }
.sheet.wide { width: min(980px, 100%); }
.sheet h2 { margin: 0 0 12px; font-size: 20px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sheet h3 { margin: 16px 0 8px; font-size: 16px; }
.x { border: 0; background: #e2e8f0; border-radius: 50%; width: 32px; height: 32px; font-size: 18px; line-height: 1; flex: none; }
label.field { display: block; margin: 10px 0; }
label.field > span { display: block; font-weight: 600; margin-bottom: 4px; }
.input { width: 100%; border: 1px solid #cbd5e1; border-radius: 10px; padding: 10px; background: #fff; }
textarea.input { min-height: 70px; resize: vertical; }
.muted { color: var(--muted); font-size: 14px; }
.err { color: var(--red); font-weight: 600; min-height: 1em; }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab { flex: 1; border: 2px solid var(--line); background: #fff; border-radius: 10px; padding: 8px; font-weight: 600; }
.tab.on { border-color: var(--blue); background: #e0f2fe; }

.levels { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.level { border: 3px solid transparent; background: #f8fafc; border-radius: 12px; padding: 6px 2px; display: flex; flex-direction: column; align-items: center; font-size: 14px; font-weight: 600; }
.level small { font-weight: 400; color: var(--muted); font-size: 11px; }
.level.on, .opt.on { border-color: var(--blue); background: #e0f2fe; }
.opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 6px; }
.opt { border: 3px solid var(--line); background: #fff; border-radius: 12px; padding: 10px 6px; font-weight: 600; }

.photos { display: flex; gap: 6px; flex-wrap: wrap; }
.thumb { position: relative; width: 72px; height: 72px; border-radius: 8px; overflow: hidden; background: #e2e8f0; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb button { position: absolute; top: 2px; right: 2px; border: 0; border-radius: 50%; width: 22px; height: 22px; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; padding: 0; }
.addphoto { width: 72px; height: 72px; border: 2px dashed #94a3b8; border-radius: 8px; display: grid; place-items: center; color: var(--muted); font-size: 13px; text-align: center; cursor: pointer; }

/* หมุด */
.pin { border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 13px; }
.pin.square { border-radius: 6px; }
.pin.old { opacity: .35; }
.pin.done { filter: grayscale(.8); opacity: .6; }
.leaflet-popup-content { margin: 12px 14px; font-family: 'Sarabun', system-ui, sans-serif; font-size: 15px; min-width: 220px; }
.pop h4 { margin: 0 0 4px; font-size: 16px; }
.pop .meta { color: var(--muted); font-size: 13px; }
.pop p { margin: 6px 0; }
.pop .photos .thumb { width: 60px; height: 60px; }
.pop .row { margin-top: 8px; }
.status { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 13px; font-weight: 600; }
.st-waiting { background: #fee2e2; color: #991b1b; }
.st-accepted { background: #fef3c7; color: #92400e; }
.st-done, .st-delivered { background: #dcfce7; color: #166534; }
.history { border-left: 3px solid var(--line); padding-left: 8px; margin-top: 8px; font-size: 13px; }

/* แอดมิน */
.list { display: flex; flex-direction: column; gap: 8px; }
.card { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.card .title { font-weight: 700; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 4px 12px; margin: 6px 0; }
.perm-grid label { display: flex; gap: 6px; align-items: flex-start; }

.legend-row { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
.toast { position: fixed; left: 50%; bottom: 110px; transform: translateX(-50%); z-index: 3000; background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; max-width: 90vw; text-align: center; }
.toast.bad { background: var(--red); }

@media (min-width: 720px) {
  .actions { left: 50%; transform: translateX(-50%); width: 640px; border-radius: 16px 16px 0 0; border: 1px solid var(--line); }
  #map { bottom: 0; }
  .legend-btn { bottom: 16px; }
}
