:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-muted: #6b7280;
  --text-subtle: #9ca3af;
  --primary: #0284c7;
  --primary-hover: #0369a1;
  --primary-soft: #e0f2fe;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --accent: #1f2937;
  --shadow-sm: 0 1px 2px rgba(17, 24, 39, 0.04);
  --shadow: 0 4px 12px rgba(17, 24, 39, 0.06);
  --shadow-lg: 0 20px 50px rgba(17, 24, 39, 0.18);
  --radius: 8px;
}

* { box-sizing: border-box; }

.hidden { display: none !important; }

#app { display: flex; flex-direction: column; height: 100vh; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* Login */
.login-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #e0f2fe, #f6f8fb);
  z-index: 100;
}
.login-card {
  background: var(--surface);
  padding: 36px 36px 28px;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: min(400px, 92vw);
}
.login-card h1 {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}
.login-card h1::before { content: "✉"; color: var(--primary); margin-right: 10px; }
.login-card .login-sub { margin: 0 0 22px; color: var(--text-muted); font-size: 13px; }
.login-card label {
  display: block;
  margin: 12px 0;
  font-size: 12px;
  font-weight: 500;
  color: #4b5563;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.login-card input {
  width: 100%;
  padding: 10px 12px;
  margin-top: 6px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font: inherit;
  font-size: 14px;
  text-transform: none;
  color: var(--text);
  background: var(--surface);
}
.login-card input:focus {
  outline: 0;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15);
}
.login-card button {
  width: 100%;
  margin-top: 14px;
  padding: 11px;
  background: var(--primary);
  color: #fff;
  border: 0;
  border-radius: 6px;
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}
.login-card button:hover { background: var(--primary-hover); }

.user-badge {
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 6px;
}

/* Header */
header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
  z-index: 10;
}
header h1 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  flex: 1;
  color: var(--text);
  letter-spacing: -0.01em;
}
header h1::before {
  content: "✉";
  display: inline-block;
  margin-right: 10px;
  color: var(--primary);
  font-size: 18px;
}
header button {
  font: inherit;
  font-weight: 500;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}
header button:hover { background: #f9fafb; border-color: #9ca3af; }
header #composeBtn {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  box-shadow: var(--shadow-sm);
}
header #composeBtn:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
header #accountSelect {
  font: inherit;
  padding: 7px 34px 7px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path d='M2 4l4 4 4-4' stroke='%236b7280' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>") no-repeat right 12px center;
  color: var(--text);
  max-width: 320px;
  min-width: 220px;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  text-overflow: ellipsis;
}
header #accountSelect:focus { outline: 0; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15); }

/* Settings dialog */
#settingsDialog { width: min(720px, 94vw); max-height: 90vh; overflow: auto; }
.settings h3 {
  margin: 8px 0 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
}
.accounts-list { list-style: none; margin: 0 0 20px; padding: 0; border: 1px solid var(--border); border-radius: 6px; }
.accounts-list li {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.accounts-list li:last-child { border-bottom: 0; }
.accounts-list li.empty {
  text-align: center;
  color: var(--text-subtle);
  font-style: italic;
  padding: 18px;
}
.accounts-list .info { flex: 1; min-width: 0; }
.accounts-list .info .name { font-weight: 600; color: var(--text); }
.accounts-list .info .email { color: var(--text-muted); font-size: 12px; }
.accounts-list .info .servers { color: var(--text-subtle); font-size: 11px; font-family: ui-monospace, Menlo, monospace; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.accounts-list .actions { display: flex; gap: 6px; }
.accounts-list .actions button {
  padding: 5px 10px; font-size: 12px; border-radius: 5px;
  border: 1px solid var(--border-strong); background: var(--surface); cursor: pointer;
}
.accounts-list .actions .del { color: var(--danger); border-color: #fca5a5; }
.accounts-list .actions .del:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.accounts-list .actions .edit:hover { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }

#accountForm fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 16px 14px;
  margin: 14px 0;
}
#accountForm legend {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 0 6px;
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 14px; }
.grid-2 label.checkbox {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  text-transform: none;
  letter-spacing: 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  margin-top: 6px;
}
.grid-2 label.checkbox input { width: auto; margin: 0; }
.form-msg { font-size: 12px; margin-top: 10px; min-height: 16px; }
.form-msg.error { color: var(--danger); }
.form-msg.success { color: #059669; }
.form-msg.hint { color: var(--text-muted); }

/* Main layout */
main {
  flex: 1;
  display: grid;
  grid-template-columns: 240px 380px 1fr;
  min-height: 0;
  background: var(--bg);
}

aside, #messageList, #viewer { min-height: 0; overflow: auto; }

aside {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 8px;
}
aside::before {
  content: "Folders";
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-subtle);
  padding: 0 12px 8px;
}

/* Folder list */
#folderList, #messages { list-style: none; margin: 0; padding: 0; }
#folderList li {
  padding: 9px 12px;
  margin: 2px 4px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  color: #374151;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.12s;
}
#folderList li::before {
  content: "📁";
  font-size: 14px;
  opacity: 0.7;
}
#folderList li:hover { background: #f3f4f6; }
#folderList li.active {
  background: var(--primary-soft);
  color: var(--primary);
}
#folderList li.active::before { content: "📂"; opacity: 1; }

/* Message list */
#messageList {
  background: var(--surface);
  border-right: 1px solid var(--border);
}
#messages li {
  padding: 12px 16px;
  border-bottom: 1px solid #f3f4f6;
  cursor: pointer;
  display: grid;
  grid-template-columns: 36px 1fr auto;
  grid-template-rows: auto auto;
  column-gap: 12px;
  row-gap: 2px;
  transition: background 0.12s;
  position: relative;
}
#messages li:hover { background: #f9fafb; }
#messages li.active { background: var(--primary-soft); }
#messages li.active::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--primary);
}

#messages li .avatar {
  grid-row: 1 / span 2;
  width: 36px; height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #38bdf8, #0369a1);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#messages li .from {
  grid-column: 2;
  font-weight: 500;
  color: var(--text);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#messages li .date {
  grid-column: 3;
  color: var(--text-subtle);
  font-size: 11px;
  white-space: nowrap;
  align-self: center;
}
#messages li .subj {
  grid-column: 2 / span 2;
  font-size: 13px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#messages li.unread .from,
#messages li.unread .subj { color: var(--text); font-weight: 600; }
#messages li.unread .date { color: var(--primary); font-weight: 600; }

#messages > li:first-child, #messages > li:last-child { /* no-op */ }
#messages li#loadingMore, #messages li.empty {
  display: block;
  text-align: center;
  color: var(--text-subtle);
  font-style: italic;
  cursor: default;
  padding: 18px;
}
#messages li#loadingMore:hover, #messages li.empty:hover { background: transparent; }

/* Viewer */
#viewer {
  background: var(--surface);
  padding: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#viewerContent {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  padding: 28px 32px;
  gap: 14px;
}
#viewerContent.empty {
  align-items: center;
  justify-content: center;
  color: var(--text-subtle);
  font-size: 15px;
}
#viewerContent .toolbar {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
#viewerContent .toolbar .action {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  transition: all 0.15s;
}
#viewerContent .toolbar .action:hover {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary);
}
#viewerContent h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.3;
}
.meta {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 13px;
}
.meta .avatar-lg {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #38bdf8, #0369a1);
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.meta .meta-info { flex: 1; line-height: 1.6; }
.meta .meta-info .name { color: var(--text); font-weight: 600; font-size: 14px; }
.meta .meta-info .row { color: var(--text-muted); font-size: 12px; }
.meta .meta-info .row strong { color: #4b5563; font-weight: 500; }
.meta .meta-date { color: var(--text-subtle); font-size: 12px; white-space: nowrap; }

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.attachments .att-label {
  width: 100%;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.attachments .att {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 12px;
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  width: 260px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.attachments .att:hover {
  background: #e9eef5;
  border-color: var(--primary);
}
.attachments .att:active { transform: translateY(1px); }
.attachments .att-icon {
  font-size: 20px;
  line-height: 1;
  flex-shrink: 0;
}
.attachments .att-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.attachments .att-name {
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachments .att-meta {
  color: var(--text-subtle);
  font-size: 11px;
}
.attachments .att-download {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.attachments .att-download:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.preview-dialog {
  border: 0;
  border-radius: 10px;
  padding: 0;
  max-width: 92vw;
  max-height: 92vh;
  background: #0b1220;
  color: #fff;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
}
.preview-dialog::backdrop { background: rgba(0,0,0,0.6); }
.preview-wrap {
  display: flex;
  flex-direction: column;
  max-width: 92vw;
  max-height: 92vh;
}
.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: #111827;
  border-bottom: 1px solid #1f2937;
  font-size: 13px;
}
.preview-header #previewName {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.preview-actions { display: flex; gap: 8px; flex-shrink: 0; }
.preview-actions button {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 6px;
  border: 1px solid #374151;
  background: #1f2937;
  color: #fff;
  cursor: pointer;
}
.preview-actions button:hover { background: var(--primary); border-color: var(--primary); }
.preview-body {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  overflow: auto;
}
.preview-body img {
  max-width: 100%;
  max-height: calc(92vh - 80px);
  object-fit: contain;
  border-radius: 4px;
}
.preview-loading, .preview-note {
  color: #9ca3af;
  font-size: 13px;
  padding: 20px;
}

.body {
  white-space: pre-wrap;
  line-height: 1.65;
  font-size: 14px;
  color: #1f2937;
  flex: 1;
  min-height: 0;
  display: flex;
}
.body iframe { width: 100%; height: 100%; flex: 1; border: 0; background: #fff; }

/* Buttons */
.delete {
  align-self: flex-end;
  background: var(--surface);
  color: var(--danger);
  border: 1px solid var(--border-strong);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  transition: all 0.15s;
}
.delete:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

/* Dialog */
dialog {
  border: 0;
  border-radius: var(--radius);
  padding: 24px 28px;
  width: min(600px, 92vw);
  box-shadow: var(--shadow-lg);
  background: var(--surface);
}
dialog::backdrop { background: rgba(17, 24, 39, 0.45); backdrop-filter: blur(2px); }
dialog h3 {
  margin: 0 0 16px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
dialog label {
  display: block;
  margin: 12px 0;
  font-size: 12px;
  font-weight: 500;
  color: #4b5563;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
dialog input, dialog textarea {
  width: 100%;
  padding: 10px 12px;
  margin-top: 6px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font: inherit;
  font-size: 14px;
  text-transform: none;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}
dialog input:focus, dialog textarea:focus {
  outline: 0;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15);
}
dialog textarea { resize: vertical; min-height: 140px; }
dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 0;
  margin: 18px 0 0;
}
dialog button {
  padding: 9px 18px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  transition: all 0.15s;
}
dialog button:hover { background: #f9fafb; }
#sendBtn {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 110px;
  justify-content: center;
}
#sendBtn:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
#sendBtn:disabled { opacity: 0.75; cursor: wait; }
#sendBtn .btn-spinner {
  display: none;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
#sendBtn.loading .btn-spinner { display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Toast */
.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: #111827;
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 10px 30px rgba(17, 24, 39, 0.25);
  z-index: 200;
  opacity: 1;
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}
.toast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(12px);
}
.toast.success { background: #065f46; }
.toast.error { background: #991b1b; }

.error {
  color: var(--danger);
  padding: 12px 16px;
  font-size: 13px;
}

/* Scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 10px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: #9ca3af; background-clip: padding-box; border: 2px solid transparent; }
