:root {
  color-scheme: light dark;
  --canvas: light-dark(#f3f5f6, #111519);
  --surface: light-dark(#ffffff, #191e24);
  --surface-2: light-dark(#edf1f3, #252c34);
  --surface-3: light-dark(#f8fafb, #151a20);
  --line: light-dark(#d8e0e5, #343d47);
  --text: light-dark(#17212b, #edf2f5);
  --muted: light-dark(#62717d, #a5b0ba);
  --primary: light-dark(#1769aa, #69aee7);
  --primary-ink: light-dark(#ffffff, #0c1821);
  --success: light-dark(#177548, #5ac68e);
  --success-soft: light-dark(#e5f4ec, #173329);
  --warning: light-dark(#996100, #efba51);
  --warning-soft: light-dark(#fbefd7, #3a2d16);
  --danger: light-dark(#b23b3b, #ef8585);
  --danger-soft: light-dark(#fae8e8, #3c2023);
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-family: var(--font);
  font-size: 14px;
  letter-spacing: 0;
  background: var(--canvas);
  color: var(--text);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 320px; min-height: 100vh; background: var(--canvas); color: var(--text); }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, select, input[type="checkbox"] { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }
svg { width: 16px; height: 16px; flex: 0 0 16px; }
h1, h2, h3, p { margin-top: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }

.login-screen { display: grid; min-height: 100vh; place-items: center; padding: 24px; }
.login-form { width: min(360px, 100%); padding: 24px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.login-form h1 { margin: 24px 0 18px; font-size: 20px; font-weight: 500; }
.brand-login { padding: 0; border: 0; }

.app-shell { display: grid; grid-template-columns: 188px minmax(0, 1fr); min-height: 100vh; }
.sidebar { display: flex; min-width: 0; flex-direction: column; border-right: 1px solid var(--line); background: var(--surface-3); }
.brand { display: flex; min-height: 62px; align-items: center; gap: 10px; padding: 0 16px; border-bottom: 1px solid var(--line); font-weight: 500; }
.brand-mark { display: grid; width: 30px; height: 30px; flex: 0 0 30px; place-items: center; border-radius: 6px; background: var(--primary); color: var(--primary-ink); }
.nav { display: flex; flex: 1; flex-direction: column; gap: 4px; padding: 12px 9px; }
.nav-button { display: flex; width: 100%; min-height: 40px; align-items: center; gap: 10px; padding: 8px 10px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); text-align: left; }
.nav-button:hover { background: var(--surface-2); color: var(--text); }
.nav-button[aria-current="page"] { background: var(--surface-2); color: var(--text); font-weight: 500; }
.nav-button[aria-current="page"] svg { color: var(--primary); }
.account { display: grid; grid-template-columns: 32px minmax(0, 1fr) 36px; align-items: center; gap: 9px; padding: 12px; border-top: 1px solid var(--line); }
.account > span:nth-child(2) { min-width: 0; }
.account strong, .account small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account strong { font-size: 13px; font-weight: 500; }
.account small { color: var(--muted); }
.avatar { display: grid; width: 32px; height: 32px; place-items: center; border-radius: 50%; background: var(--surface-2); color: var(--primary); font-weight: 500; }

.main { min-width: 0; }
.topbar { display: flex; min-height: 62px; align-items: center; justify-content: space-between; gap: 12px; padding: 0 20px; border-bottom: 1px solid var(--line); background: var(--surface); }
.topbar h1 { margin: 0; font-size: 18px; font-weight: 500; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.content { padding: 20px; }
.view { min-width: 0; }

.button, .icon-button { display: inline-flex; min-height: 38px; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); }
.button { padding: 7px 12px; }
.icon-button { width: 38px; padding: 0; }
.button:hover, .icon-button:hover { background: var(--surface-2); }
.button-primary { border-color: var(--primary); background: var(--primary); color: var(--primary-ink); }
.button-primary:hover { background: var(--primary); filter: brightness(.96); }
.button-danger { color: var(--danger); }
.button-compact { min-height: 34px; padding: 5px 10px; font-size: 13px; }
.button-block { width: 100%; }
.icon-button[data-tooltip] { position: relative; }
.icon-button[data-tooltip]::after { position: absolute; z-index: 20; right: 0; bottom: calc(100% + 7px); width: max-content; max-width: 180px; padding: 5px 7px; border-radius: 4px; background: var(--text); color: var(--surface); content: attr(data-tooltip); font-size: 12px; opacity: 0; pointer-events: none; transform: translateY(2px); transition: opacity .12s ease, transform .12s ease; }
.icon-button[data-tooltip]:hover::after, .icon-button[data-tooltip]:focus-visible::after { opacity: 1; transform: translateY(0); }

.metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 20px; }
.metric { min-width: 0; padding: 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.metric > span { display: block; color: var(--muted); font-size: 12px; }
.metric strong { display: block; margin-top: 5px; font-size: 23px; font-variant-numeric: tabular-nums; font-weight: 500; }
.metric small { display: block; margin-top: 3px; color: var(--muted); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 13px; }
.section-head h2, .panel h2 { margin: 0; font-size: 15px; font-weight: 500; }
.section-head p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.toolbar { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; margin-bottom: 11px; }

input, select, textarea { width: 100%; min-height: 38px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; outline: none; background: var(--surface); color: var(--text); }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 20%, transparent); }
input[readonly] { cursor: default; color: var(--muted); }
select { width: auto; min-width: 130px; }
textarea { resize: vertical; }
.search { position: relative; width: min(310px, 100%); }
.search svg { position: absolute; left: 10px; top: 50%; color: var(--muted); transform: translateY(-50%); pointer-events: none; }
.search input { padding-left: 34px; }
.field { display: grid; gap: 6px; margin-bottom: 13px; }
.field:last-child { margin-bottom: 0; }
.field > span { color: var(--muted); font-size: 12px; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.address-input { min-height: 64px; }
.body-input { min-height: 220px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }

.table-wrap { min-width: 0; overflow-x: auto; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
th, td { padding: 11px 12px; border-bottom: 1px solid var(--line); overflow: hidden; text-align: left; text-overflow: ellipsis; vertical-align: middle; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
th { color: var(--muted); font-size: 12px; font-weight: 400; }
td { font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-3); }
.name-cell { font-weight: 500; }
.name-cell small { display: block; margin-top: 3px; overflow: hidden; color: var(--muted); font-weight: 400; text-overflow: ellipsis; }
.row-actions { display: flex; align-items: center; justify-content: flex-end; gap: 5px; }
.template-table th:nth-child(1) { width: 23%; }
.template-table th:nth-child(2) { width: 27%; }
.template-table th:nth-child(3) { width: 12%; }
.template-table th:nth-child(4) { width: 11%; }
.template-table th:nth-child(5) { width: 17%; }
.template-table th:nth-child(6) { width: 88px; }
.recipient-table th:nth-child(1) { width: 31%; }
.recipient-table th:nth-child(2) { width: 24%; }
.recipient-table th:nth-child(3) { width: 15%; }
.recipient-table th:nth-child(4) { width: 20%; }
.recipient-table th:nth-child(5) { width: 48px; }
.empty { padding: 34px 16px; color: var(--muted); text-align: center; }
.status { display: inline-flex; max-width: 100%; align-items: center; gap: 5px; padding: 3px 7px; border-radius: 4px; font-size: 12px; font-weight: 500; }
.status::before { width: 6px; height: 6px; flex: 0 0 6px; border-radius: 50%; background: currentColor; content: ""; }
.status-success { background: var(--success-soft); color: var(--success); }
.status-warning { background: var(--warning-soft); color: var(--warning); }
.status-danger { background: var(--danger-soft); color: var(--danger); }
.status-muted { background: var(--surface-2); color: var(--muted); }

.compose-grid { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(240px, .7fr); gap: 15px; align-items: start; }
.compose-side { display: grid; gap: 12px; }
.panel { min-width: 0; padding: 15px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.panel h2 { margin-bottom: 14px; }
.switch-row { display: flex; min-height: 48px; align-items: center; justify-content: space-between; gap: 12px; }
.switch-row strong, .switch-row small { display: block; }
.switch-row strong { font-size: 13px; font-weight: 500; }
.switch-row small { margin-top: 3px; color: var(--muted); }
.switch-row > input { width: 38px; min-height: 22px; height: 22px; flex: 0 0 38px; appearance: none; padding: 0; border: 0; border-radius: 12px; background: var(--surface-2); position: relative; }
.switch-row > input::after { position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); content: ""; transition: transform .15s ease; }
.switch-row > input:checked { background: var(--primary); }
.switch-row > input:checked::after { background: var(--primary-ink); transform: translateX(16px); }
.field-switch { align-self: end; margin-bottom: 13px; padding: 0 3px; }
.send-summary { margin: 0 0 14px; }
.send-summary div { display: flex; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.send-summary div:last-child { border-bottom: 0; }
.send-summary dt { color: var(--muted); }
.send-summary dd { margin: 0; font-weight: 500; }
.form-message { min-height: 18px; margin: 10px 0 0; color: var(--danger); font-size: 12px; text-align: center; }
.form-message.success { color: var(--success); }
.align-left { text-align: left; }

.job-list { display: grid; gap: 11px; }
.job-row { display: grid; grid-template-columns: minmax(150px, 1.15fr) minmax(220px, 2fr) auto; align-items: center; gap: 16px; padding: 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.job-title { font-weight: 500; }
.job-meta { margin-top: 4px; color: var(--muted); font-size: 12px; }
.progress-head { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 7px; font-size: 12px; }
.progress-native, .bar-progress { width: 100%; height: 8px; appearance: none; border: 0; background: transparent; }
.progress-native::-webkit-progress-bar, .bar-progress::-webkit-progress-bar { border-radius: 4px; background: var(--surface-2); }
.progress-native::-webkit-progress-value, .bar-progress::-webkit-progress-value { border-radius: 4px; background: var(--primary); }
.progress-native::-moz-progress-bar, .bar-progress::-moz-progress-bar { border-radius: 4px; background: var(--primary); }
.job-actions { display: flex; gap: 6px; }

.analytics-grid, .settings-grid { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(240px, .8fr); gap: 15px; }
.bars { display: grid; gap: 11px; }
.bar-row { display: grid; grid-template-columns: 90px minmax(0, 1fr) 42px; align-items: center; gap: 9px; font-size: 12px; }
.bar-label { color: var(--muted); }
.bar-progress { height: 9px; }
.bar-value { text-align: right; }
.recent-list { margin: 0; padding: 0; list-style: none; }
.recent-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 9px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.recent-list li:last-child { border-bottom: 0; }
.recent-list span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-list time { color: var(--muted); font-size: 12px; }
.connection { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.connection > span { width: 8px; height: 8px; flex: 0 0 8px; border-radius: 50%; background: var(--success); }
.connection > span.connection-warning { background: var(--warning); }
.connection strong { font-size: 13px; font-weight: 500; }
.settings-note { margin: 14px 0 0; color: var(--muted); font-size: 12px; text-align: right; }

dialog { width: min(620px, calc(100% - 24px)); max-height: calc(100% - 24px); padding: 0; overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); }
dialog::backdrop { background: light-dark(rgb(23 33 43 / 35%), rgb(0 0 0 / 65%)); }
.dialog-form > header { display: flex; min-height: 56px; align-items: center; justify-content: space-between; gap: 12px; padding: 0 16px; border-bottom: 1px solid var(--line); }
.dialog-form > header h2 { margin: 0; font-size: 16px; font-weight: 500; }
.dialog-content { padding: 16px; }
.dialog-form > footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 16px; border-top: 1px solid var(--line); }

.toast { position: fixed; z-index: 50; right: 18px; bottom: 18px; max-width: min(360px, calc(100% - 36px)); padding: 11px 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--text); color: var(--surface); box-shadow: 0 8px 24px rgb(0 0 0 / 18%); }

@media (max-width: 920px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); }
  .brand { min-height: 54px; }
  .nav { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); padding: 8px; }
  .nav-button { justify-content: center; }
  .account { display: none; }
  .compose-grid, .analytics-grid, .settings-grid { grid-template-columns: 1fr; }
  .job-row { grid-template-columns: 1fr; }
  .job-actions { justify-content: flex-start; }
}

@media (max-width: 700px) {
  .content { padding: 14px; }
  .metrics { grid-template-columns: 1fr; }
  .toolbar { align-items: stretch; flex-direction: column; }
  .toolbar .search { width: 100%; }
  .toolbar select { width: 100%; }
  .field-grid { grid-template-columns: 1fr; gap: 0; }
  table th:nth-child(5), table td:nth-child(5), table th:nth-child(6), table td:nth-child(6) { display: none; }
  .template-table th:nth-child(2), .template-table td:nth-child(2), .template-table th:nth-child(4), .template-table td:nth-child(4), .template-table th:nth-child(5), .template-table td:nth-child(5) { display: none; }
  .template-table th:nth-child(1) { width: auto; }
  .template-table th:nth-child(3) { width: 96px; }
  .template-table th:nth-child(6) { display: table-cell; width: 88px; }
  .template-table td:nth-child(6) { display: table-cell; }
  .recipient-table th:nth-child(2), .recipient-table td:nth-child(2), .recipient-table th:nth-child(4), .recipient-table td:nth-child(4) { display: none; }
  .recipient-table th:nth-child(1) { width: auto; }
  .recipient-table th:nth-child(3) { width: 92px; }
  .recipient-table th:nth-child(5) { display: table-cell; width: 48px; }
  .recipient-table td:nth-child(5) { display: table-cell; }
}

@media (max-width: 480px) {
  .nav-button { min-height: 44px; padding: 8px 5px; }
  .nav-button span { display: none; }
  .topbar { min-height: 56px; padding: 8px 12px; }
  .topbar h1 { font-size: 16px; }
  .optional-label { display: none; }
  .top-actions .button { width: 38px; padding: 0; }
  .section-head { align-items: flex-start; }
  .template-table [data-template-delete] { display: none; }
  .template-table th:nth-child(6) { width: 48px; }
  th, td { padding: 10px 8px; }
  .body-input { min-height: 180px; }
  .dialog-form > footer .button { flex: 1; }
  .bar-row { grid-template-columns: 72px minmax(0, 1fr) 36px; gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
