.ride-chat {
  position: fixed; top: calc(108px + env(safe-area-inset-top)); right: 20px;
  z-index: 28; color: #f3f6ef; font: 14px/1.45 system-ui, sans-serif;
}
.ride-chat[hidden], .ride-chat [hidden] { display: none !important; }
.ride-chat-toggle {
  display: block; margin-left: auto; min-height: 42px; min-width: 70px;
  padding: 8px 14px; border: 1px solid #bad3be; border-radius: 5px;
  background: #12352f; color: #f5faee; font-size: 14px; font-weight: 700;
}
/* Incoming conversation uses the existing button rather than a new overlay. */
.ride-chat-toggle.has-unread {
  display: flex; flex-direction: column; align-items: flex-start; gap: 3px;
  width: min(318px, calc(100vw - 24px)); text-align: left;
  padding: 9px 12px; border-color: #d9f38b; box-shadow: 0 5px 20px #0007;
}
.ride-chat-toggle-title { display: block; font-size: 13px; line-height: 1.2; }
.ride-chat-toggle-preview {
  display: flex; align-items: baseline; gap: 6px; width: 100%;
  min-width: 0; font-weight: 400; font-size: 14px; line-height: 1.25;
}
.ride-chat-toggle-preview[hidden] { display: none; }
.ride-chat-toggle-preview strong { color: #d9f38b; flex: none; max-width: 38%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ride-chat-toggle-preview [data-chat-message] { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #f5faee; }
.ride-chat-panel {
  display: flex; flex-direction: column; width: 338px; max-width: calc(100vw - 24px);
  height: 430px; max-height: calc(var(--ride-chat-view-height, 100dvh) - 175px);
  margin-top: 8px; border: 1px solid #80a798; border-radius: 7px;
  background: #112a25; box-shadow: 0 8px 30px #0006; overflow: hidden;
}
.ride-chat-panel header { display: flex; justify-content: space-between; align-items: center; padding: 13px 15px 11px; border-bottom: 1px solid #416154; }
.ride-chat-panel h2 { margin: 0; font-size: 18px; letter-spacing: 0; }
.ride-chat-panel header p { margin: 1px 0 0; color: #c0d3c7; font-size: 12px; }
.ride-chat-panel header button { width: 40px; height: 40px; padding: 0; border: 0; background: transparent; color: #f3f6ef; font-size: 28px; }
.ride-chat-scroll { flex: 1; min-height: 60px; overflow-y: auto; overscroll-behavior: contain; padding: 0 15px; }
#rideChatEmpty { color: #b8d0c2; padding: 19px 0; line-height: 1.6; }
#rideChatMessages { list-style: none; margin: 0; padding: 0; }
#rideChatMessages li { padding: 11px 0; border-bottom: 1px solid #314c40; overflow-wrap: anywhere; }
#rideChatMessages li:last-child { border-bottom: none; }
#rideChatMessages li > div { display: flex; gap: 10px; align-items: baseline; }
#rideChatMessages strong { color: #edf4e7; font-size: 13px; }
#rideChatMessages .ride-chat-own strong { color: #d9f38b; }
#rideChatMessages time { color: #a8beb1; font-size: 11px; }
#rideChatMessages p { margin: 3px 0 0; font-size: 15px; white-space: pre-wrap; line-height: 1.45; }
#rideChatForm { padding: 10px 13px 12px; border-top: 1px solid #416154; }
#rideChatForm label { display: block; margin-bottom: 5px; color: #d5e2d9; font-size: 12px; }
#rideChatInput {
  display: block; width: 100%; min-height: 54px; max-height: 110px; resize: vertical; box-sizing: border-box;
  padding: 9px 10px; border: 1px solid #739383; border-radius: 4px; background: #071f19;
  color: #f6f9f1; font: 16px/1.4 system-ui, sans-serif;
}
#rideChatInput::placeholder { color: #a7beb0; opacity: 1; }
.ride-chat-send-row { display: flex; gap: 10px; align-items: center; margin-top: 8px; }
#rideChatStatus { flex: 1; margin: 0; color: #bdcfc3; font-size: 11px; line-height: 1.35; }
#rideChatSend { min-width: 66px; min-height: 40px; padding: 7px 13px; border: 0; border-radius: 4px; background: #d9f38b; color: #172e22; font-weight: 750; }
#rideChatSend:disabled { opacity: .45; }
.ride-chat :focus-visible { outline: 3px solid #e8fbaf; outline-offset: 2px; }
.ride-chat-newest { align-self: center; padding: 5px 12px; border: 1px solid #99b892; color: #e5f7d5; background: #214538; font-size: 12px; }
body.clean #rideChat,
body:has(#setup:not([hidden])) #rideChat,
body:has(#outfitPanel:not([hidden])) #rideChat { display: none !important; }
@media (max-width: 600px) {
  .ride-chat { top: calc(108px + var(--ride-chat-view-top, 0px)); right: 12px; }
  .ride-chat-panel { width: calc(100vw - 24px); height: 410px; max-height: calc(var(--ride-chat-view-height, 100dvh) - 164px); }
  .ride-chat-toggle { min-height: 40px; }
  /* The ride help control occupies the lower-left corner above the pedals. */
  .ride-chat-toggle.has-unread { width: min(250px, calc(100vw - 140px)); }
}
@media (max-width:600px){
 .ride-chat{top:auto;bottom:var(--ride-chat-controls-clearance,calc(180px + env(safe-area-inset-bottom)));right:12px}
 .ride-chat-panel{position:fixed;top:calc(156px + var(--ride-chat-view-top,0px));right:12px;margin-top:0}
 .ride-chat:has(.ride-chat-panel:not([hidden])) .ride-chat-toggle{visibility:hidden}
}

@media(max-width:600px){
 .ride-chat.compact-compose .ride-chat-panel{top:calc(8px + var(--ride-chat-view-top,0px));height:410px;max-height:calc(var(--ride-chat-view-height,100dvh) - 16px)}
 .ride-chat.compact-compose .ride-chat-panel header{padding:6px 12px}
 .ride-chat.compact-compose .ride-chat-scroll{min-height:0}
 .ride-chat.compact-compose #rideChatForm{padding:7px 12px}
 .ride-chat.compact-compose #rideChatInput{min-height:44px;max-height:70px}
}

/* Host conversation actions grow upward; chat must not sit over their row. */
@media(max-width:600px){
 body:has(#hostHud:not([hidden])) .ride-chat{top:calc(108px + env(safe-area-inset-top));bottom:auto}
}

/* When a host leaves, the recovery card replaces the ride controls. Keep the
   existing chat launcher below the card rather than floating over its exit. */
@media(max-width:600px){
 body:has(#routeWaiting.host-offline:not([hidden])) .ride-chat{
  top:auto;
  bottom:calc(16px + env(safe-area-inset-bottom));
 }
}

/* Watch-and-talk phone layout for a landscape host camera; no second player. */
@media (max-width:520px) and (min-height:568px) and (orientation:portrait) {
  body.guest-host-main .ride-chat.landscape-watch-chat {
    top:calc(var(--watch-video-top) + var(--watch-video-height) + 8px);
    left:12px;right:12px;
    bottom:calc(76px + env(safe-area-inset-bottom));
    height:auto;width:auto;
  }
  /* The guest's everyday voice controls get a single strip above the watch
     picture. An always-open two-row audio panel obscured half the live video. */
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) .ride-top {
    row-gap:2px;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestConversation:not([hidden]) {
    display:grid;grid-template-columns:minmax(0,2fr) minmax(0,1fr);
    gap:6px;align-items:start;margin:0;width:100%;max-width:none;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestConversation:has(#guestRideVolume[hidden]) {
    grid-template-columns:1fr;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestConversation .host-conversation-buttons {
    display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:6px;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestConversation .host-conversation-buttons button {
    min-width:0;min-height:44px;padding:6px 4px;font-size:10px;line-height:1.2;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestRideVolume {
    min-width:0;margin:0;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestRideVolume summary {
    box-sizing:border-box;min-height:44px;padding:5px 7px;
    gap:3px;font-size:10px;line-height:1.2;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) .guest-volume-name::before {
    margin-right:3px;
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat:not(.compact-compose)) #guestConversation [data-guest-audio-status] {
    grid-column:1/-1;
  }
  .ride-chat.landscape-watch-chat .ride-chat-panel {
    position:absolute;inset:0;width:100%;height:100%;max-height:none;
    margin:0;border-radius:12px;box-shadow:0 7px 22px #0006;
  }
  .ride-chat.landscape-watch-chat .ride-chat-panel header {padding:7px 11px 5px}
  .ride-chat.landscape-watch-chat .ride-chat-panel h2 {font-size:15px}
  .ride-chat.landscape-watch-chat .ride-chat-panel header p {display:none}
  .ride-chat.landscape-watch-chat .ride-chat-panel header button {width:30px;height:30px}
  .ride-chat.landscape-watch-chat .ride-chat-scroll {min-height:30px;padding:0 11px}
  .ride-chat.landscape-watch-chat #rideChatEmpty {padding:10px 0;font-size:12px}
  .ride-chat.landscape-watch-chat #rideChatMessages li {padding:5px 0}
  .ride-chat.landscape-watch-chat #rideChatForm {padding:6px 10px 8px}
  .ride-chat.landscape-watch-chat #rideChatForm label {
    position:absolute;width:1px;height:1px;margin:-1px;padding:0;
    overflow:hidden;white-space:nowrap;clip-path:inset(50%);
  }
  .ride-chat.landscape-watch-chat #rideChatInput {
    min-height:38px;max-height:46px;resize:none;padding:7px 9px;
  }
  .ride-chat.landscape-watch-chat .ride-chat-send-row {margin-top:4px}
  .ride-chat.landscape-watch-chat #rideChatSend {min-height:34px}
  .ride-chat.landscape-watch-chat:has(.ride-chat-panel[hidden]) {
    top:auto;bottom:var(--ride-chat-controls-clearance,calc(76px + env(safe-area-inset-bottom)));
  }
  .ride-chat.landscape-watch-chat.compact-compose .ride-chat-panel {
    position:fixed;top:calc(8px + var(--ride-chat-view-top,0px));
    left:12px;right:12px;bottom:auto;
    width:calc(100vw - 24px);
    height:min(410px,calc(var(--ride-chat-view-height,100dvh) - 16px));
    max-height:calc(var(--ride-chat-view-height,100dvh) - 16px);
  }
}

/* Rotating a phone must not cut the reply action off the bottom of the ride.
   The compact landscape chat occupies the right side beside the live picture. */
@media (min-width:521px) and (max-height:500px) {
  body.guest-host-main .ride-chat:has(.ride-chat-panel:not([hidden])) .ride-chat-toggle {
    visibility:hidden;
  }
  body.guest-host-main .ride-chat .ride-chat-panel {
    position:fixed;
    top:calc(64px + var(--ride-chat-view-top,0px));
    right:12px;
    bottom:auto;
    width:min(340px,calc(100vw - 24px));
    height:min(calc(var(--ride-chat-view-height,100dvh) - 76px),calc(100dvh - 76px));
    max-height:calc(100dvh - 76px);
    margin:0;
  }
  body.guest-host-main .ride-chat .ride-chat-panel header {
    padding:5px 11px;
  }
  body.guest-host-main .ride-chat .ride-chat-panel header p {
    display:none;
  }
  body.guest-host-main .ride-chat .ride-chat-panel h2 {
    font-size:15px;
  }
  body.guest-host-main .ride-chat .ride-chat-scroll {
    min-height:0;
  }
  body.guest-host-main .ride-chat #rideChatForm {
    padding:5px 9px 7px;
  }
  body.guest-host-main .ride-chat #rideChatForm label {
    position:absolute;width:1px;height:1px;margin:-1px;padding:0;
    overflow:hidden;white-space:nowrap;clip-path:inset(50%);
  }
  body.guest-host-main .ride-chat #rideChatInput {
    min-height:38px;max-height:50px;resize:none;padding:6px 9px;
  }
  body.guest-host-main .ride-chat .ride-chat-send-row {
    margin-top:3px;
  }
  body.guest-host-main .ride-chat #rideChatSend {
    min-height:34px;
  }
  body.guest-host-main .ride-chat.compact-compose .ride-chat-panel {
    top:calc(8px + var(--ride-chat-view-top,0px));
    height:min(calc(var(--ride-chat-view-height,100dvh) - 16px),calc(100dvh - 16px));
    max-height:calc(100dvh - 16px);
  }
}

/* Compact phones still watch the real outing while chatting. The quick
   reactions return when chat closes rather than burying the host picture. */
@media (max-width:520px) and (min-height:568px) and (max-height:739px) and (orientation:portrait) {
  body.guest-host-main .ride-chat.landscape-watch-chat:not(.compact-compose) {
    bottom:calc(12px + env(safe-area-inset-bottom));
  }
  body.guest-host-main:has(.ride-chat.landscape-watch-chat) #guestPanel .ride-buttons {
    display:none;
  }
  body.guest-host-main .ride-chat.landscape-watch-chat:has(.ride-chat-panel[hidden]) {
    bottom:calc(16px + env(safe-area-inset-bottom));
  }
  body.guest-host-main .ride-chat.landscape-watch-chat #rideChatEmpty {
    padding:3px 0;
    line-height:1.35;
  }
  body.guest-host-main .ride-chat.landscape-watch-chat #rideChatForm {
    padding:3px 10px 4px;
  }
}

/* On a truly short landscape phone the normal floating chat launcher rises
   onto the rightmost voice action. Put the closed launcher in the clear corner
   between the topbar and the rider-presence row; the open chat panel keeps its
   existing full-size placement. */
@media (min-width:521px) and (max-width:600px) and (max-height:360px) {
 body.guest-host-main:has(#guestPanel:not([hidden])) .ride-chat:not(:has(.ride-chat-panel:not([hidden]))) {
  top:88px;bottom:auto;right:12px;
 }
}

/* --- Zero-overlap chat panel bounds across mobile portrait & landscape --- */
@media (max-width: 600px) {
  .ride-chat-toggle:not(.has-unread) {
    min-height: 40px;
    min-width: 58px;
    padding: 8px 10px;
    font-size: 12px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-panel {
    top: max(calc(234px + var(--ride-chat-view-top, 0px) + env(safe-area-inset-top, 0px)), calc(var(--ride-status-bottom, 226px) + 8px));
    height: auto;
    max-height: calc(var(--ride-chat-view-height, 100dvh) - 504px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-panel header {
    padding: 6px 12px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-panel header p {
    display: none;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-panel h2 {
    font-size: 15px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-panel header button {
    width: 32px;
    height: 32px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-scroll {
    min-height: 28px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) #rideChatEmpty {
    padding: 8px 0;
    font-size: 12px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) #rideChatForm {
    padding: 6px 10px 8px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) #rideChatForm label {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    white-space: nowrap;
    clip-path: inset(50%);
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) #rideChatInput {
    min-height: 36px;
    max-height: 48px;
    resize: none;
    padding: 6px 8px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-send-row {
    margin-top: 4px;
  }
  body:not(.guest-host-main):has(#guestPanel:not([hidden])) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) #rideChatSend {
    min-height: 34px;
  }
  .ride-chat.compact-compose .ride-chat-panel,
  .ride-chat.landscape-watch-chat.compact-compose .ride-chat-panel {
    top: calc(68px + var(--ride-chat-view-top, 0px) + env(safe-area-inset-top, 0px));
    height: min(390px, calc(var(--ride-chat-view-height, 100dvh) - 76px - env(safe-area-inset-top, 0px)));
    max-height: calc(var(--ride-chat-view-height, 100dvh) - 76px - env(safe-area-inset-top, 0px));
  }
}
@media (max-width: 600px) and (max-height: 760px) {
  body:not(.guest-host-main):has(#rideChatPanel:not([hidden])) #guestPanel {
    display: none !important;
  }
  body:not(.guest-host-main) .ride-chat:not(.landscape-watch-chat):not(.compact-compose) .ride-chat-panel {
    top: max(calc(234px + var(--ride-chat-view-top, 0px) + env(safe-area-inset-top, 0px)), calc(var(--ride-status-bottom, 226px) + 8px));
    height: auto;
    max-height: calc(var(--ride-chat-view-height, 100dvh) - max(246px, calc(var(--ride-status-bottom, 226px) + 20px)) - env(safe-area-inset-bottom, 0px));
  }
}
@media (min-width: 521px) and (max-height: 500px) {
  .ride-chat:has(.ride-chat-panel:not([hidden])) .ride-chat-toggle {
    visibility: hidden;
  }
  .ride-chat .ride-chat-panel,
  body.guest-host-main .ride-chat .ride-chat-panel,
  body.guest-host-main .ride-chat.compact-compose .ride-chat-panel {
    position: fixed;
    top: calc(58px + var(--ride-chat-view-top, 0px) + env(safe-area-inset-top, 0px));
    right: calc(12px + env(safe-area-inset-right, 0px));
    bottom: auto;
    width: min(340px, calc(100vw - 24px));
    height: min(calc(var(--ride-chat-view-height, 100dvh) - 68px), calc(100dvh - 68px));
    max-height: calc(100dvh - 68px);
    margin: 0;
  }
  .ride-chat .ride-chat-panel header {
    padding: 5px 11px;
  }
  .ride-chat .ride-chat-panel header p {
    display: none;
  }
  .ride-chat .ride-chat-panel h2 {
    font-size: 15px;
  }
  .ride-chat .ride-chat-scroll {
    min-height: 0;
  }
  .ride-chat #rideChatForm {
    padding: 5px 9px 7px;
  }
  .ride-chat #rideChatForm label {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    white-space: nowrap;
    clip-path: inset(50%);
  }
  .ride-chat #rideChatInput {
    min-height: 36px;
    max-height: 46px;
    resize: none;
    padding: 6px 9px;
  }
  .ride-chat .ride-chat-send-row {
    margin-top: 3px;
  }
  .ride-chat #rideChatSend {
    min-height: 32px;
  }
}
