:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --topbar-base: 68px;
  --topbar-height: calc(var(--topbar-base) + var(--safe-top));
  color-scheme: light;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --text: #182230;
  --muted: #667085;
  --line: #e4e9f0;
  --accent: #5b5bd6;
  --accent-dark: #4545b7;
  --accent-soft: #efefff;
  --success: #087443;
  --danger: #b42318;
  --shadow: 0 18px 42px rgba(26, 42, 65, 0.08);
}

[hidden] { display: none !important; }
* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); }
button, input, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { touch-action: manipulation; }

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 32px; }
.login-card { width: min(440px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 24px; box-shadow: var(--shadow); padding: 42px; text-align: center; }
.brand-mark { width: 58px; height: 58px; border-radius: 18px; display: inline-grid; place-items: center; background: linear-gradient(145deg, #6767e8, #4141b5); color: white; font-size: 25px; font-weight: 800; letter-spacing: -0.04em; box-shadow: 0 10px 24px rgba(91,91,214,.28); }
.brand-mark.small { width: 36px; height: 36px; border-radius: 11px; font-size: 16px; box-shadow: none; flex: 0 0 auto; }
.login-card h1 { margin: 8px 0 10px; font-size: 30px; letter-spacing: -0.04em; }
.eyebrow { margin: 18px 0 4px; color: var(--accent); text-transform: uppercase; font-size: 11px; font-weight: 800; letter-spacing: .12em; }
.muted { color: var(--muted); }
.fineprint { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 22px 0 0; }

.button { border: 0; border-radius: 11px; min-height: 40px; padding: 0 15px; cursor: pointer; font-weight: 700; transition: .15s ease; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; }
.button:disabled { opacity: .55; cursor: not-allowed; }
.button.primary { background: var(--accent); color: #fff; box-shadow: 0 8px 18px rgba(91,91,214,.2); }
.button.primary:hover:not(:disabled) { background: var(--accent-dark); transform: translateY(-1px); }
.button.ghost { background: #eef1f7; color: #344054; }
.button.ghost:hover:not(:disabled) { background: #e4e9f1; }
.button.text { background: transparent; color: var(--muted); padding-inline: 8px; }
.button.danger-ghost { background: #fff1f0; color: var(--danger); }
.button.full { width: 100%; margin-top: 22px; min-height: 48px; }
.button.compact { min-height: 38px; }

.app-shell { min-height: 100vh; }
.topbar { height: var(--topbar-height); background: rgba(255,255,255,.96); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; padding: var(--safe-top) max(22px, var(--safe-right)) 0 max(22px, var(--safe-left)); position: sticky; top: 0; z-index: 30; }
.brand-row, .top-actions { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-copy { min-width: 0; }
.brand-row strong { display: block; font-size: 15px; }
.topbar-subtitle { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.workspace { display: grid; grid-template-columns: 228px minmax(320px, 400px) minmax(420px, 1fr); min-height: calc(100vh - var(--topbar-height)); }
.sidebar, .message-list-panel, .reader-panel { min-width: 0; }
.sidebar { background: #111827; color: #fff; padding: 18px 13px; display: flex; flex-direction: column; gap: 14px; }
.mode-nav { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
.mode-button { border: 1px solid rgba(255,255,255,.08); background: rgba(255,255,255,.04); color: #cfd7e5; border-radius: 12px; min-height: 44px; padding: 7px 8px; font-weight: 800; cursor: pointer; display: grid; place-items: center; gap: 2px; font-size: 11px; }
.mode-button:hover { background: rgba(255,255,255,.08); }
.mode-button.active { background: rgba(103,103,232,.22); border-color: rgba(137,137,255,.45); color: #fff; }
.mode-icon { font-size: 16px; line-height: 1; }
.sidebar-title { padding: 4px 10px 0; font-size: 11px; text-transform: uppercase; letter-spacing: .1em; color: #95a1b5; font-weight: 800; }
.mailboxes { display: grid; gap: 7px; }
.mailbox { width: 100%; border: 1px solid transparent; background: transparent; color: #d8deea; padding: 11px 12px; border-radius: 12px; cursor: pointer; text-align: left; display: grid; gap: 4px; min-width: 0; }
.mailbox:hover { background: rgba(255,255,255,.06); }
.mailbox.active { background: rgba(103,103,232,.18); border-color: rgba(137,137,255,.35); color: #fff; }
.mailbox .mailbox-label { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailbox .mailbox-email { font-size: 11px; color: #9da8b8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer { margin-top: auto; display: flex; gap: 8px; align-items: center; padding: 14px 10px 4px; color: #9da8b8; font-size: 11px; line-height: 1.35; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #38b27d; box-shadow: 0 0 0 4px rgba(56,178,125,.12); flex: 0 0 auto; }

.message-list-panel { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.panel-header { padding: 18px 17px 12px; display: flex; align-items: end; justify-content: space-between; gap: 14px; border-bottom: 1px solid var(--line); }
.panel-header .eyebrow { margin: 0 0 4px; }
.panel-header h2 { margin: 0; font-size: 19px; letter-spacing: -.02em; }
.toggle-row { display: flex; gap: 7px; align-items: center; font-size: 12px; color: var(--muted); white-space: nowrap; }
.toggle-row input { width: auto; }
.search-row { display: grid; grid-template-columns: 1fr auto auto; gap: 7px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
input, textarea { width: 100%; border: 1px solid #d4dce7; background: #fff; border-radius: 10px; padding: 10px 12px; outline: none; color: var(--text); }
input:focus, textarea:focus { border-color: #8a8ae6; box-shadow: 0 0 0 3px rgba(91,91,214,.12); }
textarea { resize: vertical; min-height: 130px; line-height: 1.5; }
.list-status { min-height: 31px; padding: 9px 14px 5px; font-size: 12px; color: var(--muted); }
.messages { overflow: auto; flex: 1; min-height: 0; }
.message-row { border: 0; border-bottom: 1px solid #edf0f5; background: #fff; width: 100%; padding: 14px 16px; text-align: left; cursor: pointer; display: grid; gap: 7px; position: relative; }
.message-row:hover { background: #f8f9fc; }
.message-row.active { background: #f1f2ff; box-shadow: inset 3px 0 0 var(--accent); }
.message-row.unread .row-from, .message-row.unread .row-subject { font-weight: 800; }
.row-top { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.row-from { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-date { font-size: 11px; color: var(--muted); white-space: nowrap; }
.row-subject { font-size: 13px; color: #344054; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-badges { display: flex; gap: 6px; min-height: 17px; }
.badge { font-size: 10px; color: var(--accent-dark); background: #ececff; padding: 2px 7px; border-radius: 999px; font-weight: 800; }
.draft-badge { color: #875400; background: #fff5d6; }
.spam-badge { color: #9f1239; background: #ffe4e6; }
.list-empty { min-height: 280px; display: grid; place-content: center; justify-items: center; gap: 7px; text-align: center; color: var(--muted); padding: 24px; }
.list-empty strong { color: #344054; }
.list-empty span { font-size: 12px; max-width: 250px; line-height: 1.5; }
.list-empty-icon { width: 48px; height: 48px; border-radius: 16px; display: grid; place-items: center; background: #eef1f7; font-size: 20px; margin-bottom: 4px; }

.reader-panel { background: var(--surface-soft); padding: 22px 22px calc(22px + var(--safe-bottom)); overflow: auto; max-height: calc(100vh - var(--topbar-height)); }
.mobile-back { display: none; margin-bottom: 12px; }
.empty-state { min-height: 70vh; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--muted); }
.empty-state h2 { color: #344054; margin: 14px 0 4px; }
.empty-state p { margin: 0; }
.empty-icon { width: 64px; height: 64px; border-radius: 20px; background: #eceff5; display: grid; place-items: center; font-size: 25px; }
.message-view { max-width: 880px; margin: 0 auto; }
.message-meta { background: var(--surface); border: 1px solid var(--line); border-radius: 18px 18px 0 0; padding: 22px 24px; }
.message-meta .eyebrow { margin-top: 0; }
.message-meta h1 { font-size: 24px; line-height: 1.25; letter-spacing: -.03em; margin: 5px 0 18px; overflow-wrap: anywhere; }
.draft-meta { border-radius: 18px; }
.meta-grid { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 7px 14px; font-size: 13px; }
.meta-grid > span:nth-child(odd) { color: var(--muted); }
.meta-grid > * { overflow-wrap: anywhere; }
.message-toolbar { background: #fff; border-inline: 1px solid var(--line); padding: 10px 24px; border-top: 1px solid #eef1f5; display: flex; gap: 8px; flex-wrap: wrap; }
.spam-notice { background: #fff7ed; color: #9a3412; border: 1px solid #fed7aa; border-top: 0; padding: 10px 24px; font-size: 12px; line-height: 1.45; }

.conversation-thread { background: #f7f8fb; border: 1px solid var(--line); border-top: 0; padding: 18px; display: grid; gap: 12px; }
.thread-message { background: #fff; border: 1px solid #e4e7ec; border-radius: 15px; overflow: hidden; box-shadow: 0 1px 2px rgba(16,24,40,.04); }
.thread-message.outgoing { background: #f7f6ff; border-color: #d9d6fe; margin-left: clamp(12px, 5vw, 52px); }
.thread-message.incoming { margin-right: clamp(12px, 5vw, 52px); }
.thread-message.selected { box-shadow: 0 0 0 2px rgba(79,70,229,.16); }
.thread-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 14px 16px 9px; }
.thread-person { min-width: 0; display: grid; gap: 2px; }
.thread-person strong { font-size: 13px; color: #101828; overflow-wrap: anywhere; }
.thread-recipient { font-size: 11px; color: var(--muted); overflow-wrap: anywhere; }
.thread-side { flex: 0 0 auto; display: grid; justify-items: end; gap: 4px; }
.thread-direction { display: inline-flex; align-items: center; min-height: 21px; padding: 2px 7px; border-radius: 999px; font-size: 10px; font-weight: 800; background: #eef1f7; color: #475467; }
.thread-message.outgoing .thread-direction { background: #e9e7ff; color: #4338ca; }
.thread-date { font-size: 10px; color: var(--muted); white-space: nowrap; }
.thread-body { margin: 0; padding: 10px 16px 16px; font-family: inherit; font-size: 13.5px; line-height: 1.65; white-space: pre-wrap; overflow-wrap: anywhere; color: #1d2939; }
.thread-attachments { border-top: 1px solid #eef1f5; padding: 9px 16px 12px; display: flex; gap: 6px; flex-wrap: wrap; }
.thread-attachment { font-size: 10px; padding: 4px 7px; border-radius: 999px; background: #eef1f7; color: #475467; }
.message-body { margin: 0; background: #fff; border: 1px solid var(--line); border-top: 0; padding: 24px; min-height: 220px; font-family: inherit; font-size: 14px; line-height: 1.7; white-space: pre-wrap; overflow-wrap: anywhere; }
.attachments { background: #fff; border: 1px solid var(--line); border-top: 0; padding: 16px 24px; }
.attachments h3 { font-size: 13px; margin: 0 0 10px; }
.attachments ul { margin: 0; padding-left: 20px; font-size: 13px; }
.reply-card { margin-top: 18px; background: #fff; border: 1px solid var(--line); border-radius: 18px; padding: 22px 24px; box-shadow: var(--shadow); }
.draft-editor { margin-top: 14px; }
.reply-heading { display: flex; justify-content: space-between; gap: 15px; align-items: start; }
.reply-heading .eyebrow { margin-top: 0; }
.reply-heading h2 { margin: 3px 0 18px; font-size: 18px; }
.chip { background: #eef1f7; border-radius: 999px; padding: 6px 10px; font-size: 11px; color: #475467; max-width: 48%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.field-label { display: block; margin: 12px 0 6px; font-size: 12px; font-weight: 800; color: #475467; }
.reply-actions { display: flex; justify-content: space-between; gap: 12px; align-items: center; margin-top: 14px; }
.reply-status { font-size: 12px; color: var(--muted); min-height: 17px; }
.reply-status.success { color: var(--success); font-weight: 700; }
.reply-status.error { color: var(--danger); font-weight: 700; }
.action-buttons { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.draft-note { background: #f7f8fb; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-top: 14px; color: var(--muted); font-size: 11px; line-height: 1.45; }

.ai-reply-panel { margin: 4px 0 16px; padding: 14px; border: 1px solid #d9d6fe; background: linear-gradient(135deg, #faf9ff 0%, #f4f7ff 100%); border-radius: 14px; }
.ai-reply-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.ai-reply-heading > div { display: flex; align-items: center; gap: 8px; }
.ai-badge { display: inline-flex; align-items: center; min-height: 24px; padding: 3px 8px; border-radius: 999px; background: #e9e7ff; color: #4338ca; font-size: 11px; font-weight: 900; }
.ai-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.button.ai { background: #4f46e5; color: #fff; border-color: #4f46e5; }
.button.ai:hover { background: #4338ca; border-color: #4338ca; }
.ai-note { margin: 9px 0 0; color: var(--muted); font-size: 11px; line-height: 1.45; }

.toast { position: fixed; right: 22px; bottom: 22px; background: #111827; color: #fff; padding: 12px 15px; border-radius: 12px; box-shadow: var(--shadow); font-size: 13px; z-index: 50; max-width: min(360px, calc(100vw - 28px)); }
.loading::after { content: ""; display: inline-block; width: 13px; height: 13px; border: 2px solid rgba(91,91,214,.25); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; margin-left: 7px; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 200px minmax(300px, 350px) minmax(380px, 1fr); }
  .reader-panel { padding: 18px; }
}

@media (max-width: 900px) and (min-width: 761px) {
  .workspace { grid-template-columns: 176px 300px minmax(340px, 1fr); }
  .sidebar { padding-inline: 10px; }
  .mailbox { padding-inline: 9px; }
  .message-meta, .message-body, .reply-card { padding-left: 18px; padding-right: 18px; }
  .conversation-thread { padding: 14px; }
  .action-buttons { width: 100%; }
  .split-actions, .draft-actions { align-items: stretch; flex-direction: column; }
}

@media (max-width: 760px) {
  :root { --topbar-base: 60px; }
  .topbar { height: var(--topbar-height); padding: var(--safe-top) max(10px, var(--safe-right)) 0 max(10px, var(--safe-left)); }
  .brand-mark.small { width: 32px; height: 32px; }
  .brand-row { gap: 8px; max-width: 54%; }
  .brand-row strong { font-size: 14px; }
  .topbar-subtitle { font-size: 10px; }
  .top-actions { gap: 6px; }
  .top-actions .button span { display: none; }
  #logoutButton::before { content: "↪"; font-size: 16px; }
  .button.compact { padding-inline: 10px; min-height: 36px; font-size: 12px; }

  .workspace { display: block; min-height: calc(100vh - var(--topbar-height)); }
  .sidebar { position: sticky; top: var(--topbar-height); z-index: 20; padding: 9px 10px 10px; gap: 8px; border-bottom: 1px solid rgba(255,255,255,.08); }
  .mode-nav { display: flex; gap: 7px; overflow-x: auto; scrollbar-width: none; }
  .mode-nav::-webkit-scrollbar { display: none; }
  .mode-button { min-height: 38px; padding: 5px 8px; display: flex; gap: 5px; flex: 1 0 104px; font-size: 10.5px; }
  .mode-icon { font-size: 14px; }
  .sidebar-title { display: none; }
  .mailboxes { display: flex; overflow-x: auto; gap: 7px; padding-bottom: 2px; scrollbar-width: none; }
  .mailboxes::-webkit-scrollbar { display: none; }
  .mailbox { min-width: 174px; max-width: 220px; padding: 8px 10px; flex: 0 0 auto; }
  .mailbox .mailbox-label { font-size: 12px; }
  .mailbox .mailbox-email { font-size: 10px; }
  .sidebar-footer { display: none; }

  .message-list-panel { border-right: 0; min-height: calc(100vh - 160px); }
  .panel-header { padding: 14px 14px 10px; align-items: start; }
  .panel-header h2 { font-size: 17px; }
  .toggle-row { font-size: 11px; }
  .search-row { grid-template-columns: 1fr auto; padding: 10px 12px; }
  .search-row .button.text { grid-column: 1 / -1; justify-content: flex-start; min-height: 30px; padding-left: 2px; }
  .message-row { padding: 13px 14px; min-height: 72px; }

  .reader-panel { display: none; max-height: none; min-height: calc(100vh - var(--topbar-height)); padding: 12px; overflow: visible; }
  .workspace.mobile-reader-active .sidebar,
  .workspace.mobile-reader-active .message-list-panel { display: none; }
  .workspace.mobile-reader-active .reader-panel { display: block; }
  .mobile-back { display: inline-flex; position: sticky; top: calc(var(--topbar-height) + 10px); z-index: 5; }
  .empty-state { min-height: 60vh; }
  .message-meta, .message-body, .reply-card, .attachments, .spam-notice { padding-left: 16px; padding-right: 16px; }
  .conversation-thread { padding: 10px; gap: 10px; }
  .thread-message.outgoing { margin-left: 14px; }
  .thread-message.incoming { margin-right: 14px; }
  .thread-head { padding: 12px 13px 8px; }
  .thread-body { padding: 9px 13px 14px; font-size: 13px; }
  .message-meta h1 { font-size: 21px; }
  .meta-grid { grid-template-columns: 54px minmax(0,1fr); gap: 7px 10px; font-size: 12px; }
  .message-body { min-height: 160px; font-size: 13px; }
  .reply-heading { flex-direction: column; gap: 0; }
  .chip { max-width: 100%; margin-bottom: 4px; }
  .reply-actions { flex-direction: column; align-items: stretch; }
  .action-buttons { display: grid; grid-template-columns: 1fr; width: 100%; }
  .action-buttons .button { width: 100%; min-height: 46px; }
  .ai-reply-panel { padding: 12px; }
  .ai-reply-heading { align-items: flex-start; flex-direction: column; gap: 4px; }
  .ai-actions { display: grid; grid-template-columns: 1fr; width: 100%; }
  .ai-actions .button { width: 100%; min-height: 46px; }
  textarea { min-height: 180px; font-size: 16px; }
  input { font-size: 16px; }
  .toast { left: 14px; right: 14px; bottom: calc(14px + var(--safe-bottom)); max-width: none; text-align: center; }
}

@media (max-width: 480px) {
  .top-actions .button span { display: none; }
  #logoutButton::before { content: "↪"; font-size: 16px; }
  .brand-row { max-width: 66%; }
  .topbar-subtitle { display: none; }
  .login-shell { padding: 18px; }
  .login-card { padding: 30px 22px; border-radius: 20px; }
  .login-card h1 { font-size: 27px; }
  .panel-header { flex-direction: column; gap: 8px; }
  .mode-button { flex: 1 1 0; justify-content: center; }
  .mailbox { min-width: 158px; }
}

/* Native modal keeps an unfinished composition while browsing folders. */
#composeDialog { width: min(720px, 95vw); max-height: 92dvh; padding: 0; border: 0; border-radius: 16px; overflow: auto; }
#composeDialog::backdrop { background: rgb(17 24 39 / 60%); }
#composeDialog .reply-card { margin: 0; }
#composeDialog input, #composeDialog select, #composeDialog textarea { width: 100%; box-sizing: border-box; }
#composeDialog .action-buttons { flex-wrap: wrap; }
