:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --surface-hover: #263348;
  --border: #283548;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #ef4444;
  --success: #10b981;
  --warning: #f59e0b;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-hover: #eff6ff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #059669;
  --warning: #d97706;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

code {
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s ease;
}

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

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

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

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

.btn-danger {
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.35);
}

.btn-ghost {
  background: transparent;
}

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

.btn-xs {
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 6px;
}

.btn-block {
  width: 100%;
}

.btn-icon {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
}

.btn-icon:hover {
  background: var(--surface-2);
  color: var(--text);
}

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

/* Inputs */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
textarea,
select {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 13.5px;
  outline: none;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--primary);
}

/* Login View */
.login-wrapper {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, rgba(59, 130, 246, 0.14), transparent 55%), var(--bg);
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow);
}

.login-header {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
}

.brand-mark {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: rgba(59, 130, 246, 0.15);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-header h1 {
  font-size: 19px;
  font-weight: 700;
}

.login-form .field {
  margin-bottom: 16px;
}

.login-form label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 6px;
}

.email-input-group {
  display: flex;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.email-input-group input {
  border: none;
  background: transparent;
}

.domain-suffix {
  padding: 0 12px;
  color: var(--muted);
  font-size: 13px;
  border-left: 1px solid var(--border);
  white-space: nowrap;
}

.hint {
  display: block;
  margin-top: 5px;
  font-size: 11.5px;
  color: var(--muted);
}

.login-footer {
  margin-top: 20px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.security-pill {
  font-size: 11.5px;
  color: var(--success);
  font-weight: 600;
}

.alert {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 14px;
}

.alert-error {
  background: rgba(239, 68, 68, 0.14);
  border: 1px solid rgba(239, 68, 68, 0.35);
  color: #fca5a5;
}

[data-theme="light"] .alert-error {
  color: #b91c1c;
}

/* App Shell */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 16px;
  flex-shrink: 0;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-compact {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
}

.topbar-search {
  flex: 1;
  max-width: 520px;
  display: flex;
  gap: 6px;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 4px 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12.5px;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.avatar-sm {
  width: 26px;
  height: 26px;
  font-size: 12px;
}

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* Sidebar */
.sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-compose {
  padding: 14px;
}

.btn-compose {
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
}

.nav-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 0 12px 12px;
  border-bottom: 1px solid var(--border);
}

.nav-tab {
  padding: 6px 4px;
  font-size: 12px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 600;
}

.nav-tab.active {
  background: var(--surface-2);
  color: var(--primary);
}

#folder-panel {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.sidebar-section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 8px;
  padding: 0 6px;
}

.folder-list {
  list-style: none;
}

.folder-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  margin-bottom: 2px;
  font-size: 13.5px;
}

.folder-item:hover {
  background: var(--surface-2);
}

.folder-item.active {
  background: rgba(59, 130, 246, 0.15);
  color: var(--primary);
  font-weight: 600;
}

.unread-badge {
  background: var(--primary);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
}

.sidebar-footer {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
}

.quota-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--muted);
  margin-bottom: 5px;
}

.quota-bar {
  height: 6px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}

.quota-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.3s ease;
}

/* 3-Pane Mail Area */
.mail-panes {
  flex: 1;
  display: flex;
  min-width: 0;
}

.list-pane {
  width: 380px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.list-toolbar {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.list-toolbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-pills {
  display: flex;
  gap: 4px;
}

.pill {
  padding: 4px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
}

.pill.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.bulk-bar {
  padding: 8px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 12px;
}

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

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

.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s ease;
}

.msg-row:hover {
  background: var(--surface-2);
}

.msg-row.selected {
  background: rgba(59, 130, 246, 0.12);
}

.msg-row.unread .msg-sender,
.msg-row.unread .msg-subj {
  font-weight: 700;
}

.msg-row-main {
  flex: 1;
  min-width: 0;
}

.msg-row-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 3px;
}

.msg-sender {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13.5px;
}

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

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

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

.star-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 15px;
}

.star-btn.starred {
  color: var(--warning);
}

.list-pagination {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
}

/* Reader Pane */
.reader-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
}

.reader-empty {
  margin: auto;
  text-align: center;
  padding: 32px;
}

.empty-icon {
  font-size: 46px;
  opacity: 0.35;
  margin-bottom: 10px;
}

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

.reader-toolbar {
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.reader-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.reader-header {
  padding: 18px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.reader-subject-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.reader-subject-row h2 {
  font-size: 18px;
  font-weight: 700;
}

.auth-badge {
  font-size: 11.5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-weight: 600;
  white-space: nowrap;
}

.auth-pass {
  background: rgba(16, 185, 129, 0.15);
  color: var(--success);
}

.reader-meta {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.sender-block {
  display: flex;
  gap: 12px;
  align-items: center;
}

.sender-line {
  font-weight: 600;
}

.recipient-line {
  font-size: 12.5px;
}

.remote-banner {
  padding: 8px 20px;
  background: rgba(245, 158, 11, 0.14);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
}

.attachment-strip {
  padding: 10px 20px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.att-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  font-size: 12.5px;
}

.att-chip:hover {
  border-color: var(--primary);
}

.reader-body-wrap {
  flex: 1;
  background: #ffffff;
  min-height: 0;
}

.msg-iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

/* Single Panes (Contacts & Settings) */
.single-pane {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.pane-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
}

.pane-card h3,
.pane-card h2 {
  margin-bottom: 14px;
}

.inline-form {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 10px;
  margin-bottom: 18px;
}

.stack-form .field {
  margin-bottom: 14px;
}

.stack-form label {
  display: block;
  font-size: 12.5px;
  margin-bottom: 6px;
  font-weight: 600;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  max-width: 1040px;
}

.full-span {
  grid-column: 1 / -1;
}

.conn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 14px;
}

.conn-box {
  padding: 14px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  line-height: 1.9;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

th {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
}

/* Modals & Compose */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 740px;
  max-height: 90vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.compose-form {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.compose-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
}

.compose-row label {
  width: 54px;
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 600;
}

.compose-row input {
  border: none;
  background: transparent;
  padding: 4px 0;
}

.editor-toolbar {
  display: flex;
  gap: 4px;
  padding: 8px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.editor-toolbar button {
  padding: 4px 9px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
}

.comp-editor {
  flex: 1;
  min-height: 240px;
  max-height: 360px;
  overflow-y: auto;
  padding: 16px;
  outline: none;
  background: var(--surface);
  color: var(--text);
}

.comp-att-list {
  padding: 6px 16px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.compose-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.compose-footer-left {
  display: flex;
  gap: 8px;
}

.headers-pre {
  padding: 16px;
  overflow: auto;
  font-size: 12px;
  background: var(--surface-2);
  color: var(--text);
  max-height: 70vh;
}

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

.toast {
  padding: 11px 16px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 13px;
}

.toast-success {
  border-left: 4px solid var(--success);
}

.toast-error {
  border-left: 4px solid var(--danger);
}

.mobile-only {
  display: none;
}

@media (max-width: 960px) {
  .mobile-only {
    display: inline-flex;
  }
  .hide-sm {
    display: none;
  }
  .sidebar {
    position: fixed;
    top: 56px;
    bottom: 0;
    left: -260px;
    z-index: 50;
    transition: left 0.2s ease;
  }
  .sidebar.open {
    left: 0;
  }
  .list-pane {
    width: 100%;
  }
  .reader-pane {
    display: none;
  }
  .mail-panes.reading .list-pane {
    display: none;
  }
  .mail-panes.reading .reader-pane {
    display: flex;
    width: 100%;
  }
  .settings-grid,
  .conn-grid,
  .inline-form {
    grid-template-columns: 1fr;
  }
}
