:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e3e6ea;
  --text: #1c1e21;
  --muted: #6b7280;
  --accent: #111111;
  --gold: #c9a227;
  --gold-soft: #fbf5e3;
  --unread-bg: #ffffff;
  --read-bg: #f3f4f6;
  --hover: #eef0f3;
  --selected: #fdf6e0;
  --danger: #c0392b;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
}
:root[data-theme="dark"] {
  --bg: #16181c;
  --panel: #1e2126;
  --border: #30343a;
  --text: #e7e9ea;
  --muted: #9aa0a6;
  --accent: #e7e9ea;
  --gold: #d4af37;
  --gold-soft: #2c2717;
  --unread-bg: #23262c;
  --read-bg: #1a1c20;
  --hover: #262a30;
  --selected: #2c2717;
  --danger: #e0645a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}
:root[data-theme="dark"] .btn.primary { color: #111; }
:root[data-theme="dark"] .compose-head { background: #0c0d0f; }
:root[data-theme="dark"] .msg-body iframe { background: #fff; }
:root[data-theme="dark"] .avatar { color: #111; }
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); }
button, input, select { font: inherit; color: inherit; }
a { color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
.error { color: var(--danger); background: #fdecea; border: 1px solid #f5c6c2; padding: 8px 10px; border-radius: 6px; }

/* Brand */
.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark { background: var(--accent); color: var(--gold); font-weight: 800; letter-spacing: 1px; padding: 4px 8px; border-radius: 6px; font-size: 13px; }
.brand-text { font-weight: 600; font-size: 16px; }
.brand-text em { font-style: normal; color: var(--muted); font-weight: 400; }
.brand.small .brand-text { font-size: 15px; }

/* Buttons */
.btn { border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 7px 12px; cursor: pointer; white-space: nowrap; text-decoration: none; display: inline-flex; align-items: center; gap: 4px; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.primary:hover { background: #333; }
.btn.primary:disabled { opacity: 0.6; cursor: default; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.compose { width: 100%; justify-content: center; background: var(--gold); border-color: var(--gold); color: #111; font-weight: 600; padding: 10px; margin-bottom: 12px; }
.btn.compose:hover { background: #d9b13a; }
.icon-btn { border: none; background: transparent; border-radius: 50%; width: 32px; height: 32px; cursor: pointer; font-size: 16px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; color: var(--muted); }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn.light { color: #ddd; }
.icon-btn.light:hover { background: rgba(255,255,255,0.15); color: #fff; }
.link { border: none; background: none; color: var(--muted); cursor: pointer; padding: 0 4px; }
.link:hover { color: var(--text); text-decoration: underline; }

/* Login */
.login { height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow); }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card input { border: 1px solid var(--border); border-radius: 8px; padding: 10px; font-size: 14px; color: var(--text); }
.login-card input:focus { outline: 2px solid var(--gold); border-color: transparent; }

/* App shell */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--border); }
.menu-toggle { display: none; }
.search { display: flex; align-items: center; background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: 2px 6px 2px 14px; width: min(520px, 40vw); }
.search input { flex: 1; border: none; background: transparent; padding: 7px 0; outline: none; min-width: 0; }
.mailbox-switch select { border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; background: var(--panel); max-width: 260px; }
.user-menu { position: relative; }
.avatar { width: 34px; height: 34px; border-radius: 50%; border: none; background: var(--accent); color: var(--gold); font-weight: 700; cursor: pointer; }
.dropdown { position: absolute; right: 0; top: 40px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); min-width: 220px; z-index: 30; overflow: hidden; }
.dropdown-head { padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; border-bottom: 1px solid var(--border); }
.dropdown-item { display: block; width: 100%; text-align: left; padding: 10px 14px; border: none; background: none; cursor: pointer; }
.dropdown-item:hover { background: var(--hover); }

.layout { flex: 1; display: grid; grid-template-columns: 220px minmax(320px, 420px) 1fr; min-height: 0; }

/* Sidebar */
.sidebar { padding: 14px 10px; border-right: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.folders { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; }
.folders li { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 999px; cursor: pointer; margin-bottom: 2px; }
.folders li:hover { background: var(--hover); }
.folders li.active { background: var(--gold-soft); font-weight: 600; }
.folders li .count { margin-left: auto; font-size: 12px; color: var(--muted); }
.folders li.active .count, .folders li.has-unread .count { color: var(--text); font-weight: 600; }
.folders .ficon { width: 18px; text-align: center; color: var(--muted); }
.sidebar-foot { padding: 10px 12px 0; border-top: 1px solid var(--border); margin-top: 8px; word-break: break-all; }

/* List */
.list-pane { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.list-toolbar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.list-title { font-weight: 600; padding-left: 4px; }
.select-all { display: flex; align-items: center; padding: 0 4px; cursor: pointer; }
.bulk-actions { display: none; align-items: center; gap: 2px; }
.list-toolbar.has-selection .bulk-actions { display: flex; }
.list-toolbar.has-selection .list-title { display: none; }
.bulk-count { padding: 0 8px 0 2px; white-space: nowrap; }
.list { flex: 1; overflow: auto; }
.empty { padding: 40px 20px; text-align: center; }
.row { position: relative; display: grid; grid-template-columns: 18px 26px 1fr auto; gap: 8px; padding: 9px 10px 9px 6px; border-bottom: 1px solid var(--border); cursor: pointer; background: var(--read-bg); align-items: start; }
.row.unread { background: var(--unread-bg); }
.row:hover { background: var(--hover); box-shadow: inset 0 0 0 1px var(--border); }
.row.selected { background: var(--selected); }
.row .rowcheck { padding-top: 2px; }
.row .rowcheck input { cursor: pointer; }
.row .star { color: #c9c9c9; font-size: 16px; padding-top: 1px; text-align: center; user-select: none; }
.row .star.on, .row .star:hover { color: var(--gold); }
.row .main { min-width: 0; }
.row .line1 { display: flex; align-items: baseline; gap: 6px; }
.row .from { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 70%; }
.row.unread .from, .row.unread .subject { font-weight: 700; }
.row .badge { font-size: 11px; color: var(--muted); background: var(--hover); border-radius: 999px; padding: 0 6px; }
.row .subject { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); margin-top: 2px; }
.row .meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.row .clip { font-size: 13px; }
.row.unread .meta .date { color: var(--text); font-weight: 600; }
.row .quick-actions { display: none; gap: 2px; align-items: center; position: absolute; right: 8px; top: 6px; background: var(--hover); border-radius: 8px; padding: 2px; box-shadow: var(--shadow); }
.row:hover .quick-actions { display: flex; }
.row:hover .meta .date, .row:hover .meta .clip { visibility: hidden; }
.row .quick-actions .icon-btn { width: 26px; height: 26px; font-size: 14px; }

/* Reading */
.read-pane { display: flex; flex-direction: column; min-height: 0; overflow: auto; background: var(--bg); }
.read-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; }
.read-empty-icon { font-size: 48px; opacity: 0.25; }
.thread { padding: 10px 18px 60px; }
.thread-toolbar { display: flex; gap: 6px; align-items: center; position: sticky; top: 0; background: var(--bg); padding: 6px 0; z-index: 2; }
.thread-subject { margin: 10px 0 12px; font-size: 20px; font-weight: 600; }
.only-mobile { display: none; }

.crm-card { background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--gold); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 14px; display: flex; flex-wrap: wrap; gap: 10px 22px; align-items: center; }
.crm-card:empty { display: none; }
.crm-card .who { display: flex; flex-direction: column; }
.crm-card .who strong { font-size: 15px; }
.crm-card .kv { display: flex; flex-direction: column; font-size: 12px; }
.crm-card .kv span:first-child { color: var(--muted); }
.crm-card .opps { display: flex; flex-wrap: wrap; gap: 6px; }
.crm-card .opp { border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; font-size: 12px; text-decoration: none; background: var(--bg); }
.crm-card .opp:hover { border-color: var(--gold); }
.crm-card .actions { margin-left: auto; display: flex; gap: 6px; }
.crm-card.none { border-left-color: var(--border); }

.msg { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; }
.msg-head { display: flex; gap: 10px; padding: 12px 14px; cursor: pointer; align-items: flex-start; }
.msg-head .av { width: 36px; height: 36px; border-radius: 50%; background: var(--gold-soft); color: #7a5f12; display: flex; align-items: center; justify-content: center; font-weight: 700; flex: none; }
.msg-head .h-main { flex: 1; min-width: 0; }
.msg-head .h-from { font-weight: 600; }
.msg-head .h-from span { font-weight: 400; color: var(--muted); }
.msg-head .h-to { color: var(--muted); font-size: 12px; margin-top: 2px; }
.msg-head .h-date { color: var(--muted); font-size: 12px; white-space: nowrap; }
.msg-head .h-snippet { display: none; font-size: 13px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg.collapsed .msg-body, .msg.collapsed .msg-actions { display: none; }
.msg.collapsed .h-snippet { display: block; }
.msg.collapsed .h-to { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-body { padding: 0 14px 14px; }
.msg-body iframe { width: 100%; border: none; background: #fff; border-radius: 6px; min-height: 80px; }
.msg-body .plain { white-space: pre-wrap; font-family: inherit; line-height: 1.5; }
.msg-body .images-note { font-size: 12px; color: var(--muted); margin-bottom: 8px; display: flex; gap: 8px; align-items: center; }
.atts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.att { border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; text-decoration: none; font-size: 13px; background: var(--bg); display: inline-flex; gap: 6px; align-items: center; }
.att:hover { border-color: var(--gold); }
.msg-actions { display: flex; gap: 6px; padding: 0 14px 12px; }

/* Compose */
.compose-wrap { position: fixed; right: 24px; bottom: 0; width: min(680px, calc(100vw - 32px)); z-index: 40; }
.compose { background: var(--panel); border-radius: 12px 12px 0 0; box-shadow: var(--shadow); border: 1px solid var(--border); display: flex; flex-direction: column; max-height: calc(100vh - 40px); }
.compose-head { background: var(--accent); color: #fff; padding: 10px 14px; border-radius: 12px 12px 0 0; display: flex; align-items: center; gap: 4px; font-weight: 600; cursor: default; }
.compose.min .compose-body, .compose.min .compose-foot { display: none; }
.compose-body { display: flex; flex-direction: column; overflow: auto; }
.compose .field { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--border); padding: 6px 14px; }
.compose .field label { color: var(--muted); width: 54px; font-size: 13px; }
.compose .field input { flex: 1; border: none; outline: none; padding: 6px 0; min-width: 0; }
.editor-toolbar { display: flex; gap: 2px; align-items: center; padding: 4px 10px; border-bottom: 1px solid var(--border); }
.editor-toolbar button { border: none; background: none; width: 30px; height: 28px; border-radius: 6px; cursor: pointer; }
.editor-toolbar button:hover { background: var(--hover); }
.editor-toolbar select { border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; font-size: 12px; max-width: 220px; }
.editor { min-height: 220px; padding: 12px 14px; outline: none; line-height: 1.5; overflow: auto; }
.editor blockquote { border-left: 3px solid var(--border); margin: 8px 0; padding-left: 10px; color: #444; }
.attach-list { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 8px; }
.attach-list:empty { display: none; }
.attach-list .chip { border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; font-size: 12px; display: inline-flex; gap: 6px; align-items: center; background: var(--bg); }
.attach-list .chip button { border: none; background: none; cursor: pointer; color: var(--muted); }
.compose-foot { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--border); }

/* Toast */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #222; color: #fff; padding: 10px 16px; border-radius: 999px; box-shadow: var(--shadow); z-index: 60; max-width: 90vw; display: flex; align-items: center; gap: 10px; }
.toast.error { background: var(--danger); }
.toast .link { color: var(--gold); padding: 0; font-weight: 600; }

/* Address autocomplete */
.autocomplete-box { position: fixed; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); z-index: 100; max-height: 240px; overflow: auto; }
.autocomplete-box .ac-item { padding: 8px 12px; cursor: pointer; display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
.autocomplete-box .ac-item .ac-name { font-weight: 500; }
.autocomplete-box .ac-item .ac-email { color: var(--muted); font-size: 12px; }
.autocomplete-box .ac-item .ac-company { margin-left: auto; color: var(--muted); font-size: 11px; }
.autocomplete-box .ac-item:hover, .autocomplete-box .ac-item.active { background: var(--hover); }
.compose .field { position: relative; }

/* Embed mode (?embed=1): strip the app shell down to just the composer, for
   loading in an iframe/modal from elsewhere (e.g. a widget on the CRM). */
html.embed .topbar,
html.embed .layout { display: none; }
html.embed .app { background: var(--panel); }
html.embed .compose-wrap { position: static; inset: auto; width: 100%; flex: 1; display: flex; min-height: 0; }
html.embed .compose-wrap.hidden { display: none; }
html.embed .compose { border: none; border-radius: 0; box-shadow: none; max-height: none; width: 100%; }
html.embed .compose-head #composeMin { display: none; }

/* Responsive */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 200px minmax(280px, 360px) 1fr; }
}
@media (max-width: 860px) {
  .menu-toggle { display: inline-flex; }
  .brand.small .brand-text { display: none; }
  .search { width: auto; flex: 1; }
  #crmLink { display: none; }
  .layout { grid-template-columns: 1fr; position: relative; }
  .sidebar { position: absolute; inset: 0 auto 0 0; width: 240px; z-index: 20; transform: translateX(-100%); transition: transform 0.15s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .list-pane { border-right: none; }
  .read-pane { display: none; }
  .layout.reading .list-pane { display: none; }
  .layout.reading .read-pane { display: flex; }
  .only-mobile { display: inline-flex; }
  .compose-wrap { right: 0; width: 100vw; }
  .row { grid-template-columns: 16px 20px 1fr auto; }
  .bulk-count { display: none; }
}

/* Quoted original in the composer */
.editor .quote-toggle { display: inline-block; background: var(--hover); border-radius: 6px; padding: 0 8px; margin: 6px 0; cursor: pointer; color: var(--muted); user-select: none; line-height: 1.4; }
.editor .quote-toggle:hover { background: #e2e5e9; }
.editor .quoted { color: #444; }
