:root {
  --red: #e53935;
  --red-dark: #b71c1c;
  --red-light: #fdecea;
  --orange: #ff6f00;
  --grey: #757575;
  --bg: #f5f5f5;
  --card: #fff;
  --border: #e0e0e0;
  --text: #212121;
  --sub: #757575;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--card);
  display: flex;
  flex-direction: column;
  box-shadow: 0 0 20px rgba(0, 0, 0, .08);
}

.page { flex: 1; display: none; flex-direction: column; min-height: 0; }
.page.active { display: flex; }

/* ─── 導覽列 ─── */
#nav {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  padding: 0;
  position: sticky;
  top: 0;
  z-index: 100;
  padding-top: var(--safe-top);
}

#nav a {
  flex: 1;
  padding: 12px 6px;
  text-align: center;
  font-size: clamp(11px, 2.8vw, 13px);
  font-weight: 600;
  color: var(--sub);
  text-decoration: none;
  border-bottom: 3px solid transparent;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#nav a.active,
#nav a:hover { color: var(--red); border-bottom-color: var(--red); }

/* ─── 頁首 ─── */
.page-header {
  padding: 14px 16px 12px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.page-header h1 { font-size: clamp(17px, 4.5vw, 20px); font-weight: 700; flex: 1; min-width: 0; }

/* ─── 按鈕 ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  min-height: 44px;
  touch-action: manipulation;
}

.btn-primary { background: var(--red); color: #fff; }
.btn-primary:hover { background: var(--red-dark); }
.btn-secondary { background: transparent; color: var(--red); border: 1.5px solid var(--red); }
.btn-orange { background: var(--orange); color: #fff; }
.btn-grey { background: var(--grey); color: #fff; }
.btn-sm { padding: 8px 14px; font-size: 13px; min-height: 40px; }
.btn-full { width: 100%; }

.btn-sos {
  background: var(--red);
  color: #fff;
  font-size: clamp(17px, 4.5vw, 19px);
  font-weight: 800;
  padding: 18px;
  border-radius: 12px;
  width: 100%;
  cursor: pointer;
  border: none;
  margin-top: 16px;
  min-height: 56px;
}

/* ─── 表單 ─── */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; color: var(--sub); margin-bottom: 6px; }

input[type=text],
input[type=email],
input[type=password],
input[type=tel],
select,
textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  color: var(--text);
  background: #fff;
  outline: none;
  min-height: 44px;
}

input:focus,
select:focus,
textarea:focus { border-color: var(--red); }

textarea { resize: vertical; min-height: 90px; font-family: inherit; }

/* ─── 登入頁 ─── */
#auth-page {
  background: #fff;
  justify-content: center;
  padding: max(24px, var(--safe-top)) 20px max(24px, var(--safe-bottom));
  min-height: 100vh;
  min-height: 100dvh;
}

.auth-logo { font-size: clamp(44px, 12vw, 56px); text-align: center; margin-bottom: 8px; }
.auth-subtitle { text-align: center; color: var(--sub); margin-bottom: 28px; font-size: clamp(14px, 3.8vw, 16px); }

.auth-tabs {
  display: flex;
  margin-bottom: 24px;
  border-bottom: 2px solid var(--border);
}

.auth-tabs button {
  flex: 1;
  padding: 12px;
  background: none;
  border: none;
  font-size: 15px;
  font-weight: 600;
  color: var(--sub);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  min-height: 48px;
}

.auth-tabs button.active { color: var(--red); border-bottom-color: var(--red); }

/* ─── 事件列表 ─── */
.event-card {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.event-card:active { background: #fafafa; }

@media (hover: hover) {
  .event-card:hover { background: #fafafa; }
}

.event-card-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 6px; }
.event-category { font-size: 13px; color: var(--sub); }
.status-badge { font-size: 11px; font-weight: 700; padding: 3px 10px; border-radius: 20px; white-space: nowrap; flex-shrink: 0; }
.status-pending { background: #fff3e0; color: var(--orange); }
.status-active { background: var(--red-light); color: var(--red); }
.status-resolved { background: #f5f5f5; color: var(--grey); }
.event-title { font-size: clamp(15px, 4vw, 16px); font-weight: 600; margin-bottom: 8px; word-break: break-word; }
.event-meta { display: flex; flex-wrap: wrap; gap: 8px 16px; font-size: 12px; color: var(--sub); }

.events-scroll,
.detail-scroll,
.create-scroll,
.profile-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* ─── 詳情 ─── */
.detail-status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 16px;
  background: #fafafa;
  border-bottom: 1px solid var(--border);
}

.detail-title { font-size: clamp(17px, 4.5vw, 20px); font-weight: 700; padding: 16px; word-break: break-word; }
.detail-desc { padding: 0 16px 12px; color: var(--sub); line-height: 1.6; word-break: break-word; }

.detail-meta { margin: 0 16px 16px; background: #fafafa; border-radius: 10px; padding: 12px; }
.detail-meta-row { display: flex; flex-wrap: wrap; gap: 4px 8px; padding: 6px 0; }
.detail-meta-row span:first-child { color: var(--sub); font-size: 13px; min-width: 56px; }
.detail-meta-row span:last-child { font-size: 13px; flex: 1; word-break: break-all; }

#event-map {
  width: 100%;
  height: clamp(200px, 45vw, 280px);
  background: #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--sub);
  font-size: 13px;
}

#event-map iframe { width: 100%; height: 100%; border: 0; display: block; }

.detail-actions {
  padding: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.detail-actions .btn { flex: 1; min-width: min(100%, 140px); }

.detail-chat-btn { margin: 0 16px 16px; padding-bottom: var(--safe-bottom); }

/* ─── 聊天 ─── */
#chat-page { background: #f5f5f5; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

.bubble { max-width: min(85%, 320px); padding: 10px 13px; border-radius: 14px; word-break: break-word; }
.bubble-mine { align-self: flex-end; background: var(--red); color: #fff; border-bottom-right-radius: 3px; }
.bubble-other { align-self: flex-start; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .08); border-bottom-left-radius: 3px; }
.bubble-sender { font-size: 11px; color: var(--sub); margin-bottom: 3px; }
.bubble-text { font-size: 15px; line-height: 1.4; }
.bubble-time { font-size: 10px; margin-top: 4px; text-align: right; opacity: .65; }

.chat-input-bar {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, var(--safe-bottom));
  background: #fff;
  border-top: 1px solid var(--border);
  align-items: flex-end;
}

.chat-input-bar textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: 10px 14px;
  border-radius: 22px;
  font-size: 16px;
  resize: none;
  border: 1.5px solid var(--border);
}

.chat-send-btn {
  height: 44px;
  min-width: 64px;
  padding: 0 16px;
  border-radius: 22px;
  background: var(--red);
  color: #fff;
  font-weight: 700;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
}

/* ─── 發出求救 ─── */
.create-scroll { padding: 16px; padding-bottom: max(16px, var(--safe-bottom)); }

.loc-bar {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 12px;
  background: #f1f8e9;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13px;
  color: #2e7d32;
  line-height: 1.4;
  word-break: break-word;
}

.loc-bar.error { background: #fff3e0; color: #e65100; }

.categories { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

.cat-btn {
  padding: 10px 16px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: #fafafa;
  font-size: 14px;
  cursor: pointer;
  color: var(--sub);
  min-height: 44px;
  touch-action: manipulation;
}

.cat-btn.selected { border-color: var(--red); background: var(--red-light); color: var(--red); font-weight: 700; }

.quick-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.quick-actions .btn { flex: 1; min-width: min(100%, 120px); font-size: 13px; }

/* ─── 個人 ─── */
.user-card { margin: 16px; border: 1px solid var(--border); border-radius: 12px; padding: 20px; }
.user-name { font-size: 20px; font-weight: 700; word-break: break-word; }
.user-email { color: var(--sub); font-size: 14px; margin-top: 4px; word-break: break-all; }
.admin-badge { display: inline-block; margin-top: 8px; background: var(--red); color: #fff; font-size: 12px; font-weight: 700; padding: 3px 12px; border-radius: 12px; }

.section { margin: 0 16px 16px; border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.section-title { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.section-hint { font-size: 13px; color: var(--sub); margin-bottom: 12px; }

.contact-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.contact-info { flex: 1; min-width: 0; }
.contact-name { font-size: 15px; font-weight: 600; }
.contact-phone { font-size: 13px; color: var(--sub); word-break: break-all; }

.add-contact-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  align-items: stretch;
}

.add-contact-row input { flex: 1 1 120px; min-width: 0; }
.add-contact-row .btn { flex: 0 0 auto; }

.empty { text-align: center; color: #bbb; padding: 40px 16px; font-size: 15px; }
.loading { text-align: center; padding: 40px; color: var(--sub); }

.toast {
  position: fixed;
  bottom: max(24px, var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 9999;
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
  max-width: calc(100vw - 32px);
  text-align: center;
}

.toast.show { opacity: 1; }

.back-btn {
  background: none;
  border: none;
  color: var(--red);
  font-size: 15px;
  cursor: pointer;
  padding: 8px 0;
  font-weight: 600;
  min-height: 44px;
  flex-shrink: 0;
}

/* ─── 發出求救：類別後標題 ─── */
.user-pill {
  font-size: 12px;
  color: var(--sub);
  background: #f5f5f5;
  padding: 4px 10px;
  border-radius: 12px;
}

.field-hint {
  font-size: 12px;
  color: var(--sub);
  margin: 4px 0 10px;
  line-height: 1.4;
}

.title-section {
  margin-top: 8px;
  padding-top: 16px;
  border-top: 1px dashed var(--border);
  animation: fadeIn .25s ease;
}

.title-section.hidden { display: none; }

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

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0;
  font-size: 15px;
  cursor: pointer;
}

.check-row input { width: 20px; height: 20px; min-height: auto; accent-color: var(--red); }

/* ─── 手動填寫位置 ─── */
.manual-loc-section {
  margin: 0 0 16px;
  padding: 16px;
  background: #fff8e1;
  border: 1.5px solid #ffb74d;
  border-radius: 10px;
}

.manual-loc-section.hidden { display: none; }

.manual-loc-alert { margin-bottom: 14px; }
.manual-loc-alert strong { display: block; color: #e65100; font-size: 15px; margin-bottom: 6px; }
.manual-loc-alert p { font-size: 13px; color: #5d4037; line-height: 1.5; }

.coord-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.coord-row input { flex: 1; min-width: 120px; }

/* ─── 求救進行中 ─── */
.active-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: max(16px, var(--safe-bottom)); }

.active-hero {
  padding: 16px;
  background: linear-gradient(135deg, #fdecea, #fff);
  border-bottom: 1px solid var(--border);
}

.active-title { font-size: clamp(17px, 4.5vw, 20px); font-weight: 800; color: var(--red-dark); word-break: break-word; }
.active-cat { font-size: 13px; color: var(--sub); margin-top: 6px; }

.gps-live { background: #e8f5e9; color: #2e7d32; font-weight: 600; margin: 12px 16px 0; }

.active-map,
.detail-map {
  width: 100%;
  height: clamp(200px, 45vw, 260px);
  background: #e8e8e8;
  margin: 0;
}

.active-map { margin-top: 0; }

.broadcast-panel {
  margin: 16px;
  padding: 16px;
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.broadcast-panel h3 {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 12px;
  color: var(--text);
  line-height: 1.4;
}

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

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

.broadcast-item-main { display: flex; align-items: flex-start; gap: 10px; flex: 1; min-width: 0; }

.broadcast-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  flex-shrink: 0;
}

.broadcast-item.status-done .broadcast-icon { background: #e8f5e9; color: #2e7d32; }
.broadcast-item.status-pending .broadcast-icon { background: #fff3e0; color: var(--orange); }

.broadcast-label { font-size: 14px; font-weight: 600; }
.broadcast-note { font-size: 12px; color: var(--sub); margin-top: 2px; }

.broadcast-send-btn { flex-shrink: 0; }

.active-actions { padding: 0 16px 16px; display: flex; flex-direction: column; gap: 10px; }

.detail-cat-pill {
  font-size: 13px;
  color: #555;
  background: #eee;
  padding: 3px 10px;
  border-radius: 12px;
}

.detail-chat-btn { margin: 0 16px 16px; padding-bottom: var(--safe-bottom); }

/* ─── RWD 斷點 ─── */
@media (max-width: 380px) {
  #nav a { font-size: 11px; padding: 10px 4px; }
  .event-meta { flex-direction: column; gap: 4px; }
}

@media (min-width: 721px) {
  body { padding: 16px 0; }
  #app { min-height: calc(100vh - 32px); border-radius: 12px; overflow: hidden; }
}

@media (min-width: 1024px) {
  #app { max-width: 800px; }
}
