.chat-shell {
    display: flex;
    flex-direction: column;
    height: calc(100dvh - var(--topnav-offset));
    background: var(--background);
    color: var(--foreground);
    width: 100%;
}
main:has(.chat-shell) {
  padding: 0;
}

body:has(.chat-shell) {
  gap: 0;
  height: 100dvh;
  overflow: hidden;
}

@media (max-width: 768px) {
  .chat-shell {
    height: calc(100dvh - var(--topnav-offset) - var(--tab-bar-height));
  }
  /* body:has(.chat-composer input[type="text"]:focus) .chat-shell {
    height: calc(100dvh - var(--topnav-offset));
  } */
  /* Composer sits above tab bar */
  /* .chat-composer {
    z-index: 11;
    position: relative;
  } */
}

.chat-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--card);
    color: var(--card-foreground);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.chat-header h3 { margin: 0; }
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    background: var(--faint);
}
.msg-row {
    display: flex;
    flex-direction: column;
    max-width: 70%;
}
.msg-row.mine {
    align-self: flex-end;
    align-items: flex-end;
}
.msg-row.theirs {
    align-self: flex-start;
    align-items: flex-start;
}
.msg-bubble {
    padding: 0.5rem 0.85rem;
    border-radius: 1rem;
    background: var(--secondary);
    color: var(--secondary-foreground);
    word-break: break-word;
}
.msg-row.mine .msg-bubble {
    background: var(--primary);
    color: var(--primary-foreground);
}
/* Emergency Message Bubbles - Enhanced Styling */
.msg-row.emergency .msg-bubble {
  position: relative;
  border-radius: 0.75rem;
  padding: 0.75rem 1rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(192, 69, 63, 0.25);
}

/* Created State - Urgent, pulsing alert */
.msg-row.emergency[data-state="created"] .msg-bubble {
  background: linear-gradient(135deg, var(--danger) 0%, #9A3630 100%);
  color: var(--danger-foreground);
  border-left: 4px solid var(--warning);
  animation: emergency-pulse 2s ease-in-out infinite;
}

/* Acknowledged State - Attention received, calmer */
.msg-row.emergency[data-state="acknowledged"] .msg-bubble {
  background: var(--warning);
  color: var(--warning-foreground);
  border-left: 4px solid var(--danger);
}

/* Resolved State - Handled, subdued */
.msg-row.emergency[data-state="resolved"] .msg-bubble {
  background: var(--primary);
  color: var(--primary-foreground);
  border-left: 4px solid var(--success);
  opacity: 0.85;
}


/* Pulse animation for created/urgent alerts */
@keyframes emergency-pulse {
  0%, 100% {
    box-shadow: 0 2px 8px rgba(192, 69, 63, 0.25);
  }
  50% {
    box-shadow: 0 2px 16px rgba(192, 69, 63, 0.5);
  }
}

/* Status badge in meta */
.msg-meta .emergency-status {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.15rem 0.4rem;
  border-radius: 0.25rem;
  font-weight: 600;
}

.msg-meta .emergency-status[data-state="created"] {
  background: var(--danger);
  color: var(--danger-foreground);
}

.msg-meta .emergency-status[data-state="acknowledged"] {
  background: var(--warning);
  color: var(--warning-foreground);
}

.msg-meta .emergency-status[data-state="resolved"] {
  background: var(--primary);
  color: var(--primary-foreground);
}
.msg-meta {
    font-size: 0.75rem;
    color: var(--muted-foreground);
    margin-top: 0.15rem;
    padding: 0 0.5rem;
    display: flex;
    gap: 0.5rem;
    align-items: center;
    white-space: nowrap;
}
.chat-composer {
    padding: 0.75rem 1.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.chat-composer > form {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    margin: 0;
    flex: 1;
    min-width: 0;
}
.chat-composer input {
    flex: 1;
    margin: 0;
}
.chat-composer button {
    white-space: nowrap;
}
.msg-bubble img {
    height: 100%;
    width: auto;
    object-fit: contain;
    border-radius: 0.5rem;
    display: block;
    cursor: pointer;
}
.msg-bubble:has(img),
.msg-bubble:has(video),
.msg-bubble:has(.msg-location) {
    height: 200px;
    padding: 0;
    overflow: hidden;
}
.msg-location {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.msg-location iframe {
    flex: 1;
    width: 100%;
    border: none;
    border-radius: 0.5rem 0.5rem 0 0;
    display: block;
}
.msg-location a {
    padding: 0.25rem 0.5rem;
    font-size: 75%;
}
.msg-bubble video {
    height: 100%;
    width: auto;
    object-fit: contain;
    border-radius: 0.5rem;
    display: block;
    cursor: pointer;
}
.chat-lightbox-content video {
    width: 100%;
    max-height: 70vh;
    border-radius: 0.5rem;
}
.msg-bubble a {
    color: inherit;
    text-decoration: underline;
}
#chat-lightbox {
    width: 80vmin;
}
.chat-lightbox-content img {
    width: 100%;
    max-height: 70vh;
    object-fit: contain;
    border-radius: 0.5rem;
}

.msg-date {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}
.msg-date-label {
    font-size: var(--text-7);
    opacity: 0.75;
}
.msg-date-value {
    font-family: "Playfair Display";
    font-size: var(--text-3);
    font-weight: 600;
    line-height: 1.2;
}
.msg-date-countdown {
    font-size: var(--text-7);
    opacity: 0.75;
}

.file-preview-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    max-height: 50vh;
    overflow-y: auto;
}
.file-preview-container img,
.file-preview-container video {
    max-height: 200px;
    max-width: 100%;
    border-radius: 0.5rem;
    object-fit: contain;
}

.msg-reply-preview {
    display: flex;
    gap: 0.35rem;
    padding: 0.25rem 0.6rem;
    border-left: 3px solid var(--primary);
    background: var(--secondary);
    border-radius: 0.35rem;
    font-size: 0.75rem;
    margin-bottom: 0.15rem;
    max-width: 100%;
    overflow: hidden;
}
.msg-reply-author {
    font-weight: 600;
    white-space: nowrap;
}
.msg-reply-text {
    color: var(--muted-foreground);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-reply-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.75rem;
    border-left: 3px solid var(--primary);
    background: var(--secondary);
    border-radius: 0.35rem;
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
}
.chat-reply-bar-name {
    font-weight: 600;
    color: var(--primary);
    white-space: nowrap;
}
.chat-reply-bar-preview {
    color: var(--muted-foreground);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}
