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

:root {
  --bg: #efeff4;
  --card: #ffffff;
  --border: rgba(60,60,67,0.13);
  --text-primary: #000000;
  --text-secondary: #8e8e93;
  --accent: #007aff;
  --unread-bg: #ffffff;
  --search-bg: rgba(118,118,128,0.12);
  --separator: rgba(60,60,67,0.13);
  --navbar-bg: #f2f2f7;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

/* ===================== SCREENS ===================== */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.screen.hidden { transform: translateX(100%); pointer-events: none; }

/* ===================== NAVBAR ===================== */
.navbar {
  background: var(--navbar-bg);
  border-bottom: 0.5px solid var(--border);
  padding: var(--tg-safe-area-inset-top, env(safe-area-inset-top, 4px)) 16px 8px;
  padding-right: calc(16px + var(--tg-content-safe-area-inset-right, 0px));
  padding-left: calc(16px + var(--tg-content-safe-area-inset-left, 0px));
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.navbar-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
}
.navbar-btn {
  font-size: 17px;
  color: var(--accent);
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}
.navbar-btn:active { opacity: 0.6; }
.navbar-btn.right { justify-content: flex-end; }
.navbar-btn.left { justify-content: flex-start; }
.navbar-icons {
  display: flex;
  align-items: center;
  gap: 20px;
  padding-right: var(--tg-content-safe-area-inset-right, 0px);
}
.navbar-icon-btn {
  background: none; border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  min-width: 36px; min-height: 36px;
  -webkit-tap-highlight-color: transparent;
}
.navbar-icon-btn:active { opacity: 0.6; }

/* Email screen navbar gets extra top padding for content safe area */
.email-navbar {
  padding-top: calc(var(--tg-safe-area-inset-top, env(safe-area-inset-top, 4px)) + var(--tg-content-safe-area-inset-top, 0px));
}

/* Back button */
.back-btn {
  display: flex;
  align-items: center;
  gap: 2px;
  color: var(--accent);
  font-size: 17px;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  background: none;
  border: none;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}
.back-btn:active { opacity: 0.6; }

/* ===================== SEARCH ===================== */
.search-bar {
  background: var(--navbar-bg);
  flex-shrink: 0;
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.25s ease, opacity 0.2s ease, padding 0.25s ease;
  padding: 0 16px;
}
.search-bar.open {
  max-height: 60px;
  opacity: 1;
  padding: 6px 16px 8px;
}
.search-input-wrap {
  flex: 1;
  background: var(--search-bg);
  border-radius: 100px;
  padding: 9px 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: text;
  touch-action: manipulation;
}
.search-icon { flex-shrink: 0; display: flex; align-items: center; pointer-events: none; }
.search-input-wrap input {
  flex: 1; min-width: 0;
  border: none; background: transparent;
  font-size: 17px; color: var(--text-primary);
  outline: none;
}
.search-input-wrap input::placeholder { color: var(--text-secondary); }
.search-clear-btn {
  flex-shrink: 0;
  border: none; background: none; padding: 0;
  cursor: pointer; line-height: 0; display: none;
}

/* ===================== INBOX ROW ===================== */
.inbox-row {
  background: var(--navbar-bg);
  border-bottom: 0.5px solid var(--border);
  padding: 6px 16px;
  margin-top: 8px;
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  position: relative; z-index: 101;
  justify-content: space-between;
  flex-shrink: 0;
}
.inbox-left {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}
.inbox-menu {
  background: none; border: none; cursor: pointer;
  padding: 4px; display: flex; align-items: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.inbox-label {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
}
.inbox-count {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  position: relative;
  top: 1px;
}
.inbox-icons {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
  margin-top: 2px;
}
.inbox-icon-btn {
  background: none; border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  min-width: 32px; min-height: 32px;
  -webkit-tap-highlight-color: transparent;
}
.inbox-icon-btn:active { opacity: 0.6; }
.navbar-icon { filter: invert(40%) sepia(0%) saturate(0%); pointer-events: none; display: block; }
.navbar-icon.active { filter: invert(29%) sepia(98%) saturate(1400%) hue-rotate(198deg) brightness(100%) contrast(104%); }

/* ===================== THREAD LIST ===================== */
.thread-list {
  flex: 1;
  overflow-y: auto;
  background: var(--card);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
}
.thread-list::-webkit-scrollbar { display: none; }

.thread-item {
  position: relative;
  overflow: hidden;
  background: var(--card);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.thread-item.swipe-ready { background: #cfe5ff; }
.thread-item-inner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  min-height: 72px;
  position: relative;
  background: var(--card);
  will-change: transform;
}
.thread-item-inner::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 78px;
  right: 0;
  height: 0.5px;
  background: var(--separator);
}

.avatar-wrap {
  position: relative;
  flex-shrink: 0;
  width: 50px;
  height: 50px;
}
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  color: #fff;
  font-family: -apple-system, sans-serif;
}
.avatar.unread {
  outline: 2.5px solid #007aff;
  outline-offset: 2px;
}

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

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

.thread-name-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
  overflow: hidden;
  min-width: 0;
}
.thread-name {
  font-size: 17px;
  font-weight: 600;
  color: #000;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex-shrink: 1;
}
.thread-name.unread { font-weight: 700; }
.thread-tags { display: flex; gap: 2px; flex-shrink: 0; align-items: center; }

/* ===================== SWIPE LEFT PANEL ===================== */
.swipe-left-panel {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  display: flex;
  align-items: stretch;
  width: 280px;
  transform: translateX(280px);
  will-change: transform;
}
.swipe-action-btn {
  display: flex; align-items: center; justify-content: center;
  width: 56px; flex-shrink: 0;
  background: #f2f2f7;
  border: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.swipe-icon-gray { filter: invert(60%) sepia(0%) saturate(0%); pointer-events: none; }
.swipe-icon-blue { filter: invert(29%) sepia(98%) saturate(1400%) hue-rotate(198deg) brightness(100%) contrast(104%); pointer-events: none; }
.swipe-icon-red { filter: invert(27%) sepia(96%) saturate(1234%) hue-rotate(338deg) brightness(95%) contrast(97%); pointer-events: none; }

.thread-time {
  font-size: 13px;
  color: #8e8e93;
}

.thread-subject {
  font-size: 14px;
  color: #3c3c43;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
  font-weight: 400;
}
.thread-subject.unread { font-weight: 500; }

.thread-preview {
  font-size: 13px;
  color: #8e8e93;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 400;
  min-height: 18px;
  display: block;
}

.thread-count {
  display: inline-block;
  background: transparent;
  border: 1px solid #c7c7cc;
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 12px;
  color: #8e8e93;
  font-weight: 500;
  flex-shrink: 0;
}

/* ===================== EMAIL SCREEN ===================== */
.email-navbar-info { flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; overflow: hidden; min-width: 0; }
.email-nav-av {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; color: #fff;
  flex-shrink: 0;
  cursor: pointer;
}
.email-nav-text { min-width: 0; }
.email-navbar-name { font-size: 16px; font-weight: 700; color: #000; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.email-navbar-subject { font-size: 13px; color: #8e8e93; font-weight: 400; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  -webkit-overflow-scrolling: touch;
}
.messages::-webkit-scrollbar { display: none; }

.date-sep { text-align: center; margin: 8px 0 4px; }
.date-sep span {
  font-size: 12px;
  color: var(--text-secondary);
  background: rgba(116,116,128,0.12);
  padding: 3px 10px;
  border-radius: 10px;
}

.msg { display: flex; gap: 6px; }
.msg.in { align-self: flex-start; width: 90%; max-width: 90%; }
.msg.out { align-self: flex-end; max-width: 90%; flex-direction: row-reverse; }

.msg-av {
  width: 28px; height: 28px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: #fff;
  flex-shrink: 0;
  align-self: flex-end;
}

.bubble {
  padding: 8px 12px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.45;
}
.in .bubble {
  background: var(--card);
  border-bottom-left-radius: 4px;
  color: var(--text-primary);
}
.out .bubble {
  background: var(--accent);
  border-bottom-right-radius: 4px;
  color: #fff;
}

.bubble-from { font-size: 12px; color: var(--accent); font-weight: 600; margin-bottom: 2px; }
.bubble-addr { font-size: 10px; color: var(--text-secondary); margin-bottom: 5px; }
.bubble-body { color: var(--text-primary); line-height: 1.5; white-space: pre-wrap; word-break: break-word; font-size: 16px; }
.out .bubble-body { color: #fff; }
.bubble-time { font-size: 11px; color: var(--text-secondary); text-align: right; margin-top: 4px; }
.out .bubble-time { color: rgba(255,255,255,0.7); }
.sig-toggle { font-size: 16px; color: var(--link-color, #007aff); cursor: pointer; margin-top: 4px; user-select: none; }
.sig-text { margin-top: 6px; font-size: 16px; color: #636366; white-space: pre-wrap; word-break: break-word; border-top: 1px solid var(--border); padding-top: 6px; }

/* Loading state */
.bubble-loading { color: var(--text-secondary); font-size: 14px; padding: 16px 0; text-align: center; }

/* Email HTML content */
.email-html img { max-width: 100%; height: auto; }
.email-html a { color: var(--accent); }
.email-html * { max-width: 100%; word-break: break-word; }

/* ===================== REPLY BAR ===================== */
.reply-bar {
  background: var(--navbar-bg);
  border-top: 0.5px solid var(--border);
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
  backdrop-filter: blur(20px);
}
.reply-attach {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}
.reply-attach:active { opacity: 0.6; }
.reply-input {
  flex: 1;
  background: var(--card);
  border: 0.5px solid var(--border);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 15px;
  color: var(--text-primary);
  font-family: inherit;
  resize: none;
  outline: none;
  min-height: 36px;
  max-height: 100px;
  line-height: 1.4;
}
.reply-input::placeholder { color: var(--text-secondary); }
.reply-send {
  width: 36px; height: 36px;
  background: var(--accent);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s;
}
.reply-send:active { transform: scale(0.92); opacity: 0.8; }

/* ===================== EMPTY / ERROR ===================== */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 60vh;
  gap: 16px;
  padding: 32px;
  text-align: center;
}
.empty-state p {
  font-size: 17px;
  color: #8e8e93;
  font-weight: 400;
  margin: 0;
}
.empty-state img {
  filter: invert(60%) sepia(0%) saturate(0%);
  opacity: 0.5;
}

/* ===================== TOAST ===================== */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: rgba(0,0,0,0.75);
  color: #fff;
  font-size: 14px;
  padding: 9px 18px;
  border-radius: 20px;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.22s, transform 0.22s;
  pointer-events: none;
  z-index: 9999;
}
.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===================== CONTACT SHEET ===================== */
#contact-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s;
}
#contact-overlay.open {
  opacity: 1;
  pointer-events: all;
}
#contact-sheet {
  background: var(--card);
  border-radius: 20px 20px 0 0;
  width: 100%;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.28s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
#contact-overlay.open #contact-sheet { transform: translateY(0); }

.sheet-handle {
  width: 36px; height: 4px;
  background: rgba(60,60,67,0.2);
  border-radius: 2px;
  margin: 10px auto 6px;
  touch-action: none;
}
.sheet-top {
  padding: 16px 16px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 0.5px solid var(--border);
}
.sheet-top-info { flex: 1; min-width: 0; }
.sheet-top-name { font-size: 16px; font-weight: 600; }
.sheet-top-addr { font-size: 13px; color: var(--text-secondary); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-close {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: rgba(116,116,128,0.12);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.sheet-close:active { opacity: 0.6; }

.sheet-action {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border-bottom: 0.5px solid var(--separator);
  text-decoration: none;
  cursor: pointer;
}
.sheet-action:active { background: var(--bg); }
.sheet-action-icon {
  width: 36px; height: 36px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.sheet-action-text { flex: 1; min-width: 0; }
.sheet-action-label { font-size: 11px; color: var(--text-secondary); }
.sheet-action-value { font-size: 15px; color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===================== COMPOSE SHEET ===================== */
#compose-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 300;
  display: flex; align-items: flex-end;
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s;
}
#compose-overlay.open { opacity: 1; pointer-events: all; }
#compose-sheet {
  background: var(--card);
  border-radius: 20px 20px 0 0;
  width: 100%; max-height: 85vh; overflow-y: auto;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.28s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
#compose-overlay.open #compose-sheet { transform: translateY(0); }
.compose-field { border-bottom: 0.5px solid var(--border); padding: 10px 16px; }
.compose-label { font-size: 11px; color: var(--text-secondary); margin-bottom: 3px; }
.compose-input { width: 100%; font-size: 15px; border: none; outline: none; background: transparent; color: var(--text-primary); font-family: inherit; }
#compose-body-input { min-height: 120px; resize: none; line-height: 1.5; padding-top: 4px; }

/* Spinner */
.spinner {
  width: 24px; height: 24px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
.pull-indicator {
  position: fixed; left: 50%;
  transform: translateX(-50%);
  width: 32px; height: 32px;
  border: 3px solid #e5e5ea; border-top-color: #007aff;
  border-radius: 50%; opacity: 0;
  transition: opacity 0.2s; pointer-events: none; z-index: 100;
}
.pull-indicator.visible { opacity: 1; }
.pull-indicator.spinning { animation: pull-spin 0.8s linear infinite; }
@keyframes pull-spin { to { transform: translateX(-50%) rotate(360deg); } }

#load-more-spinner { display: none; justify-content: center; padding: 14px 0 20px; }

/* ===================== SKELETON ===================== */
@keyframes shimmer {
  0%   { background-position: -300px 0; }
  100% { background-position: 300px 0; }
}
.skeleton-line {
  height: 13px; border-radius: 6px;
  background: linear-gradient(90deg,#ebebeb 25%,#d8d8d8 50%,#ebebeb 75%);
  background-size: 600px 100%;
  animation: shimmer 1.3s infinite linear;
  margin-bottom: 10px;
}
.skeleton-item { display: flex; gap: 12px; padding: 12px 16px; align-items: center; }
.skeleton-avatar {
  width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(90deg,#ebebeb 25%,#d8d8d8 50%,#ebebeb 75%);
  background-size: 600px 100%;
  animation: shimmer 1.3s infinite linear;
}
.skeleton-lines { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.skeleton-lines .skeleton-line { margin-bottom: 0; }
.skeleton-lines .skeleton-line.short { width: 55%; }

/* ===================== NAVBAR ACCOUNT ===================== */
.navbar-account {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.navbar-account:active { opacity: 0.7; }
.navbar-av {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: #fff;
}
.navbar-email-label {
  font-size: 9px; color: #8e8e93; text-align: center;
  max-width: 180px; white-space: normal; word-break: break-all; line-height: 1.2;
}
.icon-choice-btn {
  aspect-ratio: 1; border-radius: 10px;
  border: 1.5px solid #e5e5ea; background: transparent;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; -webkit-tap-highlight-color: transparent; padding: 0;
}
.icon-choice-btn:active { opacity: 0.7; }

/* ===================== GENERIC OVERLAY/SHEET ===================== */
.bottom-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 200;
  display: flex; align-items: flex-end;
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s;
}
.bottom-overlay.open { opacity: 1; pointer-events: all; }
.bottom-sheet {
  background: var(--card);
  border-radius: 20px 20px 0 0;
  width: 100%;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.28s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  max-height: 75vh; overflow-y: auto;
}
.bottom-overlay.open .bottom-sheet { transform: translateY(0); }

/* ===================== TAG SHEET ===================== */
#tag-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 250;
  display: flex; align-items: flex-end;
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s;
}
#tag-overlay.open { opacity: 1; pointer-events: all; }
#tag-sheet {
  background: var(--card);
  border-radius: 20px 20px 0 0;
  width: 100%;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.28s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
#tag-overlay.open #tag-sheet { transform: translateY(0); }

/* Tag icon grid */
#tag-icon-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; padding: 0 16px 8px; }
