:root {
  /* Semantic Surface & Text Tokens */
  --bg-page: #f4f6f8;
  --bg-surface: #ffffff;
  --bg-subtle: #f8fafc;
  --bg-hover: #f1f5f9;
  --bg-selected: #eff6ff;

  --border-default: #e2e8f0;
  --border-strong: #cbd5e1;

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;

  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --accent-soft: #dbeafe;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --success: #15803d;
  --star-active: #d97706;

  /* Spacing & Radius Scale */
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 18px 40px rgba(15, 23, 42, 0.14);

  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-page);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.muted-sm {
  margin: 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.muted-xs {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.stack-sm {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stack-md {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.btn:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
.nav-item:focus-visible,
.email-row:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-ghost[aria-pressed='true'] {
  background: var(--accent-soft);
  color: var(--accent);
}

.btn-danger-ghost {
  background: transparent;
  color: var(--danger);
}

.btn-danger-ghost:hover:not(:disabled) {
  background: var(--danger-soft);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12.5px;
}

.btn-xs {
  padding: 4px 8px;
  font-size: 12px;
  border-color: var(--border-default);
}

.btn-full {
  width: 100%;
}

.btn-icon {
  padding: 6px 9px;
}

.btn-link {
  background: none;
  border: none;
  padding: 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
}

.btn-link:hover {
  text-decoration: underline;
}

/* Form Controls */
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
}

.field input,
.field textarea,
.field select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13.5px;
}

.field textarea {
  resize: vertical;
  min-height: 110px;
}

.field-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.field-grow {
  flex: 1 1 220px;
}

.field-name {
  flex: 0 1 180px;
}

.error-text {
  margin: 0;
  font-size: 13px;
  color: var(--danger);
  font-weight: 500;
}

/* Banners */
.banner {
  padding: 10px 12px;
  border-radius: var(--radius-md);
  font-size: 12.5px;
  line-height: 1.45;
}

.banner-info {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e3a8a;
}

.banner-error {
  margin: 8px 12px 0;
  background: var(--danger-soft);
  border: 1px solid #fecaca;
  color: var(--danger);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
}

.pill-neutral {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
}

/* Login Screen */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.login-card {
  width: 100%;
  max-width: 410px;
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: 28px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: #ffffff;
  font-size: 20px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-mark-sm {
  width: 30px;
  height: 30px;
  font-size: 15px;
}

.login-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.login-subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-secondary);
}

/* Main 3-Pane Shell */
.app-shell {
  height: 100vh;
  display: grid;
  grid-template-columns: 248px 360px 1fr;
  background: var(--bg-surface);
  overflow: hidden;
}

.mobile-topbar,
.mobile-only {
  display: none;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 30;
}

/* Sidebar */
.sidebar {
  background: var(--bg-subtle);
  border-right: 1px solid var(--border-default);
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.sidebar-header {
  padding: 14px 14px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand-inline {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-text strong,
.brand-text span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-action {
  padding: 4px 14px 12px;
}

.nav-section {
  padding: 0 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
}

.nav-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--bg-selected);
  color: var(--accent);
  font-weight: 600;
}

.nav-count {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.nav-item.active .nav-count {
  color: var(--accent);
  font-weight: 700;
}

.nav-divider {
  height: 1px;
  background: var(--border-default);
  margin: 12px 14px;
}

/* Catch-all Alias Sidebar List */
.alias-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0 8px;
  overflow: hidden;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px 8px;
}

.section-heading h2 {
  margin: 0;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.alias-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.alias-row {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: var(--radius-sm);
}

.alias-row.active {
  background: var(--bg-selected);
}

.alias-select-btn {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 8px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  border-radius: var(--radius-sm);
}

.alias-select-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.alias-row.active .alias-select-btn {
  color: var(--accent);
  font-weight: 600;
}

.alias-name-wrap {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}

.alias-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

.alias-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.alias-edit-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  opacity: 0.6;
}

.alias-row:hover .alias-edit-btn,
.alias-edit-btn:focus-visible {
  opacity: 1;
  background: var(--bg-hover);
}

.sidebar-footer {
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border-default);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Middle Email List Pane */
.list-pane {
  border-right: 1px solid var(--border-default);
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background: var(--bg-surface);
}

.list-toolbar {
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--border-default);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.search-box input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  background: var(--bg-subtle);
  font-size: 13px;
  color: var(--text-primary);
}

.toolbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.pane-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
}

.active-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.email-list-container {
  flex: 1;
  overflow-y: auto;
}

.email-row {
  width: 100%;
  border: none;
  border-bottom: 1px solid var(--border-default);
  background: var(--bg-surface);
  padding: 11px 12px;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: inherit;
  transition: background-color 0.12s ease;
}

.email-row:hover {
  background: var(--bg-subtle);
}

.email-row.selected {
  background: var(--bg-selected);
}

.email-row.unread {
  background: #fafcff;
}

.email-row.unread.selected {
  background: var(--bg-selected);
}

.email-row-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.email-sender-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.unread-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.email-sender {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.email-row.unread .email-sender,
.email-row.unread .email-subject {
  font-weight: 700;
}

.email-time {
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.email-row-mid {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.email-subject {
  font-size: 13px;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.email-row-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}

.email-snippet {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.star-indicator {
  color: var(--star-active);
  font-size: 12px;
  flex-shrink: 0;
}

/* Alias Color Badges (10 high-contrast semantic tokens) */
.alias-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  font-family: var(--font-mono);
  border: 1px solid transparent;
  max-width: 170px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 0;
}

.badge-blue   { background: #dbeafe; color: #1e40af; border-color: #bfdbfe; }
.badge-teal   { background: #ccfbf1; color: #115e59; border-color: #99f6e4; }
.badge-emerald{ background: #d1fae5; color: #065f46; border-color: #a7f3d0; }
.badge-amber  { background: #fef3c7; color: #92400e; border-color: #fde68a; }
.badge-rose   { background: #ffe4e6; color: #9f1239; border-color: #fecdd3; }
.badge-cyan   { background: #cffafe; color: #155e75; border-color: #a5f3fc; }
.badge-orange { background: #ffedd5; color: #9a3412; border-color: #fed7aa; }
.badge-sky    { background: #e0f2fe; color: #075985; border-color: #bae6fd; }
.badge-lime   { background: #ecfccb; color: #3f6212; border-color: #d9f99d; }
.badge-slate  { background: #e2e8f0; color: #334155; border-color: #cbd5e1; }

.dot-blue    { background: #2563eb; }
.dot-teal    { background: #0d9488; }
.dot-emerald { background: #059669; }
.dot-amber   { background: #d97706; }
.dot-rose    { background: #e11d48; }
.dot-cyan    { background: #0891b2; }
.dot-orange  { background: #ea580c; }
.dot-sky     { background: #0284c7; }
.dot-lime    { background: #65a30d; }
.dot-slate   { background: #475569; }

/* Right Detail Pane */
.detail-pane {
  height: 100%;
  overflow-y: auto;
  background: var(--bg-page);
  display: flex;
  flex-direction: column;
}

.empty-state {
  margin: auto;
  max-width: 400px;
  text-align: center;
  padding: 32px 20px;
  color: var(--text-secondary);
}

.empty-icon {
  font-size: 32px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.empty-state h2 {
  margin: 0 0 6px;
  font-size: 16px;
  color: var(--text-primary);
}

.empty-state p {
  margin: 0;
  font-size: 13px;
}

.detail-content {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.detail-topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-default);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

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

.alias-source-banner {
  margin: 14px 20px 0;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.alias-source-left {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 13px;
}

.detail-header {
  padding: 14px 20px 8px;
}

.detail-subject {
  margin: 0 0 4px;
  font-size: 19px;
  font-weight: 700;
  color: var(--text-primary);
  word-break: break-word;
}

.thread-messages {
  padding: 8px 20px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.message-card-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-default);
  background: var(--bg-subtle);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.message-meta-lines {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.message-from-line {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 13.5px;
}

.direction-pill {
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
}

.direction-inbound {
  background: #e0f2fe;
  color: #075985;
}

.direction-outbound {
  background: #dcfce7;
  color: #166534;
}

.message-card-body {
  padding: 16px;
}

.mail-text {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.65;
  color: var(--text-primary);
}

.remote-img-bar {
  margin-bottom: 10px;
  padding: 8px 10px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: #92400e;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.mail-iframe {
  width: 100%;
  min-height: 280px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  background: #ffffff;
}

.attachments-box {
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border-default);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-subtle);
  color: var(--text-primary);
  font-size: 12.5px;
  text-decoration: none;
}

.attachment-chip:hover {
  background: var(--bg-hover);
}

.attachment-chip-skipped {
  border-style: dashed;
  color: var(--text-muted);
  cursor: default;
}

/* Skeleton Loader */
.skeleton-list {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.skeleton-item {
  height: 62px;
  border-radius: var(--radius-md);
  background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* Modals */
.modal {
  width: min(640px, calc(100vw - 24px));
  padding: 0;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  background: var(--bg-surface);
  color: var(--text-primary);
}

.modal-sm {
  width: min(420px, calc(100vw - 24px));
}

.modal::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-default);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.modal-body {
  padding: 16px 18px;
}

.modal-footer {
  padding-top: 8px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

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

.file-chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}

/* Toasts */
.toast-container {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: #0f172a;
  color: #ffffff;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
}

.toast-error {
  background: var(--danger);
}

/* Responsive Breakpoints (1024px, 768px, 320px) */
@media (max-width: 1024px) {
  .app-shell {
    grid-template-columns: 220px 300px 1fr;
  }
}

@media (max-width: 820px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: 52px 1fr;
  }

  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 12px;
    border-bottom: 1px solid var(--border-default);
    background: var(--bg-surface);
  }

  .mobile-title {
    font-weight: 700;
    font-size: 15px;
  }

  .mobile-only {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 270px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-lg);
  }

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

  .detail-pane {
    display: none;
  }

  .app-shell.mobile-reading .list-pane {
    display: none;
  }

  .app-shell.mobile-reading .detail-pane {
    display: flex;
  }
}
