/**
 * Messaging Styles - School Portal
 * FAB, modal, recipient picker, presets, and send form
 * Uses CSS variables from main portal styles
 */

/* ============================================
   Skeleton Loader Base
   ============================================ */
@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* Inbox item skeleton */
.inbox-skeleton {
  display: flex;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--border-color, #e2e8f0);
}

.inbox-skeleton-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
  background: linear-gradient(90deg, 
    var(--bg-card, #f7fafc) 25%, 
    var(--bg-card-hover, #edf2f7) 50%, 
    var(--bg-card, #f7fafc) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s ease-in-out infinite;
}

.inbox-skeleton-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.inbox-skeleton-line {
  height: 12px;
  border-radius: 4px;
  background: linear-gradient(90deg, 
    var(--bg-card, #f7fafc) 25%, 
    var(--bg-card-hover, #edf2f7) 50%, 
    var(--bg-card, #f7fafc) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s ease-in-out infinite;
}

.inbox-skeleton-line.name { width: 40%; }
.inbox-skeleton-line.preview { width: 80%; }
.inbox-skeleton-line.time { width: 25%; }

/* Message bubble skeleton */
.message-skeleton {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  max-width: 70%;
}

.message-skeleton.sent {
  align-self: flex-end;
}

.message-skeleton.received {
  align-self: flex-start;
}

.message-skeleton-bubble {
  height: 40px;
  border-radius: 16px;
  background: linear-gradient(90deg, 
    var(--bg-card, #f7fafc) 25%, 
    var(--bg-card-hover, #edf2f7) 50%, 
    var(--bg-card, #f7fafc) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s ease-in-out infinite;
}

.message-skeleton.sent .message-skeleton-bubble {
  width: 150px;
}

.message-skeleton.received .message-skeleton-bubble {
  width: 200px;
}

/* ============================================
   Floating Action Button (FAB)
   ============================================ */
.message-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4a90d9 0%, #2c5282 100%);
  color: white;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(74, 144, 217, 0.4);
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.message-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(74, 144, 217, 0.5);
}

.message-fab:active {
  transform: scale(0.95);
}

.message-fab:focus {
  outline: 3px solid rgba(74, 144, 217, 0.5);
  outline-offset: 2px;
}

/* FAB icon */
.message-fab svg {
  width: 28px;
  height: 28px;
  fill: currentColor;
}

/* ============================================
   Unread Badge
   ============================================ */
.unread-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  background: #dc3545;
  color: white;
  font-size: 12px;
  font-weight: 600;
  line-height: 20px;
  text-align: center;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(220, 53, 69, 0.3);
}

.unread-badge:empty {
  display: none;
}

/* ============================================
   Message Modal
   ============================================ */
html.messaging-modal-open,
body.messaging-modal-open {
  overflow: hidden !important;
  overscroll-behavior-y: none;
}

.message-modal {
  display: none;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 10000;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
  overscroll-behavior: contain;
}

.message-modal.active {
  display: flex;
}

#messageModal .modal-close {
  position: absolute !important;
  top: 10px !important;
  right: 10px !important;
  width: 38px !important;
  height: 38px !important;
  min-width: 38px !important;
  min-height: 38px !important;
  padding: 0 !important;
  margin: 0 !important;
  border: none !important;
  border-radius: 999px !important;
  background: rgba(0,0,0,0.28) !important;
  color: #fff !important;
  font-size: 22px !important;
  line-height: 38px !important;
  text-align: center !important;
  z-index: 20 !important;
}

.message-modal-content {
  background: var(--bg-container, #ffffff);
  border-radius: 16px;
  padding: 24px;
  max-width: 480px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.message-modal-content h3 {
  margin: 0 0 20px;
  color: var(--text-heading, #2c5282);
  font-size: 20px;
  font-weight: 600;
}

/* Form sections within modal */
.form-section {
  margin-bottom: 20px;
}

.form-section:last-child {
  margin-bottom: 0;
}

.form-section label {
  display: block;
  margin-bottom: 8px;
  color: var(--text-secondary, #4a5568);
  font-size: 14px;
  font-weight: 500;
}

/* Modal close button (X) */
.message-modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 32px;
  height: 32px;
  background: var(--bg-card, #f7fafc);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary, #4a5568);
  font-size: 18px;
  transition: background 0.2s;
}

.message-modal-close:hover {
  background: var(--bg-card-hover, #edf2f7);
}

/* ============================================
   Recipient Picker
   ============================================ */
.recipient-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.recipient-btn {
  padding: 10px 16px;
  min-height: 44px;
  background: var(--bg-card, #f7fafc);
  border: 2px solid var(--border-light, #e2e8f0);
  border-radius: 24px;
  color: var(--text-primary, #1a1a1a);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.recipient-btn:hover {
  background: var(--bg-nav-hover, #bee3f8);
  border-color: var(--border-primary, #4a90d9);
}

.recipient-btn.selected {
  background: var(--bg-nav-active, #4a90d9);
  border-color: var(--bg-nav-active, #4a90d9);
  color: white;
}

.recipient-btn.selected:hover {
  background: #3182ce;
  border-color: #3182ce;
}

/* Recipient emoji */
.recipient-btn .emoji {
  font-size: 16px;
}

/* ============================================
   Preset Grid
   ============================================ */
.preset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.preset-btn {
  padding: 12px 14px;
  min-height: 44px;
  background: var(--bg-card, #f7fafc);
  border: 1px solid var(--border-light, #e2e8f0);
  border-radius: 10px;
  color: var(--text-primary, #1a1a1a);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: all 0.2s ease;
  line-height: 1.3;
}

.preset-btn:hover {
  background: var(--bg-nav-hover, #bee3f8);
  border-color: var(--border-primary, #4a90d9);
  transform: translateY(-1px);
}

.preset-btn:active {
  transform: translateY(0);
  background: var(--bg-nav, #ebf4ff);
}

.preset-btn .preset-emoji {
  display: block;
  font-size: 20px;
  margin-bottom: 4px;
}

.preset-btn .preset-text {
  font-size: 13px;
  color: var(--text-secondary, #4a5568);
}

/* ============================================
   Free-form Textarea & Character Counter
   ============================================ */
.message-input-wrapper {
  position: relative;
}

.message-textarea {
  width: 100%;
  min-height: 100px;
  padding: 14px;
  font-size: 16px; /* Prevents iOS zoom on focus */
  font-family: inherit;
  line-height: 1.5;
  color: var(--text-primary, #1a1a1a);
  background: var(--bg-card, #f7fafc);
  border: 2px solid var(--border-light, #e2e8f0);
  border-radius: 12px;
  resize: vertical;
  box-sizing: border-box;
  transition: border-color 0.2s ease;
}

.message-textarea:focus {
  outline: none;
  border-color: var(--border-primary, #4a90d9);
}

.message-textarea::placeholder {
  color: var(--text-muted, #718096);
}

.char-counter {
  text-align: right;
  font-size: 12px;
  color: var(--text-muted, #718096);
  margin-top: 6px;
}

.char-counter.warning {
  color: #d69e2e;
}

.char-counter.error {
  color: #e53e3e;
}

/* ============================================
   Send Button
   ============================================ */
.send-btn {
  width: 100%;
  padding: 14px 24px;
  min-height: 48px;
  background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.send-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #2f855a 0%, #276749 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(56, 161, 105, 0.3);
}

.send-btn:active:not(:disabled) {
  transform: translateY(0);
}

.send-btn:disabled {
  background: var(--bg-badge, #e2e8f0);
  color: var(--text-muted, #718096);
  cursor: not-allowed;
  opacity: 0.7;
}

.send-btn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

/* Loading state */
.send-btn.loading {
  pointer-events: none;
}

.send-btn.loading::after {
  content: '';
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-left: 8px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ============================================
   Status Messages
   ============================================ */
.send-status {
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  margin-top: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.send-status.success {
  background: rgba(56, 161, 105, 0.1);
  color: #276749;
  border: 1px solid rgba(56, 161, 105, 0.3);
}

.send-status.success::before {
  content: '✓';
  font-weight: bold;
}

.send-status.error {
  background: rgba(229, 62, 62, 0.1);
  color: #c53030;
  border: 1px solid rgba(229, 62, 62, 0.3);
}

.send-status.error::before {
  content: '✗';
  font-weight: bold;
}

/* ============================================
   Focus Visible for Keyboard Navigation
   ============================================ */
.inbox-item:focus-visible,
.message-bubble:focus-visible,
.preset-btn:focus-visible,
.recipient-btn:focus-visible,
button:focus-visible {
  outline: 2px solid var(--primary, #4a90d9);
  outline-offset: 2px;
}

.inbox-item:focus:not(:focus-visible),
.message-bubble:focus:not(:focus-visible) {
  outline: none;
}

/* ============================================
   Dark Mode Overrides
   ============================================ */
[data-theme="dark"] .message-fab {
  background: linear-gradient(135deg, #63b3ed 0%, #4a90d9 100%);
  box-shadow: 0 4px 12px rgba(99, 179, 237, 0.3);
}

[data-theme="dark"] .message-fab:hover {
  box-shadow: 0 6px 20px rgba(99, 179, 237, 0.4);
}

[data-theme="dark"] .unread-badge {
  background: #f56565;
}

[data-theme="dark"] .message-modal {
  background: rgba(0, 0, 0, 0.7);
}

[data-theme="dark"] .message-modal-content {
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
}

[data-theme="dark"] .message-modal-close {
  background: var(--bg-card-hover, #4a5568);
}

[data-theme="dark"] .message-modal-close:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .recipient-btn {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
}

[data-theme="dark"] .recipient-btn:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .preset-btn {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
}

[data-theme="dark"] .preset-btn:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .message-textarea {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
  color: var(--text-primary, #e2e8f0);
}

[data-theme="dark"] .message-textarea::placeholder {
  color: var(--text-muted, #718096);
}

[data-theme="dark"] .send-btn:disabled {
  background: var(--bg-badge, #4a5568);
  color: var(--text-muted, #718096);
}

[data-theme="dark"] .send-status.success {
  background: rgba(104, 211, 145, 0.15);
  color: #68d391;
  border-color: rgba(104, 211, 145, 0.3);
}

[data-theme="dark"] .send-status.error {
  background: rgba(252, 129, 129, 0.15);
  color: #fc8181;
  border-color: rgba(252, 129, 129, 0.3);
}

/* ============================================
   Mobile Responsive Adjustments
   ============================================ */
@media (max-width: 768px) {
  .message-fab {
    width: 52px;
    height: 52px;
    bottom: 20px;
    right: 16px;
    font-size: 22px;
  }

  .message-fab svg {
    width: 24px;
    height: 24px;
  }

  .unread-badge {
    min-width: 18px;
    height: 18px;
    font-size: 11px;
    line-height: 18px;
    top: -2px;
    right: -2px;
  }

  .message-modal {
    padding: 0;
    align-items: stretch;
  }

  .message-modal-content {
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    padding: 16px 12px calc(92px + env(safe-area-inset-bottom, 0px)) 12px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .message-modal-content h3 {
    font-size: 18px;
  }

  .form-section {
    margin-bottom: 16px;
  }

  .recipient-picker {
    gap: 6px;
  }

  .recipient-btn {
    padding: 8px 14px;
    font-size: 13px;
    min-height: 44px;
  }

  .preset-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
  }

  .preset-btn {
    padding: 10px 12px;
    min-height: 44px;
  }

  .message-textarea {
    min-height: 80px;
    padding: 12px;
  }

  .send-btn {
    padding: 12px 20px;
    min-height: 48px;
  }

  .send-status {
    padding: 10px 14px;
    font-size: 13px;
  }
}

/* ============================================
   Prevent Horizontal Overflow
   ============================================ */
.modal-content,
.message-modal-content {
  max-width: 100%;
  box-sizing: border-box;
  overflow-x: hidden;
}

.inbox-item,
.message-bubble {
  max-width: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Long names/text handling */
.inbox-item-name,
.inbox-item-sender,
.thread-recipient-name,
.thread-participant {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================
   iOS Safe Area Support
   ============================================ */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .message-fab {
    bottom: calc(24px + env(safe-area-inset-bottom, 0));
  }
  
  .message-modal-content {
    padding-bottom: calc(24px + env(safe-area-inset-bottom, 0));
  }
  
  .message-input-container {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
}

/* Extra small screens - iPhone SE (320px) */
@media (max-width: 380px) {
  .preset-grid {
    grid-template-columns: 1fr 1fr;
  }
  
  .message-modal-content {
    padding: 1rem;
  }
  
  .inbox-item {
    padding: 10px;
  }
  
  .message-input-container {
    flex-direction: column;
    gap: 8px;
  }
  
  .message-input-container textarea {
    width: 100%;
  }
  
  .message-input-container button {
    width: 100%;
  }
  
  .emoji-picker-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ============================================
   Modal Tabs
   ============================================ */
.modal-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 20px;
  border-bottom: 2px solid var(--border-light, #e2e8f0);
  padding-right: 48px; /* keep tabs clear of close button */
}

.modal-tab {
  flex: 1;
  padding: 12px 16px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary, #4a5568);
  cursor: pointer;
  transition: all 0.2s ease;
}

.modal-tab:hover {
  color: var(--text-primary, #1a1a1a);
  background: var(--bg-nav-hover, #ebf4ff);
}

.modal-tab.active {
  color: var(--border-primary, #4a90d9);
  border-bottom-color: var(--border-primary, #4a90d9);
}

.modal-view {
  display: none;
}

.modal-view.active {
  display: block;
  height: auto !important;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto !important;
  padding-bottom: calc(88px + env(safe-area-inset-bottom, 0px));
}

/* Inbox view uses grid - no bottom padding needed */
.modal-view[data-view="inbox"].active {
  padding-bottom: 0 !important;
}

.inbox-list-view {
  display: none;
}

.inbox-list-view.active {
  display: block;
}

[data-theme="dark"] .modal-tab:hover {
  background: var(--bg-nav-hover, #4a5568);
}

/* ============================================
   Inbox View
   ============================================ */
.inbox-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.inbox-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-heading, #2c5282);
  margin: 0;
}

.inbox-refresh-btn {
  padding: 8px 12px;
  background: var(--bg-card, #f7fafc);
  border: 1px solid var(--border-light, #e2e8f0);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}

.inbox-refresh-btn:hover {
  background: var(--bg-nav-hover, #ebf4ff);
}

.inbox-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 400px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.inbox-item {
  position: relative;
  border-radius: 10px;
  margin-bottom: 10px;
  overflow: hidden;
  background: var(--bg-card, #f7fafc);
}

.inbox-item-main {
  display: flex;
  align-items: stretch;
  gap: 12px;
  padding: 14px;
  cursor: pointer;
  position: relative;
  z-index: 2;
  background: inherit;
  transition: transform 0.22s ease, background 0.2s;
}

.inbox-item:hover .inbox-item-main {
  background: var(--bg-nav-hover, #ebf4ff);
}

.inbox-item.unread .inbox-item-main {
  background: #ebf4ff;
  font-weight: 600;
}

.inbox-item.unread .inbox-item-sender {
  font-weight: 700;
}

.inbox-item.swipe-open .inbox-item-main {
  transform: translateX(-96px);
}

.inbox-item-avatar {
  font-size: 24px;
  flex-shrink: 0;
  width: 56px;
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-badge, #e2e8f0);
  border-radius: 14px;
  overflow: hidden;
  align-self: stretch;
}

.inbox-item-avatar .avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.inbox-item-swipe-action {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 96px;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  z-index: 1;
}

.inbox-item-delete-action {
  width: 100%;
  border: none;
  background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

/* Avatar image (custom emoji) */
.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

/* Avatar initial fallback */
.avatar-initial {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-secondary, #4a5568);
  background: var(--bg-badge, #e2e8f0);
  border-radius: 14px;
  text-transform: uppercase;
}

.inbox-item-content {
  flex: 1;
  min-width: 0;
}

.inbox-item-sender {
  font-size: 14px;
  color: var(--text-primary, #1a1a1a);
  margin-bottom: 4px;
}

.inbox-item-preview {
  font-size: 13px;
  color: var(--text-secondary, #4a5568);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 400;
}

.inbox-item-time {
  font-size: 12px;
  color: var(--text-muted, #718096);
  margin-top: 4px;
}

.inbox-item-desktop-delete {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 6px 8px;
  background: rgba(229, 62, 62, 0.1);
  border: 1px solid rgba(229, 62, 62, 0.35);
  border-radius: 8px;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, background 0.2s;
  font-size: 14px;
  line-height: 1;
}

.inbox-item:hover .inbox-item-desktop-delete,
.inbox-item:focus-within .inbox-item-desktop-delete {
  opacity: 1;
  pointer-events: auto;
}

.inbox-item-desktop-delete:hover {
  background: rgba(229, 62, 62, 0.2);
}

@media (hover: none) {
  .inbox-item-desktop-delete {
    display: none;
  }
}

.inbox-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted, #718096);
  font-size: 18px;
}

.inbox-error {
  text-align: center;
  padding: 20px;
  color: #c53030;
  background: rgba(229, 62, 62, 0.1);
  border-radius: 10px;
}

.inbox-loading {
  text-align: center;
  padding: 20px;
  color: var(--text-muted, #718096);
}

[data-theme="dark"] .inbox-item.unread {
  background: rgba(74, 144, 217, 0.15);
}

[data-theme="dark"] .inbox-item.unread .inbox-item-main {
  background: rgba(74, 144, 217, 0.15);
}

@media (prefers-color-scheme: dark) {
  .inbox-item.unread .inbox-item-main {
    background: rgba(74, 144, 217, 0.15);
  }
}

[data-theme="dark"] .inbox-item.unread .inbox-item-sender {
  color: #fff;
}

[data-theme="dark"] .inbox-item.unread .inbox-item-preview {
  color: rgba(255, 255, 255, 0.8);
}

[data-theme="dark"] .inbox-item.unread .inbox-item-time {
  color: rgba(255, 255, 255, 0.6);
}

/* Arthur Typing Indicator */
.arthur-typing-bubble {
  display: inline-block;
  padding: 12px 18px;
  background: var(--bg-card, #3d4a5c);
  border-radius: 18px 18px 18px 4px;
  margin: 8px 0;
  max-width: 80px;
}

.arthur-typing-dots {
  display: flex;
  gap: 4px;
  align-items: center;
}

.arthur-typing-dots span {
  width: 8px;
  height: 8px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  animation: arthurTypingPulse 1.4s ease-in-out infinite;
}

.arthur-typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.arthur-typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes arthurTypingPulse {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

[data-theme="dark"] .avatar-initial {
  background: var(--bg-badge, #4a5568);
  color: var(--text-secondary, #a0aec0);
}

[data-theme="dark"] .inbox-item {
  background: var(--bg-card, #3d4a5c);
}

[data-theme="dark"] .inbox-item:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .inbox-refresh-btn {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
}

[data-theme="dark"] .inbox-refresh-btn:hover {
  background: var(--bg-nav-hover, #4a5568);
}

/* ============================================
   Message Detail View
   ============================================ */
.message-detail {
  display: none;
}

.message-detail.active {
  display: block;
}

.message-detail-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-light, #e2e8f0);
}

.message-detail-back {
  padding: 8px 12px;
  background: var(--bg-card, #f7fafc);
  border: 1px solid var(--border-light, #e2e8f0);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.message-detail-back:hover {
  background: var(--bg-nav-hover, #ebf4ff);
}

.message-detail-sender {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-heading, #2c5282);
}

.message-detail-time {
  font-size: 12px;
  color: var(--text-muted, #718096);
  margin-left: auto;
}

.message-detail-content {
  padding: 16px;
  background: var(--bg-card, #f7fafc);
  border-radius: 10px;
  line-height: 1.6;
  color: var(--text-primary, #1a1a1a);
  margin-bottom: 16px;
}

.message-detail-actions {
  display: flex;
  gap: 10px;
}

.reply-btn {
  flex: 1;
  padding: 12px 16px;
  background: linear-gradient(135deg, #4a90d9 0%, #2c5282 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.reply-btn:hover {
  background: linear-gradient(135deg, #3182ce 0%, #2a4365 100%);
}

.delete-detail-btn {
  padding: 12px 16px;
  background: var(--bg-card, #f7fafc);
  border: 1px solid #e53e3e;
  color: #e53e3e;
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.2s;
}

.delete-detail-btn:hover {
  background: rgba(229, 62, 62, 0.1);
}

/* ============================================
   Reply View
   ============================================ */
.reply-view {
  display: none;
}

.reply-view.active {
  display: block;
}

.reply-quote {
  background: var(--bg-card, #f7fafc);
  border-left: 3px solid var(--border-primary, #4a90d9);
  padding: 12px 14px;
  margin-bottom: 16px;
  border-radius: 0 8px 8px 0;
}

.reply-quote-sender {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary, #4a5568);
  margin-bottom: 4px;
}

.reply-quote-content {
  font-size: 14px;
  color: var(--text-muted, #718096);
  font-style: italic;
}

.reply-textarea {
  width: 100%;
  min-height: 100px;
  padding: 14px;
  font-size: 16px;
  font-family: inherit;
  line-height: 1.5;
  color: var(--text-primary, #1a1a1a);
  background: var(--bg-card, #f7fafc);
  border: 2px solid var(--border-light, #e2e8f0);
  border-radius: 12px;
  resize: vertical;
  box-sizing: border-box;
  margin-bottom: 12px;
}

.reply-textarea:focus {
  outline: none;
  border-color: var(--border-primary, #4a90d9);
}

.reply-actions {
  display: flex;
  gap: 10px;
}

.reply-cancel-btn {
  padding: 12px 16px;
  background: var(--bg-card, #f7fafc);
  border: 1px solid var(--border-light, #e2e8f0);
  border-radius: 10px;
  cursor: pointer;
  font-size: 15px;
}

.reply-cancel-btn:hover {
  background: var(--bg-nav-hover, #ebf4ff);
}

.reply-send-btn {
  flex: 1;
  padding: 12px 16px;
  background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}

.reply-send-btn:hover {
  background: linear-gradient(135deg, #2f855a 0%, #276749 100%);
}

[data-theme="dark"] .message-detail-back,
[data-theme="dark"] .reply-cancel-btn {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
}

[data-theme="dark"] .message-detail-content,
[data-theme="dark"] .reply-quote {
  background: var(--bg-card, #3d4a5c);
}

[data-theme="dark"] .reply-textarea {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
}

[data-theme="dark"] .delete-detail-btn {
  background: transparent;
  border-color: #fc8181;
  color: #fc8181;
}

[data-theme="dark"] .delete-detail-btn:hover {
  background: rgba(252, 129, 129, 0.15);
}

/* ============================================
   Thread View Container
   ============================================ */
.thread-view {
  display: none;
}

.thread-view.active {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.thread-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-light, #e2e8f0);
}

.thread-back-btn {
  padding: 8px 12px;
  background: var(--bg-card, #f7fafc);
  border: 1px solid var(--border-light, #e2e8f0);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.thread-back-btn:hover {
  background: var(--bg-nav-hover, #ebf4ff);
}

.thread-participant {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-heading, #2c5282);
}

.thread-messages {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px 8px 8px 8px;
  max-height: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  position: relative;
}

/* ============================================
   Pull UP to Refresh (at bottom)
   ============================================ */
.pull-up-refresh {
  position: sticky;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted, #718096);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  background: transparent;
}

.pull-up-refresh.visible {
  opacity: 1;
}

.pull-refresh-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border-light, rgba(255,255,255,0.2));
  border-top-color: var(--accent-color, #4a90d9);
  border-radius: 50%;
  margin-right: 8px;
}

.pull-up-refresh.refreshing .pull-refresh-spinner {
  animation: ptr-spin 0.8s linear infinite;
}

@keyframes ptr-spin {
  to { transform: rotate(360deg); }
}

.thread-messages.pulling-up {
  transform: translateY(calc(-1 * var(--pull-up-distance, 0px)));
  transition: none;
}

.thread-messages.snapping {
  transition: transform 0.3s ease;
}

/* ============================================
   Game Board (Tic-Tac-Toe)
   ============================================ */
.game-container {
  padding: 8px;
  text-align: center;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  box-sizing: border-box;
}

.game-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  width: 234px;
  margin: 0 auto 12px;
  background: rgba(255,255,255,0.15);
  border-radius: 10px;
  padding: 5px;
}

.game-cell {
  background: var(--bg-container, #2d3748);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 42px;
  font-weight: bold;
  width: 70px;
  height: 70px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.game-cell.clickable {
  cursor: pointer;
}

.game-cell.clickable:hover {
  background: var(--bg-nav-hover, rgba(255,255,255,0.1));
  transform: scale(1.05);
}

.game-cell.clickable:active {
  transform: scale(0.95);
  background: var(--bg-nav-hover, rgba(255,255,255,0.15));
}

.game-cell span {
  pointer-events: none;
}

.x-symbol {
  color: #f56565;
}

.o-symbol {
  color: #4299e1;
}

.game-status {
  font-size: 20px;
  font-weight: 700;
  color: #ffffff;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
  margin-bottom: 10px;
  letter-spacing: 0.3px;
}

.game-rematch-btn {
  background: var(--accent-color, #4a90d9);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.game-rematch-btn:hover {
  background: var(--accent-hover, #357abd);
}

/* Game status - winner states */
.game-status.winner {
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 10px;
}

.game-status.you-won {
  color: #22c55e;
  text-shadow: 0 0 8px rgba(34, 197, 94, 0.3);
}

.game-status.you-lost {
  color: #ef4444;
  text-shadow: 0 0 8px rgba(239, 68, 68, 0.3);
}

.game-status.draw {
  color: #f59e0b;
  text-shadow: 0 0 8px rgba(245, 158, 11, 0.3);
}

/* Winner celebration overlay */
.game-celebration-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  animation: celebration-fade-in 0.3s ease-out;
}

@keyframes celebration-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.game-celebration-overlay .winner-avatar {
  width: 300px;
  height: 300px;
  border-radius: 50%;
  object-fit: cover;
  border: 6px solid #22c55e;
  box-shadow: 0 0 60px rgba(34, 197, 94, 0.6);
  animation: winner-bounce 0.6s ease-out;
}

@keyframes winner-bounce {
  0% { transform: scale(0.3); opacity: 0; }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

.game-celebration-overlay .winner-text {
  color: white;
  font-size: 28px;
  font-weight: 700;
  margin-top: 20px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.game-celebration-overlay .winner-emoji {
  font-size: 48px;
  margin-top: 10px;
  animation: emoji-pop 0.4s ease-out 0.3s both;
}

@keyframes emoji-pop {
  0% { transform: scale(0); }
  80% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

/* ============================================
   Swipe to Delete (iOS style)
   ============================================ */
.thread-message {
  position: relative;
  overflow: visible;
  transform: translateX(var(--swipe-x, 0));
  transition: transform 0.2s ease;
}

.thread-message.swiping {
  transition: none;
}

.thread-message::after {
  content: 'Delete';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 80px;
  background: #e53e3e;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  transform: translateX(100%);
  opacity: 0;
  transition: opacity 0.2s ease;
  border-radius: 8px;
  cursor: pointer;
}

.thread-message.swipe-active::after,
.thread-message.swipe-open::after {
  opacity: 1;
}

.thread-message.swipe-open {
  transform: translateX(-80px);
}

/* ============================================
   Chat Bubble Base
   ============================================ */
.thread-message {
  display: flex;
  width: 100%;
  gap: 8px;
}

/* Message avatar in thread view */
.message-avatar {
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-badge, #e2e8f0);
}

.message-avatar .avatar-img {
  width: 100%;
  height: 100%;
}

.message-avatar .avatar-initial {
  font-size: 18px;
}

.thread-message .message-avatar {
  font-size: 20px;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-badge, #e2e8f0);
  border-radius: 50%;
  flex-shrink: 0;
}

.message-bubble {
  padding: 10px 14px;
  line-height: 1.4;
  word-wrap: break-word;
}

.message-bubble .message-sender {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary, #4a5568);
  margin-bottom: 4px;
}

.message-bubble .message-content {
  font-size: 16px;
}

.message-bubble .message-time {
  font-size: 11px;
  color: var(--text-muted, #718096);
  margin-top: 2px;
  text-align: right;
  line-height: 1;
}

/* ============================================
   Sent Messages (right-aligned, blue)
   ============================================ */
.thread-message.sent {
  justify-content: flex-end;
}

.thread-message.sent .message-bubble {
  max-width: 80%;
  background: linear-gradient(135deg, #4a90d9, #2c5282);
  color: white;
  border-radius: 18px 18px 4px 18px;
}

.thread-message.sent .message-bubble .message-sender {
  display: none; /* Don't show sender name for own messages */
}

.thread-message.sent .message-bubble .message-time {
  color: rgba(255, 255, 255, 0.7);
}

/* ============================================
   Received Messages (left-aligned, gray)
   ============================================ */
.thread-message.received {
  justify-content: flex-start;
}

.thread-message.received .message-bubble {
  max-width: 80%;
  background: var(--bg-card, #f7fafc);
  color: var(--text-primary, #1a1a1a);
  border-radius: 18px 18px 18px 4px;
}

/* Game messages: allow wider bubble for game boards */
.message-bubble:has(.game-container) {
  max-width: min(92%, 340px) !important;
  padding: 12px !important;
}

/* Emoji-large messages: remove balloon chrome */
.message-bubble.emoji-large-bubble {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  padding: 4px 0 !important;
}
.message-bubble.emoji-large-bubble .bubble-time {
  margin-top: 6px;
  opacity: 0.8;
}

/* ============================================
   Thread Input Bar (iMessage Style)
   ============================================ */
.thread-input-bar {
  position: sticky;
  bottom: 0;
  z-index: 20;
  display: flex;
  gap: 12px;
  align-items: flex-end;
  margin-top: 0;
  padding: 6px 10px calc(6px + env(safe-area-inset-bottom, 0px)) 10px;
  border-top: none;
  background: rgba(34, 45, 68, 0.98);
  backdrop-filter: blur(8px);
}

.native-reply-textarea {
  flex: 1;
  border: 1px solid var(--border-color, #cbd5e0);
  border-radius: 20px;
  padding: 10px 14px;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.4;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  background-color: var(--bg-card, #f7fafc);
  color: var(--text-primary, #1a202c);
  transition: border-color 0.2s;
  overflow-y: auto;
}

.native-reply-textarea:focus {
  outline: none;
  border-color: #4a90d9;
}

.native-reply-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background-color: #007aff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  margin-bottom: 3px;
  transition: all 0.2s;
}

.native-reply-btn:disabled {
  background-color: #cbd5e0;
  cursor: not-allowed;
}

.native-reply-btn:not(:disabled):hover {
  background-color: #0066d6;
  transform: scale(1.05);
}

.native-reply-btn:not(:disabled):active {
  transform: scale(0.95);
}

/* ============================================
   Dark Mode - Thread View
   ============================================ */
[data-theme="dark"] .thread-back-btn {
  background: var(--bg-card, #3d4a5c);
  border-color: var(--border-light, #4a5568);
}

[data-theme="dark"] .thread-back-btn:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .thread-message .message-avatar {
  background: var(--bg-badge, #4a5568);
}

[data-theme="dark"] .thread-message.sent .message-bubble {
  background: linear-gradient(135deg, #63b3ed, #4a90d9);
}

[data-theme="dark"] .thread-message.received .message-bubble {
  background: var(--bg-card, #3d4a5c);
  color: var(--text-primary, #e2e8f0);
}

[data-theme="dark"] .message-bubble .message-time {
  color: var(--text-muted, #a0aec0);
}

[data-theme="dark"] .thread-actions {
  border-color: var(--border-light, #4a5568);
}

/* ============================================
   Split View - iPad/Desktop
   ============================================ */
@media (min-width: 900px) {
  .message-modal {
    padding: 16px;
  }

  .message-modal-content {
    max-width: 1200px;
    width: min(96vw, 1200px);
    /* Use full available height - minimal padding */
    height: calc(var(--app-height, 100vh) - 16px);
    max-height: calc(var(--app-height, 100vh) - 16px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  
  /* Force modal view to fill content area */
  .message-modal-content > .modal-view {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    height: 100% !important;
  }
  
  /* Ensure inbox view fills available space */
  .modal-view[data-view="inbox"].active {
    flex: 1 !important;
    min-height: 0;
    height: 100% !important;
  }
  
  /* Thread view fills remaining space in grid */
  .modal-view[data-view="inbox"].active .thread-view {
    height: 100% !important;
    display: flex !important;
    flex-direction: column !important;
  }
  
  /* Messages area grows to fill space */
  .modal-view[data-view="inbox"].active .thread-messages {
    flex: 1 1 auto !important;
    min-height: 0;
  }
  
  /* Input bar stays at bottom */
  .modal-view[data-view="inbox"].active .thread-input-bar {
    flex-shrink: 0;
    margin-top: auto;
  }

  .modal-view.active {
    overflow: hidden !important;
  }
  
  /* Inbox view needs full height for grid layout */
  .modal-view[data-view="inbox"].active {
    height: 100% !important;
  }

  /* Wide mode: smaller inbox list, larger message area */
  .modal-view[data-view="inbox"].active {
    display: grid;
    grid-template-columns: 280px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    gap: 20px;
    height: 100% !important;
    min-height: 0 !important;
    align-items: stretch;
  }
  
  /* Force grid children to fill cell height */
  .modal-view[data-view="inbox"].active > * {
    min-height: 0 !important;
    height: 100% !important;
  }

  .inbox-list-view.active {
    display: flex;
    flex-direction: column;
    min-height: 0;
    border-right: 1px solid var(--border-light, #e2e8f0);
    padding-right: 12px;
  }

  .inbox-list {
    max-height: none;
    flex: 1;
    min-height: 0;
  }

  .thread-view.active {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .thread-back-btn {
    display: none;
  }

  .thread-messages {
    max-height: none;
    flex: 1;
    min-height: 0;
  }
  
  /* Larger input bar buttons for tablet touch */
  .thread-input-bar .attach-btn {
    width: 44px;
    height: 44px;
    font-size: 22px;
    min-width: 44px;
  }
  
  /* Larger game cells on wide screens for touch */
  .game-board {
    width: 234px;
  }
  
  .game-cell {
    width: 70px;
    height: 70px;
    font-size: 42px;
  }

  .thread-input-bar {
    position: sticky;
    bottom: 0;
    background: var(--bg-container, #ffffff);
    padding-bottom: 4px;
    margin-top: 8px;
  }
}

/* ============================================
   Mobile - Thread View
   ============================================ */
@media (max-width: 768px) {
  .thread-view.active {
    height: 100%;
    min-height: 0;
  }

  .thread-messages {
    max-height: none;
    flex: 1;
    min-height: 0;
    padding: 12px 4px 8px 4px;
  }
  
  .thread-message {
    max-width: 95%;
  }
  
  .message-bubble {
    padding: 8px 12px;
  }
  
  .message-bubble .message-content {
    font-size: 16px;
  }

  .thread-view.active .thread-input-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    margin-top: 0;
    z-index: 40;
  }
}


@media (max-width: 600px) {
  #messageModal .modal-content {
      width: 100%;
      height: 100%;
      max-height: 100%;
      margin: 0;
      border-radius: 0;
      display: flex;
      flex-direction: column;
  }
  
  .modal-body {
      flex: 1;
      overflow-y: auto;
  }
  
  .thread-messages {
      max-height: none;
      flex: 1;
      overflow-y: auto;
  }
}

/* Media attachments */
.attach-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border-light);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
.attach-btn:active {
  transform: scale(0.95);
  background: var(--bg-card-hover, #e2e8f0);
}
.composer-row { display:flex; gap:8px; align-items:flex-end; }

/* Override for grid layout - apply full grid template */
.composer-row.input-bar-grid { 
  display: grid !important;
  grid-template-columns: auto 1fr auto !important;
  grid-template-rows: 1fr 1fr 1fr !important;
  grid-template-areas:
    "game   input send"
    "emoji  input send"
    "media  input send" !important;
  gap: 4px 8px !important;
  align-items: stretch !important;
}
.media-preview-strip { display:flex; gap:8px; margin-top:8px; overflow-x:auto; }
.media-chip { width:68px; height:68px; border-radius:10px; overflow:hidden; border:1px solid var(--border-light); position:relative; flex:0 0 auto; background:var(--bg-nav); }
.media-chip img,.media-chip video { width:100%; height:100%; object-fit:cover; }
.media-chip-remove { position:absolute; top:2px; right:2px; width:20px; height:20px; border:none; border-radius:50%; background:rgba(0,0,0,.6); color:#fff; cursor:pointer; }
.bubble-media-grid { display:grid; grid-template-columns:repeat(auto-fill,minmax(120px,1fr)); gap:6px; margin-top:6px; }
.bubble-media-item { border-radius:10px; overflow:hidden; border:1px solid rgba(255,255,255,.2); position:relative; }
.bubble-media-item img,.bubble-media-item video { width:100%; height:140px; object-fit:cover; display:block; }
.media-download-btn { position:absolute; right:6px; bottom:6px; border:none; border-radius:999px; padding:4px 8px; background:rgba(0,0,0,.6); color:#fff; font-size:12px; }
.media-viewer-overlay { position:fixed; inset:0; background:rgba(0,0,0,.9); z-index:12000; display:flex; align-items:center; justify-content:center; }
.media-viewer-overlay img,.media-viewer-overlay video { max-width:95vw; max-height:90vh; object-fit:contain; }

/* Dedicated emoji fullscreen viewer */
.emoji-viewer-overlay {
  position: fixed;
  inset: 0;
  z-index: 12500;
  opacity: 0;
  transition: opacity 180ms ease;
}
.emoji-viewer-overlay.active { opacity: 1; }
.emoji-viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 6, 18, 0.94);
  backdrop-filter: blur(4px);
}
.emoji-viewer-shell {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 18px;
  box-sizing: border-box;
  transform: scale(0.98);
  transition: transform 180ms ease;
}
.emoji-viewer-overlay.active .emoji-viewer-shell { transform: scale(1); }
.emoji-viewer-close {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top, 0px));
  left: 12px;
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 50%;
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 30px;
  line-height: 1;
}
.emoji-viewer-title {
  position: absolute;
  top: calc(18px + env(safe-area-inset-top, 0px));
  left: 0;
  right: 0;
  text-align: center;
  color: #fff;
  font-weight: 700;
  font-size: 20px;
  pointer-events: none;
}
.emoji-viewer-stage {
  width: min(92vw, 640px);
  height: min(70vh, 640px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.emoji-viewer-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 16px;
  box-shadow: 0 14px 40px rgba(0,0,0,0.45);
  transition: transform 180ms ease;
}
.emoji-viewer-stage.zoomed .emoji-viewer-image { transform: scale(1.8); }
.emoji-viewer-fallback {
  font-size: min(32vw, 180px);
  line-height: 1;
}
.emoji-viewer-meta {
  position: absolute;
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  left: 0;
  right: 0;
  text-align: center;
  color: rgba(255,255,255,0.85);
  font-size: 13px;
}
.media-play-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 28px;
  text-shadow: 0 2px 8px rgba(0,0,0,.8);
  pointer-events: none;
}

/* ============================================
   Message Reactions
   ============================================ */

.reaction-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
  padding-top: 4px;
}

.reaction-badge {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px 8px;
  background: var(--bg-nav, #1a1a2e);
  border: 1px solid var(--border-color, #2d2d44);
  border-radius: 12px;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
  user-select: none;
}

.reaction-badge:hover {
  transform: scale(1.1);
  background: var(--bg-card-hover, #252540);
}

.reaction-badge:active {
  transform: scale(0.95);
}

.reaction-badge.mine {
  background: var(--accent-light, #3b82f620);
  border-color: var(--accent, #3b82f6);
}

.reaction-badge .reaction-count {
  font-size: 11px;
  color: var(--text-muted, #888);
  margin-left: 2px;
}

.reaction-badge.overflow {
  font-size: 11px;
  color: var(--text-muted, #888);
  background: transparent;
  border: none;
  padding: 2px 4px;
}

/* Position badges at bottom of bubble */
.message-bubble {
  position: relative;
}

/* Sent messages: badges on right */
.thread-message.sent .reaction-badges {
  justify-content: flex-end;
}

/* Received messages: badges on left */
.thread-message.received .reaction-badges {
  justify-content: flex-start;
}

/* ============================================
   Reaction Picker
   ============================================ */

.reaction-trigger {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: var(--bg-nav, #1a1a2e);
  border: 1px solid var(--border-color, #2d2d44);
  font-size: 14px;
  cursor: pointer;
  opacity: 0.8;
  transition: opacity 0.15s, transform 0.15s;
  z-index: 10;
}

.reaction-trigger:hover {
  opacity: 1;
  transform: scale(1.15);
}

.reaction-picker-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 10000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}

.reaction-picker-overlay.active {
  opacity: 1;
}

.reaction-picker-panel {
  width: 100%;
  max-width: 420px;
  max-height: 70vh;
  overflow-y: auto;
  background: var(--bg-container, #0f0f1a);
  border-radius: 20px 20px 0 0;
  padding: 12px 12px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translateY(100%);
  transition: transform 0.25s ease-out;
}

/* Swipe handle for closing */
.picker-swipe-handle {
  width: 40px;
  height: 5px;
  background: var(--text-muted, #6b7280);
  border-radius: 3px;
  margin: 0 auto 12px;
  opacity: 0.5;
}

.reaction-picker-overlay.active .reaction-picker-panel {
  transform: translateY(0);
}

.reaction-quick-picks {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding: 8px 0 12px;
  border-bottom: 1px solid var(--border-color, #2d2d44);
  max-width: 100%;
  overflow-x: hidden;
}

.reaction-quick-pick {
  font-size: 32px;
  width: 60px;
  height: 60px;
  padding: 4px;
  background: none;
  border: none;
  cursor: pointer;
  transition: transform 0.1s;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.reaction-quick-pick:hover {
  background: var(--bg-card-hover, #252540);
}

.reaction-quick-pick:active {
  transform: scale(1.3);
}

.reaction-expand-btn {
  display: block;
  width: 100%;
  padding: 12px;
  margin-top: 8px;
  background: none;
  border: 1px solid var(--border-color, #2d2d44);
  border-radius: 12px;
  color: var(--text-muted, #888);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}

.reaction-expand-btn:hover {
  background: var(--bg-card-hover, #252540);
}

.reaction-full-grid {
  max-height: 200px;
  overflow-y: auto;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color, #2d2d44);
}

.reaction-emoji-row {
  display: flex;
  justify-content: flex-start;
  gap: 4px;
  margin-bottom: 8px;
}

.reaction-grid-emoji {
  font-size: 24px;
  padding: 6px;
  background: none;
  border: none;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.1s, transform 0.1s;
}

.reaction-grid-emoji:hover {
  background: var(--bg-card-hover, #252540);
}

.reaction-grid-emoji:active {
  transform: scale(1.2);
}

/* Emoji-only large message rendering */
.emoji-large-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0 6px;
}
.emoji-large-item {
  object-fit: cover;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.18);
}
.emoji-large-text {
  font-size: 56px;
  line-height: 1;
}
.emoji-large-1 .emoji-large-item { width: 120px; height: 120px; }
.emoji-large-2 .emoji-large-item { width: 92px; height: 92px; }
.emoji-large-3 .emoji-large-item { width: 74px; height: 74px; }

.asset-hover-tooltip {
  position: fixed;
  transform: translate(-50%, 0);
  background: rgba(18,18,22,0.95);
  color: #fff;
  font-size: 12px;
  line-height: 1.2;
  padding: 6px 10px;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.35);
  z-index: 12000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.16s ease;
  max-width: 200px;
  text-align: center;
}
.asset-hover-tooltip.show { opacity: 1; }

/* Long-press visual feedback */
.message-bubble.pressing {
  transform: scale(0.98);
  transition: transform 0.1s;
}

/* Desktop: show trigger on hover */
@media (hover: hover) {
  .thread-message.received .message-bubble:hover {
    cursor: pointer;
  }
}

/* Prevent ALL native touch behaviors on received bubbles for clean long-press */
.thread-message.received .message-bubble {
  -webkit-user-select: none !important;
  user-select: none !important;
  -webkit-touch-callout: none !important;
  -webkit-tap-highlight-color: transparent !important;
}

/* Specifically block image long-press menu */
.thread-message.received .message-bubble img {
  pointer-events: none;
}

/* Ensure reaction badges are clickable */
.reaction-badge {
  pointer-events: auto !important;
  cursor: pointer !important;
  -webkit-tap-highlight-color: rgba(0,0,0,0.1) !important;
}

/* Received messages: flex row for bubble + react button */
.thread-message.received {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 8px;
}

/* Reaction button - hidden by default, shown on tap */
.bubble-react-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--accent, #3b82f6);
  color: white;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  opacity: 0;
  transform: scale(0.5);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s, background 0.15s;
}

.bubble-react-btn.visible {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.bubble-react-btn:active {
  transform: scale(1.15);
  background: var(--accent-hover, #2563eb);
}

/* Preferred split: desktop hover trigger only, mobile inline button only */
@media (hover: hover) and (pointer: fine) {
  .bubble-react-btn {
    display: none !important;
  }
}

@media (hover: none), (pointer: coarse) {
  .reaction-trigger {
    display: none !important;
  }
}

/* ============================================================================
   DIFFICULTY PICKER (for Arthur games)
   ============================================================================ */

.difficulty-picker-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}

.difficulty-picker-overlay.active {
  opacity: 1;
}

.difficulty-picker-panel {
  width: 90%;
  max-width: 320px;
  background: var(--bg-container, #0f0f1a);
  border-radius: 20px;
  padding: 24px 20px;
  transform: scale(0.9);
  opacity: 0;
  transition: transform 0.25s ease-out, opacity 0.2s;
}

.difficulty-picker-overlay.active .difficulty-picker-panel {
  transform: scale(1);
  opacity: 1;
}

.difficulty-header {
  text-align: center;
  margin-bottom: 20px;
}

.difficulty-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary, #fff);
  display: block;
  margin-bottom: 8px;
}

.difficulty-subtitle {
  font-size: 14px;
  color: var(--text-muted, #9ca3af);
  margin: 0;
}

.difficulty-options {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.difficulty-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px;
  border: 2px solid var(--border-color, #2d2d44);
  border-radius: 14px;
  background: var(--bg-secondary, #1a1a2e);
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: left;
}

.difficulty-btn:hover {
  border-color: var(--accent, #3b82f6);
  background: var(--bg-hover, #252540);
}

.difficulty-btn:active {
  transform: scale(0.98);
}

.diff-emoji {
  font-size: 32px;
  flex-shrink: 0;
}

.diff-label {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary, #fff);
  display: block;
}

.diff-desc {
  font-size: 12px;
  color: var(--text-muted, #9ca3af);
  display: block;
  margin-top: 2px;
}

/* Difficulty colors */
.difficulty-easy:hover {
  border-color: #22c55e;
}
.difficulty-easy .diff-label {
  color: #22c55e;
}

.difficulty-medium:hover {
  border-color: #f59e0b;
}
.difficulty-medium .diff-label {
  color: #f59e0b;
}

.difficulty-hard:hover {
  border-color: #ef4444;
}
.difficulty-hard .diff-label {
  color: #ef4444;
}

.difficulty-cancel {
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted, #9ca3af);
  font-size: 14px;
  cursor: pointer;
  transition: color 0.15s;
}

.difficulty-cancel:hover {
  color: var(--text-primary, #fff);
}

/* ============================================================================
   ARTHUR TRASH TALK (in-game speech bubble)
   ============================================================================ */

.arthur-trash-talk {
  margin: 12px 0;
  padding: 10px 14px;
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  border-radius: 12px;
  text-align: center;
  animation: trashTalkPop 0.3s ease-out;
}

.game-rematch-btn {
  margin-top: 12px !important;
}

.trash-talk-text {
  color: #fff;
  font-size: 13px;
  font-weight: 500;
}

@keyframes trashTalkPop {
  0% {
    opacity: 0;
    transform: scale(0.8);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

/* ============================================================================
   DRAW CELEBRATION
   ============================================================================ */

.draw-celebration .celebration-content {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
}

.draw-content {
  text-align: center;
}

.draw-emoji {
  font-size: 80px;
  margin-bottom: 16px;
  animation: draw-shake 0.5s ease-in-out;
}

.draw-text {
  font-size: 32px;
  font-weight: 700;
  color: #fff;
  margin-bottom: 8px;
}

.draw-subtext {
  font-size: 16px;
  color: rgba(255,255,255,0.8);
}

@keyframes draw-shake {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-10deg); }
  75% { transform: rotate(10deg); }
}

/* ============================================================================
   GAME VS HEADER
   ============================================================================ */

.game-vs-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 12px;
  padding: 8px 0;
  background: transparent;
}

.vs-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  min-width: 70px;
}

.vs-avatar {
  width: 70px;
  height: 70px;
  border-radius: 12px;
  object-fit: cover;
  border: 2px solid rgba(255,255,255,0.5);
  background: transparent;
}

.vs-player-x .vs-avatar {
  border-color: #ef4444;
  box-shadow: 0 0 8px rgba(239, 68, 68, 0.5);
}

.vs-player-o .vs-avatar {
  border-color: #3b82f6;
  box-shadow: 0 0 8px rgba(59, 130, 246, 0.5);
}

.vs-avatar-placeholder {
  width: 70px;
  height: 70px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: bold;
  background: transparent;
  border: 2px solid rgba(255,255,255,0.5);
}

.vs-player-x .vs-avatar-placeholder {
  color: #ef4444;
  border-color: #ef4444;
}

.vs-player-o .vs-avatar-placeholder {
  color: #3b82f6;
  border-color: #3b82f6;
}

.vs-name {
  font-size: 11px;
  font-weight: 600;
  color: rgba(255,255,255,0.9);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.vs-badge {
  font-size: 18px;
  font-weight: 800;
  color: #fbbf24;
  text-shadow: 0 0 10px rgba(251, 191, 36, 0.5);
  padding: 0 8px;
  animation: vs-pulse 1.5s ease-in-out infinite;
}

@keyframes vs-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.1); opacity: 0.8; }
}

/* ============================================================================
   CELEBRATION VS LAYOUT (Win/Loss)
   ============================================================================ */

.celebration-vs .celebration-vs-container {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 20px;
}

/* Winner avatar - big, top-left */
.celebration-winner {
  position: relative;
  align-self: flex-start;
  margin-left: 20px;
  z-index: 2;
}

.celebration-winner-avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid #22c55e;
  box-shadow: 0 0 20px rgba(34, 197, 94, 0.6);
  animation: winner-bounce 0.5s ease-out;
}

@keyframes winner-bounce {
  0% { transform: scale(0.5); opacity: 0; }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

/* Result emoji - centered */
.celebration-emoji {
  font-size: 64px;
  text-shadow: 0 0 20px rgba(255,255,255,0.5);
  animation: emoji-pop 0.4s ease-out 0.2s both;
}

@keyframes emoji-pop {
  0% { transform: scale(0) rotate(-20deg); opacity: 0; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

/* Loser avatar - small, bottom-right, fading */
.celebration-loser {
  position: relative;
  align-self: flex-end;
  margin-right: 20px;
  z-index: 1;
}

.celebration-loser-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #ef4444;
  box-shadow: 0 0 10px rgba(239, 68, 68, 0.4);
  opacity: 0.6;
  animation: loser-fade 0.5s ease-out 0.3s both;
}

@keyframes loser-fade {
  0% { transform: scale(1.2); opacity: 0; }
  100% { transform: scale(1); opacity: 0.6; }
}

/* Placeholder styling */
.celebration-avatar-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(0,0,0,0.3);
}

.winner-placeholder {
  width: 120px;
  height: 120px;
  font-size: 60px;
  border: 4px solid #22c55e;
}

.loser-placeholder {
  width: 60px;
  height: 60px;
  font-size: 30px;
  border: 3px solid #ef4444;
  opacity: 0.6;
}

/* Result text */
.celebration-result-text {
  font-size: 28px;
  font-weight: 700;
  color: #fff;
  margin-top: 8px;
  animation: text-slide 0.4s ease-out 0.4s both;
}

.you-won .celebration-result-text {
  color: #22c55e;
  text-shadow: 0 0 10px rgba(34, 197, 94, 0.5);
}

.you-lost .celebration-result-text {
  color: #ef4444;
  text-shadow: 0 0 10px rgba(239, 68, 68, 0.5);
}

@keyframes text-slide {
  0% { transform: translateY(20px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}

/* Confetti */
.confetti-container {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  top: -20px;
  left: calc(5% + var(--i) * 4.5%);
  animation: confetti-fall 2s ease-out forwards;
  animation-delay: calc(var(--i) * 0.05s);
  border-radius: 2px;
}

.confetti:nth-child(odd) {
  background: linear-gradient(135deg, #22c55e, #16a34a);
  width: 8px;
  height: 12px;
}

.confetti:nth-child(3n) {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  width: 6px;
  height: 14px;
}

@keyframes confetti-fall {
  0% {
    transform: translateY(0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(300px) rotate(720deg);
    opacity: 0;
  }
}

/* ============================================================================
   DRAW CELEBRATION (Both angry + handshake)
   ============================================================================ */

.draw-celebration {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.95) 0%, rgba(139, 92, 246, 0.95) 100%);
}

.draw-vs-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-bottom: 16px;
}

.draw-player {
  animation: draw-slide-in 0.4s ease-out both;
}

.draw-player-left {
  animation-delay: 0s;
}

.draw-player-right {
  animation-delay: 0.15s;
}

@keyframes draw-slide-in {
  0% { transform: translateX(-30px); opacity: 0; }
  100% { transform: translateX(0); opacity: 1; }
}

.draw-player-right {
  animation-name: draw-slide-in-right;
}

@keyframes draw-slide-in-right {
  0% { transform: translateX(30px); opacity: 0; }
  100% { transform: translateX(0); opacity: 1; }
}

.draw-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid rgba(255,255,255,0.5);
  box-shadow: 0 0 15px rgba(0,0,0,0.3);
}

.draw-avatar-placeholder {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  background: rgba(0,0,0,0.2);
  border: 3px solid rgba(255,255,255,0.5);
}

.draw-handshake {
  font-size: 48px;
  animation: handshake-shake 0.5s ease-in-out 0.3s;
}

@keyframes handshake-shake {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-15deg); }
  75% { transform: rotate(15deg); }
}

.draw-text {
  font-size: 28px;
  font-weight: 700;
  color: #fff;
  text-align: center;
  animation: text-slide 0.4s ease-out 0.4s both;
}

.draw-subtext {
  font-size: 14px;
  color: rgba(255,255,255,0.8);
  text-align: center;
  animation: text-slide 0.4s ease-out 0.5s both;
}

/* ============================================================================
   LANDSCAPE TABLET OPTIMIZATION - Tighter spacing for more message room
   ============================================================================ */
@media (min-width: 768px) and (orientation: landscape) {
  
  /* Maximize horizontal space - minimal edge padding */
  .message-modal {
    padding: 4px;
  }
  
  .message-modal-content {
    width: calc(100vw - 8px) !important;
    max-width: none !important;
    border-radius: 8px;
  }
  
  /* Narrower sidebar, less gap - FILL HEIGHT */
  .modal-view[data-view="inbox"].active {
    grid-template-columns: 220px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    gap: 12px;
    height: 100% !important;
    min-height: 0 !important;
    align-items: stretch;
  }
  
  /* Force grid children to fill cell height */
  .modal-view[data-view="inbox"].active > * {
    min-height: 0 !important;
    height: 100% !important;
  }
  
  /* Compact sidebar padding */
  .inbox-list-view.active {
    padding-right: 8px;
  }
  
  /* Tighter tab bar */
  .message-tabs {
    padding: 8px 16px;
    margin-bottom: 12px;
    gap: 0;
  }
  
  .message-tab-btn {
    padding: 8px 20px;
    font-size: 14px;
  }
  
  /* Compact thread list items */
  .inbox-thread-item {
    padding: 8px 10px;
    gap: 8px;
  }
  
  .inbox-thread-item .avatar {
    width: 40px;
    height: 40px;
  }
  
  .inbox-thread-name {
    font-size: 14px;
  }
  
  .inbox-thread-preview {
    font-size: 12px;
  }
  
  /* Compact thread header */
  .thread-header {
    padding: 8px 12px;
    gap: 8px;
  }
  
  .thread-header .avatar {
    width: 36px;
    height: 36px;
  }
  
  .thread-header .name {
    font-size: 15px;
  }
  
  /* Tighter message list */
  .thread-messages {
    padding: 8px 12px;
    gap: 6px;
  }
  
  /* Compact message bubbles */
  .message-bubble {
    padding: 8px 12px;
    max-width: 75%;
  }
  
  .message-bubble .content {
    font-size: 14px;
  }
  
  .message-bubble .timestamp {
    font-size: 10px;
  }
  
  /* Compact input bar */
  .thread-input-bar {
    padding: 6px 10px;
    gap: 6px;
  }
  
  .thread-input-bar .attach-btn {
    width: 38px;
    height: 38px;
    font-size: 18px;
    min-width: 38px;
  }
  
  .thread-input-bar input {
    padding: 8px 14px;
    font-size: 14px;
  }
  
  .thread-input-bar .send-btn {
    width: 38px;
    height: 38px;
  }
  
  /* Compact game board */
  .game-container {
    padding: 10px;
  }
  
  .game-vs-header {
    padding: 6px 10px;
    margin-bottom: 8px;
    gap: 6px;
  }
  
  .vs-avatar {
    width: 60px;
    height: 60px;
  }
  
  .vs-badge {
    font-size: 16px;
    padding: 0 6px;
  }
  
  .game-board {
    width: 234px;
  }
  
  .game-cell {
    width: 70px;
    height: 70px;
    font-size: 42px;
  }
  
  .game-status {
    font-size: 20px;
    margin-top: 8px;
  }
  
  .arthur-trash-talk {
    margin: 6px 0;
    padding: 6px 10px;
    font-size: 12px;
  }
  
  .game-rematch-btn {
    padding: 6px 12px;
    font-size: 12px;
    margin-top: 8px !important;
  }
  
  /* Messages section header */
  .inbox-list-header {
    padding: 0 0 8px 0;
    margin-bottom: 8px;
  }
  
  .inbox-list-header h3 {
    font-size: 14px;
  }
}

/* ============================================================================
   PWA FULLSCREEN MODE - Safe area handling
   ============================================================================ */

/* When running as PWA, expand to full viewport */
@supports (padding: env(safe-area-inset-top)) {
  
  /* Modal uses full safe area */
  .message-modal {
    padding-top: env(safe-area-inset-top, 8px);
    padding-left: env(safe-area-inset-left, 4px);
    padding-right: env(safe-area-inset-right, 4px);
    padding-bottom: env(safe-area-inset-bottom, 4px);
  }
  
  /* Input bar respects bottom safe area */
  .thread-input-bar {
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
  }
}

/* PWA standalone mode detection via CSS */
@media (display-mode: standalone) {
  
  /* Maximize space in PWA mode */
  .message-modal {
    padding: env(safe-area-inset-top, 0px) 0 0 0 !important;
    /* Fix iOS 100vh bug - use JS-provided --app-height as primary */
    height: 100vh;
    height: 100dvh;
    height: var(--app-height, 100vh);
  }
  
  .message-modal-content {
    width: 100vw !important;
    max-width: none !important;
    /* Fix iOS 100vh bug - JS variable is most reliable */
    height: calc(100vh - env(safe-area-inset-top, 0px));
    height: calc(100dvh - env(safe-area-inset-top, 0px));
    height: calc(var(--app-height, 100vh) - env(safe-area-inset-top, 0px)) !important;
    max-height: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
  }
}

/* Additional fix when pwa-standalone class is added by JS */
html.pwa-standalone .message-modal {
  height: var(--app-height, 100vh) !important;
}

html.pwa-standalone .message-modal-content {
  height: calc(var(--app-height, 100vh) - env(safe-area-inset-top, 0px)) !important;
}

/* PWA standalone mode - continued */
@media (display-mode: standalone) {
  
  /* Close button respects safe area */
  .message-modal .modal-close-btn {
    top: calc(8px + env(safe-area-inset-top, 0px));
    right: calc(8px + env(safe-area-inset-right, 0px));
  }
  
  /* Full-width tabs */
  .message-tabs {
    padding-left: env(safe-area-inset-left, 16px);
    padding-right: env(safe-area-inset-right, 16px);
  }
  
  /* Sidebar respects left safe area */
  .inbox-list-view {
    padding-left: env(safe-area-inset-left, 0px);
  }
  
  /* Thread respects right safe area */
  .thread-view {
    padding-right: env(safe-area-inset-right, 0px);
  }
}

/* ============================================================================
   INPUT BAR - 3-Row Grid Layout
   ============================================================================ */

.thread-input-bar.input-bar-grid {
  display: grid !important;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas:
    "game   input send"
    "emoji  input send"
    "media  input send";
  gap: 4px 8px;
  padding: 8px 12px !important;
  align-items: stretch;
}

.input-bar-grid .game-btn {
  grid-area: game;
}

.input-bar-grid .emoji-btn {
  grid-area: emoji;
}

.input-bar-grid .media-btn {
  grid-area: media;
}

.input-bar-grid .native-reply-textarea {
  grid-area: input;
  min-height: 80px;
  resize: none;
}

.input-bar-grid .native-reply-btn {
  grid-area: send;
  height: auto !important;
  min-height: 80px;
  width: 50px;
}

/* Buttons in grid - consistent sizing */
.input-bar-grid .attach-btn {
  width: 36px;
  height: 100%;
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  padding: 4px;
}

/* Send View - 2-Row Grid (no game button) */
.send-input-grid {
  display: grid !important;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas:
    "emoji  input"
    "media  input";
  gap: 4px 8px;
  align-items: stretch;
}

.send-input-grid .emoji-btn {
  grid-area: emoji;
}

.send-input-grid .media-btn {
  grid-area: media;
}

.send-input-grid .native-reply-textarea,
.send-input-grid .message-textarea {
  grid-area: input;
  min-height: 70px;
  resize: none;
}

.send-input-grid .attach-btn {
  width: 36px;
  height: 100%;
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  padding: 4px;
}

/* Round send button */
.send-round-btn {
  width: 56px !important;
  height: auto !important;
  min-height: 80px;
  border-radius: 28px !important;
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%) !important;
  border: none !important;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.send-round-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(34, 197, 94, 0.4);
}

.send-round-btn:active {
  transform: scale(0.95);
}

.send-round-btn svg {
  color: #fff;
}

/* ============================================================================
   APP MENU - iMessage-style balloon popup
   ============================================================================ */

.app-menu-container {
  position: relative;
  z-index: 50;
}

.app-menu-toggle {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 50%;
  border: 1px solid var(--border-light, #e2e8f0);
  background: var(--bg-card, #ffffff);
  color: var(--text-primary, #1a202c);
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.app-menu-toggle:hover {
  background: var(--bg-nav-hover, #f1f5f9);
}

.app-menu-icon {
  display: inline-block;
  transition: transform 0.3s ease;
}

.app-menu-toggle.open .app-menu-icon {
  transform: rotate(45deg);
}

/* Balloon popup */
.app-menu-balloon {
  position: absolute;
  bottom: calc(100% + 12px);
  left: 50%;
  transform: translateX(-50%) scale(0);
  transform-origin: bottom center;
  min-width: 140px;
  background: var(--bg-card, #ffffff);
  border-radius: 14px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.05);
  padding: 8px 0;
  opacity: 0;
  visibility: hidden;
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
  z-index: 100;
}

.app-menu-balloon.open {
  transform: translateX(-50%) scale(1);
  opacity: 1;
  visibility: visible;
}

/* Arrow pointing down */
.app-menu-arrow {
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-top: 10px solid var(--bg-card, #ffffff);
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.1));
}

/* Menu items */
.app-menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.15s ease;
  width: 100%;
  border: none;
  background: transparent;
  text-align: left;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
  touch-action: manipulation;
}

.app-menu-item:hover {
  background: var(--bg-nav-hover, #f1f5f9);
}

.app-menu-item:first-child {
  border-radius: 14px 14px 0 0;
}

.app-menu-item:last-of-type {
  border-radius: 0 0 14px 14px;
}

.app-menu-emoji {
  font-size: 24px;
  line-height: 1;
  width: 28px;
  text-align: center;
}

.app-menu-label {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-primary, #1a202c);
}

/* Backdrop */
.app-menu-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease;
  z-index: 45;
}

.app-menu-backdrop.open {
  opacity: 1;
  visibility: visible;
}

/* Dark theme */
[data-theme="dark"] .app-menu-toggle {
  background: var(--bg-nav, #2d3748);
  border-color: var(--border-light, #4a5568);
  color: var(--text-primary, #e2e8f0);
}

[data-theme="dark"] .app-menu-toggle:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .app-menu-balloon {
  background: var(--bg-card, #2d3748);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .app-menu-arrow {
  border-top-color: var(--bg-card, #2d3748);
}

[data-theme="dark"] .app-menu-item:hover {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .app-menu-label {
  color: var(--text-primary, #e2e8f0);
}

[data-theme="dark"] .app-menu-backdrop {
  background: rgba(0, 0, 0, 0.5);
}

/* App menu fixes - backdrop must cover screen and be tappable */
.app-menu-backdrop {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  z-index: 1;
  pointer-events: none;
}

.app-menu-backdrop.open {
  pointer-events: auto;
}

.app-menu-icon {
  font-size: 26px;
  font-weight: 400;
  line-height: 1;
}

/* Ensure emojis render properly */
.app-menu-emoji {
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", "EmojiOne Color", sans-serif;
  font-size: 22px !important;
  display: inline-block;
  width: 28px;
  text-align: center;
}

/* Ensure balloon appears above backdrop */
.app-menu-balloon {
  z-index: 10 !important;
  left: 10px !important;
  transform: translateX(0) scale(0) !important;
  transform-origin: bottom left !important;
}

.app-menu-balloon.open {
  transform: translateX(0) scale(1) !important;
}

/* Arrow positioning for left-aligned balloon */
.app-menu-arrow {
  left: 22px !important;
  transform: translateX(0) !important;
}

.app-menu-container {
  z-index: 50;
  position: relative;
}

.app-menu-toggle {
  z-index: 5;
  position: relative;
}

/* App menu keyboard focus and touch feedback */
.app-menu-item:focus {
  outline: none;
  background: var(--bg-nav-hover, #f1f5f9);
}

.app-menu-item.focused {
  background: var(--bg-nav-hover, #f1f5f9);
  outline: 2px solid var(--accent, #3b82f6);
  outline-offset: -2px;
}

.app-menu-item:active {
  background: var(--bg-nav-active, #e2e8f0);
  transform: scale(0.98);
}

[data-theme="dark"] .app-menu-item:focus,
[data-theme="dark"] .app-menu-item.focused {
  background: var(--bg-nav-hover, #4a5568);
}

[data-theme="dark"] .app-menu-item:active {
  background: var(--bg-nav-active, #2d3748);
}

/* App menu tap feedback animation */
@keyframes app-menu-tap {
  0% { transform: scale(1); background: var(--bg-nav-hover, #f1f5f9); }
  50% { transform: scale(0.95); background: var(--accent, #3b82f6); }
  100% { transform: scale(1); background: var(--bg-nav-hover, #f1f5f9); }
}

.app-menu-item.tapped {
  animation: app-menu-tap 0.2s ease-out;
}

.app-menu-item.tapped .app-menu-label {
  color: white;
}

[data-theme="dark"] .app-menu-item.tapped {
  animation: app-menu-tap 0.2s ease-out;
}

@keyframes app-menu-tap-dark {
  0% { transform: scale(1); background: var(--bg-nav-hover, #4a5568); }
  50% { transform: scale(0.95); background: var(--accent, #3b82f6); }
  100% { transform: scale(1); background: var(--bg-nav-hover, #4a5568); }
}

[data-theme="dark"] .app-menu-item.tapped {
  animation: app-menu-tap-dark 0.2s ease-out;
}
