:root {
  --primary: #0f3b75;
  --primary-hover: #0a2a54;
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --unread-bg: #eff6ff;
  --green: #059669;
  --green-bg: #ecfdf5;
  --danger: #dc2626;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.app-shell {
  display: grid;
  grid-template-columns: 260px 360px 1fr;
  height: 100dvh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: calc(14px + var(--safe-top)) 14px calc(14px + var(--safe-bottom));
  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 18px;
}
.brand-title { font-weight: 700; font-size: 15px; color: var(--primary); }
.brand-sub { font-size: 11px; color: var(--muted); }

.account-box {
  display: flex; gap: 6px; margin-bottom: 12px;
}
.account-select {
  flex: 1; min-width: 0; padding: 7px 8px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 12px; background: var(--bg); color: var(--text);
}

.compose-wrap { margin-bottom: 16px; }

.nav-section { margin-bottom: 16px; }
.nav-label {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  color: var(--muted); padding: 0 8px 6px; letter-spacing: 0.04em;
}

.folder-list { display: flex; flex-direction: column; gap: 2px; }
.folder-item {
  width: 100%; border: none; background: transparent;
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 10px; border-radius: 8px; cursor: pointer;
  font-size: 13px; color: var(--text); text-align: left;
  transition: background 0.15s;
}
.folder-item:hover { background: #f1f5f9; }
.folder-item.active {
  background: #e0ecff; color: var(--primary); font-weight: 600;
}
.badge {
  background: var(--primary); color: #fff;
  font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 99px;
}

/* List Pane */
.list-pane {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  position: relative;
}

.pane-header {
  display: flex; align-items: center; gap: 10px;
  padding: calc(12px + var(--safe-top)) 14px 12px;
  border-bottom: 1px solid var(--border);
}
.pane-title { margin: 0; font-size: 16px; font-weight: 700; flex: 1; }

.search-wrap { padding: 10px 14px; border-bottom: 1px solid var(--border); }
.search-wrap input {
  width: 100%; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 13px; background: var(--bg);
}
.filter-pills {
  display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap;
}
.filter-pill {
  border: 1px solid var(--border); background: #fff; color: var(--muted);
  font-size: 11px; font-weight: 600; padding: 4px 9px; border-radius: 99px; cursor: pointer;
}
.filter-pill.active {
  background: var(--primary); color: #fff; border-color: var(--primary);
}

.bulk-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 14px; background: #f1f5f9; border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.bulk-select-all {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  font-weight: 600; color: #334155;
}
.bulk-actions { display: flex; align-items: center; gap: 6px; }

.msg-row-inner {
  display: flex; align-items: flex-start; gap: 10px;
}
.msg-check {
  margin-top: 3px; width: 15px; height: 15px; cursor: pointer; accent-color: var(--primary);
}
.msg-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }

.list-toolbar {
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  display: flex; gap: 8px; align-items: center; justify-content: space-between;
  background: #f8fafc;
}

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

.msg-row {
  padding: 12px 14px; border-bottom: 1px solid var(--border);
  cursor: pointer; transition: background 0.12s;
  display: flex; flex-direction: column; gap: 4px;
}
.msg-row:hover { background: #f8fafc; }
.msg-row.unread { background: var(--unread-bg); }
.msg-row.active { background: #dbeafe; border-left: 3px solid var(--primary); }

.msg-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.msg-sender { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-row.unread .msg-sender, .msg-row.unread .msg-subject { font-weight: 700; }
.msg-date { font-size: 11px; color: var(--muted); white-space: nowrap; }
.msg-subject { font-size: 13px; color: #1e293b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-badges { display: flex; align-items: center; gap: 6px; margin-top: 2px; flex-wrap: wrap; }

.track-pill {
  font-size: 11px; padding: 2px 7px; border-radius: 99px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 4px;
}
.track-pill.opened { background: var(--green-bg); color: var(--green); }
.track-pill.sent { background: #f1f5f9; color: var(--muted); }
.att-pill { font-size: 11px; color: var(--muted); }

.pagination-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--border); background: var(--surface);
}
.page-info { font-size: 12px; color: var(--muted); }

/* Reader Pane */
.reader-pane {
  background: var(--bg);
  display: flex; flex-direction: column;
  height: 100%; overflow: hidden;
}
.reader-empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; color: var(--muted);
}
.empty-icon { font-size: 42px; margin-bottom: 8px; opacity: 0.4; }

.reader-content {
  flex: 1; display: flex; flex-direction: column; height: 100%; overflow: hidden;
  background: var(--surface);
}
.reader-header {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  border-bottom: 1px solid var(--border);
}
.reader-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }

.reader-meta {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.reader-subject { margin: 0 0 12px; font-size: 18px; font-weight: 700; line-height: 1.3; }
.meta-row { display: flex; align-items: center; gap: 12px; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--primary); color: #fff; font-weight: 700;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.meta-info { flex: 1; min-width: 0; }
.meta-from { font-size: 13px; font-weight: 600; }
.meta-to { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta-date { font-size: 12px; color: var(--muted); white-space: nowrap; }

.tracking-banner {
  margin-top: 12px; padding: 10px 12px; border-radius: 8px;
  background: var(--green-bg); border: 1px solid #a7f3d0;
  font-size: 12px; color: #065f46;
}
.tracking-banner.pending {
  background: #f8fafc; border-color: var(--border); color: var(--muted);
}

.attachments-bar {
  margin-top: 12px; display: flex; flex-wrap: wrap; gap: 8px;
}
.att-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 12px; color: var(--text); text-decoration: none; background: var(--bg);
}
.att-link:hover { border-color: var(--primary); color: var(--primary); }

.reader-body-wrap { flex: 1; overflow: hidden; background: #fff; }
.reader-frame { width: 100%; height: 100%; border: none; }

/* Tracking Dashboard Cards */
.track-card {
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.track-card-title { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.track-card-sub { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.track-events { font-size: 11px; color: #475569; margin-top: 6px; padding-left: 10px; border-left: 2px solid var(--border); }

/* Buttons & Inputs */
.btn-primary {
  background: var(--primary); color: #fff; border: none;
  padding: 10px 16px; border-radius: 9px; font-weight: 600; font-size: 13px;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-block { width: 100%; }

.btn-outline, .btn-outline-sm {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 8px; font-size: 13px; font-weight: 500; cursor: pointer;
}
.btn-outline-sm { padding: 5px 10px; font-size: 12px; }
.btn-danger-sm {
  background: #fef2f2; color: var(--danger); border: 1px solid #fecaca;
  padding: 5px 10px; border-radius: 8px; font-size: 12px; font-weight: 500; cursor: pointer;
}
.icon-btn {
  background: transparent; border: none; font-size: 18px; cursor: pointer;
  padding: 6px 8px; border-radius: 8px; color: var(--text);
}
.icon-btn:hover { background: #f1f5f9; }

/* Modals */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 16px;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 440px;
  max-height: 92dvh; display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
}
.modal-lg { max-width: 720px; }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-footer {
  display: flex; justify-content: flex-end; gap: 8px; padding-top: 8px;
}
.modal-footer.space-between { justify-content: space-between; }

.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group label, .form-row label { font-size: 12px; font-weight: 600; color: #334155; }
.form-row { display: grid; grid-template-columns: 74px 1fr; align-items: center; gap: 10px; }
.split-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"], textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 13px; font-family: inherit;
}
.readonly-input { background: var(--bg); color: var(--muted); }
.hint { font-size: 11px; color: var(--muted); }
.code-area { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.sig-preview { border: 1px solid var(--border); padding: 10px; border-radius: 8px; background: #fff; }

.editor-toolbar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 6px 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
.fmt-btn {
  background: #fff; border: 1px solid var(--border); border-radius: 6px;
  padding: 4px 10px; cursor: pointer; font-size: 12px;
}
.track-toggle {
  margin-left: auto; display: flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; color: var(--primary); cursor: pointer;
}
.compose-editor {
  min-height: 220px; max-height: 320px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: 8px; padding: 12px;
  font-size: 14px; line-height: 1.5; outline: none; background: #fff;
}

.error-box {
  background: #fef2f2; color: var(--danger); border: 1px solid #fecaca;
  padding: 8px 12px; border-radius: 8px; font-size: 12px;
}
.empty-state { padding: 32px 16px; text-align: center; color: var(--muted); font-size: 13px; }

.toast {
  position: fixed; bottom: calc(20px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 10px 18px; border-radius: 99px;
  font-size: 13px; font-weight: 500; z-index: 200; box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.user-pill {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 13px; font-weight: 600; color: var(--text);
}

.auth-overlay {
  position: fixed; inset: 0; background: #f1f5f9;
  display: flex; align-items: center; justify-content: center;
  z-index: 150; padding: 16px;
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border-radius: 16px; padding: 24px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.12);
  display: flex; flex-direction: column; gap: 16px;
}
.auth-brand { display: flex; align-items: center; gap: 12px; }
.auth-tabs {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
  background: var(--bg); padding: 4px; border-radius: 10px;
}
.auth-tab {
  border: none; background: transparent; padding: 8px; border-radius: 7px;
  font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.auth-tab.active { background: #fff; color: var(--primary); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.auth-form { display: flex; flex-direction: column; gap: 12px; }

.split-3 { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; }
.users-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.user-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px;
}

.mobile-only { display: none; }
.fab {
  position: fixed; right: 18px; bottom: calc(22px + var(--safe-bottom));
  width: 54px; height: 54px; border-radius: 50%;
  background: var(--primary); color: #fff; border: none;
  font-size: 22px; box-shadow: 0 8px 20px rgba(15, 59, 117, 0.35);
  cursor: pointer; z-index: 30;
}

/* Mobile / iPhone Responsive */
@media (max-width: 900px) {
  .mobile-only { display: inline-flex; align-items: center; justify-content: center; }
  .app-shell { grid-template-columns: 1fr; position: relative; }

  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: 280px; z-index: 60;
    transform: translateX(-100%); transition: transform 0.22s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
  }
  .sidebar.open { transform: translateX(0); }
  .drawer-backdrop {
    position: fixed; inset: 0; background: rgba(15, 23, 42, 0.4); z-index: 50;
  }

  .reader-pane {
    position: fixed; inset: 0; z-index: 40;
    transform: translateX(100%); transition: transform 0.22s ease;
  }
  .reader-pane.mobile-active { transform: translateX(0); }

  .form-row { grid-template-columns: 1fr; gap: 4px; }
  .modal { max-height: 95dvh; }
}
