:root {
    --bg: #10131a;
    --panel: #171b24;
    --panel-2: #202635;
    --text: #e7eaf0;
    --muted: #9aa4b5;
    --line: #2b3345;
    --primary: #4d8dff;
    --primary-hover: #397cf0;
    --danger: #d94f4f;
    --danger-hover: #c94444;
    --warning: #d9a441;
    --warning-hover: #c8912f;
    --secondary: #394255;
    --secondary-hover: #465169;
    --success: #3ca66b;
    --error-bg: #3a1e24;
    --error-border: #9b3e4c;
    --success-bg: #193326;
    --success-border: #3ca66b;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
}

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

a:hover {
    color: white;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    padding: 16px 24px;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
}

.brand-title {
    font-size: 20px;
    font-weight: 700;
}

.brand-subtitle {
    color: var(--muted);
    font-size: 13px;
    margin-top: 2px;
}

.nav {
    display: flex;
    align-items: center;
    gap: 10px;
}

.nav a {
    padding: 8px 12px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.nav a:hover {
    background: var(--secondary-hover);
}

.container {
    width: min(1200px, calc(100% - 32px));
    margin: 24px auto;
}

.page-header {
    margin-bottom: 18px;
}

h1,
h2 {
    margin: 0 0 10px 0;
}

h1 {
    font-size: 28px;
}

h2 {
    font-size: 20px;
}

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

.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 18px;
    margin-bottom: 18px;
}

.grid {
    display: grid;
    gap: 18px;
}

.grid.two {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.login-shell {
    display: flex;
    justify-content: center;
    margin-top: 60px;
}

.login-card {
    width: min(420px, 100%);
}

.form-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 14px;
}

label {
    color: var(--muted);
    font-size: 13px;
}

input {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #0d1017;
    color: var(--text);
    outline: none;
}

input:focus {
    border-color: var(--primary);
}

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 8px 12px;
    border-radius: 8px;
    border: 0;
    cursor: pointer;
    color: white;
    font-weight: 600;
}

.button-primary {
    background: var(--primary);
}

.button-primary:hover {
    background: var(--primary-hover);
}

.button-secondary {
    background: var(--secondary);
}

.button-secondary:hover {
    background: var(--secondary-hover);
}

.button-danger {
    background: var(--danger);
}

.button-danger:hover {
    background: var(--danger-hover);
}

.button-warning {
    background: var(--warning);
}

.button-warning:hover {
    background: var(--warning-hover);
}

table {
    width: 100%;
    border-collapse: collapse;
    overflow: hidden;
}

th,
td {
    padding: 10px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

th {
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}

.right {
    text-align: right;
}

.actions {
    width: 420px;
}

.inline-form {
    max-width: 500px;
}

.mini-form {
    display: inline-block;
    margin: 0 4px 6px 0;
}

.password-form {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.password-form input {
    max-width: 220px;
}

.flash-wrap {
    margin-bottom: 18px;
}

.flash {
    padding: 12px 14px;
    border-radius: 10px;
    margin-bottom: 8px;
    border: 1px solid var(--line);
}

.flash-error {
    background: var(--error-bg);
    border-color: var(--error-border);
}

.flash-success {
    background: var(--success-bg);
    border-color: var(--success-border);
}

.flash-warning {
    background: #332a16;
    border-color: var(--warning);
}

.qr-wrap {
    background: white;
    display: inline-block;
    padding: 14px;
    border-radius: 12px;
    margin: 12px 0 20px 0;
}

.qr-wrap img {
    display: block;
    width: 240px;
    height: 240px;
}

@media (max-width: 800px) {
    .topbar {
        flex-direction: column;
        align-items: flex-start;
    }

    .nav {
        flex-wrap: wrap;
    }

    .grid.two {
        grid-template-columns: 1fr;
    }

    .actions {
        width: auto;
    }

    .password-form {
        flex-direction: column;
    }

    table {
        font-size: 13px;
    }
}

.grid.three {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.stat-card {
    margin-bottom: 18px;
}

.stat-label {
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 8px;
}

.stat-value {
    font-size: 32px;
    font-weight: 700;
}

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

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 16px;
}

.filter-links {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.filter-links a {
    padding: 7px 10px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--muted);
    font-size: 13px;
}

.filter-links a:hover,
.filter-links a.active {
    background: var(--secondary-hover);
    color: var(--text);
}

.table-wrap {
    width: 100%;
    overflow-x: auto;
}

.wide-table {
    min-width: 1300px;
}

.clip {
    max-width: 260px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 72px;
    padding: 4px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    background: var(--secondary);
    color: var(--text);
}

.badge-pending {
    background: #5b6275;
}

.badge-rendered {
    background: #4d6f9f;
}

.badge-sent {
    background: #2f8a57;
}

.badge-retry {
    background: #9b7328;
}

.badge-failed,
.badge-render_failed,
.badge-smtp_failed,
.badge-api_error {
    background: #9b3e4c;
}

.badge-cancelled,
.badge-skipped_duplicate {
    background: #5f5368;
}

.detail-row td {
    border-bottom: 1px solid var(--line);
    background: #121720;
    font-size: 13px;
}

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

.detail-spacer {
    display: inline-block;
    width: 18px;
}

.error-text {
    color: #ffb3bd;
}

.actions-small {
    width: 110px;
    white-space: nowrap;
}

.button-small {
    min-height: 28px;
    padding: 5px 9px;
    font-size: 12px;
}

.code-area {
    width: 100%;
    min-height: 180px;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #0d1017;
    color: var(--text);
    font-family: Consolas, Monaco, "Courier New", monospace;
    font-size: 13px;
    line-height: 1.45;
    resize: vertical;
    outline: none;
}

.code-area:focus {
    border-color: var(--primary);
}

.code-area.short {
    min-height: 220px;
}

.code-area.tall {
    min-height: 520px;
}

.checkbox-row label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
}

.checkbox-row input {
    width: auto;
}

.soft-line {
    border: 0;
    border-top: 1px solid var(--line);
    margin: 24px 0;
}

.button-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 14px;
}

.actions-template {
    width: 260px;
    white-space: nowrap;
}

.help-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
}

.help-grid h3 {
    margin: 0 0 8px 0;
    font-size: 15px;
}

.help-code {
    margin: 0;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0d1017;
    color: var(--muted);
    overflow-x: auto;
    font-family: Consolas, Monaco, "Courier New", monospace;
    font-size: 13px;
}

@media (max-width: 900px) {
    .help-grid {
        grid-template-columns: 1fr;
    }

    .actions-template {
        width: auto;
    }
}

.helper-note {
    color: var(--muted);
    font-size: 12px;
    margin: 0 0 8px 0;
}

code {
    font-family: Consolas, Monaco, "Courier New", monospace;
    color: var(--text);
}

.detail-row code {
    color: var(--muted);
    white-space: pre-wrap;
}

.bulk-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 14px;
}

.compact-table {
    width: 100%;
    min-width: 1050px;
    border-collapse: collapse;
}

.select-col {
    width: 38px;
    text-align: center;
}

.select-col input {
    width: auto;
}

.actions-left {
    width: 150px;
    white-space: nowrap;
}

.no-action-scroll {
    overflow-x: auto;
}

.no-action-scroll .actions-left,
.no-action-scroll .select-col {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--panel);
}

.no-action-scroll .actions-left {
    left: 38px;
}

.no-action-scroll tbody .actions-left,
.no-action-scroll tbody .select-col {
    background: var(--panel);
}

.no-action-scroll thead .actions-left,
.no-action-scroll thead .select-col {
    background: var(--panel-2);
}

@media (max-width: 900px) {
    .compact-table {
        min-width: 950px;
    }

    .actions-left {
        width: 130px;
    }
}

select {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #0d1017;
    color: var(--text);
    outline: none;
}

select:focus {
    border-color: var(--primary);
}

.CodeMirror {
    border: 1px solid var(--line);
    border-radius: 8px;
    font-family: Consolas, Monaco, "Courier New", monospace;
    font-size: 13px;
    line-height: 1.45;
}

.CodeMirror-focused {
    border-color: var(--primary);
}

.cm-jinja-dynamic-tag {
    color: #7ee787 !important;
    font-weight: 700;
}

.cm-jinja-queue-tag {
    color: #79c0ff !important;
    font-weight: 700;
}

.cm-jinja-chunk-tag {
    color: #d2a8ff !important;
    font-weight: 700;
}

.cm-jinja-section-tag {
    color: #ffa657 !important;
    font-weight: 700;
}

.cm-jinja-control-tag {
    color: #ff7b72 !important;
    font-weight: 700;
}

.cm-jinja-comment-tag {
    color: #8b949e !important;
    font-style: italic;
}

.cm-jinja-generic-tag {
    color: #f2cc60 !important;
    font-weight: 700;
}

.editor-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 8px 0 12px 0;
}

.editor-legend > span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 8px;
    border-radius: 999px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    color: var(--muted);
    font-size: 12px;
}

.editor-legend .legend-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    min-width: 10px;
    border-radius: 50%;
    border: 0;
    padding: 0;
}

.editor-legend .legend-dynamic {
    background: #7ee787 !important;
}

.editor-legend .legend-queue {
    background: #79c0ff !important;
}

.editor-legend .legend-chunk {
    background: #d2a8ff !important;
}

.editor-legend .legend-section {
    background: #ffa657 !important;
}

.editor-legend .legend-control {
    background: #ff7b72 !important;
}