/* ─── CSS Variables / Themes ──────────────────────────────────────────────── */
:root {
  --accent: #1a7f5a;
  --accent-dark: #145f44;
  --accent-light: #22a872;

  --bg-primary: #ffffff;
  --bg-secondary: #f0f2f5;
  --bg-tertiary: #e9ecef;
  --bg-input: #f0f2f5;
  --bg-header: #1a7f5a;
  --bg-msg-out: #d9fdd3;
  --bg-msg-in: #ffffff;
  --bg-modal: rgba(0,0,0,0.5);

  --text-primary: #111b21;
  --text-secondary: #667781;
  --text-muted: #8696a0;
  --text-header: #ffffff;
  --text-msg-out: #111b21;
  --text-msg-in: #111b21;
  --text-link: #027eb5;

  --border: #e9edef;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.15);
  --shadow-lg: 0 4px 20px rgba(0,0,0,0.2);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 20px;
  --radius-bubble: 18px;

  --header-h: 60px;
  --input-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

[data-theme="dark"] {
  --bg-primary: #0b141a;
  --bg-secondary: #111b21;
  --bg-tertiary: #1f2c34;
  --bg-input: #1f2c34;
  --bg-header: #1f2c34;
  --bg-msg-out: #005c4b;
  --bg-msg-in: #1f2c34;

  --text-primary: #e9edef;
  --text-secondary: #8696a0;
  --text-muted: #667781;
  --text-header: #e9edef;
  --text-msg-out: #e9edef;
  --text-msg-in: #e9edef;
  --text-link: #53bdeb;

  --border: #222d35;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.4);
  --shadow-lg: 0 4px 20px rgba(0,0,0,0.5);
}

/* ─── Reset & Base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, sans-serif;
  font-size: 15px;
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; }
input, textarea { font: inherit; color: inherit; outline: none; }
a { color: var(--text-link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ─── Screens ────────────────────────────────────────────────────────────── */
.screen {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
}

/* ─── Auth Screen ────────────────────────────────────────────────────────── */
.auth-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px calc(40px + var(--safe-bottom));
  gap: 12px;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  box-sizing: border-box;
}

.auth-logo { font-size: 56px; }
.auth-title { font-size: 28px; font-weight: 700; color: var(--accent); margin: 0; }
.auth-subtitle { font-size: 14px; color: var(--text-secondary); margin: 0 0 8px; }

.auth-tabs {
  display: flex;
  gap: 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  width: 100%;
}

.auth-tab {
  flex: 1;
  padding: 10px;
  font-size: 14px;
  font-weight: 500;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  border: none;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.auth-tab.active {
  background: var(--accent);
  color: white;
}

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

.auth-error {
  color: #e74c3c;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}

.btn-full { width: 100%; }

.settings-advanced summary { color: var(--text-secondary); font-size: 13px; }

/* ─── Views ──────────────────────────────────────────────────────────────── */
#screen-main {
  position: fixed;
  inset: 0;
}

.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  transition: transform 0.25s ease;
}

.view.hidden { display: none; }

/* ─── App Header ─────────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: var(--bg-header);
  color: var(--text-header);
  min-height: calc(var(--header-h) + var(--safe-top));
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  z-index: 10;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-subtitle {
  font-size: 12px;
  opacity: 0.8;
}

.header-contact {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.header-actions {
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text-header);
  transition: background 0.15s;
}

.icon-btn:active { background: rgba(255,255,255,0.15); }

/* ─── Status Bar ─────────────────────────────────────────────────────────── */
.status-bar {
  padding: 4px 16px;
  font-size: 12px;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  text-align: center;
  min-height: 0;
}

.status-bar:empty { display: none; }

/* ─── Chat List ──────────────────────────────────────────────────────────── */
.chat-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.chat-item:active { background: var(--bg-secondary); }

.contact-avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  flex-shrink: 0;
}

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

.chat-name {
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

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

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

.unread-badge {
  background: var(--accent);
  color: white;
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 12px;
  font-weight: 600;
}

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

.empty-hint { font-size: 13px; opacity: 0.7; }

/* ─── Messages Container ─────────────────────────────────────────────────── */
.messages-container {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--bg-secondary);
  background-image:
    radial-gradient(circle at 20% 80%, rgba(26,127,90,0.03) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(26,127,90,0.03) 0%, transparent 50%);
}

/* ─── Message Bubbles ────────────────────────────────────────────────────── */
.msg-group {
  display: flex;
  flex-direction: column;
  max-width: 75%;
}

.msg-group.out { align-self: flex-end; align-items: flex-end; }
.msg-group.in { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
  position: relative;
  padding: 8px 12px;
  border-radius: var(--radius-bubble);
  box-shadow: var(--shadow-sm);
  max-width: 100%;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg-group.out .msg-bubble {
  background: var(--bg-msg-out);
  color: var(--text-msg-out);
  border-bottom-right-radius: 4px;
}

.msg-group.in .msg-bubble {
  background: var(--bg-msg-in);
  color: var(--text-msg-in);
  border-bottom-left-radius: 4px;
}

.msg-text { font-size: 15px; line-height: 1.4; }

.msg-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 3px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.msg-group.out .msg-time { justify-content: flex-end; }

.msg-status { font-size: 12px; }

/* Date separator */
.date-separator {
  text-align: center;
  margin: 12px 0;
}

.date-separator span {
  display: inline-block;
  background: var(--bg-msg-in);
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 10px;
  box-shadow: var(--shadow-sm);
}

/* ─── Location Message ───────────────────────────────────────────────────── */
.msg-location {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 180px;
}

.location-map-preview {
  width: 100%;
  height: 120px;
  border-radius: var(--radius-sm);
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  overflow: hidden;
}

.location-info { font-size: 13px; }
.location-coords { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.btn-open-maps {
  padding: 6px 12px;
  background: var(--accent);
  color: white;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  text-align: center;
  margin-top: 4px;
  display: block;
  transition: background 0.15s;
}

.btn-open-maps:active { background: var(--accent-dark); }

/* ─── Photo Message ──────────────────────────────────────────────────────── */
.msg-photos {
  display: grid;
  gap: 3px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  max-width: 240px;
}

.msg-photos.count-1 { grid-template-columns: 1fr; }
.msg-photos.count-2 { grid-template-columns: 1fr 1fr; }
.msg-photos.count-3 { grid-template-columns: 1fr 1fr; }
.msg-photos.count-3 .photo-thumb:first-child { grid-column: 1/-1; }
.msg-photos.count-4, .msg-photos.count-many { grid-template-columns: 1fr 1fr; }

.photo-thumb {
  aspect-ratio: 1;
  object-fit: cover;
  cursor: pointer;
  display: block;
  width: 100%;
  transition: opacity 0.15s;
}

.photo-thumb:active { opacity: 0.8; }

.msg-bubble.photo-bubble { padding: 3px; }

/* ─── Input Area ─────────────────────────────────────────────────────────── */
.input-area {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 8px calc(8px + var(--safe-bottom));
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.input-wrapper {
  flex: 1;
  background: var(--bg-input);
  border-radius: 22px;
  display: flex;
  align-items: flex-end;
  padding: 8px 14px;
  min-height: 44px;
}

.msg-input {
  flex: 1;
  background: none;
  border: none;
  resize: none;
  max-height: 120px;
  line-height: 1.5;
  font-size: 15px;
  color: var(--text-primary);
  overflow-y: auto;
}

.msg-input::placeholder { color: var(--text-muted); }

.input-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  transition: background 0.1s, transform 0.1s;
}

.input-btn:active { transform: scale(0.9); }

.send-btn {
  background: var(--accent);
  color: white;
  font-size: 18px;
}

.send-btn:active { background: var(--accent-dark); }

/* ─── Files Preview ──────────────────────────────────────────────────────── */
.files-preview {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  overflow-x: auto;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.file-preview-item {
  position: relative;
  flex-shrink: 0;
  width: 70px;
  height: 70px;
  border-radius: var(--radius-sm);
  overflow: hidden;
}

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

.file-preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0,0,0,0.7);
  color: white;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ─── Attach Menu ────────────────────────────────────────────────────────── */
.attach-menu {
  display: flex;
  gap: 0;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.attach-option {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px;
  font-size: 13px;
  color: var(--text-secondary);
  transition: background 0.1s;
}

.attach-option:active { background: var(--bg-secondary); }
.attach-icon { font-size: 32px; }

/* ─── Emoji Picker ───────────────────────────────────────────────────────── */
.emoji-picker {
  position: absolute;
  bottom: calc(var(--input-h) + var(--safe-bottom));
  left: 0;
  right: 0;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  height: 280px;
  display: flex;
  flex-direction: column;
  z-index: 50;
  box-shadow: var(--shadow-lg);
}

.emoji-cats {
  display: flex;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  scrollbar-width: none;
}

.emoji-cats::-webkit-scrollbar { display: none; }

.emoji-cat-btn {
  padding: 8px 12px;
  font-size: 20px;
  flex-shrink: 0;
  transition: background 0.1s;
  border-bottom: 2px solid transparent;
}

.emoji-cat-btn.active { border-bottom-color: var(--accent); }
.emoji-cat-btn:active { background: var(--bg-secondary); }

.emoji-grid {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  overflow-y: auto;
  padding: 8px;
  gap: 2px;
  -webkit-overflow-scrolling: touch;
}

.emoji-btn {
  width: 38px;
  height: 38px;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: background 0.1s, transform 0.1s;
}

.emoji-btn:active { background: var(--bg-secondary); transform: scale(1.3); }

/* ─── Settings ───────────────────────────────────────────────────────────── */
.settings-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.settings-section {
  padding: 20px 16px;
  border-bottom: 8px solid var(--bg-secondary);
}

.settings-section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 14px;
}

.settings-label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
  margin-top: 12px;
}

.settings-label:first-child { margin-top: 0; }

.settings-input {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  color: var(--text-primary);
  transition: border-color 0.15s;
}

.settings-input:focus { border-color: var(--accent); }

.password-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.password-wrapper .settings-input { padding-right: 48px; }

.toggle-password {
  position: absolute;
  right: 12px;
  font-size: 18px;
  color: var(--text-secondary);
}

.settings-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
  line-height: 1.5;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
}

/* Toggle Switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 28px;
}

.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--bg-tertiary);
  border-radius: 28px;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: var(--shadow-sm);
}

.toggle-switch input:checked + .toggle-slider { background: var(--accent); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(22px); }

/* Buttons */
.btn-primary {
  display: block;
  width: 100%;
  padding: 13px;
  background: var(--accent);
  color: white;
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  margin-top: 14px;
  transition: background 0.15s, transform 0.1s;
}

.btn-primary:active { background: var(--accent-dark); transform: scale(0.98); }

.btn-secondary {
  display: block;
  width: 100%;
  padding: 13px;
  background: var(--bg-input);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 500;
  text-align: center;
  margin-top: 14px;
  border: 1px solid var(--border);
  transition: background 0.15s;
}

.btn-secondary:active { background: var(--bg-tertiary); }

/* Contact in settings */
.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.contact-item-info { flex: 1; min-width: 0; }
.contact-item-name { font-size: 15px; font-weight: 500; }
.contact-item-email { font-size: 12px; color: var(--text-secondary); }

.btn-contact-delete {
  color: #e74c3c;
  font-size: 18px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.add-contact-form { padding-top: 12px; }

/* Theme Options */
.theme-options {
  display: flex;
  gap: 8px;
}

.theme-option {
  flex: 1;
  padding: 10px 8px;
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  font-size: 14px;
  border: 2px solid transparent;
  transition: border-color 0.15s, background 0.15s;
  text-align: center;
}

.theme-option.active { border-color: var(--accent); }
.theme-option:active { background: var(--bg-tertiary); }

/* App info */
.app-info {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.8;
}

/* ─── Toast ──────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #323232;
  color: white;
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-size: 14px;
  z-index: 1000;
  max-width: 80vw;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.2s ease;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ─── Image Viewer ───────────────────────────────────────────────────────── */
.image-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.95);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-viewer-close {
  position: absolute;
  top: calc(var(--safe-top) + 12px);
  right: 16px;
  width: 44px;
  height: 44px;
  background: rgba(255,255,255,0.15);
  color: white;
  border-radius: 50%;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 201;
}

.image-viewer-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* ─── Modal ──────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-modal);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow-lg);
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.modal-actions .btn-primary,
.modal-actions .btn-secondary {
  flex: 1;
  margin-top: 0;
}

/* ─── Scroll to bottom button ───────────────────────────────────────────── */
.scroll-bottom-btn {
  position: absolute;
  bottom: calc(var(--input-h) + 16px + var(--safe-bottom));
  right: 16px;
  width: 42px;
  height: 42px;
  background: var(--bg-primary);
  border-radius: 50%;
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text-secondary);
  z-index: 20;
  transition: opacity 0.2s;
}

/* ─── Loading spinner ────────────────────────────────────────────────────── */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--bg-tertiary);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 20px auto;
}

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

/* ─── Scrollbar ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-tertiary); border-radius: 4px; }

/* ─── Media Queries ──────────────────────────────────────────────────────── */
@media (min-width: 600px) {
  #screen-main {
    max-width: 480px;
    margin: 0 auto;
    box-shadow: var(--shadow-lg);
  }
}
