:root {
  /* Modern Color Palette */
  --bg: #f8fafc;
  --bg-gradient: linear-gradient(135deg, #e0f2fe 0%, #f1f5f9 50%, #e0e7ff 100%);
  --panel: #ffffff;
  --panel-glass: rgba(255, 255, 255, 0.92);
  --soft: #f1f5f9;
  --soft-hover: #e2e8f0;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #0284c7;
  --accent-dark: #0369a1;
  --accent-light: #e0f2fe;
  --danger: #ef4444;
  --danger-dark: #dc2626;
  --danger-light: #fee2e2;
  --success: #10b981;

  /* Elevation & Shadows */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.08), 0 8px 10px -6px rgba(0, 0, 0, 0.04);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.04);

  /* Radius & Motion */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  color: var(--text);
  background: var(--bg-gradient);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
}

button {
  border: 0;
  cursor: pointer;
  background: transparent;
}

.hidden,
body[data-connected="1"] .login-screen,
body[data-connected="0"] .mail-app {
  display: none !important;
}

/* ==================== LOGIN SCREEN ==================== */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.login-card {
  width: min(480px, 92vw);
  background: var(--panel-glass);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.9);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  padding: 28px 24px;
  animation: slideUp 0.25s ease-out;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
}

.login-card h1::before {
  content: "@";
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  border-radius: var(--radius-md);
  font-size: 18px;
  flex-shrink: 0;
}

.login-card p {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.5;
}

label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: #ffffff;
  color: var(--text);
  outline: none;
  transition: var(--transition);
}

input, select {
  height: 40px;
  padding: 0 12px;
}

textarea {
  min-height: 120px;
  padding: 10px 12px;
  resize: vertical;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15);
  background: #ffffff;
}

details {
  margin-bottom: 16px;
  background: var(--soft);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  border: 1px solid var(--line);
}

summary {
  cursor: pointer;
  color: var(--accent-dark);
  font-weight: 600;
  font-size: 13px;
  user-select: none;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.login-card button[type="submit"], .primary {
  width: 100%;
  height: 42px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
  color: #ffffff;
  font-weight: 700;
  font-size: 14px;
  padding: 0 18px;
  box-shadow: 0 3px 10px rgba(2, 132, 199, 0.25);
  transition: var(--transition);
}

.login-card button[type="submit"]:hover, .primary:hover {
  opacity: 0.95;
  transform: translateY(-1px);
  box-shadow: 0 5px 14px rgba(2, 132, 199, 0.35);
}

.status {
  margin-top: 14px;
  font-size: 13px;
  line-height: 1.4;
  word-break: break-word;
}

/* ==================== SIDEBAR OVERLAY ==================== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
  z-index: 1040;
}

.sidebar-overlay.active {
  display: block;
}

/* ==================== MAIL APP MAIN LAYOUT ==================== */
.mail-app {
  display: grid;
  grid-template-columns: 220px 320px minmax(0, 1fr);
  height: 100vh;
  padding: 12px;
  gap: 12px;
  max-width: 100vw;
  box-sizing: border-box;
}

.sidebar, .inbox, .reader {
  background: var(--panel-glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* --- SIDEBAR --- */
.sidebar {
  padding: 16px 14px;
  gap: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
  font-weight: 800;
  color: var(--text);
}

.brand span {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  flex-shrink: 0;
}

.mobile-drawer-close {
  display: none;
  font-size: 18px;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  margin-left: auto;
}

.mobile-drawer-close:hover {
  background: var(--soft);
  color: var(--text);
}

.compose-link {
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.secondary-action {
  height: 38px;
  border-radius: var(--radius-md);
  background: var(--soft);
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 600;
  font-size: 13.5px;
  transition: var(--transition);
}

.secondary-action:hover {
  background: var(--soft-hover);
  border-color: var(--line-strong);
}

.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 3px;
  overflow-y: auto;
  flex: 1;
}

.sidebar nav button {
  min-height: 38px;
  padding: 0 12px;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 13.5px;
  text-align: left;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar nav button:hover {
  background: var(--soft);
  color: var(--text);
}

.sidebar nav button.active {
  background: var(--accent-light);
  color: var(--accent-dark);
}

.account {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  background: var(--soft);
  border: 1px solid var(--line);
  overflow: hidden;
  flex-wrap: wrap;
}

#accountSwitcher {
  min-width: 0;
  flex: 1 1 100%;
  height: 32px;
  font-size: 12px;
}

.account-email-current {
  display: none;
}

#addAccountBtn,
#logoutBtn {
  height: 30px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  background: #ffffff;
  border: 1px solid var(--line);
  font-weight: 600;
  font-size: 11px;
  flex: 1;
}

#addAccountBtn {
  color: var(--accent-dark);
}

.account strong {
  font-size: 12.5px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

#logoutBtn {
  color: var(--danger-dark);
  transition: var(--transition);
}

#logoutBtn:hover {
  background: var(--danger-light);
  border-color: #fca5a5;
}

/* --- INBOX COLUMN --- */
.inbox {
  display: flex;
  flex-direction: column;
}

.topbar {
  padding: 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.topbar-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mobile-menu-btn {
  display: none;
  font-size: 20px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  background: var(--soft);
  border: 1px solid var(--line);
  color: var(--text);
}

.topbar h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: var(--text);
}

.topbar input[type="search"] {
  height: 36px;
  background: var(--soft);
  border: 1px solid var(--line);
  font-size: 13px;
}

.topbar input[type="search"]:focus {
  background: #ffffff;
}

.message-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.message {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
  text-align: left;
  cursor: pointer;
  transition: var(--transition);
}

.message:hover {
  background: #f0f9ff;
}

.message.active {
  background: #e0f2fe;
  border-left: 4px solid var(--accent);
}

.message.selected {
  background: #bae6fd !important;
  border-left: 4px solid var(--accent-dark) !important;
}

.message.unread {
  background: #fafafa;
}

.message.unread .sender,
.message.unread .subject {
  font-weight: 800;
  color: var(--text);
}

.message-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}

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

.time {
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
}

.subject {
  font-size: 12.5px;
  color: #334155;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview {
  font-size: 11.5px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Pagination */
#pagination {
  padding: 8px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.pagination-btn {
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  background: var(--soft);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  transition: var(--transition);
}

.pagination-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.pagination-info {
  font-size: 11.5px;
  color: var(--muted);
}

/* --- READER COLUMN --- */
.reader {
  display: flex;
  flex-direction: column;
}

.reader header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.mobile-back-btn {
  display: none;
  height: 32px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  background: var(--accent-light);
  color: var(--accent-dark);
  font-weight: 700;
  font-size: 12.5px;
  border: 1px solid #bae6fd;
}

.actions button {
  height: 32px;
  border-radius: var(--radius-sm);
  background: #ffffff;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 0 10px;
  font-weight: 600;
  font-size: 12.5px;
  transition: var(--transition);
}

.actions button:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
  background: var(--accent-light);
}

#deleteBtn:hover {
  color: var(--danger);
  border-color: #fca5a5;
  background: var(--danger-light);
}

#subject {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  line-height: 1.3;
  color: var(--text);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  color: var(--muted);
  font-size: 12.5px;
}

.meta span {
  overflow-wrap: anywhere;
}

.reader-body-wrapper {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background: #ffffff;
}

#body {
  margin: 0;
  font-size: 14.5px;
  line-height: 1.6;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  max-width: 100%;
}

/* Strict Anti-Overflow for HTML Email Content inside #body */
#body img, #body video, #body svg {
  max-width: 100% !important;
  height: auto !important;
  border-radius: var(--radius-sm);
}

#body table {
  max-width: 100% !important;
  display: block;
  overflow-x: auto;
  border-collapse: collapse;
}

#body pre, #body code {
  max-width: 100% !important;
  white-space: pre-wrap;
  word-break: break-all;
  overflow-x: auto;
  background: var(--soft);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
}

#body iframe {
  max-width: 100% !important;
}

/* --- QUICK COMPOSER POPUP --- */
.composer {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: min(500px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  background: #ffffff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  border: 1px solid var(--line);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: popIn 0.2s ease-out;
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.96) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.composer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: var(--text);
  color: #ffffff;
}

.composer-head strong {
  font-size: 14px;
}

.composer-head-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.composer-head button {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-sm);
  min-width: 24px;
  height: 24px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.composer-head-actions #composeClose {
  border-radius: 50%;
  width: 24px;
  padding: 0;
}

.composer-head button:hover {
  background: rgba(255, 255, 255, 0.3);
}

.composer input, .composer textarea {
  border: none;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 8px 14px;
  font-size: 13.5px;
}

.composer input:focus, .composer textarea:focus {
  box-shadow: none;
  border-color: var(--accent);
}

.quick-recipient-field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
  margin: 0 14px;
  border-bottom: 1px solid var(--line);
}

.quick-recipient-field input {
  flex: 1;
  min-width: 140px;
  border: 0;
  margin: 0;
}

.composer-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: var(--soft);
  border-top: 1px solid var(--line);
}

.composer-foot .primary {
  width: auto;
  height: 36px;
  font-size: 13px;
}

/* --- BULK ACTIONS FLOATING TOOLBAR --- */
.bulk-actions-toolbar {
  display: none;
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #ffffff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  padding: 8px 14px;
  gap: 12px;
  z-index: 1000;
  align-items: center;
  width: min(480px, calc(100vw - 32px));
}

.bulk-actions-toolbar.active {
  display: flex;
}

.bulk-actions-toolbar .selection-count {
  color: #ffffff;
  font-size: 13px;
  font-weight: 600;
}

.bulk-actions-toolbar button.delete {
  background: var(--danger);
  color: white;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 12.5px;
  margin-left: auto;
}

/* Context menu for message selection */
.message-context-menu {
  display: block;
  position: fixed;
  z-index: 10000;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 6px;
  min-width: 170px;
}

.message-context-menu .context-menu-item {
  display: block;
  width: 100%;
  padding: 6px 10px;
  border: none;
  background: transparent;
  cursor: pointer;
  text-align: left;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 12.5px;
  transition: var(--transition);
}

.message-context-menu .context-menu-item:hover {
  background: var(--soft);
  color: var(--accent-dark);
}

.message-context-menu .context-menu-group {
  position: relative;
}

.message-context-menu .context-menu-group > .context-menu-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.message-context-menu .context-submenu {
  position: absolute;
  left: calc(100% + 4px);
  top: 0;
  display: none;
  min-width: 170px;
  max-height: min(300px, calc(100vh - 16px));
  overflow-y: auto;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}

.message-context-menu .context-menu-group:hover > .context-submenu,
.message-context-menu .context-menu-group:focus-within > .context-submenu {
  display: block;
}

.message-context-menu .context-menu-loading {
  display: block;
  padding: 6px 10px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

@media (max-width: 600px) {
  .message-context-menu .context-submenu {
    left: auto;
    right: calc(100% + 4px);
    min-width: 145px;
  }
}

/* ==================== RESPONSIVE ADAPTATIONS ==================== */

/* Tablet (768px - 1023px) */
@media (max-width: 1023px) {
  .mail-app {
    grid-template-columns: 200px 280px minmax(0, 1fr);
    padding: 8px;
    gap: 8px;
  }
}

/* Mobile (< 768px) */
@media (max-width: 767px) {
  .mail-app {
    display: block;
    position: relative;
    padding: 0;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
  }

  /* Off-canvas sidebar drawer on mobile */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 270px;
    z-index: 1050;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    transform: translateX(-100%);
    transition: transform 0.22s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: var(--shadow-xl);
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  .mobile-drawer-close, .mobile-menu-btn, .mobile-back-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  /* Mobile Active View Switching: Inbox List vs Reader */
  .inbox, .reader {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border-radius: 0;
    border: none;
  }

  .mail-app:not(.reader-active) .reader {
    display: none;
  }

  .mail-app.reader-active .inbox {
    display: none;
  }

  .mail-app.reader-active .reader {
    display: flex;
    z-index: 100;
  }

  /* Mobile Quick Composer Modal */
  .composer {
    right: 0;
    bottom: 0;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    z-index: 2000;
  }

  .grid-2 {
    grid-template-columns: 1fr;
  }
}
