:root {
  --bg: #ffffff;
  --bg-subtle: #f9fafb;
  --bg-hover: #f3f4f6;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-2: #374151;
  --muted: #6b7280;
  --faint: #9ca3af;
  --primary: #111827;
  --primary-hover: #1f2937;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --success: #15803d;
  --success-soft: #f0fdf4;
  --warn: #a16207;
  --warn-soft: #fefce8;
  --danger: #b91c1c;
  --danger-soft: #fef2f2;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; padding: 0 10px 24px; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: var(--primary); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border: 0; background: none; border-radius: 6px;
  color: var(--text-2); cursor: pointer; text-align: left;
}
.nav-item svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.nav-item:hover { background: var(--bg-hover); }
.nav-item.active { background: var(--bg-hover); color: var(--text); font-weight: 500; }

.provider-status {
  margin-top: auto;
  display: flex; align-items: center; gap: 8px;
  padding: 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); font-size: 12px; color: var(--muted); cursor: pointer; text-align: left;
}
.provider-status:hover { background: var(--bg-subtle); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex-shrink: 0; }
.dot.on { background: #16a34a; }
.dot.off { background: #d97706; }

.main { flex: 1; min-width: 0; }
.view { max-width: 1120px; margin: 0 auto; padding: 32px 40px 120px; }

.page-head { margin-bottom: 24px; }
.page-head h1 { font-size: 22px; font-weight: 600; margin: 0 0 4px; letter-spacing: -0.01em; }
.page-head p { margin: 0; color: var(--muted); }

/* ---------- Cards ---------- */
.card { border: 1px solid var(--border); border-radius: 10px; background: var(--bg); margin-bottom: 20px; }
.card-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 16px;
  padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.card-head h2 { font-size: 15px; font-weight: 600; margin: 0; display: flex; align-items: center; gap: 10px; }
.card-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.card-body { padding: 20px; }
.step {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%; border: 1px solid var(--border-strong);
  font-size: 11px; font-weight: 600; color: var(--muted);
}
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 6px;
  border: 1px solid var(--border-strong); background: var(--bg);
  font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--bg-subtle); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { border-color: transparent; background: none; color: var(--text-2); }
.btn-ghost:hover { background: var(--bg-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-danger-solid, .btn-danger-solid:hover { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:focus-visible, .nav-item:focus-visible, .option:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Form controls ---------- */
.input, .select, .textarea {
  height: 34px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--bg);
  outline: none; min-width: 0;
}
.select { padding-right: 28px; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' fill='none' stroke='%236b7280' stroke-width='1.5'%3E%3Cpath d='m1 1 4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; }
.textarea { height: auto; padding: 10px; resize: vertical; line-height: 1.55; font-size: 13.5px; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field > label, .label { font-size: 12px; font-weight: 500; color: var(--text-2); }
.field .input, .field .select, .field .textarea { width: 100%; }
.search { position: relative; }
.search .input { padding-left: 32px; width: 260px; }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 14px; height: 14px; stroke: var(--faint); fill: none; stroke-width: 1.6; }

/* ---------- Toolbar & table ---------- */
.toolbar { display: flex; align-items: center; gap: 8px; padding: 14px 20px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.spacer { flex: 1; }
.table-wrap { max-height: 440px; overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { position: sticky; top: 0; background: var(--bg-subtle); font-weight: 500; color: var(--muted); font-size: 12px; z-index: 1; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--bg-subtle); }
tbody tr.is-selected { background: var(--accent-soft); }
tbody tr.is-invalid { cursor: default; color: var(--faint); }
tbody tr:last-child td { border-bottom: 0; }
td.chk, th.chk { width: 40px; padding-right: 0; }
input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--primary); cursor: pointer; vertical-align: middle; }
.empty-cell { text-align: center; color: var(--muted); padding: 40px; cursor: default; }
.cell-empty { color: var(--faint); }

.badge { display: inline-flex; align-items: center; height: 20px; padding: 0 7px; border-radius: 10px; font-size: 11px; font-weight: 500; background: var(--bg-hover); color: var(--muted); margin-left: 6px; }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }

.notice { margin: 14px 20px 0; padding: 10px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg-subtle); font-size: 13px; color: var(--text-2); display: flex; gap: 8px; align-items: flex-start; }
.notice-warn { background: var(--warn-soft); border-color: #fde68a; color: #713f12; }
.notice-danger { background: var(--danger-soft); border-color: #fecaca; color: #7f1d1d; }
.notice-success { background: var(--success-soft); border-color: #bbf7d0; color: #14532d; }
.notice ul { margin: 4px 0 0; padding-left: 18px; }

/* ---------- Template options ---------- */
.compose-grid { display: grid; grid-template-columns: 220px 1fr; gap: 24px; }
.option-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.option {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; background: var(--bg);
  cursor: pointer; text-align: left; display: flex; flex-direction: column; gap: 2px; min-height: 68px;
}
.option:hover { border-color: var(--border-strong); background: var(--bg-subtle); }
.option.active { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.option-title { font-weight: 500; }
.option-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.option-dashed { border-style: dashed; }
.placeholder { color: var(--muted); padding: 18px; border: 1px dashed var(--border-strong); border-radius: var(--radius); text-align: center; }

/* ---------- Editor & preview ---------- */
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 0; }
.split > div { padding: 20px; min-width: 0; }
.split > div + div { border-left: 1px solid var(--border); background: var(--bg-subtle); }
.vars { display: flex; flex-wrap: wrap; gap: 6px; }
.var-chip {
  font-family: var(--mono); font-size: 11.5px; padding: 3px 8px; border-radius: 4px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text-2); cursor: pointer;
}
.var-chip:hover { border-color: var(--border-strong); background: var(--bg-hover); }
.editor-actions { display: flex; gap: 8px; margin-top: 4px; flex-wrap: wrap; align-items: center; }
.dirty-flag { font-size: 12px; color: var(--warn); }

.preview-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.preview-head .select { max-width: 240px; }
.email-preview { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); }
.email-meta { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 13px; display: grid; grid-template-columns: 56px 1fr; gap: 4px 8px; }
.email-meta span { min-width: 0; overflow-wrap: anywhere; }
.email-meta span:nth-child(odd) { color: var(--muted); }
.email-meta .subject { font-weight: 600; }
.email-body { padding: 16px; word-wrap: break-word; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; min-height: 200px; }
mark.missing { background: var(--warn-soft); color: var(--warn); border: 1px dashed #facc15; border-radius: 3px; padding: 0 3px; font-family: var(--mono); font-size: 12px; }

/* ---------- Attachments ---------- */
.att-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.att {
  display: flex; align-items: center; gap: 10px; padding: 6px 8px 6px 6px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--bg); min-width: 0;
  color: var(--text); text-decoration: none; font-size: 13px;
}
a.att:hover { background: var(--bg-subtle); }
.att-thumb { width: 32px; height: 32px; border-radius: 4px; object-fit: cover; flex-shrink: 0; border: 1px solid var(--border); background: var(--bg-subtle); }
.att-ext { display: inline-flex; align-items: center; justify-content: center; font-size: 9.5px; font-weight: 600; color: var(--muted); letter-spacing: 0.02em; }
.att-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: inherit; text-decoration: none; }
a.att-name:hover { text-decoration: underline; }
.att-size { font-size: 12px; color: var(--faint); flex-shrink: 0; }
.att-remove { border: 0; background: none; width: 24px; height: 24px; border-radius: 4px; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; flex-shrink: 0; }
.att-remove:hover { background: var(--bg-hover); color: var(--danger); }
.att-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.btn.is-busy { cursor: progress; opacity: 0.7; }
.email-atts { padding: 0 16px 16px; }
.email-atts .att-list { margin: 0; }
.option-meta { font-size: 11.5px; color: var(--faint); margin-top: 2px; }

/* ---------- Formatting toolbar ---------- */
.rt-toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.rt-toolbar svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }
.rt-images { display: flex; flex-direction: column; gap: 6px; }
.rt-image { display: flex; align-items: center; gap: 12px; padding: 6px 8px 6px 6px; border: 1px solid var(--border); border-radius: 6px; }
.rt-image img { height: 36px; max-width: 120px; object-fit: contain; border-radius: 4px; background: var(--bg-subtle); }
.rt-image label { display: flex; align-items: center; gap: 6px; }
.input-sm { height: 28px; width: 72px; padding: 0 8px; font-size: 12px; }
.format-help { font-size: 12px; color: var(--faint); margin: 0; }
.sig-preview { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-height: 80px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5; }
.sig-preview img, .email-body img { max-width: 100%; }
.email-body a, .sig-preview a { color: #1a56db; }

/* ---------- Send bar ---------- */
.send-bar {
  position: sticky; bottom: 16px; z-index: 5;
  display: flex; align-items: center; gap: 16px;
  padding: 12px 16px 12px 20px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg);
  box-shadow: 0 4px 16px rgba(17, 24, 39, 0.06);
}
.send-bar strong { font-weight: 600; }

/* ---------- Templates manager ---------- */
.tpl-layout { display: grid; grid-template-columns: 240px 1fr; gap: 20px; align-items: start; }
.list { list-style: none; margin: 0; padding: 6px; }
.list-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%;
  padding: 8px 10px; border: 0; background: none; border-radius: 6px; cursor: pointer; text-align: left; color: var(--text-2);
}
.list-item:hover { background: var(--bg-hover); }
.list-item.active { background: var(--bg-hover); color: var(--text); font-weight: 500; }
.count { font-size: 12px; color: var(--faint); font-weight: 400; }
.tpl-main { display: grid; grid-template-columns: 220px 1fr; }
.tpl-main > div { min-width: 0; }
.tpl-main > div:first-child { border-right: 1px solid var(--border); }
.tpl-list-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 6px; }
.section-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--faint); font-weight: 600; }
.empty-state { padding: 48px 24px; text-align: center; color: var(--muted); }
.empty-state h3 { color: var(--text); font-size: 15px; margin: 0 0 4px; font-weight: 600; }
.help { font-size: 12px; color: var(--muted); margin: 0 0 8px; }
code { font-family: var(--mono); font-size: 12px; background: var(--bg-hover); padding: 1px 5px; border-radius: 4px; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(17, 24, 39, 0.35); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal { background: var(--bg); border-radius: 12px; width: 100%; max-width: 520px; max-height: calc(100vh - 40px); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(17, 24, 39, 0.18); }
.modal-head { padding: 18px 20px 0; }
.modal-head h3 { margin: 0; font-size: 16px; font-weight: 600; }
.modal-body { padding: 14px 20px; overflow: auto; color: var(--text-2); }
.modal-body p { margin: 0 0 10px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.summary { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; }
.summary-row { display: flex; justify-content: space-between; gap: 16px; padding: 9px 12px; font-size: 13px; }
.summary-row + .summary-row { border-top: 1px solid var(--border); }
.summary-row span:first-child { color: var(--muted); }
.summary-row span:last-child { text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal .notice { margin: 0 0 12px; }
.result-list { max-height: 200px; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); font-size: 12.5px; }
.result-list div { padding: 7px 12px; display: flex; justify-content: space-between; gap: 12px; }
.result-list div + div { border-top: 1px solid var(--border); }
pre.env { background: var(--bg-subtle); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; font-family: var(--mono); font-size: 12px; overflow: auto; margin: 8px 0 0; }

/* ---------- Sidebar footer & account ---------- */
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.sidebar-foot .provider-status { margin-top: 0; }
.user-box { display: flex; align-items: center; gap: 10px; padding: 8px; border: 0; background: none; border-radius: 8px; cursor: pointer; text-align: left; min-width: 0; }
.user-box:hover { background: var(--bg-hover); }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--bg-hover); border: 1px solid var(--border); display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600; color: var(--text-2); flex-shrink: 0; }
.user-text { display: flex; flex-direction: column; min-width: 0; }
.user-name, .user-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-name { font-size: 13px; font-weight: 500; }
.user-email { font-size: 11.5px; color: var(--muted); }
.pw-details summary { cursor: pointer; font-size: 13px; color: var(--text-2); }
.link-btn { border: 0; background: none; padding: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: inherit; }

/* ---------- Sign-in ---------- */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; background: var(--bg-subtle); }
.auth-card { width: 100%; max-width: 380px; background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 28px; }
.auth-title { font-size: 20px; font-weight: 600; margin: 0 0 4px; }
.auth-sub { margin: 0 0 20px; font-size: 13px; }
.auth-card .input { width: 100%; }
.auth-error { color: var(--danger); margin: 0 0 12px; min-height: 1em; }
.auth-submit { width: 100%; height: 38px; }
.auth-foot { margin: 16px 0 0; text-align: center; color: var(--muted); }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--primary); color: #fff; padding: 9px 16px; border-radius: 8px; font-size: 13px; z-index: 60; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .split, .compose-grid, .tpl-layout, .tpl-main { grid-template-columns: 1fr; }
  .split > div + div { border-left: 0; border-top: 1px solid var(--border); }
  .tpl-main > div:first-child { border-right: 0; border-bottom: 1px solid var(--border); }
}
@media (max-width: 720px) {
  .app { flex-direction: column; }
  .sidebar { width: auto; height: auto; position: static; flex-direction: row; align-items: center; padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--border); gap: 8px; }
  .brand { padding: 0 8px 0 4px; }
  .brand span:last-child { display: none; }
  .nav { flex-direction: row; }
  .sidebar-foot { margin: 0 0 0 auto; flex-direction: row; align-items: center; }
  .provider-status { padding: 8px; }
  .provider-text, .user-text { display: none; }
  .view { padding: 20px 16px 120px; }
  .card-head { flex-wrap: wrap; }
  .search .input { width: 100%; }
  .search { flex: 1 1 100%; }
  .send-bar { flex-wrap: wrap; }
}

/* Send now / Schedule toggle */
.segmented { display: inline-flex; align-self: flex-start; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented label { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; font-size: 13px; cursor: pointer; }
.segmented label + label { border-left: 1px solid var(--border); }
.segmented input { accent-color: var(--primary); margin: 0; }
.segmented label:has(input:checked) { background: var(--bg-hover); font-weight: 500; }
