:root {
  color-scheme: light;
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #edf2f7;
  --text: #142033;
  --muted: #637083;
  --line: #dce4ee;
  --primary: #136f63;
  --primary-2: #0f5b52;
  --accent: #0b73bd;
  --danger: #b42318;
  --warning: #9a6700;
  --ok: #067647;
  --shadow: 0 20px 45px rgba(20, 32, 51, .08);
}

body.dark {
  color-scheme: dark;
  --bg: #111827;
  --surface: #182235;
  --surface-2: #22304a;
  --text: #eef3f8;
  --muted: #a9b7c7;
  --line: #34435a;
  --primary: #30b39f;
  --primary-2: #76d7c8;
  --accent: #f4a261;
  --danger: #ff8a80;
  --warning: #ffd166;
  --ok: #7bd88f;
  --shadow: 0 20px 45px rgba(0, 0, 0, .22);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, color-mix(in srgb, var(--primary) 12%, transparent), transparent 28rem),
    linear-gradient(180deg, var(--bg), color-mix(in srgb, var(--bg) 92%, var(--surface-2)));
  color: var(--text);
}
button, input, select, textarea { font: inherit; }
button {
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: white;
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
}
button:disabled { opacity: .62; cursor: progress; }
button.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
button.danger { background: var(--danger); }
button.icon { width: 40px; height: 40px; padding: 0; display: inline-grid; place-items: center; }
button.small { padding: 7px 9px; font-size: 12px; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
}
textarea { min-height: 88px; resize: vertical; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; font-weight: 700; }
a { color: var(--primary); }
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-panel {
  width: min(430px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; font-weight: 800; font-size: 18px; line-height: 1.18; }
.brand-stack { align-items: flex-start; }
.brand-logo {
  width: 48px;
  height: 48px;
  object-fit: contain;
  flex: 0 0 auto;
  filter: drop-shadow(0 8px 12px rgba(19, 111, 99, .16));
}
.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px 1fr;
}
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}
.nav {
  display: grid;
  gap: 8px;
  margin-top: 28px;
  padding-bottom: 18px;
}
.nav button {
  background: transparent;
  color: var(--muted);
  text-align: left;
  border: 1px solid transparent;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 10px 12px;
  width: 100%;
}
.nav button.active, .nav button:hover {
  color: var(--text);
  background: color-mix(in srgb, var(--primary) 10%, var(--surface));
  border-color: color-mix(in srgb, var(--primary) 28%, var(--line));
}
.menu-icon {
  width: 20px;
  height: 20px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
}
.menu-icon svg {
  width: 20px;
  height: 20px;
  display: block;
  fill: currentColor;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.content { min-width: 0; }
.topbar {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 72%, var(--surface));
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 2;
}
.page { padding: 24px; display: grid; gap: 22px; }
.title h1 { margin: 0; font-size: 26px; letter-spacing: 0; }
.title p { margin: 5px 0 0; color: var(--muted); }
.grid { display: grid; gap: 16px; }
.help-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.page-helper {
  margin: 0;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface-2) 72%, var(--surface));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 700;
}
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  box-shadow: 0 10px 28px rgba(20,32,51,.04);
}
.card.embedded {
  box-shadow: none;
  padding: 14px;
  background: var(--surface-2);
}
.stat .value { font-size: 30px; font-weight: 800; margin-top: 10px; }
.stat .label { color: var(--muted); font-weight: 700; font-size: 13px; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.toolbar input, .toolbar select { max-width: 260px; }
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: 0 10px 28px rgba(20,32,51,.04); }
table { width: 100%; border-collapse: collapse; min-width: 780px; }
th, td { padding: 12px 14px; border-bottom: 1px solid var(--line); text-align: left; font-size: 14px; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; background: color-mix(in srgb, var(--surface-2) 82%, var(--surface)); }
.report-preview { max-width: 100%; }
.report-table table { min-width: 760px; }
.report-table th, .report-table td { max-width: 220px; white-space: normal; overflow-wrap: anywhere; }
.truncate-cell {
  display: inline-block;
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: bottom;
  white-space: nowrap;
}
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.media-card { display: grid; gap: 12px; }
.media-card img,
.media-preview {
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
}
.media-card input { font-size: 12px; }
.template-preview {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fff;
  color: #111827;
}
.template-preview img { max-width: 100%; height: auto; }
.report-summary {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin: 0 0 10px;
  color: var(--muted);
}
.table-sort {
  appearance: none;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  padding: 0;
  cursor: pointer;
  text-align: left;
}
.table-sort.active,
.table-sort:hover {
  color: var(--primary);
}
.twofa-qr {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.twofa-qr img {
  width: 220px;
  height: 220px;
}

@media (max-width: 760px) {
  .report-table table,
  .report-table thead,
  .report-table tbody,
  .report-table tr,
  .report-table th,
  .report-table td {
    display: block;
    min-width: 0;
    width: 100%;
  }

  .report-table thead { display: none; }
  .report-table tr { border-bottom: 1px solid var(--line); padding: 8px 0; }
  .report-table td {
    display: grid;
    grid-template-columns: minmax(120px, 38%) 1fr;
    gap: 10px;
    max-width: none;
  }
  .report-table td::before {
    content: attr(data-label);
    font-weight: 700;
    color: var(--muted);
  }
}
tbody tr:hover { background: color-mix(in srgb, var(--primary) 5%, transparent); }
tbody tr.row-expiring { background: color-mix(in srgb, var(--warning) 15%, var(--surface)); }
tbody tr.row-expiring:hover { background: color-mix(in srgb, var(--warning) 22%, var(--surface)); }
tbody tr.row-expired { background: color-mix(in srgb, var(--danger) 14%, var(--surface)); }
tbody tr.row-expired:hover { background: color-mix(in srgb, var(--danger) 21%, var(--surface)); }
tr:last-child td { border-bottom: 0; }
.pill { display: inline-flex; border-radius: 999px; padding: 4px 9px; font-size: 12px; font-weight: 800; background: var(--surface-2); }
.pill.active, .pill.sent, .pill.renewed { color: var(--ok); }
.pill.accepted, .pill.delivered { color: var(--accent); }
.pill.expired, .pill.failed { color: var(--danger); }
.pill.expiring_soon, .pill.retry { color: var(--warning); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid .wide { grid-column: 1 / -1; }
.modal {
  position: fixed;
  inset: 0;
  background: rgba(5, 12, 24, .56);
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 5;
}
.modal.modal-fullscreen {
  padding: 0;
  place-items: stretch;
}
.modal-panel {
  width: min(760px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 22px;
}
.modal-panel-wide { width: min(1180px, 100%); }
.modal-panel-full {
  width: 100vw;
  height: 100vh;
  max-height: 100vh;
  border-radius: 0;
  padding: 14px;
}
.modal-head { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 18px; }
.email-builder {
  display: grid;
  grid-template-columns: minmax(220px, 280px) minmax(280px, 380px) minmax(0, 1fr);
  gap: 16px;
}
.email-builder-full {
  grid-template-columns: minmax(220px, 280px) minmax(260px, 360px) minmax(0, 1fr);
  height: calc(100vh - 96px);
  min-height: 0;
}
.snippet-palette {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.layout-palette {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}
.layout-item { justify-content: flex-start; }
.swatch-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.color-swatch {
  width: 30px;
  height: 30px;
  border-radius: 999px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line);
  padding: 0;
}
.snippet-item {
  min-height: 40px;
  text-align: center;
}
.builder-panel, .builder-preview {
  display: grid;
  gap: 12px;
  align-content: start;
  min-height: 0;
  overflow: hidden;
}
.builder-blocks {
  display: grid;
  gap: 10px;
  max-height: 360px;
  overflow: auto;
}
.email-builder-full .builder-blocks { max-height: none; height: 100%; }
.email-builder-full .builder-panel .card { max-height: 38vh; overflow: auto; }
.builder-dropzone {
  min-height: 320px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 10px;
  background: color-mix(in srgb, var(--surface-2) 42%, transparent);
}
.builder-block {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-2);
  cursor: grab;
}
.builder-block.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 18%, transparent);
}
.builder-block textarea { min-height: 74px; }
.builder-fields.hidden { display: none; }
.builder-preview iframe {
  width: 100%;
  min-height: 640px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.builder-topbar {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}
.builder-topbar .active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.wysiwyg-email {
  min-height: 640px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f3f6fb;
  padding: 18px;
  overflow: auto;
}
.email-builder-full .wysiwyg-email {
  height: 100%;
  min-height: 0;
}
.wysiwyg-shell {
  width: min(640px, 100%);
  margin: 0 auto;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  overflow: hidden;
  color: #111827;
}
.wysiwyg-email[data-device="mobile"] .wysiwyg-shell { width: min(390px, 100%); }
.wysiwyg-dropzone { padding: 30px 28px; min-height: 360px; }
.wysiwyg-block {
  position: relative;
  outline: 1px dashed transparent;
  border-radius: 8px;
  padding: 6px;
  margin: 0 0 8px;
  cursor: grab;
}
.wysiwyg-block:hover,
.wysiwyg-block:focus-within,
.wysiwyg-block.active {
  outline-color: color-mix(in srgb, var(--primary) 45%, var(--line));
  background: rgba(19,111,99,.04);
}
.wysiwyg-handle {
  display: inline-flex;
  font-size: 11px;
  font-weight: 800;
  color: var(--primary);
  background: #eef8f6;
  border: 1px solid #c6e8e1;
  border-radius: 999px;
  padding: 2px 7px;
  margin-bottom: 6px;
}
[contenteditable="true"] { outline: none; }
.wysiwyg-html-content {
  min-height: 280px;
  color: #111827;
  overflow-wrap: anywhere;
}
.wysiwyg-html-content img,
.wysiwyg-html-content video,
.wysiwyg-html-content iframe {
  max-width: 100%;
  height: auto;
}
.wysiwyg-html-content table { max-width: 100%; }
.hidden { display: none !important; }
.toast { position: fixed; right: 18px; bottom: 18px; background: var(--text); color: var(--surface); padding: 12px 14px; border-radius: 8px; z-index: 9; }
.charts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.bars { display: flex; align-items: end; gap: 8px; height: 190px; padding-top: 18px; }
.bar { flex: 1; background: var(--primary); min-height: 4px; border-radius: 6px 6px 0 0; position: relative; }
.bar span { position: absolute; bottom: -22px; left: 50%; transform: translateX(-50%); color: var(--muted); font-size: 11px; white-space: nowrap; }
.split { display: flex; justify-content: space-between; gap: 12px; align-items: center; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.checkbox-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
.check {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px;
}
.check input { width: auto; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.helper-card {
  display: grid;
  gap: 8px;
  align-content: start;
}
.helper-card strong { font-size: 15px; }
.helper-card .menu-icon { color: var(--primary); }
.status-line {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  font-size: 13px;
  font-weight: 700;
  background: var(--surface);
}
.status-line.info { color: var(--muted); }
.status-line.success { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 35%, var(--line)); }
.status-line.error { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); }
h2 { margin: 0 0 12px; font-size: 18px; }

@media (max-width: 920px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .nav { grid-template-columns: repeat(3, 1fr); }
  .brand-stack { align-items: center; }
  .stats, .charts, .help-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .checkbox-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .email-builder { grid-template-columns: 1fr; }
  .modal-panel-full { width: 100vw; height: 100vh; max-height: 100vh; border-radius: 0; }
  .email-builder-full { height: calc(100vh - 96px); min-height: 0; }
  .email-builder-full .builder-blocks,
  .email-builder-full .wysiwyg-email { height: 100%; min-height: 0; }
}

@media (max-width: 640px) {
  .page, .topbar, .sidebar { padding: 16px; }
  .nav { grid-template-columns: 1fr 1fr; }
  .nav button { font-size: 13px; }
  .stats, .charts, .form-grid, .help-grid { grid-template-columns: 1fr; }
  .checkbox-grid { grid-template-columns: 1fr; }
  .form-grid .wide { grid-column: auto; }
  .topbar { height: auto; align-items: flex-start; gap: 12px; flex-direction: column; }
}
