:root {
  --bg: #0a0a0a;
  --bg-2: #0f0f10;
  --panel: #111113;
  --panel-2: #161618;
  --hover: #1a1a1d;
  --selected: #1f1f24;
  --border: #1f1f23;
  --border-strong: #2a2a2e;
  --border-focus: #3f3f46;

  --text: #fafafa;
  --text-2: #d4d4d8;
  --text-muted: #a1a1aa;
  --text-dim: #71717a;
  --text-faint: #52525b;

  --accent: #22c55e;
  --accent-hover: #16a34a;
  --accent-soft: rgba(34, 197, 94, 0.1);
  --accent-border: rgba(34, 197, 94, 0.3);

  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.1);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.6);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.6), 0 2px 6px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: #27272a; border-radius: 999px; border: 2px solid transparent; background-clip: padding-box; }
*::-webkit-scrollbar-thumb:hover { background: #3f3f46; background-clip: padding-box; border: 2px solid transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-feature-settings: 'cv02', 'cv03', 'cv04', 'cv11';
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--text); text-decoration: none; }
a:hover { color: var(--accent); }

button {
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  transition: background .12s ease, border-color .12s ease, color .12s ease, transform .04s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  line-height: 1;
  height: 30px;
  white-space: nowrap;
}
button:hover { background: var(--hover); border-color: var(--border-strong); }
button:active { transform: translateY(1px); }
button.primary {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
  font-weight: 600;
}
button.primary:hover { background: var(--text-2); border-color: var(--text-2); }
button.accent {
  background: var(--accent);
  color: #0a0a0a;
  border-color: var(--accent);
  font-weight: 600;
}
button.accent:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
}
button.ghost:hover { background: var(--hover); color: var(--text); }
button.danger { color: var(--danger); border-color: rgba(239, 68, 68, 0.3); }
button.danger:hover { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
button.icon { padding: 0; width: 30px; }
button.sm { height: 26px; padding: 0 10px; font-size: 12px; }

input, select, textarea {
  font-family: inherit;
  font-size: 13px;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  width: 100%;
  height: 32px;
  transition: border-color .12s ease, box-shadow .12s ease;
}
textarea { height: auto; min-height: 80px; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--text-faint); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.04);
}
select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23a1a1aa' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 6px;
  letter-spacing: -0.01em;
}

.field { margin-bottom: 14px; }

/* Icons */
.icon-svg { width: 16px; height: 16px; flex-shrink: 0; stroke-width: 2; }
.icon-svg.sm { width: 14px; height: 14px; }
.icon-svg.lg { width: 20px; height: 20px; }

/* === LOGIN === */
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: radial-gradient(ellipse at top, rgba(34, 197, 94, 0.06) 0%, transparent 50%), var(--bg);
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 36px 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-lg);
}
.login-brand {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 24px;
}
.login-brand .logo {
  width: 32px; height: 32px;
  background: var(--accent);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: #0a0a0a;
}
.login-brand h1 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.login-brand .small { color: var(--text-muted); font-weight: 400; font-size: 12px; }

.login-card .sub {
  color: var(--text-muted);
  margin-bottom: 24px;
  font-size: 13px;
}
.login-card button { width: 100%; height: 38px; }
.login-card .err {
  color: var(--danger);
  font-size: 12px;
  margin-top: 12px;
  min-height: 16px;
  text-align: center;
}

/* === APP LAYOUT === */
.app {
  display: grid;
  grid-template-columns: 260px 380px 1fr;
  height: 100vh;
  background: var(--bg);
}

/* === SIDEBAR === */
.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.sidebar-head {
  padding: 18px 18px 14px;
  display: flex; align-items: center; gap: 10px;
}
.brand-logo {
  width: 28px; height: 28px;
  background: var(--accent);
  border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  color: #0a0a0a;
  flex-shrink: 0;
}
.brand-text { font-weight: 600; font-size: 14px; letter-spacing: -0.01em; }
.brand-text .sub { color: var(--text-dim); font-weight: 400; font-size: 11px; display: block; }

.user-card {
  margin: 6px 12px 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.user-card .avatar {
  width: 32px; height: 32px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}
.user-card .info { flex: 1; min-width: 0; }
.user-card .info .name {
  font-size: 13px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-card .info .email {
  font-size: 11px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.role-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 4px;
}
.role-badge.admin {
  background: var(--accent-soft);
  color: var(--accent);
}
.role-badge.agent {
  background: rgba(168, 162, 158, 0.1);
  color: var(--text-muted);
}

.section-title {
  padding: 16px 18px 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
  display: flex; align-items: center; justify-content: space-between;
}

.mailbox-list { padding: 0 8px; flex: 1; overflow-y: auto; }
.mailbox-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-2);
  transition: background .1s, color .1s;
}
.mailbox-item:hover { background: var(--hover); color: var(--text); }
.mailbox-item.active {
  background: var(--selected);
  color: var(--text);
  font-weight: 500;
}
.mailbox-item .mb-icon { color: var(--text-dim); }
.mailbox-item.active .mb-icon { color: var(--accent); }
.mailbox-item .mb-name {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mailbox-item.catch-all-item .mb-icon { color: var(--accent); }
.mailbox-item.catch-all-item .mb-name {
  font-weight: 500;
  background: linear-gradient(135deg, var(--accent), #0ea5e9);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.mailbox-item .badge {
  background: var(--accent);
  color: #0a0a0a;
  font-size: 10px; font-weight: 600;
  padding: 1px 6px;
  border-radius: 999px;
  min-width: 18px;
  text-align: center;
  line-height: 14px;
}

.sidebar-footer {
  padding: 10px 12px 14px;
  border-top: 1px solid var(--border);
  display: flex; gap: 6px;
}
.sidebar-footer button { flex: 1; height: 32px; font-size: 12px; }

/* === EMAIL LIST === */
.list {
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
.list-head {
  padding: 16px 18px 12px;
  display: flex; flex-direction: column; gap: 12px;
  border-bottom: 1px solid var(--border);
}
.list-head .title-row {
  display: flex; align-items: center; justify-content: space-between;
}
.list-head .title { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.list-head .count { color: var(--text-dim); font-size: 12px; margin-left: 6px; font-weight: 400; }
.list-head .actions { display: flex; gap: 4px; }

.search-box {
  position: relative;
  display: flex; align-items: center;
}
.search-box .icon-svg {
  position: absolute;
  left: 10px;
  color: var(--text-dim);
  pointer-events: none;
}
.search-box input { padding-left: 32px; height: 32px; font-size: 13px; }

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

.list-item {
  display: flex; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .1s;
  position: relative;
}
.list-item:hover { background: var(--hover); }
.list-item.active { background: var(--selected); }
.list-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 2px;
  background: var(--accent);
}
.list-item.unread .from, .list-item.unread .subj { color: var(--text); font-weight: 600; }
.list-item.unread .unread-dot {
  position: absolute;
  left: 7px; top: 50%;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--accent);
  transform: translateY(-50%);
}

.list-item .avatar {
  width: 32px; height: 32px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}

.list-item .body {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  gap: 2px;
}
.list-item .row {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 8px;
}
.list-item .from {
  font-size: 13px;
  color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1; min-width: 0;
}
.list-item .when {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
}
.list-item .subj {
  font-size: 13px;
  color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.list-item .preview {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.list-item .tags {
  display: flex; gap: 6px; align-items: center;
  margin-top: 4px;
  font-size: 10px;
  color: var(--text-dim);
}
.list-item .tag-to {
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 4px;
}
.list-item .tag-attach {
  display: inline-flex; align-items: center; gap: 3px;
  color: var(--text-muted);
}

.list-empty {
  text-align: center;
  padding: 80px 24px;
  color: var(--text-dim);
  font-size: 13px;
}
.list-empty .icon-svg {
  width: 36px; height: 36px;
  margin: 0 auto 12px;
  display: block;
  color: var(--text-faint);
}

/* === DETAIL PANE === */
.detail {
  background: var(--bg);
  display: flex; flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
.detail-empty {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-dim);
  gap: 12px;
  font-size: 13px;
}
.detail-empty .icon-svg {
  width: 48px; height: 48px;
  color: var(--text-faint);
  stroke-width: 1.5;
}

.detail-head {
  padding: 20px 28px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}
.detail-head .toolbar {
  display: flex; gap: 6px;
  margin-bottom: 16px;
}
.detail-head .subj {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0 0 14px;
  word-break: break-word;
  line-height: 1.3;
}
.detail-head .meta {
  display: flex; gap: 10px;
  align-items: flex-start;
}
.detail-head .meta .avatar {
  width: 36px; height: 36px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}
.detail-head .meta .meta-info {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 4px;
}
.detail-head .meta-row {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 13px;
  flex-wrap: wrap;
}
.detail-head .meta-from {
  font-weight: 500;
  color: var(--text);
}
.detail-head .meta-from .from-addr { color: var(--text-muted); font-weight: 400; }
.detail-head .meta-when {
  margin-left: auto;
  color: var(--text-dim);
  font-size: 12px;
}
.detail-head .meta-secondary {
  font-size: 12px;
  color: var(--text-dim);
}
.detail-head .meta-secondary .label {
  color: var(--text-faint);
  margin-right: 4px;
}
.detail-head .meta-secondary .value { color: var(--text-muted); }

.detail-status-banner {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(245, 158, 11, 0.1);
  border: 1px solid rgba(245, 158, 11, 0.3);
  color: #fcd34d;
  font-size: 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}

.detail-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 28px 28px;
}
.body-card {
  background: #ffffff;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  overflow: hidden;
  box-shadow: var(--shadow);
  min-height: 100px;
}
.body-card iframe {
  display: block;
  width: 100%;
  border: none;
  background: #ffffff;
  min-height: 240px;
}
.body-card.text-only {
  background: var(--panel);
  padding: 22px 24px;
  border: 1px solid var(--border);
  color: var(--text-2);
}
.body-card pre {
  margin: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.65;
  color: var(--text-2);
}
.body-card pre a { color: var(--accent); text-decoration: underline; }

.attachments-block {
  margin-top: 16px;
  padding: 12px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  color: var(--text-muted);
  display: flex; align-items: center; gap: 8px;
}

/* === ADMIN === */
.admin-wrap {
  padding: 32px;
  max-width: 1200px;
  margin: 0 auto;
}
.admin-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 28px;
}
.admin-head h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.admin-head .actions { display: flex; gap: 8px; }

.admin-tabs {
  display: flex; gap: 2px;
  margin-bottom: 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  width: fit-content;
}
.admin-tab {
  padding: 7px 14px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  border-radius: 6px;
  transition: background .12s, color .12s;
  user-select: none;
}
.admin-tab:hover { color: var(--text); }
.admin-tab.active {
  background: var(--panel-2);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 16px;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.card-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.row-flex {
  display: flex;
  gap: 12px;
  align-items: end;
  flex-wrap: wrap;
}
.row-flex > div { flex: 1; min-width: 160px; }
.row-flex .field { margin-bottom: 0; }

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
}
thead th {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 500;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--panel);
  position: sticky; top: 0;
}
tbody td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text-2);
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--hover); }
tbody td .actions { display: flex; gap: 4px; }
tbody td .ellipsis {
  max-width: 200px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  display: inline-block;
  vertical-align: middle;
}

.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
}
.tag.success { background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent-border); }
.tag.muted { background: var(--panel-2); color: var(--text-muted); border: 1px solid var(--border); }
.tag.danger { background: var(--danger-soft); color: var(--danger); border: 1px solid rgba(239, 68, 68, 0.3); }
.tag.warning { background: rgba(245, 158, 11, 0.1); color: #fcd34d; border: 1px solid rgba(245, 158, 11, 0.3); }

.kbd {
  font-family: ui-monospace, 'SF Mono', Menlo, monospace;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
  color: var(--text-muted);
}

/* === TOAST === */
.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  display: flex; align-items: center; gap: 8px;
  animation: toast-in .25s ease;
}
.toast.error { border-color: rgba(239, 68, 68, 0.5); color: #fca5a5; }
.toast.success { border-color: rgba(34, 197, 94, 0.4); color: var(--accent); }
@keyframes toast-in {
  from { transform: translateY(8px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

/* === MODAL === */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: modal-in .18s ease;
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-head {
  padding: 18px 22px;
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.modal-body { padding: 20px 22px; }
.modal-footer {
  display: flex; justify-content: flex-end; gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.settings-row {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px;
  padding: 14px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  margin-bottom: 14px;
}
.settings-title { font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.settings-desc { font-size: 12px; color: var(--text-muted); line-height: 1.5; }
.settings-hint { font-size: 11px; color: var(--text-dim); margin-top: 6px; }
.settings-hint code { background: var(--panel-2); padding: 1px 5px; border-radius: 3px; border: 1px solid var(--border); font-size: 11px; }
.settings-status { font-size: 12px; margin-top: 12px; min-height: 14px; color: var(--text-muted); }
.settings-status.error { color: var(--danger); }
.settings-status.success { color: var(--accent); }

/* iOS-style switch */
.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--border-strong);
  border-radius: 999px;
  transition: background .2s;
}
.switch .slider:before {
  position: absolute; content: "";
  height: 16px; width: 16px;
  left: 3px; top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform .2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider:before { transform: translateX(18px); }

/* === RESPONSIVE === */
@media (max-width: 1100px) {
  .app { grid-template-columns: 220px 320px 1fr; }
}
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; }
  .sidebar { max-height: 200px; }
  .list { max-height: 50vh; }
}
