:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-hover: #f1f3f6;
  --border: #e3e6eb;
  --text: #1c2127;
  --text-muted: #6b7280;
  --accent: #3d5afe;
  --accent-hover: #3049e0;
  --accent-soft: #eef1ff;
  --danger: #e5484d;
  --danger-soft: #fdecec;
  --success: #1a9e6b;
  --radius: 10px;
  --shadow: 0 4px 20px rgba(20, 24, 33, 0.06);
  --shadow-lg: 0 12px 40px rgba(20, 24, 33, 0.16);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f26;
    --surface-hover: #262a33;
    --border: #2c313a;
    --text: #e7e9ee;
    --text-muted: #8b93a1;
    --accent: #6f85ff;
    --accent-hover: #8497ff;
    --accent-soft: #232a4d;
    --danger: #ff6b6f;
    --danger-soft: #3a2226;
    --success: #35c98d;
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }

code {
  background: var(--surface-hover);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12px;
}

/* ---------- buttons / inputs ---------- */

button { font-family: inherit; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

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

.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-ghost:hover { background: var(--surface-hover); }

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

.btn-block { width: 100%; }

.icon-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 16px;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }

input[type=text], input[type=email], input[type=password], input[type=date], textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 9px 11px;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }

textarea { resize: vertical; }

label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 12px;
}
label input, label select, label textarea { margin-top: 6px; font-weight: 400; color: var(--text); }

.checkbox {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}
.checkbox input { width: auto; margin: 0; }

/* ---------- provider picker (add-account form) ---------- */

.field { margin-bottom: 12px; }
.field-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.provider-choice { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.provider-option {
  display: block;
  position: relative;
  margin: 0;
  padding: 10px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.provider-option:hover { background: var(--surface-hover); }
.provider-option input { position: absolute; opacity: 0; pointer-events: none; }
.provider-option:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.provider-option:has(input:checked) .provider-name { color: var(--accent); }
.provider-name { display: block; font-size: 13px; font-weight: 600; color: var(--text); }
.provider-hint {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

.account-item-provider {
  flex: none;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-hover);
  border-radius: 4px;
  padding: 1px 5px;
}

.row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.error-text {
  color: var(--danger);
  font-size: 12.5px;
  min-height: 1em;
  margin: 4px 0 0;
}

/* ---------- gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 50;
  padding: 20px;
}
.gate-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  padding: 32px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}
.gate-icon { font-size: 34px; margin-bottom: 8px; }
.gate-card h1 { font-size: 19px; margin: 0 0 8px; }
.gate-card p { margin: 0 0 18px; font-size: 13px; }
.gate-card input { margin-bottom: 14px; text-align: center; }

/* ---------- app shell ---------- */

.app {
  display: grid;
  grid-template-columns: 260px 360px 1fr;
  height: 100vh;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 14.5px; }

.sidebar-section { padding: 14px 12px; border-bottom: 1px solid var(--border); }
.sidebar-section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 0 6px 8px;
}
.sidebar-footer { padding: 16px; font-size: 12.5px; }

.account-list, .folder-list { display: flex; flex-direction: column; gap: 2px; }

.account-item, .folder-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.account-item:hover, .folder-item:hover { background: var(--surface-hover); }
.account-item.active, .folder-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.account-avatar {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.account-item-text { flex: 1; min-width: 0; }
.account-item-label { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-item-sub { display: flex; align-items: center; gap: 5px; min-width: 0; }
.account-item-email { font-size: 11px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-item .icon-btn { opacity: 0; width: 22px; height: 22px; font-size: 13px; }
.account-item:hover .icon-btn { opacity: 1; }

.folder-item .folder-icon { flex-shrink: 0; }
.folder-item-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- list pane ---------- */

.list-pane {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.toolbar {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--surface);
}
.toolbar-search {
  display: grid;
  grid-template-columns: 1fr 1fr auto auto;
  gap: 6px;
  align-items: center;
}
.toolbar-search .checkbox { font-size: 12px; white-space: nowrap; }

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

.message-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.message-item:hover { background: var(--surface-hover); }
.message-item.active { background: var(--accent-soft); }
.message-item.unread .message-subject { font-weight: 700; }
.message-item.unread::before {
  content: "";
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 6px;
  vertical-align: middle;
}

.message-row-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12.5px;
  margin-bottom: 3px;
}
.message-from { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-date { color: var(--text-muted); flex-shrink: 0; }
.message-subject { font-size: 13px; margin-bottom: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-snippet { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-item .clip { margin-left: 4px; }

.list-pagination { padding: 10px; border-top: 1px solid var(--border); }

/* ---------- reading pane ---------- */

.reading-pane {
  overflow-y: auto;
  background: var(--surface);
  display: flex;
  flex-direction: column;
}
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  gap: 8px;
}
.empty-icon { font-size: 40px; }

#reading-back { display: none; margin-bottom: 10px; }
.reading-header { padding: 20px 24px; border-bottom: 1px solid var(--border); }
.reading-subject { font-size: 19px; font-weight: 700; margin: 0 0 12px; }
.reading-meta-row { display: flex; gap: 10px; font-size: 13px; margin-bottom: 4px; }
.reading-meta-label { color: var(--text-muted); width: 46px; flex-shrink: 0; }
.reading-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }

.reading-attachments { padding: 14px 24px; border-bottom: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: 8px; }
.attachment-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface-hover);
  padding: 7px 12px;
  border-radius: 20px;
  font-size: 12.5px;
  cursor: pointer;
}
.attachment-chip:hover { border-color: var(--accent); color: var(--accent); }
.attachment-chip .size { color: var(--text-muted); }

.reading-body { padding: 20px 24px; flex: 1; }
.reading-body iframe { width: 100%; min-height: 300px; border: none; }
.reading-body pre { white-space: pre-wrap; word-break: break-word; font-family: inherit; margin: 0; }

/* ---------- modals ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-card {
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px;
}
.modal-card-wide { max-width: 620px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.modal-header h2 { font-size: 17px; margin: 0; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.attachment-chip-list { display: flex; flex-wrap: wrap; gap: 6px; margin: -6px 0 12px; }
.attachment-chip-list .attachment-chip.removable { padding-right: 8px; }
.attachment-chip .remove { color: var(--danger); font-weight: 700; margin-left: 4px; }

/* ---------- toasts ---------- */

.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 11px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  max-width: 340px;
  animation: toast-in 0.15s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.success { background: var(--success); color: #fff; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- misc ---------- */

.spinner {
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
  margin: 24px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.list-empty, .list-error {
  padding: 32px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* ---------- responsive ---------- */

@media (max-width: 1000px) {
  .app { grid-template-columns: 220px 1fr; }
  .reading-pane {
    position: fixed;
    inset: 0;
    left: 0;
    z-index: 40;
    transform: translateX(100%);
    transition: transform 0.2s ease;
  }
  .reading-pane.open { transform: translateX(0); }
  #reading-back { display: inline-flex !important; }
}

@media (max-width: 680px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0;
    z-index: 45;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .list-pane { border-right: none; }
}
