:root {
  --bg: #ffffff;
  --bg2: #f1f3f4;
  --sidebar: #f1f3f4;
  --surface: #ffffff;
  --border: #e0e0e0;
  --text: #202124;
  --text2: #5f6368;
  --accent: #1a73e8;
  --accent-hover: #1557b0;
  --accent-text: #ffffff;
  --unread-bg: #f8f9fa;
  --selected: #e8f0fe;
  --hover: #f1f3f4;
  --danger: #d93025;
  --success: #34a853;
  --warn: #fbbc04;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #202124;
    --bg2: #292a2d;
    --sidebar: #292a2d;
    --surface: #292a2d;
    --border: #3c4043;
    --text: #e8eaed;
    --text2: #9aa0a6;
    --accent: #8ab4f8;
    --accent-hover: #aecbfa;
    --accent-text: #202124;
    --unread-bg: #36373a;
    --selected: #1e3059;
    --hover: #3c4043;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}

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

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

/* ── LOGIN ─────────────────────────────────────── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 48px 40px;
  width: 360px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-card h1 { font-size: 24px; margin-bottom: 8px; }
.login-card p { color: var(--text2); margin-bottom: 28px; }
.login-card input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg2);
  color: var(--text);
  font-size: 15px;
  margin-bottom: 16px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-error { color: var(--danger); font-size: 13px; margin-bottom: 12px; min-height: 20px; }

/* ── APP LAYOUT ─────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 56px 1fr;
  height: 100vh;
}

/* ── HEADER ─────────────────────────────────────── */
header {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 10;
}

header h1 { font-size: 18px; font-weight: 600; flex: 1; }
header .logo { font-size: 22px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 20px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background .15s;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--hover); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-danger:hover { background: var(--danger); color: white; }
.btn-sm { padding: 5px 12px; font-size: 13px; }
.btn-icon { padding: 8px; border-radius: 50%; border: none; background: transparent; cursor: pointer; color: var(--text); font-size: 18px; }
.btn-icon:hover { background: var(--hover); }

/* ── SIDEBAR ─────────────────────────────────────── */
#sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px 0;
}

.sidebar-section { margin-bottom: 8px; }
.sidebar-label {
  padding: 4px 16px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: .5px;
}

.folder-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 16px;
  cursor: pointer;
  border-radius: 0 20px 20px 0;
  margin-right: 12px;
  font-size: 14px;
  color: var(--text);
  transition: background .1s;
  position: relative;
}
.folder-item:hover { background: var(--hover); }
.folder-item.active { background: var(--selected); color: var(--accent); font-weight: 600; }
.folder-icon { font-size: 16px; width: 20px; text-align: center; }
.folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-count {
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 11px;
  font-weight: 600;
}

.account-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  cursor: pointer;
}
.account-header:hover { color: var(--text); }
.account-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ── MAIN CONTENT ────────────────────────────────── */
#main {
  display: grid;
  grid-template-columns: 380px 1fr;
  overflow: hidden;
}

/* ── MESSAGE LIST ────────────────────────────────── */
#message-list {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.list-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.list-header h2 { font-size: 16px; font-weight: 600; flex: 1; }

.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-bar input {
  flex: 1;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg2);
  color: var(--text);
  font-size: 13px;
}
.search-bar input:focus { outline: none; border-color: var(--accent); }

.messages-scroll { flex: 1; overflow-y: auto; }

.message-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto auto;
  gap: 2px 10px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
  position: relative;
}
.message-item:hover { background: var(--hover); }
.message-item.active { background: var(--selected); }
.message-item.unread { background: var(--unread-bg); }
.message-item.unread .msg-subject { font-weight: 700; }
.message-item.unread .msg-from { font-weight: 700; }

.msg-checkbox {
  grid-row: 1 / 3;
  align-self: center;
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}
.msg-from { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-date { font-size: 12px; color: var(--text2); white-space: nowrap; align-self: center; }
.msg-subject { grid-column: 2; font-size: 13px; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-preview { grid-column: 2 / 4; font-size: 12px; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .8; }
.msg-flag { font-size: 12px; }

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  font-size: 13px;
  color: var(--text2);
}

/* ── EMAIL VIEW ──────────────────────────────────── */
#email-view {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
}

.email-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.email-scroll { flex: 1; overflow-y: auto; padding: 24px 32px; }

.email-header { margin-bottom: 24px; }
.email-subject { font-size: 22px; font-weight: 700; margin-bottom: 16px; line-height: 1.3; }

.email-meta {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--text2);
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.email-meta-row { display: flex; gap: 8px; }
.email-meta-label { font-weight: 600; color: var(--text); min-width: 40px; }

.email-body { margin-top: 20px; }
.email-body iframe {
  width: 100%;
  border: none;
  min-height: 300px;
  background: white;
  border-radius: 4px;
}

.email-body pre {
  white-space: pre-wrap;
  font-family: inherit;
  line-height: 1.6;
}

.attachment-list { margin-top: 16px; display: flex; flex-wrap: wrap; gap: 8px; }
.attachment-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  font-size: 13px;
  color: var(--text2);
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text2);
  gap: 12px;
}
.empty-state .icon { font-size: 48px; opacity: .5; }
.empty-state p { font-size: 15px; }

/* ── MODALS ─────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 32px rgba(0,0,0,.3);
}

.modal-header {
  display: flex;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h2 { flex: 1; font-size: 18px; }
.modal-body { padding: 20px 24px; overflow-y: auto; flex: 1; }
.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-shrink: 0;
}

/* ── FORMS ───────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text2); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg2);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-hint { font-size: 12px; color: var(--text2); margin-top: 4px; }

/* ── SETTINGS ────────────────────────────────────── */
.account-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  background: var(--bg2);
}
.account-card-info { flex: 1; }
.account-card-name { font-weight: 600; }
.account-card-email { font-size: 12px; color: var(--text2); }
.account-dot-lg { width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 16px; font-weight: 700; color: white; }

/* ── MIGRATION ───────────────────────────────────── */
.migration-step {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.progress-bar-wrap {
  background: var(--border);
  border-radius: 99px;
  height: 8px;
  overflow: hidden;
  margin: 8px 0;
}
.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 99px;
  transition: width .3s;
}

.migration-log {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  font-size: 12px;
  font-family: monospace;
  max-height: 200px;
  overflow-y: auto;
  color: var(--text2);
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}
.badge-running { background: #e8f0fe; color: #1a73e8; }
.badge-completed { background: #e6f4ea; color: #34a853; }
.badge-failed { background: #fce8e6; color: #d93025; }
.badge-pending { background: var(--bg2); color: var(--text2); }

/* ── COMPOSE ─────────────────────────────────────── */
.compose-modal { max-width: 680px; }
.compose-modal textarea {
  min-height: 240px;
  resize: vertical;
  line-height: 1.6;
}

/* ── LOADING ─────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-row {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: var(--text2);
  gap: 12px;
}

/* ── TOAST ───────────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius);
  background: #323232;
  color: white;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: slideIn .2s ease;
  max-width: 320px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.hidden { display: none !important; }
