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

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --sidebar-bg: #1e2432;
  --sidebar-text: #a8b3cf;
  --sidebar-active: #ffffff;
  --sidebar-hover: rgba(255,255,255,0.07);
  --primary: #4f6ef7;
  --primary-hover: #3b5bf5;
  --danger: #ef4444;
  --text: #1a1d2e;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --unread-dot: #4f6ef7;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 4px 16px rgba(0,0,0,0.04);
}

body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }

/* ── Login ──────────────────────────────────────────── */
#login-screen { display: flex; align-items: center; justify-content: center; height: 100vh; background: linear-gradient(135deg, #1e2432 0%, #2d3654 100%); }

.login-box { background: var(--surface); border-radius: 16px; padding: 48px 40px; width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }

.login-logo { width: 52px; height: 52px; background: var(--primary); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; }
.login-logo svg { width: 28px; height: 28px; stroke: white; }

.login-box h1 { text-align: center; font-size: 1.5rem; font-weight: 700; color: var(--text); margin-bottom: 6px; }
.login-subtitle { text-align: center; color: var(--text-muted); font-size: 0.875rem; margin-bottom: 28px; }

.error-banner { background: #fef2f2; border: 1px solid #fecaca; color: #dc2626; border-radius: 8px; padding: 10px 14px; font-size: 0.875rem; margin-bottom: 16px; }

.field { margin-bottom: 16px; }
.field label { display: block; font-size: 0.8125rem; font-weight: 500; color: var(--text-muted); margin-bottom: 6px; }
.field input { width: 100%; padding: 10px 14px; border: 1.5px solid var(--border); border-radius: 8px; font-size: 0.9375rem; outline: none; transition: border-color .15s; }
.field input:focus { border-color: var(--primary); }

/* ── Buttons ────────────────────────────────────────── */
.btn { display: inline-flex; align-items: center; gap: 7px; padding: 9px 18px; border-radius: 8px; font-size: 0.9rem; font-weight: 500; cursor: pointer; border: none; transition: background .15s, transform .1s; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1.5px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-full { width: 100%; justify-content: center; padding: 11px; margin-top: 4px; }
.btn-icon { background: none; border: none; cursor: pointer; padding: 6px; border-radius: 6px; color: var(--text-muted); display: flex; align-items: center; }
.btn-icon:hover { background: var(--bg); color: var(--text); }
.btn-icon svg { width: 18px; height: 18px; }

/* ── App Layout ─────────────────────────────────────── */
#app { display: grid; grid-template-columns: 230px 320px 1fr; height: 100vh; }

/* ── Sidebar ────────────────────────────────────────── */
.sidebar { background: var(--sidebar-bg); display: flex; flex-direction: column; padding: 0; overflow: hidden; }

.sidebar-header { display: flex; align-items: center; gap: 10px; padding: 20px 20px 16px; color: white; font-weight: 700; font-size: 1rem; }
.sidebar-header svg { width: 22px; height: 22px; stroke: var(--primary); flex-shrink: 0; }

.btn-compose { margin: 4px 16px 16px; background: var(--primary); color: white; border: none; justify-content: center; border-radius: 8px; padding: 10px; font-size: 0.875rem; }
.btn-compose:hover { background: var(--primary-hover); }
.btn-compose svg { width: 16px; height: 16px; }

.sidebar-nav { flex: 1; padding: 4px 8px; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; color: var(--sidebar-text); font-size: 0.875rem; cursor: pointer; transition: background .15s, color .15s; user-select: none; }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-item:hover { background: var(--sidebar-hover); color: var(--sidebar-active); }
.nav-item.active { background: rgba(79,110,247,0.2); color: var(--primary); }

.badge { background: var(--primary); color: white; font-size: 0.7rem; font-weight: 600; padding: 1px 6px; border-radius: 20px; margin-left: auto; }

.sidebar-footer { padding: 16px; border-top: 1px solid rgba(255,255,255,0.08); display: flex; align-items: center; gap: 10px; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--primary); color: white; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.9rem; flex-shrink: 0; }
.user-info { flex: 1; min-width: 0; }
.user-name { color: white; font-size: 0.85rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-email { color: var(--sidebar-text); font-size: 0.75rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-footer .btn-icon { color: var(--sidebar-text); }
.sidebar-footer .btn-icon:hover { background: var(--sidebar-hover); color: white; }

/* ── Email List ─────────────────────────────────────── */
.email-list { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden; }

.list-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 14px; border-bottom: 1px solid var(--border); }
.list-header h2 { font-size: 1rem; font-weight: 600; }

.list-items { flex: 1; overflow-y: auto; }
.list-items::-webkit-scrollbar { width: 4px; }
.list-items::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.email-item { display: flex; flex-direction: column; padding: 14px 20px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .1s; position: relative; }
.email-item:hover { background: var(--bg); }
.email-item.active { background: #eef1ff; }
.email-item.unread .item-sender { font-weight: 700; color: var(--text); }
.email-item.unread::before { content: ''; position: absolute; left: 8px; top: 50%; transform: translateY(-50%); width: 6px; height: 6px; border-radius: 50%; background: var(--unread-dot); }

.item-row1 { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 3px; }
.item-sender { font-size: 0.875rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }
.item-time { font-size: 0.75rem; color: var(--text-muted); flex-shrink: 0; margin-left: 8px; }
.item-subject { font-size: 0.875rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); margin-bottom: 3px; }
.item-preview { font-size: 0.8rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 300px; color: var(--text-muted); gap: 12px; }
.empty-state svg { width: 48px; height: 48px; opacity: 0.3; }
.empty-state p { font-size: 0.9rem; }

/* ── Email Detail ───────────────────────────────────── */
.email-detail { background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }

.detail-placeholder { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: var(--text-muted); gap: 14px; }
.detail-placeholder svg { width: 56px; height: 56px; opacity: 0.2; }
.detail-placeholder p { font-size: 0.9rem; }

.detail-inner { display: flex; flex-direction: column; height: 100%; overflow: hidden; }

.detail-toolbar { display: flex; align-items: center; justify-content: space-between; padding: 16px 24px; background: var(--surface); border-bottom: 1px solid var(--border); }
.detail-actions { display: flex; gap: 6px; }

.detail-content { flex: 1; overflow-y: auto; padding: 28px 32px; }
.detail-content::-webkit-scrollbar { width: 4px; }
.detail-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.detail-subject { font-size: 1.375rem; font-weight: 700; color: var(--text); margin-bottom: 18px; line-height: 1.3; }

.detail-meta { background: var(--surface); border-radius: var(--radius); padding: 16px 20px; margin-bottom: 24px; box-shadow: var(--shadow); }
.meta-row { display: flex; gap: 10px; font-size: 0.85rem; margin-bottom: 6px; }
.meta-row:last-child { margin-bottom: 0; }
.meta-label { color: var(--text-muted); min-width: 60px; }
.meta-value { color: var(--text); font-weight: 500; }
.meta-date { color: var(--text-muted); font-size: 0.8rem; }

.detail-body { background: var(--surface); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); line-height: 1.7; font-size: 0.9375rem; color: var(--text); white-space: pre-wrap; word-break: break-word; }
.detail-body iframe { width: 100%; border: none; border-radius: 6px; }

/* ── Compose Modal ──────────────────────────────────── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); backdrop-filter: blur(2px); display: flex; align-items: center; justify-content: center; z-index: 100; }

.modal { background: var(--surface); border-radius: 14px; width: 560px; max-width: 95vw; box-shadow: 0 20px 60px rgba(0,0,0,0.25); display: flex; flex-direction: column; max-height: 90vh; }

.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px 16px; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 1rem; font-weight: 600; }

.compose-fields { border-bottom: 1px solid var(--border); }
.compose-row { display: flex; align-items: center; padding: 12px 20px; border-bottom: 1px solid var(--border); }
.compose-row:last-child { border-bottom: none; }
.compose-row label { color: var(--text-muted); font-size: 0.85rem; min-width: 60px; }
.compose-row input { flex: 1; border: none; outline: none; font-size: 0.9375rem; background: transparent; }

#compose-form textarea { flex: 1; min-height: 220px; border: none; outline: none; resize: none; padding: 16px 20px; font-size: 0.9375rem; font-family: inherit; line-height: 1.6; color: var(--text); }

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

#send-status { font-size: 0.85rem; }
#send-status.success { color: #16a34a; }
#send-status.error { color: var(--danger); }

/* ── Loading spinner ────────────────────────────────── */
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.3); border-top-color: white; border-radius: 50%; animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive ─────────────────────────────────────── */
@media (max-width: 900px) {
  #app { grid-template-columns: 200px 1fr; }
  .email-detail { display: none; }
  .email-detail.mobile-open { display: flex; position: fixed; inset: 0; z-index: 50; }
}
@media (max-width: 600px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .email-list { display: block; }
}
