/* Chat and voice calls (customer, driver, vendor). Uses the app tokens. */

/* ---------- unread badge on chat buttons ---------- */
[data-chat-for] { position: relative; }
.chat-badge {
  position: absolute; top: -4px; right: -4px; min-width: 19px; height: 19px; padding: 0 5px;
  border-radius: var(--r-pill); background: var(--orange-600); color: #fff; font-size: 11px; font-weight: 800;
  display: grid; place-items: center; box-shadow: 0 0 0 2px var(--surface); pointer-events: none;
}

/* ---------- chat screen ---------- */
body.chat-mode #topbar, body.chat-mode #bottom-nav { display: none !important; }
body.chat-mode { overflow: hidden; }
body.chat-mode #main { padding: 0 !important; max-width: none !important; }

.chat-screen {
  position: fixed; inset: 0; z-index: 85; display: flex; flex-direction: column;
  background: var(--bg); max-width: 760px; margin: 0 auto;
}
@media (min-width: 760px) { .chat-screen { border-inline: 1px solid var(--line); box-shadow: var(--shadow-3); } }
.chat-boot { margin: auto; display: flex; flex-direction: column; align-items: center; gap: 10px; color: var(--muted); padding: 24px; text-align: center; }
.chat-boot.is-error strong { color: var(--ink); font-size: 18px; }

.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line); box-shadow: var(--shadow-1); position: relative; z-index: 2;
}
.chat-avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: linear-gradient(140deg, var(--green-600), var(--green-800)); color: #fff; font-weight: 800; font-size: 17px;
}
.chat-who { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.chat-who h1 { font: 700 16.5px/1.2 var(--font-display); margin: 0; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-who small { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-call { background: var(--green-50); color: var(--green-700); }
.chat-call:hover { background: var(--green-100); }

.chat-call-menu {
  position: absolute; right: 12px; top: 64px; z-index: 5; background: var(--surface); border-radius: var(--r);
  box-shadow: var(--shadow-3); border: 1px solid var(--line); padding: 6px; min-width: 220px;
}
.chat-call-menu button { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px; border: 0; background: none; border-radius: var(--r-sm); text-align: left; cursor: pointer; color: var(--ink); }
.chat-call-menu button:hover, .chat-call-menu button:focus-visible { background: var(--green-50); }
.chat-call-menu .i { color: var(--green-700); }
.chat-call-menu span { display: flex; flex-direction: column; }
.chat-call-menu small { color: var(--muted); font-size: 12px; }

.chat-context {
  display: flex; align-items: center; gap: 8px; margin: 10px 12px 0; padding: 9px 12px; border-radius: var(--r-sm);
  background: var(--orange-50); color: var(--orange-700); font-size: 13px; font-weight: 600; text-decoration: none; border: 1px solid var(--orange-100);
}
.chat-context span { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-context .i { width: 17px; height: 17px; flex: none; }

.chat-log { flex: 1; overflow-y: auto; list-style: none; margin: 0; padding: 14px 12px 10px; display: flex; flex-direction: column; gap: 2px; overscroll-behavior: contain; }
.chat-loading, .chat-empty { margin: auto; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.chat-empty { flex-direction: column; text-align: center; max-width: 280px; gap: 6px; }
.chat-empty .i { width: 40px; height: 40px; padding: 10px; border-radius: 50%; background: var(--green-50); color: var(--green-700); }
.chat-empty strong { color: var(--ink); font-size: 16px; }
.chat-empty span { font-size: 13.5px; }

.chat-day { align-self: center; margin: 10px 0 6px; }
.chat-day span { font-size: 11.5px; font-weight: 700; color: var(--muted); background: var(--surface-2); padding: 3px 10px; border-radius: var(--r-pill); }

.msg { display: flex; flex-direction: column; max-width: min(80%, 460px); }
.msg.first { margin-top: 8px; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.msg-name { font-size: 11.5px; font-weight: 700; color: var(--green-800); margin: 0 0 3px 12px; }
.bubble {
  padding: 9px 13px; border-radius: 18px; font-size: 15px; line-height: 1.38; white-space: pre-wrap; overflow-wrap: anywhere;
  box-shadow: var(--shadow-1);
}
.msg.mine .bubble { background: var(--green-700); color: #fff; border-bottom-right-radius: 6px; }
.msg.theirs .bubble { background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg:not(.first).mine .bubble { border-top-right-radius: 6px; }
.msg:not(.first).theirs .bubble { border-top-left-radius: 6px; }
.msg-meta { font-size: 11px; color: var(--muted); margin: 3px 6px 0; display: flex; align-items: center; gap: 5px; }
.msg-meta .seen { color: var(--green-700); font-weight: 700; }
.msg.sending .bubble { opacity: .65; }
.msg.failed .bubble { background: var(--bad-50); color: var(--bad); border: 1px solid #f1c4c5; }
.msg.failed .link-btn { color: var(--bad); display: inline-flex; gap: 4px; align-items: center; font-size: 12px; }

.chat-event {
  align-self: center; display: flex; align-items: center; gap: 7px; margin: 10px 0; padding: 7px 13px; border-radius: var(--r-pill);
  background: var(--surface); border: 1px solid var(--line); font-size: 13px; color: var(--ink-2); box-shadow: var(--shadow-1);
}
.chat-event .i { width: 16px; height: 16px; color: var(--green-700); }
.chat-event.is-missed .i { color: var(--bad); transform: rotate(135deg); }
.chat-event time { color: var(--muted); font-size: 11.5px; }

.chat-closed { margin: 0 12px 8px; padding: 10px 12px; border-radius: var(--r-sm); background: var(--surface-2); color: var(--muted); font-size: 13.5px; text-align: center; }

.chat-quick { display: flex; gap: 8px; overflow-x: auto; padding: 4px 12px 8px; scrollbar-width: none; }
.chat-quick::-webkit-scrollbar { display: none; }
.chat-quick .chip { white-space: nowrap; background: var(--surface); box-shadow: var(--shadow-1); font-size: 13px; }

.chat-compose {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px; padding-bottom: max(10px, var(--safe-b));
  background: var(--surface); border-top: 1px solid var(--line);
}
.chat-compose textarea {
  flex: 1; resize: none; border: 1.5px solid var(--line-2); border-radius: 22px; padding: 10px 16px; font: inherit; font-size: 15px;
  line-height: 1.35; max-height: 132px; background: var(--surface-2); color: var(--ink);
}
.chat-compose textarea:focus { outline: none; border-color: var(--green-600); background: var(--surface); box-shadow: 0 0 0 3px var(--green-100); }
.chat-compose.is-closed textarea { opacity: .6; }
.chat-send {
  width: 46px; height: 46px; flex: none; border-radius: 50%; border: 0; display: grid; place-items: center; cursor: pointer;
  background: var(--orange-500); color: var(--ink); box-shadow: var(--shadow-2); transition: transform .12s ease, opacity .12s;
}
.chat-send .i { width: 21px; height: 21px; }
.chat-send:disabled { background: var(--surface-2); color: var(--muted); box-shadow: none; cursor: default; }
.chat-send:not(:disabled):active { transform: scale(.94); }

/* ---------- call screen ---------- */
body.call-open { overflow: hidden; }
.call-screen {
  position: fixed; inset: 0; z-index: 300; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(120% 80% at 50% 0%, #136b3a 0%, var(--green-900) 55%, #04220f 100%);
  color: #fff; animation: call-in .22s ease-out;
}
@keyframes call-in { from { opacity: 0; transform: scale(.98); } }
.call-card { width: min(100%, 400px); min-height: min(640px, calc(100dvh - 32px)); display: flex; flex-direction: column; align-items: center; position: relative; padding: 24px 8px 18px; }
.call-min { position: absolute; top: 4px; left: 4px; width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .12); color: #fff; display: grid; place-items: center; cursor: pointer; }
.call-min .i { transform: rotate(90deg); }
.call-peer { margin-top: 9vh; display: flex; flex-direction: column; align-items: center; text-align: center; gap: 4px; }
.call-avatar { position: relative; width: 112px; height: 112px; border-radius: 50%; display: grid; place-items: center; margin-bottom: 14px; }
.call-avatar span {
  width: 100%; height: 100%; border-radius: 50%; display: grid; place-items: center; font: 800 44px/1 var(--font-display);
  background: linear-gradient(140deg, var(--orange-400), var(--orange-600)); color: #fff; position: relative; z-index: 1;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
}
.call-avatar.is-ringing::before, .call-avatar.is-ringing::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%; border: 2px solid rgba(255, 162, 63, .7); animation: call-ring 2s ease-out infinite;
}
.call-avatar.is-ringing::after { animation-delay: 1s; }
.call-avatar.is-live span { box-shadow: 0 0 0 6px rgba(34, 166, 80, .45), 0 10px 30px rgba(0, 0, 0, .3); }
@keyframes call-ring { from { transform: scale(1); opacity: 1; } to { transform: scale(1.7); opacity: 0; } }
.call-peer h2 { font: 700 26px/1.2 var(--font-display); margin: 0; color: #fff; }
.call-sub { margin: 0; font-size: 14px; color: rgba(255, 255, 255, .75); }
.call-status { margin: 10px 0 0; font-size: 16px; font-weight: 600; color: var(--orange-400); font-variant-numeric: tabular-nums; min-height: 1.4em; }
.phase-active .call-status { color: #9ee6b5; }
.phase-ended .call-status, .phase-missed .call-status, .phase-declined .call-status, .phase-cancelled .call-status, .phase-busy .call-status, .phase-failed .call-status { color: #fff; opacity: .9; }
.call-privacy { margin-top: auto; display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: rgba(255, 255, 255, .65); }
.call-privacy .i { width: 15px; height: 15px; }
.call-actions { display: flex; justify-content: center; gap: 22px; margin-top: 18px; width: 100%; min-height: 96px; }
.call-btn { border: 0; background: none; color: #fff; display: flex; flex-direction: column; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; cursor: pointer; min-width: 72px; }
.call-btn > span { width: 66px; height: 66px; border-radius: 50%; display: grid; place-items: center; transition: transform .12s ease, background .12s; }
.call-btn .i { width: 28px; height: 28px; }
.call-btn:active > span { transform: scale(.93); }
.call-btn.accept > span { background: var(--green-500); box-shadow: 0 8px 24px rgba(34, 166, 80, .5); animation: call-bob 1.4s ease-in-out infinite; }
.call-btn.decline > span { background: #e0393e; box-shadow: 0 8px 24px rgba(224, 57, 62, .45); }
.call-btn.ghost > span { background: rgba(255, 255, 255, .14); }
.call-btn.ghost.on > span { background: #fff; color: var(--green-900); }
.call-btn:focus-visible > span { outline: 3px solid #fff; outline-offset: 3px; }
@keyframes call-bob { 50% { transform: translateY(-4px); } }

.call-pill {
  position: fixed; z-index: 290; top: max(8px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 14px; border: 0; border-radius: var(--r-pill);
  background: var(--green-800); color: #fff; font: inherit; font-size: 13.5px; box-shadow: var(--shadow-3); cursor: pointer; max-width: calc(100vw - 24px);
}
.call-pill strong { font-variant-numeric: tabular-nums; }
.call-pill-dot { width: 9px; height: 9px; border-radius: 50%; background: #5be38a; box-shadow: 0 0 0 0 rgba(91, 227, 138, .7); animation: call-dot 1.6s infinite; }
.call-pill-open { background: rgba(255, 255, 255, .16); padding: 4px 10px; border-radius: var(--r-pill); font-weight: 700; font-size: 12px; }
@keyframes call-dot { 70% { box-shadow: 0 0 0 8px rgba(91, 227, 138, 0); } 100% { box-shadow: 0 0 0 0 rgba(91, 227, 138, 0); } }

@media (prefers-reduced-motion: reduce) {
  .call-avatar.is-ringing::before, .call-avatar.is-ringing::after, .call-btn.accept > span, .call-pill-dot, .call-screen { animation: none; }
}

/* ---------- contact rows on booking / order cards ---------- */
.order-contact { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 4px; }
.order-contact .btn { position: relative; }
.order-contact .chat-badge { top: -6px; right: -6px; }
.rider-card .avatar.store { background: var(--orange-50); color: var(--orange-700); }
.rider-card .avatar.store .i { width: 22px; height: 22px; }

/* ---------- chat photos ---------- */
.chat-tools { display: flex; align-items: center; gap: 2px; flex: none; padding-bottom: 1px; }
.chat-tool { width: 42px; height: 44px; color: var(--green-700); }
.chat-tool .i { width: 22px; height: 22px; }
.chat-tool:disabled { opacity: .4; cursor: default; }
.chat-attach-preview {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--surface); border-top: 1px solid var(--line);
}
.chat-attach-preview[hidden] { display: none; }
.chat-attach-preview img { width: 56px; height: 56px; object-fit: cover; border-radius: 10px; flex: none; }
.chat-attach-preview span { flex: 1; min-width: 0; display: grid; gap: 2px; font-size: 13px; }
.chat-attach-preview small { color: var(--muted); }
.bubble.photo-bubble { padding: 4px; display: grid; gap: 4px; max-width: min(72vw, 260px); }
.chat-photo-btn { display: block; padding: 0; border: 0; background: none; cursor: zoom-in; border-radius: 14px; overflow: hidden; }
.chat-photo { display: block; width: 100%; max-height: 320px; min-height: 80px; object-fit: cover; border-radius: 14px; background: var(--surface-2); }
.chat-photo.is-loading { aspect-ratio: 4 / 3; animation: chat-photo-pulse 1.2s ease-in-out infinite; }
.photo-bubble.is-broken .chat-photo { aspect-ratio: auto; min-height: 40px; animation: none; }
.photo-caption { padding: 2px 8px 4px; white-space: pre-wrap; word-break: break-word; }
@keyframes chat-photo-pulse { 50% { opacity: .55; } }
.photo-viewer {
  position: fixed; inset: 0; z-index: 400; display: grid; place-items: center; padding: 16px;
  background: rgba(4, 20, 10, .92);
}
.photo-viewer img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
.photo-viewer-close { position: absolute; top: max(12px, var(--safe-t, 0px)); right: 12px; color: #fff; background: rgba(255,255,255,.14); font-size: 20px; }
@media (prefers-reduced-motion: reduce) { .chat-photo.is-loading { animation: none; } }
