/* ==========================================================================
   Apple Messages-inspired styles
   ========================================================================== */

:root {
  --blue: #007AFF;
  --blue-gradient-top: #2997FF;
  --blue-gradient-bottom: #0A7BFF;
  --gray-bubble: #E9E9EB;
  --gray-text: #8E8E93;
  --gray-divider: #D1D1D6;
  --bg: #FFFFFF;
  --bg-elevated: #F2F2F7;
  --bg-nav: rgba(246, 246, 246, 0.78);
  --text: #000000;
  --text-secondary: #6C6C70;
  --field-bg: #FFFFFF;
  --field-border: #D1D1D6;
  --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  --bubble-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.06);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --font-system: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
                 "Helvetica Neue", "Segoe UI", Roboto, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --gray-bubble: #262628;
    --gray-text: #98989F;
    --gray-divider: #2C2C2E;
    --bg: #000000;
    --bg-elevated: #1C1C1E;
    --bg-nav: rgba(28, 28, 30, 0.78);
    --text: #FFFFFF;
    --text-secondary: #98989F;
    --field-bg: #1C1C1E;
    --field-border: #2C2C2E;
    --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.3);
    --bubble-shadow: none;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font-system);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: var(--bg);
  color: var(--text);
  letter-spacing: -0.01em;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  padding: 0;
}

input, textarea {
  font-family: inherit;
  color: inherit;
}

/* ==========================================================================
   ENTRY / LOGIN SCREEN
   ========================================================================== */

.entry-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(10, 132, 255, 0.10), transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(91, 200, 250, 0.10), transparent 50%),
    var(--bg);
}

.entry-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg);
  border-radius: 22px;
  padding: 36px 28px 28px;
  box-shadow: var(--shadow-card);
  text-align: center;
  animation: card-in 0.5s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@media (prefers-color-scheme: dark) {
  .entry-card { background: var(--bg-elevated); }
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0)    scale(1); }
}

.entry-logo {
  width: 64px; height: 64px;
  margin: 0 auto 18px;
  display: flex; align-items: center; justify-content: center;
}

.lock-logo { width: 56px; height: 56px; }

.entry-title {
  margin: 0 0 6px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.025em;
}

.entry-sub {
  margin: 0 0 24px;
  color: var(--text-secondary);
  font-size: 15px;
  line-height: 1.35;
}

.entry-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.entry-form input {
  width: 100%;
  height: 48px;
  padding: 0 16px;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: 12px;
  font-size: 17px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.entry-form input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.18);
}

.entry-form button {
  height: 48px;
  border-radius: 12px;
  background: linear-gradient(180deg, var(--blue-gradient-top), var(--blue-gradient-bottom));
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: transform 0.08s ease, filter 0.15s;
  letter-spacing: -0.01em;
}

.entry-form button:hover { filter: brightness(1.05); }
.entry-form button:active { transform: scale(0.985); }
.entry-form button:disabled { opacity: 0.5; cursor: not-allowed; }

.entry-footer {
  margin: 18px 0 0;
  font-size: 12px;
  color: var(--text-secondary);
}

.entry-error {
  margin: 12px 0 0;
  color: #FF3B30;
  font-size: 13px;
  line-height: 1.4;
  text-align: left;
}

/* ==========================================================================
   CHAT SCREEN (shared by visitor + host)
   ========================================================================== */

.chat-screen,
.active-chat {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  background: var(--bg);
}

.active-chat { height: 100%; }

/* ----- Top nav with blur ----- */

.chat-nav {
  position: sticky;
  top: 0;
  z-index: 10;
  padding-top: var(--safe-top);
}

.nav-blur {
  position: absolute;
  inset: 0;
  background: var(--bg-nav);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--gray-divider);
}

.nav-inner {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  min-height: 52px;
}

.nav-avatar { flex-shrink: 0; }

.avatar-circle {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  background: linear-gradient(180deg, #B5B5BA, #8E8E93);
}

.avatar-host {
  background: linear-gradient(180deg, var(--blue-gradient-top), var(--blue-gradient-bottom));
}

.nav-title { flex: 1; min-width: 0; }

.nav-name {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-status {
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 1px;
}

.status-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #34C759;
  display: inline-block;
}

.nav-action,
.nav-back {
  width: 36px; height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--blue);
  border-radius: 8px;
  transition: background 0.15s;
}

.nav-action:hover,
.nav-back:hover { background: rgba(0, 122, 255, 0.10); }

.nav-back { display: none; }

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

.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.day-divider {
  text-align: center;
  margin: 8px 0 14px;
}

.day-divider span {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: capitalize;
  letter-spacing: 0.02em;
}

/* ----- Bubbles ----- */

.message-row {
  display: flex;
  margin-top: 2px;
  animation: bubble-in 0.22s cubic-bezier(0.2, 0.9, 0.3, 1.1);
}

.message-row.from-host { justify-content: flex-start; }
.message-row.from-me   { justify-content: flex-end; }

/* When on host page, our bubbles (from-host) are the blue ones */
body.host .message-row.from-host { justify-content: flex-end; }
body.host .message-row.from-me   { justify-content: flex-start; }

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px) scale(0.96); }
  to   { opacity: 1; transform: translateY(0)   scale(1); }
}

.bubble {
  max-width: 78%;
  padding: 8px 13px;
  font-size: 16px;
  line-height: 1.35;
  letter-spacing: -0.01em;
  position: relative;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: var(--bubble-shadow);
}

/* Incoming (gray) bubbles — left side */
.bubble-in {
  background: var(--gray-bubble);
  color: var(--text);
  border-radius: 18px 18px 18px 4px;
  margin-right: auto;
}

/* Outgoing (blue) bubbles — right side */
.bubble-out {
  background: linear-gradient(180deg, var(--blue-gradient-top), var(--blue-gradient-bottom));
  color: #fff;
  border-radius: 18px 18px 4px 18px;
  margin-left: auto;
}

/* Bubble tail (the little corner curve) */
.bubble-in::before,
.bubble-out::before {
  content: "";
  position: absolute;
  bottom: 0;
  width: 12px;
  height: 18px;
  background: inherit;
}

.bubble-in::before {
  left: -6px;
  border-bottom-right-radius: 16px 14px;
  background: var(--gray-bubble);
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
}

.bubble-out::before {
  right: -6px;
  border-bottom-left-radius: 16px 14px;
  background: var(--blue-gradient-bottom);
  clip-path: polygon(0 0, 100% 100%, 0 100%);
}

/* Group consecutive bubbles: only show tail on last one */
.message-row:not(.last-in-group) .bubble::before { display: none; }
.message-row:not(.last-in-group) .bubble-in { border-bottom-left-radius: 18px; }
.message-row:not(.last-in-group) .bubble-out { border-bottom-right-radius: 18px; }

/* Timestamp shown on hover (desktop) */
.message-row { position: relative; }
.timestamp {
  font-size: 11px;
  color: var(--text-secondary);
  margin: 4px 8px 6px;
  text-align: center;
  width: 100%;
}

/* Typing indicator */
.typing-row { display: flex; }
.bubble.typing {
  padding: 12px 14px;
  display: inline-flex;
  gap: 4px;
  align-items: center;
}
.bubble.typing .dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: typing 1.2s infinite ease-in-out;
}
.bubble.typing .dot:nth-child(2) { animation-delay: 0.15s; }
.bubble.typing .dot:nth-child(3) { animation-delay: 0.30s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30%           { transform: translateY(-4px); opacity: 1; }
}

/* ----- Composer ----- */

.composer {
  position: sticky;
  bottom: 0;
  background: var(--bg);
  border-top: 0.5px solid var(--gray-divider);
  padding-bottom: var(--safe-bottom);
}

.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px 8px;
}

.composer-plus {
  width: 32px; height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  flex-shrink: 0;
  margin-bottom: 3px;
}

.composer-plus[disabled] { opacity: 0.5; cursor: not-allowed; }

.composer-field {
  flex: 1;
  display: flex;
  align-items: center;
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  border-radius: 20px;
  padding: 2px 2px 2px 14px;
  min-height: 36px;
  transition: border-color 0.15s;
}

.composer-field:focus-within { border-color: #B0B0B5; }

.composer-field input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  font-size: 16px;
  padding: 7px 6px 7px 0;
  letter-spacing: -0.01em;
}

.composer-send {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.1s ease, background 0.15s;
}

.composer-send:hover { background: #0070E3; }
.composer-send:active { transform: scale(0.92); }
.composer-send:disabled {
  background: var(--gray-divider);
  cursor: not-allowed;
}

.composer-send svg { transform: translateX(-1px); }

.home-indicator {
  height: 5px;
  width: 134px;
  border-radius: 3px;
  background: var(--text);
  opacity: 0.18;
  margin: 6px auto 8px;
  display: none; /* iOS only — let safe-area handle the actual indicator */
}

/* ==========================================================================
   HOST DASHBOARD
   ========================================================================== */

.dashboard {
  display: flex;
  height: 100dvh;
  width: 100%;
}

.sidebar {
  width: 320px;
  flex-shrink: 0;
  border-right: 0.5px solid var(--gray-divider);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

.sidebar-header {
  position: sticky;
  top: 0;
  z-index: 5;
  padding-top: var(--safe-top);
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 4px;
  position: relative;
}

.sidebar-top h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.link-button {
  color: var(--blue);
  font-size: 15px;
  background: none;
}

.link-button.invisible { visibility: hidden; }

.search-wrap {
  position: relative;
  padding: 6px 16px 10px;
}

.search-wrap .search-icon {
  position: absolute;
  left: 26px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
}

.search-wrap input {
  width: 100%;
  height: 32px;
  border: none;
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 0 12px 0 32px;
  font-size: 14px;
  outline: none;
  -webkit-appearance: none;
}

.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
}

.chat-list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  cursor: pointer;
  position: relative;
  transition: background 0.12s;
  border-bottom: 0.5px solid var(--gray-divider);
}

.chat-list-item:hover { background: var(--bg-elevated); }
.chat-list-item.active { background: rgba(0, 122, 255, 0.10); }

.chat-list-item .unread-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--blue);
  position: absolute;
  left: 4px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0;
  transition: opacity 0.15s;
}

.chat-list-item.unread .unread-dot { opacity: 1; }

.chat-list-item .avatar-circle {
  width: 44px; height: 44px;
  font-size: 16px;
  flex-shrink: 0;
}

.chat-list-item-body { flex: 1; min-width: 0; }

.chat-list-item-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.chat-list-item-name {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-list-item-time {
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

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

.empty-list {
  padding: 32px 24px;
  text-align: center;
  color: var(--text-secondary);
}
.empty-list p { margin: 4px 0; }
.empty-list .hint { font-size: 13px; opacity: 0.8; }

.conversation {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

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

.no-chat p { margin: 0; font-size: 15px; }

/* Mobile-responsive dashboard */
@media (max-width: 720px) {
  .sidebar { width: 100%; }
  .conversation { display: none; position: absolute; inset: 0; z-index: 20; }
  .dashboard.viewing-chat .sidebar { display: none; }
  .dashboard.viewing-chat .conversation { display: flex; }
  .nav-back { display: inline-flex; }
}

/* ==========================================================================
   Utility
   ========================================================================== */

[hidden] { display: none !important; }

/* ==========================================================================
   ATTACHMENTS
   ========================================================================== */

/* Image bubbles: borderless, just the image */
.bubble.bubble-media {
  padding: 3px;
  background: var(--gray-bubble);
  border-radius: 18px;
  overflow: hidden;
}
.bubble.bubble-media.bubble-out {
  background: var(--blue-gradient-bottom);
}
.bubble.bubble-media.bubble-out::before,
.bubble.bubble-media.bubble-in::before {
  display: none; /* drop the tail for media bubbles to match iOS */
}

.attachment-image {
  display: block;
  max-width: 260px;
  max-height: 320px;
  width: 100%;
  height: auto;
  border-radius: 15px;
  cursor: zoom-in;
  background: rgba(0,0,0,0.04);
  object-fit: cover;
}

/* File card bubble (non-image attachments) */
.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px 4px 0;
  min-width: 200px;
  max-width: 280px;
  text-decoration: none;
  color: inherit;
}

.file-card-icon {
  width: 40px;
  height: 48px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  position: relative;
  box-shadow: 0 1px 2px rgba(0,0,0,0.12);
}

.bubble-in .file-card-icon { background: #FFFFFF; }

.file-card-ext {
  font-size: 9px;
  font-weight: 700;
  color: var(--blue);
  letter-spacing: 0.04em;
}

.file-card-body { flex: 1; min-width: 0; }

.file-card-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
}

.file-card-meta {
  font-size: 12px;
  opacity: 0.75;
  margin-top: 1px;
}

/* Pending attachments preview row (above composer) */
.pending-attachments {
  display: flex;
  gap: 8px;
  padding: 8px 12px 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.pending-attachment {
  position: relative;
  flex-shrink: 0;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px solid var(--field-border);
}

.pending-attachment.is-uploading::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: rgba(0,0,0,0.35);
}

.pending-attachment .spinner {
  position: absolute;
  width: 22px; height: 22px;
  border: 2.5px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  z-index: 1;
}

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

.pending-attachment img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.pending-attachment .file-pill-ext {
  font-size: 10px;
  font-weight: 700;
  color: var(--blue);
  letter-spacing: 0.04em;
}

.pending-attachment .remove-attachment {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

/* Drag-over highlight */
body.dragging-over .messages {
  outline: 3px dashed var(--blue);
  outline-offset: -8px;
  background: rgba(0, 122, 255, 0.04);
}

/* Image lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fade-in 0.2s ease;
  cursor: zoom-out;
}
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.lightbox img {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: 4px;
  cursor: zoom-out;
}
.lightbox-close {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.12);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
}
