/* ════════════════════════════════════════════════════════
   iMessage-style Chat UI
   ════════════════════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #ffffff;
  --bubble-mine: #007aff;
  --bubble-other: #e9e9eb;
  --text-mine: #ffffff;
  --text-other: #000000;
  --text-secondary: #8e8e93;
  --accent: #007aff;
  --danger: #ff3b30;
  --header-bg: rgba(255,255,255,0.85);
  --header-text: #000000;
  --border: rgba(0,0,0,0.08);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 48px;
}

html, body {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  margin: 0; padding: 0; overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  font-size: 17px;
  background: #fff;
  color: #000;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.hidden { display: none !important; }

/* ════════════════════════════════════════════════════════
   LOGIN
   ════════════════════════════════════════════════════════ */
.login-container {
  display: flex; align-items: center; justify-content: center;
  height: 100%; padding: 20px; background: #f2f2f7;
}
.login-card {
  background: #fff; border-radius: 16px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.06); padding: 36px 24px;
  width: 100%; max-width: 360px; text-align: center;
}
.login-icon { font-size: 40px; margin-bottom: 8px; }
.login-card h1 { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.login-subtitle { color: var(--text-secondary); font-size: 14px; margin-bottom: 24px; }

.form-group { margin-bottom: 14px; text-align: left; }
.form-group label { display: block; font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.form-group input {
  width: 100%; padding: 12px; border: 1px solid #d1d1d6;
  border-radius: 10px; font-size: 16px; outline: none;
  -webkit-appearance: none; background: #f2f2f7;
}
.form-group input:focus { border-color: var(--accent); background: #fff; }

.form-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 6px; text-align: center; }
.form-success { color: #34c759; font-size: 13px; min-height: 18px; font-weight: 600; }

/* ── Buttons ────────────────────────────────────────── */
.btn-primary {
  width: 100%; padding: 14px; border: none; border-radius: 12px;
  background: var(--accent); color: #fff; font-size: 17px; font-weight: 600;
  cursor: pointer; letter-spacing: 0.3px;
}
.btn-primary:active { background: #0066d6; }
.btn-secondary {
  padding: 10px 16px; border: 1px solid #d1d1d6; border-radius: 10px;
  background: #fff; color: var(--accent); font-size: 14px; cursor: pointer;
  text-align: center;
}
.btn-sm {
  background: none; border: none; font-size: 13px; cursor: pointer;
  color: var(--accent); padding: 4px 8px;
}
.btn-danger { color: var(--danger); }

/* ════════════════════════════════════════════════════════
   CHAT APP
   ════════════════════════════════════════════════════════ */
.chat-app {
  display: flex; flex-direction: column;
  width: 100%; height: 100%;
  background: #fff;
  position: relative; overflow: hidden;
}

/* ── Header ───────────────────────────────────────── */
.chat-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; height: var(--header-h);
  background: var(--header-bg);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0; z-index: 10;
}
.chat-header .btn-icon {
  background: none; border: none; font-size: 15px; cursor: pointer;
  color: var(--accent); padding: 8px 4px;
  min-width: 34px; text-align: center; font-weight: 400;
}
.chat-header .btn-icon:active { opacity: 0.5; }
.header-title {
  font-size: 17px; font-weight: 600; text-align: center; flex: 1;
  color: #000;
}
.header-subtitle { font-size: 11px; color: var(--text-secondary); display: block; font-weight: 400; }

/* ── Messages ─────────────────────────────────────── */
.messages-container {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 8px 12px;
  -webkit-overflow-scrolling: touch;
  background: #fff;
  position: relative;
}
.messages-end { height: 1px; }

/* Scroll-to-bottom button */
.scroll-bottom-btn {
  position: absolute; bottom: 120px; right: 16px; z-index: 20;
  width: 32px; height: 32px; border-radius: 50%;
  border: none; background: rgba(0,0,0,0.08);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 16px; color: var(--accent);
}

.system-notice {
  text-align: center; font-size: 13px; color: var(--text-secondary);
  padding: 16px 0; user-select: none;
}

/* ── iMessage Bubble ──────────────────────────────── */
.message {
  display: flex; align-items: flex-end; gap: 8px;
  margin-bottom: 2px; padding: 1px 40px 1px 0;
  animation: msgIn 0.15s ease;
}
@keyframes msgIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }
.message-mine { padding: 1px 0 1px 40px; flex-direction: row-reverse; }

/* Hide avatars for consecutive messages (handled in JS by grouping) */
.msg-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  flex-shrink: 0; overflow: hidden; align-self: flex-end;
  margin-bottom: 2px;
  background: #c7c7cc;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; color: #fff;
}
.msg-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.msg-avatar img[alt]::after {
  /* show alt text when image fails */
}
.msg-avatar img:not([src]), .msg-avatar img[src=""], .msg-avatar img[data-broken] {
  display: none;
}
/* Show fallback initial when avatar image is broken */
.msg-avatar[data-broken]::after {
  content: attr(data-initial);
  font-size: 12px; font-weight: 600; color: #8e8e93;
}
.msg-avatar-placeholder {
  width: 28px; height: 28px; border-radius: 50%;
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  font-size: 13px; color: #fff; font-weight: 600; align-self: flex-end;
  margin-bottom: 2px;
}

.msg-username {
  font-size: 13px; color: #8e8e93; margin-bottom: 2px;
  font-weight: 500; letter-spacing: -0.1px; padding-left: 2px;
}

.msg-body { max-width: 75%; min-width: 0; }

.msg-bubble {
  padding: 8px 12px; border-radius: 18px; line-height: 1.35;
  word-break: break-word; white-space: pre-wrap; font-size: 17px;
  position: relative;
}
.message:not(.message-mine) .msg-bubble {
  background: var(--bubble-other); color: var(--text-other);
}
.message-mine .msg-bubble {
  background: var(--bubble-mine); color: var(--text-mine);
}

/* Bubble tail for last in group */
.msg-bubble-tail:not(.message-mine) .msg-bubble::after {
  content: ''; position: absolute; bottom: 0; left: -6px;
  width: 14px; height: 14px;
  background: var(--bubble-other);
  clip-path: polygon(100% 0, 0 100%, 100% 100%);
}
.msg-bubble-tail.message-mine .msg-bubble::after {
  content: ''; position: absolute; bottom: 0; right: -6px;
  width: 14px; height: 14px;
  background: var(--bubble-mine);
  clip-path: polygon(0 0, 0 100%, 100% 100%);
}

.msg-image {
  max-width: 200px; max-height: 280px; border-radius: 16px;
  display: block; cursor: pointer;
}
.msg-image-zoom {
  position: fixed; z-index: 200; top: 50%; left: 50%;
  transform: translate(-50%, -50%); max-width: 94vw; max-height: 85vh;
  object-fit: contain; border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0,0,0,0.5);
}

.msg-meta {
  font-size: 11px; color: var(--text-secondary);
  margin-bottom: 1px; padding: 0 4px;
}
.message-mine .msg-meta { text-align: right; }

.msg-delete {
  display: none; background: none; border: none; color: var(--text-secondary);
  font-size: 12px; cursor: pointer; position: absolute; top: 50%; transform: translateY(-50%);
}
.message:not(.message-mine) .msg-delete { right: -28px; }
.message-mine .msg-delete { left: -28px; }
.message:hover .msg-delete, .message:active .msg-delete { display: block; }

/* Timestamp separator */
.msg-timestamp {
  text-align: center; padding: 10px 0; font-size: 11px;
  color: var(--text-secondary); font-weight: 500;
}

.load-more { text-align: center; padding: 12px 0; }
.btn-load-more {
  background: none; border: none; font-size: 13px; color: var(--accent);
  cursor: pointer; padding: 6px 12px;
}
.btn-load-more:active { opacity: 0.5; }

/* ── Typing ───────────────────────────────────────── */
.typing-indicator {
  font-size: 13px; color: var(--text-secondary);
  padding: 2px 14px 4px; flex-shrink: 0;
}
.image-preview {
  padding: 8px 14px; background: #fff; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px; flex-shrink: 0;
}
.image-preview img { width: 44px; height: 44px; object-fit: cover; border-radius: 10px; }
.preview-remove {
  background: var(--danger); color: #fff; border: none; border-radius: 50%;
  width: 18px; height: 18px; font-size: 11px; cursor: pointer;
}

/* ── iMessage Input Bar ────────────────────────────── */
.chat-input-area {
  flex-shrink: 0; background: #fff;
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.chat-input {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 10px;
}
.chat-input textarea {
  flex: 1; padding: 9px 14px; border: 1px solid #d1d1d6;
  border-radius: 20px; font-size: 17px; outline: none;
  resize: none; min-height: 36px; max-height: 100px;
  font-family: inherit; line-height: 1.25;
  background: #f2f2f7; -webkit-appearance: none;
}
.btn-emoji, .btn-plus {
  width: 32px; height: 32px; border: none; background: none;
  cursor: pointer; flex-shrink: 0; display: flex;
  align-items: center; justify-content: center;
  font-size: 18px; opacity: 0.6;
}
.btn-emoji:active, .btn-plus:active { opacity: 0.3; }
.btn-send {
  min-width: 32px; height: 32px; border: none; border-radius: 50%;
  background: linear-gradient(135deg, #007aff, #0a84ff);
  color: #fff; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; padding: 0;
  box-shadow: 0 2px 8px rgba(0,122,255,0.3);
}
.btn-send:active { transform: scale(0.92); opacity: 0.8; }
.btn-send:disabled { background: #c7c7cc; box-shadow: none; cursor: default; }

/* ── Emoji Picker ────────────────────────────────── */
.emoji-picker {
  background: #f2f2f7; border-top: 1px solid var(--border);
  max-height: 240px; overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.emoji-tabs {
  display: flex; border-bottom: 1px solid #e5e5ea;
  background: #fafafa; position: sticky; top: 0; z-index: 1;
}
.emoji-tab {
  flex: 1; padding: 8px; border: none; background: none; cursor: pointer;
  font-size: 16px; text-align: center; opacity: 0.5;
}
.emoji-tab.active { opacity: 1; border-bottom: 2px solid var(--accent); }
.emoji-grid {
  display: grid; grid-template-columns: repeat(8, 1fr);
  gap: 1px; padding: 6px;
}
.emoji-item {
  width: 100%; aspect-ratio: 1; border: none; background: none;
  cursor: pointer; font-size: 22px; display: flex;
  align-items: center; justify-content: center;
  border-radius: 6px;
}
.emoji-item:active { background: #e5e5ea; }

/* ════════════════════════════════════════════════════════
   PROFILE
   ════════════════════════════════════════════════════════ */
.profile-app {
  width: 100%; height: 100%;
  background: #f2f2f7; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.profile-header {
  display: flex; align-items: center; padding: 0 12px;
  height: var(--header-h); background: var(--header-bg);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.profile-header .btn-icon { background: none; border: none; color: var(--accent); font-size: 15px; cursor: pointer; }
.profile-header h2 { font-size: 17px; font-weight: 600; color: #000; }

.profile-avatar-section {
  background: #fff; padding: 20px 0 14px; text-align: center; margin-bottom: 16px;
}
.profile-avatar-preview {
  width: 56px; height: 56px; border-radius: 50%; margin: 0 auto 8px;
  cursor: pointer; overflow: hidden; background: #e5e5ea;
  display: flex; align-items: center; justify-content: center; font-size: 28px;
}
.profile-avatar-preview img { width: 100%; height: 100%; object-fit: cover; display: block; }
.profile-avatar-hint { color: var(--accent); font-size: 13px; }
.avatar-saved { display: inline-block; color: #34c759; font-size: 12px; font-weight: 600; margin-top: 2px; animation: fadeInOut 1.5s ease; }
@keyframes fadeInOut { 0%{opacity:0;transform:translateY(-4px)}30%{opacity:1;transform:translateY(0)}80%{opacity:1}100%{opacity:0} }
.profile-name-title { font-size: 20px; font-weight: 600; margin-top: 4px; }

.profile-block {
  background: #fff; padding: 10px 14px; margin-bottom: 1px;
  border-bottom: 1px solid var(--border);
}
.profile-block h4 { font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px; }

.avatar-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.avatar-preset {
  width: 100%; aspect-ratio: 1; border-radius: 50%; border: 2px solid transparent;
  cursor: pointer; overflow: hidden; background: #e5e5ea;
  transition: border-color 0.15s;
}
.avatar-preset img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-preset.selected { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(0,122,255,0.3); }
.avatar-preset:active { opacity: 0.85; }

.profile-form-group { margin-bottom: 10px; }
.profile-form-group label { display: block; font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.profile-form-group input {
  width: 100%; padding: 10px 12px; border: 1px solid #d1d1d6;
  border-radius: 10px; font-size: 16px; outline: none;
  -webkit-appearance: none; background: #f2f2f7;
}
.profile-form-group input:focus { border-color: var(--accent); background: #fff; }
.profile-btn-row { display: flex; gap: 8px; }
.profile-btn-row .btn-primary, .profile-btn-row .btn-secondary { flex: 1; }

/* ════════════════════════════════════════════════════════
   ADMIN
   ════════════════════════════════════════════════════════ */
.admin-app {
  display: flex; flex-direction: column;
  width: 100%; height: 100%;
  background: #f2f2f7;
}
.admin-header {
  display: flex; align-items: center; padding: 0 12px;
  height: var(--header-h); background: var(--header-bg);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.admin-header .btn-icon { background: none; border: none; color: var(--accent); font-size: 15px; cursor: pointer; }
.admin-header h2 { font-size: 17px; font-weight: 600; color: #000; }
.admin-content { flex: 1; overflow-y: auto; padding: 14px; }

/* ════════════════════════════════════════════════════════
   UTILITY
   ════════════════════════════════════════════════════════ */
.loading {
  position: fixed; inset: 0; background: rgba(0,0,0,0.2);
  display: flex; align-items: center; justify-content: center; z-index: 300;
}
.spinner {
  width: 24px; height: 24px; border: 3px solid rgba(255,255,255,0.3);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.pwa-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 500;
  background: #f2f2f7; color: #000; padding: 10px 14px;
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; gap: 10px; border-bottom: 1px solid var(--border);
  transform: translateY(-100%); transition: transform 0.3s;
}
.pwa-banner.show { transform: translateY(0); }
.pwa-banner button {
  background: var(--accent); color: #fff; border: none;
  border-radius: 8px; padding: 6px 12px; font-size: 13px; cursor: pointer;
}
.pwa-banner .close-banner { background: none; color: var(--text-secondary); font-size: 16px; padding: 0 4px; }

.btn-biometric {
  width: 100%; padding: 14px; border: 2px solid var(--accent);
  border-radius: 12px; background: rgba(0,122,255,0.08);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  gap: 8px; font-size: 16px; font-weight: 600; color: var(--accent);
}

@media (max-width: 360px) {
  .msg-body { max-width: 82%; }
  .msg-bubble { font-size: 16px; }
}
