:root {
    --bg: #f3f5f8;
    --surface: #ffffff;
    --text: #1c2430;
    --muted: #5d6b7a;
    --border: #e3e8ee;
    --brand: #16324f;
    --accent: #1f6f8b;
    --accent-hover: #185a71;
    --danger: #b42318;
    --ok: #027a48;
    --warn: #b54708;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.04);
    --radius: 12px;
    --sidebar: 240px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
[hidden] { display: none !important; }
body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.app {
    min-height: 100vh;
    display: grid;
    grid-template-columns: var(--sidebar) 1fr;
}

.sidebar {
    background: var(--brand);
    color: #e8eef4;
    padding: 24px 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.brand {
    color: #fff;
    font-weight: 700;
    font-size: 1.25rem;
    letter-spacing: 0.02em;
    margin-bottom: 16px;
    text-decoration: none;
}

.sidebar nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar nav a {
    color: #d5dee8;
    text-decoration: none;
    padding: 8px 10px;
    border-radius: 8px;
}
.sidebar nav a:hover,
.sidebar nav a.active {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    text-decoration: none;
}

.logout { margin-top: auto; }
.logout button {
    width: 100%;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.25);
    color: #fff;
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
}
.logout button:hover { background: rgba(255, 255, 255, 0.08); }

.user {
    margin: 12px 0 0;
    font-size: 0.85rem;
    color: #adc0d3;
}

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 28px 0;
}
.topbar h1 { margin: 0; font-size: 1.4rem; }
.env-badge {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    background: #e8eef4;
    color: var(--muted);
    padding: 4px 8px;
    border-radius: 999px;
}

.content { padding: 20px 28px 40px; }

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 20px;
}
.card {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: var(--shadow);
}
.card span {
    display: block;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.3;
}
.card strong {
    display: block;
    font-size: 1.45rem;
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
}
.cards-compact {
    grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
    margin-bottom: 16px;
}
.card-meta {
    margin: 8px 0 0;
    font-size: 0.8rem;
    font-weight: 400;
}
.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 14px;
    align-items: end;
    margin: 0 0 16px;
}
.filter-bar label {
    display: grid;
    gap: 4px;
    min-width: 140px;
}
.filter-bar .btn { margin-top: 0; }
.pager { margin: 12px 0 0; }
.meter {
    display: grid;
    grid-template-columns: 8rem 1fr auto;
    gap: 10px;
    align-items: center;
    margin: 8px 0;
    font-size: 0.9rem;
}
.meter-track {
    height: 10px;
    background: #e8eef4;
    border-radius: 999px;
    overflow: hidden;
}
.meter-track i {
    display: block;
    height: 100%;
    background: var(--accent);
}
.funnel { display: grid; gap: 8px; }
.funnel > div {
    display: grid;
    grid-template-columns: 7rem 4rem 1fr;
    gap: 10px;
    align-items: center;
}
.funnel i {
    display: block;
    height: 12px;
    background: var(--accent);
    border-radius: 999px;
}
.status-paused { background: #fffaeb; color: var(--warn); }
.panel + .panel,
.cards + .panel,
.panel + .cards { margin-top: 16px; }

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}
.panel h2 { margin: 0 0 12px; font-size: 1.05rem; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td {
    text-align: left;
    padding: 10px 10px;
    border-bottom: 1px solid var(--border);
    font-size: 0.95rem;
    vertical-align: middle;
}
th {
    color: var(--muted);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
}

.muted { color: var(--muted); }

.alert {
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 16px;
}
.alert-error { background: #fef3f2; color: var(--danger); }
.alert-success { background: #ecfdf3; color: var(--ok); }
.alert-warning { background: #fffaeb; color: var(--warn); }

.ai-test-panel {
    border: 1px solid #d6e4ff;
}
.ai-test-panel h2 { letter-spacing: 0.04em; }

.status {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    background: #eef2f6;
    font-size: 0.8rem;
    text-transform: capitalize;
}

.health { margin: 0; display: grid; gap: 10px; }
.health > div {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}
.health dt { font-weight: 600; }
.health dd { margin: 0; }
.ok { color: var(--ok); font-weight: 600; }
.bad { color: var(--warn); font-weight: 600; }

.auth-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background: linear-gradient(180deg, #16324f 0%, #1f6f8b 100%);
}
.auth-card {
    width: min(420px, calc(100% - 32px));
    background: var(--surface);
    padding: 28px;
    border-radius: 16px;
    box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 4px; }
.auth-card form { display: grid; gap: 8px; margin-top: 16px; }
label { font-size: 0.85rem; font-weight: 600; }
input[type="email"],
input[type="password"],
input[type="text"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
}
.btn {
    display: inline-block;
    border: 0;
    border-radius: 8px;
    padding: 10px 14px;
    font: inherit;
    cursor: pointer;
}
.btn-primary {
    background: var(--accent);
    color: #fff;
    margin-top: 8px;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary {
    background: #e8eef4;
    color: var(--text);
}
.btn-secondary:hover { background: #d7e0ea; text-decoration: none; }
.btn-ghost {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
}
.btn-ghost:hover { color: var(--text); text-decoration: none; }
.btn-danger {
    background: #b42318;
    color: #fff;
}
.btn-danger:hover { background: #912018; }
.inline-action { display: inline-block; margin: 0; }
.btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}
a.btn { text-decoration: none; }

.page-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}
.page-actions .btn,
.form-actions .btn { margin-top: 0; }

.wizard {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0;
    margin: 0 0 16px;
}
.wizard li {
    padding: 6px 10px;
    border-radius: 999px;
    background: #e8eef4;
    color: var(--muted);
    font-size: 0.85rem;
}
.wizard li.active {
    background: var(--brand);
    color: #fff;
}
.wizard li.done {
    background: #d7e8ef;
    color: var(--brand);
    cursor: pointer;
}
.wizard li[data-step] { cursor: pointer; }
.wizard li.locked,
.wizard li.locked a {
    cursor: default;
    opacity: 0.65;
}
.wizard li a {
    color: inherit;
    text-decoration: none;
}

.wizard-pane { display: none; }
.wizard-pane.is-active { display: block; }

.readonly-value {
    margin: 0;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #f7f9fb;
    font-weight: 500;
}
.mode-toggle {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 0 0 16px;
}
.mode-toggle .btn { margin-top: 0; }
.advanced-brief {
    margin: 16px 0;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fafcfd;
}
.advanced-brief summary {
    cursor: pointer;
    font-weight: 600;
}
.audience-stats { margin-top: 16px; }
.email-type-field legend {
    font-weight: 600;
    margin-bottom: 6px;
}
.choice-cards {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.choice-card {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fff;
    cursor: pointer;
    font-weight: 600;
}
.choice-card input {
    margin: 0;
}
.choice-card.is-selected,
.choice-card:has(input:checked) {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
}
.email-type-warning {
    margin: 8px 0 0;
    font-size: 0.85rem;
    font-weight: 500;
}

.campaign-form { display: grid; gap: 16px; }
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px 16px;
}
.field { display: grid; gap: 6px; font-size: 0.85rem; font-weight: 600; }
.field-full { grid-column: 1 / -1; }
select,
textarea,
input[type="number"],
input[type="url"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
    font-weight: 400;
}
textarea.codey { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9rem; }
.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}
.plain-list { margin: 0; padding-left: 18px; }
.email-text {
    white-space: pre-wrap;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9rem;
    background: #f7f9fb;
    padding: 12px;
    border-radius: 8px;
    overflow: auto;
}
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
}

.ai-chat {
    margin: 0 0 20px;
    border: 2px solid var(--accent);
    border-radius: 16px;
    background: #fff;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(22, 50, 79, 0.12);
}
.ai-chat-head {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 18px 20px 14px;
    background: linear-gradient(135deg, #16324f 0%, #1f6f8b 100%);
    color: #fff;
}
.ai-chat-kicker {
    margin: 0;
    font-size: 0.75rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #b9d7e4;
}
.ai-chat-head h2 {
    margin: 0;
    font-size: 1.35rem;
    color: #fff;
}
.ai-chat-head p {
    margin: 0;
    color: #e8eef4;
    max-width: 52rem;
}
.ai-chat-log {
    min-height: 88px;
    max-height: 360px;
    overflow-y: auto;
    padding: 16px 18px;
    display: grid;
    gap: 10px;
    background: #f4f8fb;
}
.ai-msg {
    max-width: 92%;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: 0.98rem;
    white-space: pre-wrap;
    line-height: 1.45;
}
.ai-msg-assistant {
    background: #fff;
    border: 1px solid #d5e3ec;
    box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}
.ai-msg-user {
    justify-self: end;
    background: var(--brand);
    color: #fff;
}
.ai-chat-compose {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
    padding: 14px 16px 16px;
    border-top: 1px solid #d5e3ec;
    background: #fff;
}
.ai-chat-compose textarea {
    min-height: 92px;
    border: 2px solid var(--accent);
    font-size: 1rem;
}
.ai-chat-compose .btn,
.btn-ai-send {
    margin-top: 0;
    align-self: end;
    min-width: 112px;
    padding: 12px 18px;
    font-weight: 700;
}
.ai-msg-who {
    display: block;
    font-size: 0.72rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    margin-bottom: 4px;
    opacity: 0.75;
}
.ai-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0 16px 12px;
    background: #f4f8fb;
}
.btn-chip {
    margin-top: 0;
    background: #fff;
    border: 1px solid #c5d6e2;
    color: var(--brand);
    padding: 6px 10px;
    font-size: 0.9rem;
}
.btn-chip:hover { background: #e8eef4; }
.ai-brief,
.ai-error,
.ai-loading,
.ai-start,
.ai-revise {
    padding: 16px 18px;
    border-top: 1px solid #d5e3ec;
    background: #fff;
}
.ai-brief h3 { margin: 0 0 10px; }
.ai-error { background: #fef3f2; color: var(--danger); }
.ai-loading { color: var(--muted); font-weight: 600; }
.ai-compose-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.ai-changes {
    margin: 8px 0 0;
    padding-left: 18px;
    color: var(--ok);
}
.ai-filled {
    outline: 2px solid #7bb07f;
    background: #f3faf4;
}
.email-preview-meta {
    margin: 0 0 14px;
    padding: 12px 14px;
    background: #f7f9fb;
    border: 1px solid var(--border);
    border-radius: 8px;
}
.email-preview-meta p { margin: 0 0 6px; }
.email-preview-meta p:last-child { margin-bottom: 0; }
.preview-tabs,
.template-switch,
.ai-revise-chips,
.section-toggles {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 0 0 12px;
}
.design-tools {
    margin: 0 0 16px;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fff;
}
.field-label {
    margin: 0 0 8px;
    font-weight: 600;
}
.btn-chip.is-selected {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
}
.subject-choices {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 14px;
    margin: 0 0 12px;
}
.subject-choices legend {
    font-weight: 600;
    padding: 0 6px;
}
.subject-choices label {
    display: block;
    margin: 0 0 8px;
}
.section-toggles label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-right: 12px;
}
.preview-frames {
    display: block;
    margin: 12px 0 18px;
}
.preview-frames h3 { margin: 0 0 8px; font-size: 0.95rem; }
.preview-desktop-wrap {
    width: 100%;
    max-width: 640px;
}
.email-preview {
    width: 100%;
    max-width: 640px;
    min-height: 720px;
    height: 720px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #f3f5f7;
}
.preview-mobile-wrap {
    width: 375px;
    max-width: 100%;
    margin: 0 auto;
}
.email-preview-mobile {
    width: 375px;
    max-width: 100%;
    height: 680px;
    min-height: 680px;
    border-radius: 28px;
    box-shadow: inset 0 0 0 10px #1c2430, 0 8px 24px rgba(16, 24, 40, 0.12);
}
.email-text-preview {
    margin: 0;
    padding: 16px 18px;
    max-width: 640px;
    min-height: 240px;
    white-space: pre-wrap;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 14px;
    line-height: 1.5;
}
@media (max-width: 900px) {
    .preview-desktop-wrap,
    .email-preview { max-width: 100%; }
    .preview-mobile-wrap,
    .email-preview-mobile { width: 100%; }
}
.test-email-form {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 12px;
    align-items: end;
    max-width: 520px;
}
.test-email-form .btn { margin-top: 0; }
.scoring-field legend { font-weight: 600; margin-bottom: 6px; }
.scoring-dev-notice { font-size: 0.8rem; color: #8a6d3b; }
.scoring-rule {
    display: grid;
    grid-template-columns: 1.3fr 0.9fr 1fr auto auto;
    gap: 8px;
    align-items: end;
    margin-bottom: 8px;
}
.scoring-rule .scoring-between { display: contents; }
.scoring-rule-remove { margin-bottom: 2px; }
.badge {
    display: inline-block;
    margin-left: 8px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    background: #e8eef4;
    color: #334155;
    vertical-align: middle;
}
.badge-quiet { background: #eef2f6; }
.status-draft { background: #eef2f6; }
.status-queued, .status-sending { background: #fffaeb; color: var(--warn); }
.status-completed { background: #ecfdf3; color: var(--ok); }
.status-cancelled, .status-failed { background: #fef3f2; color: var(--danger); }
.health-good { background: #ecfdf3; color: var(--ok); }
.health-watch { background: #fffaeb; color: var(--warn); }
.health-poor, .health-critical { background: #fef3f2; color: var(--danger); }
.health-insufficient_data { background: #eef2f6; }
.observer-recs { list-style: disc; padding-left: 1.2rem; }
.observer-recs li { margin: 0.4rem 0; }
.observer-panel .inline-action { margin-right: 0.5rem; }

.version-history th, .version-history td { font-size: 0.9rem; }

@media (max-width: 800px) {
    .test-email-form { grid-template-columns: 1fr; }
}

@media (max-width: 800px) {
    .app { grid-template-columns: 1fr; }
    .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; }
    .brand { margin: 0 12px 0 0; }
    .sidebar nav { flex-direction: row; flex-wrap: wrap; }
    .logout, .user { margin: 0; }
    .logout button { width: auto; padding: 8px 12px; }
    .content, .topbar { padding-left: 16px; padding-right: 16px; }
}
