/* ===== Design tokens ===== */
:root {
  --black: #0a0a0a;
  --white: #ffffff;
  --gray-50: #f7f7f8;
  --gray-100: #efeff1;
  --gray-200: #e2e2e5;
  --gray-400: #9a9aa1;
  --gray-600: #5c5c64;
  --gray-800: #262629;
  --green: #06c167;
  --green-dark: #049655;
  --red: #e11900;
  --amber: #ffb400;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow-card: 0 1px 3px rgba(0,0,0,0.06), 0 8px 24px rgba(0,0,0,0.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--gray-50);
  color: var(--black);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
a { color: inherit; }
button { font-family: inherit; cursor: pointer; }
input, textarea, button { font-size: 16px; } /* prevents iOS zoom on focus */

.app-shell {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  background: var(--white);
  position: relative;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-card);
}

/* ===== Home page ===== */
.home-header {
  background: var(--black);
  color: var(--white);
  padding: 40px 24px 32px;
  border-radius: 0 0 28px 28px;
}
.home-header h1 {
  margin: 0 0 8px;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.home-header p {
  margin: 0;
  color: var(--gray-200);
  font-size: 15px;
  line-height: 1.5;
}

.reason-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 20px;
}
.reason-btn {
  background: var(--white);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-md);
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
  box-shadow: var(--shadow-card);
}
.reason-btn:active { transform: scale(0.96); }
.reason-btn .icon { font-size: 28px; line-height: 1; }
.reason-btn .label { font-size: 13.5px; font-weight: 600; color: var(--gray-800); }
.reason-btn.emergency { border-color: rgba(225,25,0,0.25); background: #fff5f4; }
.reason-btn.emergency .label { color: var(--red); }

.footer-note {
  margin-top: auto;
  padding: 18px 24px 28px;
  text-align: center;
  color: var(--gray-400);
  font-size: 12px;
}

/* ===== Bottom sheet form ===== */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10,10,10,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 40;
}
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translate(-50%, 100%);
  width: 100%;
  max-width: 480px;
  background: var(--white);
  border-radius: 24px 24px 0 0;
  padding: 20px 20px calc(20px + var(--safe-bottom));
  z-index: 41;
  transition: transform 0.28s cubic-bezier(.32,.72,0,1);
  max-height: 88dvh;
  overflow-y: auto;
}
.sheet.open { transform: translate(-50%, 0); }

.sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--gray-200);
  border-radius: 4px;
  margin: 0 auto 16px;
}
.sheet-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 800;
  margin-bottom: 4px;
}
.sheet-sub { color: var(--gray-600); font-size: 13.5px; margin-bottom: 18px; }

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--gray-600);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.field input, .field textarea {
  width: 100%;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  background: var(--gray-50);
  outline: none;
  transition: border-color 0.15s ease;
}
.field input:focus, .field textarea:focus { border-color: var(--black); background: var(--white); }
.field textarea { resize: vertical; min-height: 84px; }

.loc-btn {
  width: 100%;
  background: var(--gray-50);
  border: 1.5px dashed var(--gray-200);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-weight: 600;
  font-size: 14px;
  color: var(--gray-800);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 16px;
}
.loc-btn.shared { border-style: solid; border-color: var(--green); color: var(--green-dark); background: #eafff4; }

.primary-btn {
  width: 100%;
  background: var(--black);
  color: var(--white);
  border: none;
  border-radius: var(--radius-sm);
  padding: 15px;
  font-weight: 700;
  font-size: 15.5px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.primary-btn:disabled { opacity: 0.5; }
.primary-btn.danger { background: var(--red); }

.close-sheet {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--gray-100);
  font-size: 16px;
  color: var(--gray-600);
  display: flex;
  align-items: center;
  justify-content: center;
}

.error-text { color: var(--red); font-size: 13px; margin-top: -6px; margin-bottom: 12px; display: none; }
.error-text.show { display: block; }

/* ===== Conversation / chat page ===== */
.chat-header {
  background: var(--black);
  color: var(--white);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.chat-header .reason-icon { font-size: 22px; }
.chat-header .titles { flex: 1; min-width: 0; }
.chat-header .titles .t1 { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-header .titles .t2 { font-size: 12px; color: var(--gray-200); display: flex; align-items: center; gap: 6px; }
.live-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 0 rgba(6,193,103,0.6);
  animation: pulse 1.8s infinite;
}
.live-dot.off { background: var(--gray-400); animation: none; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(6,193,103,0.55); }
  70% { box-shadow: 0 0 0 8px rgba(6,193,103,0); }
  100% { box-shadow: 0 0 0 0 rgba(6,193,103,0); }
}
.timer-chip {
  font-size: 12px;
  font-weight: 700;
  background: rgba(255,255,255,0.12);
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.timer-chip.low { background: rgba(225,25,0,0.25); color: #ffb3a8; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--gray-50);
}
.bubble-row { display: flex; }
.bubble-row.visitor { justify-content: flex-end; }
.bubble-row.owner { justify-content: flex-start; }
.bubble-row.system { justify-content: center; }

.bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.4;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.bubble-row.visitor .bubble { background: var(--black); color: var(--white); border-bottom-right-radius: 4px; }
.bubble-row.owner .bubble { background: var(--white); color: var(--black); border: 1px solid var(--gray-200); border-bottom-left-radius: 4px; }
.bubble-row.system .bubble {
  background: transparent; color: var(--gray-400); font-size: 12px;
  border: none; box-shadow: none; text-align: center; max-width: 90%;
}
.bubble-meta { font-size: 10.5px; opacity: 0.6; margin-top: 4px; }

.quick-replies {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 14px;
  background: var(--white);
  border-top: 1px solid var(--gray-100);
}
.quick-replies::-webkit-scrollbar { display: none; }
.qr-chip {
  flex: none;
  border: 1.5px solid var(--gray-200);
  background: var(--white);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  color: var(--gray-800);
}
.qr-chip:active { background: var(--gray-100); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 14px calc(12px + var(--safe-bottom));
  background: var(--white);
  border-top: 1px solid var(--gray-100);
}
.composer textarea {
  flex: 1;
  border: 1.5px solid var(--gray-200);
  border-radius: 20px;
  padding: 11px 16px;
  resize: none;
  max-height: 100px;
  outline: none;
  background: var(--gray-50);
}
.composer textarea:focus { border-color: var(--black); background: var(--white); }
.send-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--black);
  color: var(--white);
  border: none;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex: none;
}
.send-btn:disabled { opacity: 0.4; }

.status-banner {
  margin: 14px;
  padding: 14px;
  border-radius: var(--radius-md);
  background: var(--gray-100);
  color: var(--gray-800);
  font-size: 13.5px;
  text-align: center;
  font-weight: 600;
}
.status-banner.closed { background: #ffece9; color: #a11400; }

.center-msg {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  padding: 40px 20px;
  text-align: center;
  color: var(--gray-600);
}

/* ===== Admin ===== */
.admin-login {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--black);
}
.admin-login-card {
  background: var(--white);
  border-radius: var(--radius-lg);
  padding: 32px 26px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow-card);
}
.admin-login-card h1 { font-size: 20px; margin: 0 0 4px; }
.admin-login-card p { color: var(--gray-600); font-size: 13.5px; margin: 0 0 20px; }

.admin-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--gray-50);
}
.admin-topbar {
  background: var(--black);
  color: var(--white);
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.admin-topbar h1 { font-size: 16px; margin: 0; font-weight: 800; }
.logout-link { font-size: 12.5px; color: var(--gray-200); background: none; border: none; }

.admin-body { flex: 1; display: flex; min-height: 0; }
.conv-list {
  width: 100%;
  overflow-y: auto;
  background: var(--white);
  border-right: 1px solid var(--gray-100);
}
.conv-item {
  padding: 14px 16px;
  border-bottom: 1px solid var(--gray-100);
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.conv-item .icon { font-size: 22px; }
.conv-item .info { flex: 1; min-width: 0; }
.conv-item .row1 { font-weight: 700; font-size: 14px; display: flex; justify-content: space-between; gap: 8px; }
.conv-item .row2 { font-size: 12.5px; color: var(--gray-600); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.badge {
  font-size: 10.5px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  background: var(--green); color: var(--white); text-transform: uppercase;
}
.badge.closed, .badge.expired { background: var(--gray-400); }
.conv-time { font-size: 11px; color: var(--gray-400); flex: none; }

.empty-state { padding: 60px 24px; text-align: center; color: var(--gray-400); }

/* Full-screen conversation overlay on mobile admin */
.admin-conv-overlay {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.22s ease;
}
.admin-conv-overlay.open { transform: translateX(0); }
.back-btn {
  background: none; border: none; color: var(--white); font-size: 20px; padding: 0 4px 0 0;
}

.visitor-meta {
  padding: 10px 14px;
  background: var(--white);
  border-bottom: 1px solid var(--gray-100);
  font-size: 12.5px;
  color: var(--gray-600);
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.visitor-meta span { white-space: nowrap; }

.close-conv-btn {
  border: none;
  background: none;
  color: var(--red);
  font-size: 12.5px;
  font-weight: 700;
  padding: 0;
}

.toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--black);
  color: var(--white);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(4px); }

@media (min-width: 760px) {
  .admin-body { max-width: 1100px; margin: 0 auto; width: 100%; }
  .conv-list { width: 360px; flex: none; }
  .admin-conv-overlay { position: static; transform: none; flex: 1; border-left: 1px solid var(--gray-100); }
  .back-btn { display: none; }
}
