/* KundaliniMailer admin UI.
   Palette and layout follow the "Панель" direction: light working surface,
   grouped sidebar, semantic colour reserved for state. */

:root {
    color-scheme: light dark;

    --bg: #f7f9f9;
    --surface: #ffffff;
    --raised: #eef3f3;
    --sunk: #f1f5f5;
    --line: #e3e9e9;
    --line-2: #cfd9d9;
    --ink: #141a1c;
    --muted: #5f6d70;
    --faint: #85918f;
    --accent: #146a72;
    --accent-soft: #e6f0f0;
    --on-accent: #ffffff;
    --ok: #1d7a4d;
    --warn: #9a6512;
    --crit: #b3352b;
    --shadow: 0 1px 2px rgba(20, 26, 28, .05);

    --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
    --sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

    --side-w: 216px;
    --radius: 8px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #101415;
        --surface: #171c1d;
        --raised: #1f2627;
        --sunk: #141819;
        --line: #262f30;
        --line-2: #35403f;
        --ink: #e4eae9;
        --muted: #9aa7a6;
        --faint: #7c8888;
        --accent: #4bb8ae;
        --accent-soft: #17302f;
        --on-accent: #06201f;
        --ok: #5cc98d;
        --warn: #dfa544;
        --crit: #ef7264;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    }
}

*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--sans);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }

h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -.02em; }
h2 { margin: 0; font-size: 15px; font-weight: 650; }
h3 { margin: 0; font-size: 13px; font-weight: 650; }

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }

/* ---------- shell ---------------------------------------------------- */

.shell { display: grid; grid-template-columns: var(--side-w) 1fr; min-height: 100vh; }
.main { min-width: 0; display: flex; flex-direction: column; }
.content { padding: 20px 22px 56px; display: flex; flex-direction: column; gap: 20px; }

/* ---------- sidebar -------------------------------------------------- */

.side {
    background: var(--surface);
    border-right: 1px solid var(--line);
    padding: 16px 12px 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.side-brand { display: flex; align-items: center; gap: 9px; padding: 0 8px 14px; }
.side-brand img { width: 26px; height: 26px; border-radius: 6px; }
.side-brand span { font-size: 14px; font-weight: 650; letter-spacing: -.01em; color: var(--ink); }

.side-group {
    font-family: var(--mono);
    font-size: 9.5px;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--faint);
    padding: 14px 8px 5px;
}

.side a {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 7px 8px;
    border-radius: 5px;
    color: var(--muted);
    font-size: 13px;
    text-decoration: none;
}
.side a:hover { background: var(--raised); color: var(--ink); text-decoration: none; }
.side a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side a svg { width: 15px; height: 15px; stroke: currentColor; fill: none; stroke-width: 1.6; flex: none; }
.side a .count { margin-left: auto; font-family: var(--mono); font-size: 10.5px; font-weight: 400; }

.side-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--line); }
.side-foot form { display: block; }
.side-foot button {
    width: 100%;
    text-align: left;
    background: transparent;
    color: var(--muted);
    font-weight: 500;
    padding: 7px 8px;
}
.side-foot button:hover { background: var(--raised); color: var(--crit); }

/* ---------- status bar ----------------------------------------------- */

.statusbar {
    display: flex;
    align-items: center;
    gap: 18px;
    flex-wrap: wrap;
    padding: 8px 22px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
}
.statusbar b { color: var(--ink); font-weight: 500; }
.statusbar .spacer { flex: 1; min-width: 0; }
.statusbar .poll { display: flex; align-items: center; gap: 8px; }
.statusbar .poll-state { color: var(--faint); }

.poll-toggle {
    font-family: var(--mono);
    font-size: 11px;
    padding: 2px 9px;
    border: 1px solid var(--line-2);
    border-radius: 20px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-weight: 400;
}
.poll-toggle:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- page header ---------------------------------------------- */

.pagehead {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    padding: 16px 22px 0;
}
.pagehead .crumb { font-family: var(--mono); font-size: 10.5px; color: var(--faint); margin-bottom: 2px; }
.pagehead-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ---------- section label -------------------------------------------- */

.section-label {
    display: flex;
    align-items: center;
    gap: 10px;
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--faint);
    margin-bottom: -10px;
}
.section-label::after { content: ""; flex: 1; height: 1px; background: var(--line); }

/* ---------- cards / kpis --------------------------------------------- */

.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    flex-wrap: wrap;
    padding: 12px 15px;
    border-bottom: 1px solid var(--line);
}
.card-head .hint { font-family: var(--mono); font-size: 10.5px; color: var(--faint); }
.card-body { padding: 15px; }
.card-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 10px 15px;
    border-top: 1px solid var(--line);
    font-family: var(--mono);
    font-size: 11px;
    color: var(--faint);
}

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.kpi {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px 15px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.kpi.alert { border-color: color-mix(in srgb, var(--crit) 42%, var(--line)); }
.kpi .kpi-label { font-size: 12px; color: var(--muted); }
.kpi .kpi-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; }

/* ---------- transactional templates -------------------------------- */

.template-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; }
.template-card { display: flex; flex-direction: column; gap: 9px; min-height: 150px; padding: 18px; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.template-card:hover { border-color: var(--accent); text-decoration: none; }
.template-card .pill { align-self: flex-start; }
.template-card p { margin: 0; color: var(--muted); }
.template-card .mono { margin-top: auto; color: var(--faint); font-size: 11px; }
.asset-upload, .inline-form { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; }
.asset-upload label { min-width: 220px; }
.asset-list { display: grid; gap: 8px; margin-top: 18px; }
.asset-row { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 9px; border: 1px solid var(--line); border-radius: 6px; }
.asset-row img { width: 64px; height: 48px; object-fit: cover; border-radius: 4px; background: var(--raised); }
.asset-row code { overflow-wrap: anywhere; color: var(--accent); }
.editor-layout { display: grid; grid-template-columns: minmax(420px, 1fr) minmax(360px, 1fr); gap: 16px; align-items: start; }
.template-form { overflow: visible; }
.form-stack { display: flex; flex-direction: column; gap: 16px; }
.field-label { display: block; margin-bottom: 5px; font-size: 12px; color: var(--muted); }
.template-tokens { display: flex; flex-wrap: wrap; gap: 5px; }
.template-tokens code { padding: 4px 7px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); font-size: 11px; }
.quill-editor { min-height: 390px; background: var(--surface); color: var(--ink); }
.ql-toolbar.ql-snow, .ql-container.ql-snow { border-color: var(--line-2) !important; }
.ql-editor { min-height: 390px; font-size: 15px; line-height: 1.65; }
.ql-editor .km-system-block { padding: 12px 14px; border: 1px dashed var(--accent); border-radius: 6px; color: var(--accent); background: var(--accent-soft); font-family: var(--mono); font-size: 12px; cursor: not-allowed; }
.theme-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; padding-top: 12px; }
.color-input { display: flex; align-items: center; gap: 8px; height: 36px; }
.color-input input { width: 48px; height: 34px; padding: 2px; }
.preview-card { position: sticky; top: 12px; }
.preview-tools { display: flex; gap: 6px; }
.preview-stage { min-height: 740px; padding: 14px; overflow: auto; background: var(--raised); text-align: center; }
.preview-stage iframe { display: inline-block; width: 600px; max-width: 100%; height: 710px; border: 0; background: #fff; transition: width .2s ease; }
.template-bottom-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }

@media (max-width: 1100px) {
    .editor-layout { grid-template-columns: 1fr; }
    .preview-card { position: static; }
}

@media (max-width: 700px) {
    .template-bottom-grid, .theme-grid { grid-template-columns: 1fr; }
    .asset-row { grid-template-columns: 50px minmax(0, 1fr); }
    .asset-row form { grid-column: 2; }
}
.kpi .kpi-value {
    font-family: var(--mono);
    font-size: 26px;
    line-height: 1;
    letter-spacing: -.02em;
    font-variant-numeric: tabular-nums;
    color: var(--ink);
}
.kpi .kpi-sub { font-family: var(--mono); font-size: 10.5px; color: var(--faint); }

.volume {
    display: flex;
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.volume > div {
    flex: 1;
    min-width: 132px;
    padding: 11px 15px;
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.volume > div:last-child { border-right: 0; }
.volume .v-label { font-size: 11.5px; color: var(--muted); }
.volume .v-value { font-family: var(--mono); font-size: 17px; font-variant-numeric: tabular-nums; color: var(--ink); }

/* ---------- pills ---------------------------------------------------- */

.pill {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-family: var(--mono);
    font-size: 10.5px;
    letter-spacing: .03em;
    padding: 2px 8px;
    border-radius: 11px;
    white-space: nowrap;
}
.pill::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: currentColor; flex: none; }
.pill-ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 13%, transparent); }
.pill-warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); }
.pill-crit { color: var(--crit); background: color-mix(in srgb, var(--crit) 13%, transparent); }
.pill-idle { color: var(--muted); background: color-mix(in srgb, var(--muted) 13%, transparent); }

/* ---------- tables --------------------------------------------------- */

.table-scroll { overflow-x: auto; }

table.data { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.data th {
    font-family: var(--mono);
    font-size: 10px;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--faint);
    font-weight: 500;
    text-align: left;
    padding: 9px 14px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}
table.data td {
    padding: 9px 14px;
    border-bottom: 1px solid var(--line);
    color: var(--muted);
    vertical-align: middle;
}
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--sunk); }
table.data .strong { color: var(--ink); }
/* "4 минуты назад" must not stack into three lines. */
table.data td time { white-space: nowrap; }
table.data .trunc {
    display: block;
    max-width: 210px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
/* Keep the row action reachable without scrolling on a 1280px screen. */
table.data td:last-child { white-space: nowrap; }
.inbox-unread { background: color-mix(in srgb, var(--accent) 6%, transparent); }
.inbox-unread td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
.inbox-unread .inbox-subject { color: var(--ink); font-weight: 650; }

/* ---------- toolbar (search + filters) -------------------------------- */

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar input[type="search"] {
    min-width: 210px;
    border: 1px solid var(--line-2);
    border-radius: 5px;
    padding: 6px 10px;
    font: inherit;
    font-size: 12.5px;
    background: var(--surface);
    color: var(--ink);
}
.filters { display: flex; gap: 5px; flex-wrap: wrap; }
.filters a {
    font-family: var(--mono);
    font-size: 10.5px;
    color: var(--muted);
    border: 1px solid var(--line-2);
    border-radius: 20px;
    padding: 4px 11px;
    text-decoration: none;
}
.filters a:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.filters a.on { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.pager { display: flex; gap: 5px; align-items: center; }
.pager a, .pager span {
    min-width: 26px;
    text-align: center;
    padding: 3px 7px;
    border-radius: 4px;
    border: 1px solid var(--line-2);
    color: var(--muted);
    text-decoration: none;
    font-family: var(--mono);
    font-size: 11px;
}
.pager a:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.pager .on { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.pager .gap { border: 0; color: var(--faint); }

/* ---------- forms ---------------------------------------------------- */

form { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-size: 12.5px; color: var(--muted); }

input, textarea, select {
    border: 1px solid var(--line-2);
    border-radius: 5px;
    padding: 8px 11px;
    font: inherit;
    font-size: 13px;
    background: var(--surface);
    color: var(--ink);
    min-width: 0;
}
textarea { width: 100%; resize: vertical; }
input:disabled { background: var(--raised); color: var(--faint); }

button {
    border: 1px solid transparent;
    border-radius: 5px;
    padding: 8px 13px;
    font: inherit;
    font-size: 12.5px;
    font-weight: 600;
    color: var(--on-accent);
    background: var(--accent);
    cursor: pointer;
    white-space: nowrap;
}
button:hover { filter: brightness(1.08); }
button.ghost { background: transparent; color: var(--muted); border-color: var(--line-2); }
button.ghost:hover { border-color: var(--accent); color: var(--accent); filter: none; }
.detail-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.detail-actions form { display: block; margin-left: auto; }
.button-link {
    display: inline-flex;
    align-items: center;
    border: 1px solid var(--line-2);
    border-radius: 5px;
    padding: 7px 12px;
    color: var(--muted);
    font-size: 12.5px;
    font-weight: 600;
    text-decoration: none;
}
.button-link:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }

.form-card { max-width: 720px; }
.form-card .card-body { display: grid; gap: 14px; }
.reply-form { display: grid; gap: 14px; }
.reply-form input[readonly] { background: var(--raised); color: var(--muted); }
.reply-form textarea { min-height: 180px; line-height: 1.55; }
.reply-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.reply-actions .muted { font-size: 12px; }
.inline-form { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; }
.inline-form label { flex: 1; min-width: 220px; }
.check { display: flex; align-items: center; gap: 9px; }
.check input { min-width: 0; width: auto; }

/* ---------- definition lists ----------------------------------------- */

.facts { display: grid; grid-template-columns: 190px 1fr; gap: 10px 18px; margin: 0; }
.facts dt { color: var(--faint); font-size: 12.5px; }
.facts dd { margin: 0; color: var(--ink); font-size: 12.5px; overflow-wrap: anywhere; }
.attachment-list { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.attachment-list li { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.attachment-list .muted { font: 10.5px/1.4 var(--mono); }

/* ---------- notices -------------------------------------------------- */

.notice, .error {
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 13px;
    border: 1px solid;
}
.notice { color: var(--ok); background: color-mix(in srgb, var(--ok) 8%, transparent); border-color: color-mix(in srgb, var(--ok) 30%, transparent); }
.error { color: var(--crit); background: color-mix(in srgb, var(--crit) 8%, transparent); border-color: color-mix(in srgb, var(--crit) 30%, transparent); }
.empty { padding: 26px 15px; text-align: center; color: var(--faint); font-size: 13px; }

/* ---------- errors breakdown ----------------------------------------- */

.errlist { display: flex; flex-direction: column; gap: 13px; }
.errlist-item { display: flex; flex-direction: column; gap: 5px; }
.errlist-item .top { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.errlist-item .code { font-family: var(--mono); font-size: 11.5px; color: var(--ink); overflow-wrap: anywhere; }
.errlist-item .n { font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
.meter { height: 3px; border-radius: 2px; background: var(--line); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--crit); }

/* ---------- domain health -------------------------------------------- */

.checks { display: flex; flex-direction: column; }
.check-row {
    display: grid;
    grid-template-columns: 92px 1fr auto;
    gap: 14px;
    align-items: center;
    padding: 11px 15px;
    border-bottom: 1px solid var(--line);
}
.check-row:last-child { border-bottom: 0; }
.check-row .name { font-family: var(--mono); font-size: 11.5px; color: var(--ink); letter-spacing: .04em; }
.check-row .detail {
    font-family: var(--mono);
    font-size: 10.5px;
    color: var(--faint);
    overflow-wrap: anywhere;
}
.check-row .advice { font-size: 12px; color: var(--muted); grid-column: 2 / -1; margin-top: 2px; }

/* ---------- message preview ------------------------------------------ */

.preview {
    display: block;
    width: 100%;
    min-height: 520px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
}
.source {
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    margin: 0;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    font: 12px/1.5 var(--mono);
    color: var(--muted);
}
.token-warning {
    padding: 12px 14px;
    border-radius: 6px;
    border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
    background: color-mix(in srgb, var(--warn) 10%, transparent);
    color: var(--warn);
    font-size: 13px;
}
.token-value {
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    margin: 0;
    padding: 15px;
    border-radius: var(--radius);
    background: #10201f;
    color: #eafff7;
    font: 13px/1.5 var(--mono);
}

/* ---------- auth pages ----------------------------------------------- */

.auth {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 24px;
}
.auth-panel {
    width: min(400px, 100%);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 26px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}
.auth-panel .auth-brand { display: flex; align-items: center; gap: 10px; }
.auth-panel .auth-brand img { width: 30px; height: 30px; border-radius: 7px; }
.auth-panel h1 { font-size: 18px; }

/* ---------- responsive ------------------------------------------------ */

@media (max-width: 900px) {
    .shell { grid-template-columns: 1fr; }
    .side {
        flex-direction: row;
        align-items: center;
        gap: 4px;
        overflow-x: auto;
        border-right: 0;
        border-bottom: 1px solid var(--line);
        padding: 10px 12px;
    }
    .side-brand { padding: 0 8px 0 0; }
    .side-brand span { display: none; }
    .side-group { display: none; }
    .side a { white-space: nowrap; }
    .side a .count { margin-left: 6px; }
    .side-foot { margin-top: 0; padding-top: 0; border-top: 0; margin-left: auto; }
    .side-foot button { width: auto; }
    .content { padding: 16px 14px 44px; }
    .pagehead { padding: 14px 14px 0; }
    .statusbar { padding: 8px 14px; }
}

@media (max-width: 680px) {
    .facts { grid-template-columns: 1fr; gap: 4px 0; }
    .facts dd { margin-bottom: 8px; }

    /* Column names must survive the collapse — carried by data-label. */
    table.data thead { display: none; }
    table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
    table.data tr { border-bottom: 1px solid var(--line); padding: 6px 0; }
    table.data tbody tr:last-child { border-bottom: 0; }
    table.data td { border: 0; padding: 4px 14px; display: flex; gap: 12px; align-items: baseline; }
    table.data td::before {
        content: attr(data-label);
        flex: none;
        width: 108px;
        font-family: var(--mono);
        font-size: 9.5px;
        letter-spacing: .09em;
        text-transform: uppercase;
        color: var(--faint);
    }
    table.data td:empty { display: none; }
    table.data .trunc { max-width: none; white-space: normal; }
    .check-row { grid-template-columns: 1fr auto; }
    .check-row .detail { grid-column: 1 / -1; }
    .detail-actions { align-items: stretch; }
    .detail-actions > *, .detail-actions form, .detail-actions button { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
