/* ============================================================
   iMessage Web App — Styles
   iOS dark mode iMessage aesthetic
   ============================================================ */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --blue: #007AFF;
  --green: #34C759;
  --gray: #3A3A3C;
  --gray-light: #48484A;
  --bg: #000000;
  --surface: #1C1C1E;
  --surface2: #2C2C2E;
  --border: #38383A;
  --text: #FFFFFF;
  --text-secondary: #8E8E93;
  --text-tertiary: #636366;
  --header-bg: rgba(28, 28, 30, 0.85);
  --input-bg: #1C1C1E;
  --radius-bubble: 18px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---- Layout ---- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 680px;
  margin: 0 auto;
  position: relative;
}

/* ---- Header ---- */

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px 10px;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 0.5px solid var(--border);
  position: relative;
  z-index: 10;
  flex-shrink: 0;
}

.chat-header .back-btn {
  display: none;
  align-items: center;
  gap: 4px;
  color: var(--blue);
  font-size: 17px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 0;
  flex-shrink: 0;
}

.chat-header .back-btn svg {
  width: 10px;
  height: 17px;
}

.header-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}

.header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--green);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
  position: relative;
}

.header-avatar .presence-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--text-tertiary);
  border: 1.5px solid var(--bg);
  transition: background 0.3s;
}

.header-avatar .presence-dot.online {
  background: var(--green);
}

.header-name {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  font-size: 12px;
  color: var(--text-secondary);
  transition: color 0.3s;
}

.header-status.online {
  color: var(--green);
}

.header-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--surface2);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--blue);
  transition: background 0.15s, transform 0.1s;
}

.icon-btn:active {
  transform: scale(0.93);
  background: var(--border);
}

.icon-btn svg {
  width: 16px;
  height: 16px;
}

/* ---- Messages ---- */

.messages-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 0 4px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}

.messages-container::-webkit-scrollbar {
  width: 0;
}

.messages-list {
  display: flex;
  flex-direction: column;
  padding: 0 0 8px;
}

/* Date divider */
.date-divider {
  text-align: center;
  font-size: 12px;
  color: var(--text-tertiary);
  padding: 12px 16px 4px;
  font-weight: 500;
  letter-spacing: 0.01em;
}

/* Message row */
.message-row {
  display: flex;
  flex-direction: column;
  padding: 1px 16px;
}

.message-row.sent {
  align-items: flex-end;
}

.message-row.received {
  align-items: flex-start;
}

/* Group spacing: consecutive same-sender messages get less gap */
.message-row + .message-row.sent,
.message-row + .message-row.received {
  padding-top: 1px;
}

.message-row.group-start {
  padding-top: 8px;
}

/* Bubble */
.bubble {
  max-width: min(75%, 480px);
  padding: 9px 14px;
  border-radius: var(--radius-bubble);
  font-size: 16px;
  line-height: 1.45;
  word-break: break-word;
  position: relative;
  transition: transform 0.15s;
}

/* Bubble shape: tail on the last bubble in a group */
.message-row.sent .bubble {
  background: var(--blue);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message-row.sent.group-end .bubble {
  border-bottom-right-radius: 4px;
}

.message-row.sent:not(.group-end) .bubble {
  border-bottom-right-radius: var(--radius-bubble);
}

.message-row.received .bubble {
  background: var(--gray);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.message-row.received:not(.group-end) .bubble {
  border-bottom-left-radius: var(--radius-bubble);
}

/* Admin bubble colors */
.bubble.method-live {
  background: var(--blue);
  color: #fff;
}

.bubble.method-email {
  background: var(--green);
  color: #fff;
}

/* Bubble tap animation */
.bubble:active {
  transform: scale(0.97);
}

/* Message time label */
.message-time {
  font-size: 11px;
  color: var(--text-tertiary);
  padding: 2px 2px 0;
  display: none;
}

.message-row.group-end .message-time {
  display: block;
}

.message-row.sent .message-time {
  text-align: right;
}

/* Read receipt */
.read-receipt {
  font-size: 11px;
  color: var(--text-tertiary);
  padding: 2px 2px 0;
  text-align: right;
}

/* Media messages */
.bubble.media-bubble {
  padding: 3px;
  overflow: hidden;
  background: transparent !important;
  border: none;
}

.bubble.media-bubble img,
.bubble.media-bubble video {
  display: block;
  max-width: 260px;
  max-height: 320px;
  border-radius: 15px;
  object-fit: cover;
  cursor: pointer;
}

.bubble.file-bubble {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  min-width: 180px;
}

.file-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.file-icon svg {
  width: 20px;
  height: 20px;
  color: #fff;
}

.file-info {
  overflow: hidden;
}

.file-name {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #fff;
}

.file-size {
  font-size: 12px;
  color: rgba(255,255,255,0.7);
}

/* Typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}

.typing-indicator.visible {
  opacity: 1;
  transform: translateY(0);
}

.typing-bubble {
  background: var(--gray);
  border-radius: 18px 18px 18px 4px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.30s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.6; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* System message (calls, etc.) */
.system-message {
  text-align: center;
  font-size: 13px;
  color: var(--text-tertiary);
  padding: 8px 32px;
}

/* ---- Input area ---- */

.input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px 12px;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--border);
  flex-shrink: 0;
}

.attach-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--surface2);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--blue);
  flex-shrink: 0;
  margin-bottom: 1px;
  transition: background 0.15s;
}

.attach-btn:active { background: var(--border); }
.attach-btn svg { width: 17px; height: 17px; }

.input-wrapper {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  display: flex;
  align-items: flex-end;
  padding: 6px 8px 6px 14px;
  min-height: 36px;
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--gray-light);
}

.message-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
  padding: 0;
}

.message-input::placeholder {
  color: var(--text-tertiary);
}

.message-input::-webkit-scrollbar { width: 0; }

.send-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--blue);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s, opacity 0.2s;
  opacity: 0;
  transform: scale(0.7);
}

.send-btn.visible {
  opacity: 1;
  transform: scale(1);
}

.send-btn:active {
  transform: scale(0.9);
  background: #0066d6;
}

.send-btn svg {
  width: 14px;
  height: 14px;
  color: #fff;
  margin-left: 1px;
}

/* Media preview strip (above input) */
.media-preview {
  display: none;
  padding: 8px 12px 0;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
}

.media-preview.visible {
  display: flex;
  align-items: center;
  gap: 8px;
}

.preview-item {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: 10px;
  overflow: hidden;
  flex-shrink: 0;
}

.preview-item img,
.preview-item video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.preview-item .remove-btn {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.6);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 12px;
  line-height: 1;
}

.preview-file-info {
  font-size: 13px;
  color: var(--text-secondary);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  flex: 1;
}

/* ---- Empty state ---- */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 40px 32px;
  color: var(--text-secondary);
  text-align: center;
}

.empty-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}

.empty-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
}

.empty-subtitle {
  font-size: 15px;
  color: var(--text-secondary);
  line-height: 1.5;
}

/* ---- Call UI ---- */

.call-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  z-index: 100;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 60px 32px max(48px, env(safe-area-inset-bottom));
}

.call-overlay.visible {
  display: flex;
}

.call-avatar {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  font-weight: 600;
  color: var(--text);
}

.call-name {
  font-size: 28px;
  font-weight: 600;
  margin-top: 16px;
}

.call-status {
  font-size: 17px;
  color: var(--text-secondary);
  margin-top: 6px;
}

.call-timer {
  font-size: 17px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  display: none;
}

.call-timer.visible { display: block; }

.remote-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
}

.remote-video.visible { display: block; }

.local-video {
  position: absolute;
  top: 80px;
  right: 16px;
  width: 100px;
  height: 140px;
  object-fit: cover;
  border-radius: 12px;
  border: 2px solid #fff;
  display: none;
}

.local-video.visible { display: block; }

.call-controls {
  display: flex;
  gap: 24px;
  align-items: center;
}

.call-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s;
}

.call-btn:active { transform: scale(0.93); }

.call-btn svg {
  width: 28px;
  height: 28px;
  color: #fff;
}

.call-btn.end { background: #FF3B30; }
.call-btn.mute { background: var(--surface2); }
.call-btn.speaker { background: var(--surface2); }
.call-btn.camera { background: var(--surface2); }
.call-btn.flip { background: var(--surface2); }

.call-btn.active { background: var(--text); }
.call-btn.active svg { color: #000; }

.call-accept-row {
  display: flex;
  gap: 48px;
  align-items: center;
}

.call-accept-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s;
}

.call-accept-btn:active { transform: scale(0.93); }
.call-accept-btn svg { width: 32px; height: 32px; color: #fff; }
.call-accept-btn.accept { background: var(--green); }
.call-accept-btn.decline { background: #FF3B30; }

.call-accept-label {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  margin-top: 8px;
}

/* ---- Image lightbox ---- */

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.95);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
}

.lightbox.visible {
  display: flex;
}

.lightbox img,
.lightbox video {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.15);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

/* ---- Admin Layout ---- */

.admin-app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 320px;
  flex-shrink: 0;
  border-right: 0.5px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: transform 0.3s ease;
}

.sidebar-header {
  padding: 16px 16px 12px;
  border-bottom: 0.5px solid var(--border);
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}

.sidebar-title {
  font-size: 22px;
  font-weight: 700;
}

.sidebar-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
}

.sidebar-list::-webkit-scrollbar { width: 0; }

.session-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  cursor: pointer;
  border-bottom: 0.5px solid var(--border);
  transition: background 0.1s;
  position: relative;
}

.session-item:active,
.session-item.active {
  background: var(--surface);
}

.session-avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
  position: relative;
}

.session-avatar .presence-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--bg);
}

.session-avatar .presence-dot.online { background: var(--green); }
.session-avatar .presence-dot.offline { background: var(--text-tertiary); }

.session-info {
  flex: 1;
  min-width: 0;
}

.session-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}

.session-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-preview {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}

.session-time {
  font-size: 12px;
  color: var(--text-tertiary);
}

.unread-badge {
  background: var(--blue);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  border-radius: 10px;
  padding: 2px 7px;
  min-width: 20px;
  text-align: center;
}

.no-sessions {
  padding: 40px 16px;
  text-align: center;
  color: var(--text-tertiary);
  font-size: 15px;
}

/* Admin conversation panel */
.conversation-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.conversation-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
  gap: 12px;
}

.conversation-empty-icon {
  font-size: 56px;
}

.conversation-empty-text {
  font-size: 18px;
  font-weight: 500;
}

/* Visitor info drawer */
.visitor-info {
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 0.5px solid var(--border);
  font-size: 13px;
  color: var(--text-secondary);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
}

.visitor-info span {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Login overlay */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.login-logo {
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: var(--blue);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
}

.login-title {
  font-size: 26px;
  font-weight: 700;
  text-align: center;
}

.login-subtitle {
  font-size: 15px;
  color: var(--text-secondary);
  text-align: center;
  margin-top: -12px;
}

.login-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 16px;
  color: var(--text);
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s;
}

.login-input:focus {
  border-color: var(--blue);
}

.login-btn {
  width: 100%;
  background: var(--blue);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 17px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}

.login-btn:active {
  background: #0066d6;
}

.login-error {
  font-size: 14px;
  color: #FF3B30;
  text-align: center;
  display: none;
}

.login-error.visible { display: block; }

/* ---- Utility ---- */

.hidden { display: none !important; }

/* Conversation view needs flex when visible */
#conversationView {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Upload progress */
.upload-progress {
  width: 100%;
  height: 2px;
  background: rgba(255,255,255,0.2);
  border-radius: 1px;
  overflow: hidden;
}

.upload-progress-bar {
  height: 100%;
  background: #fff;
  border-radius: 1px;
  width: 0%;
  transition: width 0.3s;
}

/* Spinner */
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* Message send animation */
@keyframes bubble-in-right {
  from { opacity: 0; transform: translateX(12px) scale(0.95); }
  to { opacity: 1; transform: translateX(0) scale(1); }
}

@keyframes bubble-in-left {
  from { opacity: 0; transform: translateX(-12px) scale(0.95); }
  to { opacity: 1; transform: translateX(0) scale(1); }
}

.message-row.sent.new-message .bubble {
  animation: bubble-in-right 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.message-row.received.new-message .bubble {
  animation: bubble-in-left 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* ---- Responsive / Mobile ---- */

/* ---- Mobile (phone) ---- */
@media (max-width: 768px) {
  .app {
    max-width: 100%;
  }

  /* Admin: full-screen slide navigation */
  .admin-app {
    position: relative;
  }

  .sidebar {
    position: absolute;
    inset: 0;
    width: 100%;
    z-index: 10;
    transform: translateX(0);
    transition: transform 0.3s ease;
  }

  .sidebar.hidden-mobile {
    transform: translateX(-100%);
  }

  .conversation-panel {
    position: absolute;
    inset: 0;
    z-index: 5;
  }

  .chat-header .back-btn {
    display: flex;
  }
}

/* ---- Desktop ---- */
@media (min-width: 769px) {
  html, body {
    overflow: hidden;
  }

  body {
    background: #0d0d12;
    background-image:
      radial-gradient(ellipse 100% 50% at 50% 0%, rgba(0, 122, 255, 0.07) 0%, transparent 60%),
      radial-gradient(ellipse 60% 40% at 50% 100%, rgba(52, 199, 89, 0.04) 0%, transparent 60%);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Visitor chat: centered card with max width */
  .app {
    max-width: 820px;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    border-left: 0.5px solid var(--border);
    border-right: 0.5px solid var(--border);
    box-shadow: 0 0 120px rgba(0, 0, 0, 0.7);
  }

  /* Bubbles can be a bit wider on desktop */
  .bubble {
    max-width: min(65%, 600px);
  }

  /* Admin: full viewport two-column layout */
  .admin-app {
    width: 100vw;
    max-width: 1400px;
    margin: 0 auto;
    border-left: 0.5px solid var(--border);
    border-right: 0.5px solid var(--border);
  }

  .sidebar {
    width: 340px;
  }

  .chat-header .back-btn {
    display: none;
  }
}
