:root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #f0f1f4;
    --text: #1d2026;
    --muted: #677080;
    --border: #dfe2e8;
    --accent: #c62828;
    --accent-text: #ffffff;
    --link: #1f5fbf;
    --ok: #1b7f3b;      --ok-bg: #e3f4e8;
    --warn: #8a5a00;    --warn-bg: #fff1d6;
    --bad: #b3261e;     --bad-bg: #fde4e1;
    --idle: #2a5ea8;    --idle-bg: #e4edfa;
    --off: #5d6470;     --off-bg: #eceef1;
    --log-bg: #15171c;  --log-text: #d7dae0;
    --radius: 10px;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111317;
        --surface: #1a1d23;
        --surface-2: #22262e;
        --text: #e6e8ec;
        --muted: #9199a6;
        --border: #2d323b;
        --link: #7fb0ff;
        --ok: #6fd18f;      --ok-bg: #173323;
        --warn: #f0c060;    --warn-bg: #3a2e12;
        --bad: #ff8a80;     --bad-bg: #3d1a1a;
        --idle: #8fb8ff;    --idle-bg: #1b2a44;
        --off: #a3aab5;     --off-bg: #262a31;
        --log-bg: #0c0d10;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre { font-family: var(--mono); font-size: 13px; }
code { white-space: nowrap; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 17px; margin: 0 0 10px; }
h3 { font-size: 15px; margin: 16px 0 6px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.good { color: var(--ok); }
.error { color: var(--bad); }
.nowrap { white-space: nowrap; }
.wrap { word-break: break-word; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ layout */

.topbar {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 10px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); }
.logo {
    width: 22px; height: 22px; border-radius: 5px; background: var(--accent); display: inline-block; position: relative;
}
.logo::before, .logo::after { content: ""; position: absolute; background: #fff; }
.logo::before { left: 9px; top: 4px; width: 4px; height: 14px; }
.logo::after { left: 4px; top: 9px; width: 14px; height: 4px; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a { padding: 6px 10px; border-radius: 6px; color: var(--muted); white-space: nowrap; }
.topbar nav a.active, .topbar nav a:hover { color: var(--text); background: var(--surface-2); text-decoration: none; }
.who { display: flex; align-items: center; gap: 12px; }
.who form { margin: 0; }

main { max-width: 1280px; margin: 0 auto; padding: 20px; }
main.center { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; }

.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head .back { color: var(--muted); margin-right: 4px; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 18px;
    margin-bottom: 16px;
}
.card.narrow { max-width: 480px; }
.card:target { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; }
.grid2 .card { margin-bottom: 0; }
.grid2 + .card, .grid2 + section { margin-top: 16px; }

/* ------------------------------------------------- таблица ботов (Обзор) */

.jobs-card { padding: 0; }
/* Фиксированная раскладка: ширина колонок задаётся заголовками и не зависит от текста
   в ячейках — таблица не «дышит», когда при автообновлении меняются статусы. */
table.jobs { font-size: 14px; table-layout: fixed; width: 100%; }
table.jobs .h-name { width: 210px; }
table.jobs .h-status { width: 230px; }
table.jobs .h-cron { width: 150px; }
table.jobs .h-actions { width: 160px; }
table.jobs th { padding: 10px 12px; background: var(--surface-2); border-bottom: 1px solid var(--border); }
table.jobs th:first-child { border-top-left-radius: var(--radius); }
table.jobs th:last-child { border-top-right-radius: var(--radius); }
table.jobs td { padding: 12px; vertical-align: middle; }
table.jobs tbody tr:last-child td { border-bottom: 0; }
.actions-col { text-align: right; }

.job-row { cursor: pointer; }
.job-row:hover td, .job-row:focus-within td { background: var(--surface-2); }
.job-row td:first-child { box-shadow: inset 4px 0 0 var(--border); }
.job-row.st-ok td:first-child { box-shadow: inset 4px 0 0 var(--ok); }
.job-row.st-warn td:first-child { box-shadow: inset 4px 0 0 var(--warn); }
.job-row.st-bad td:first-child { box-shadow: inset 4px 0 0 var(--bad); }
.job-row.st-idle td:first-child { box-shadow: inset 4px 0 0 var(--idle); }
.c-name { padding-left: 18px !important; overflow-wrap: anywhere; }
.job-name { font-weight: 600; color: var(--text); font-size: 15px; }
.c-desc { color: var(--muted); overflow-wrap: anywhere; }
.c-status { overflow-wrap: anywhere; }
.c-status .pill + div { margin-top: 4px; }
.c-cron { white-space: nowrap; }
/* длинное выражение обрезается многоточием (целиком — в подсказке и в карточке бота) */
.c-cron .cron { max-width: 100%; vertical-align: middle; }
.c-cron .cron code { display: block; overflow: hidden; text-overflow: ellipsis; }
.c-actions { text-align: right; white-space: nowrap; }
.row-actions { display: inline-flex; align-items: center; gap: 8px; }
.row-actions form.inline { display: inline-flex; }
.c-actions .row-actions form.inline + form.inline { margin: 0; } /* отступы задаёт gap */
.btn.sm { padding: 4px 10px; font-size: 13px; border-radius: 6px; }

/* подсказка к cron: при наведении и при фокусе с клавиатуры */
.cron { display: inline-block; cursor: help; border-bottom: 1px dotted var(--muted); }
.cron, .tip { position: relative; }
.cron::after, .tip::after {
    content: attr(data-tip);
    position: absolute;
    left: 0;
    bottom: calc(100% + 8px);
    z-index: 10;
    width: max-content;
    max-width: 280px;
    padding: 6px 10px;
    border-radius: 6px;
    background: var(--text);
    color: var(--surface);
    font-size: 12.5px;
    line-height: 1.4;
    white-space: normal;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .2);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}
.cron:hover::after, .cron:focus-visible::after,
.tip:hover::after, .tip:focus-visible::after { opacity: 1; visibility: visible; }
/* у кнопок справа подсказка выравнивается по правому краю, чтобы не вылезать за экран */
.c-actions .tip::after { left: auto; right: 0; }
.cron:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
.cron-desc { display: none; }

.job-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.job-head h2 { margin: 0; }
.job-head h2 a { color: var(--text); }
.job p.small { margin: 4px 0 10px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 10px 0 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

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

.pill {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 999px;
    font-size: 12.5px;
    font-weight: 600;
    white-space: nowrap;
    background: var(--off-bg);
    color: var(--off);
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.idle { background: var(--idle-bg); color: var(--idle); }

.scheduler { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 13px; }

/* ----------------------------------------------------------- buttons */

.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
form.inline { display: inline; margin: 0; }

.btn {
    display: inline-flex;
    align-items: center;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
}
.btn:hover { border-color: var(--muted); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn:disabled { opacity: .6; cursor: wait; }
button.link { background: none; border: 0; padding: 0; color: var(--link); font: inherit; cursor: pointer; }

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

label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
input, select {
    font: inherit;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: var(--surface);
    color: var(--text);
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--link); outline-offset: 1px; }
.stack { display: flex; flex-direction: column; gap: 12px; max-width: 420px; }
.schedule-form { display: flex; flex-direction: column; gap: 8px; }
.schedule-form input[name=expr] { font-family: var(--mono); max-width: 320px; }
.cron-preview { min-height: 1.4em; }
.cron-preview.bad { color: var(--bad); }
.help { margin-top: 12px; }
.help summary, details summary { cursor: pointer; color: var(--muted); }
.inline-h { display: inline; }

.login { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }
.login-head { display: flex; align-items: center; gap: 10px; }
.login-head h1 { font-size: 19px; }
.login .btn { justify-content: center; padding: 9px; }

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; border: 1px solid transparent; }
.flash-ok { background: var(--ok-bg); color: var(--ok); }
.flash-error { background: var(--bad-bg); color: var(--bad); }
.flash-info { background: var(--idle-bg); color: var(--idle); }
main.center .flash { width: 100%; max-width: 360px; }

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

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 13px; }
table.plain td { border: 0; padding: 3px 10px 3px 0; }
tr.row-bad td { background: var(--bad-bg); }
.decision input { width: 110px; padding: 5px 8px; }
td form.inline + form.inline { margin-left: 6px; }

/* ------------------------------------------------------------- users */

table.users td { vertical-align: middle; }
tr.row-off td:not(.actions-cell) { opacity: .6; }
.actions-cell { white-space: nowrap; }
.actions-cell form.inline + form.inline { margin-left: 6px; }
.role-form select { padding: 4px 8px; }
.create-user { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.create-user input { width: 220px; }
.issued { border-color: var(--warn); background: var(--warn-bg); }
code.secret {
    font-size: 18px; padding: 6px 12px; border-radius: 6px; background: var(--surface);
    border: 1px dashed var(--warn); user-select: all; letter-spacing: .5px;
}

/* -------------------------------------------------------------- logs */

.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tab { padding: 6px 12px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.tab.active { border-color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); }
.log-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.log-controls .grow { flex: 1; min-width: 180px; }
.log-controls .check { flex-direction: row; align-items: center; gap: 6px; padding-bottom: 8px; }
.log { background: var(--log-bg); border-color: var(--log-bg); padding: 0; }
.log-lines {
    margin: 0;
    padding: 12px 14px;
    color: var(--log-text);
    max-height: 70vh;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-all;
    line-height: 1.5;
}
.log-empty { padding: 12px 14px; margin: 0; }
.ln.warning { color: #f3c969; }
.ln.error { color: #ff8a80; }
.ln.debug { color: #7d8590; }
.ln.marker { color: #79b8ff; }
pre.json { background: var(--surface-2); padding: 12px; border-radius: 8px; overflow: auto; max-height: 500px; }

/* ------------------------------------------------------------ mobile */

/* средние экраны: описание уходит под имя, чтобы таблица не ломалась */
@media (max-width: 1100px) {
    table.jobs th:nth-child(2), table.jobs td.c-desc { display: none; }
    table.jobs .h-name { width: auto; }
}

/* телефоны: каждая строка — компактный блок */
@media (max-width: 760px) {
    table.jobs thead { display: none; }
    table.jobs, table.jobs tbody { display: block; }
    table.jobs tr.job-row {
        display: grid;
        grid-template-columns: 1fr auto;
        grid-template-areas: "name status" "desc desc" "cron cron" "actions actions";
        gap: 6px 12px;
        padding: 14px 16px 14px 18px;
        border-bottom: 1px solid var(--border);
        box-shadow: inset 4px 0 0 var(--border);
    }
    table.jobs tr.job-row.st-ok { box-shadow: inset 4px 0 0 var(--ok); }
    table.jobs tr.job-row.st-warn { box-shadow: inset 4px 0 0 var(--warn); }
    table.jobs tr.job-row.st-bad { box-shadow: inset 4px 0 0 var(--bad); }
    table.jobs tr.job-row.st-idle { box-shadow: inset 4px 0 0 var(--idle); }
    table.jobs tr.job-row:last-child { border-bottom: 0; }
    table.jobs td { display: block; padding: 0 !important; border: 0; background: none !important; box-shadow: none !important; }
    .c-name { grid-area: name; }
    .c-status { grid-area: status; text-align: right; white-space: normal; }
    table.jobs td.c-desc { grid-area: desc; display: block; min-width: 0; }
    .c-cron { grid-area: cron; white-space: normal; }
    .c-cron .cron { border: 0; }
    .c-cron .cron::after { display: none; }
    .cron-desc { display: inline; margin-left: 6px; }
    .c-actions { grid-area: actions; text-align: left; white-space: normal; padding-top: 4px !important; }
    .c-actions .tip::after { display: none; }
}

@media (max-width: 720px) {
    .topbar { flex-wrap: wrap; gap: 8px 16px; padding: 10px 16px; }
    .topbar nav { order: 3; flex-basis: 100%; overflow-x: auto; }
    main { padding: 16px; }
    .grid2 { grid-template-columns: 1fr; }
    .kv { grid-template-columns: 1fr; gap: 0; }
    .kv dd { margin-bottom: 6px; }
}

/* раздел внутри страницы бота (например, розыгрыш у weeklyadmin) */
.section-block { margin: 24px 0 16px; }
.section-title { font-size: 18px; margin: 0 0 12px; }
.section-block > .grid2 + .card { margin-top: 16px; }
/* переход по якорю (#weekly, #schedule) не прячет заголовок под липкой шапкой */
[id] { scroll-margin-top: 72px; }

/* ------------------------------------------- кнопки-иконки и переключатель */

.icon-btn {
    width: 34px;
    height: 34px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text);
    cursor: pointer;
    flex: none;
}
.icon-btn:hover { border-color: var(--muted); text-decoration: none; }
.icon-btn:focus-visible { outline: 2px solid var(--link); outline-offset: 1px; }
.icon-btn.play { color: var(--ok); border-color: var(--ok); background: var(--ok-bg); }
.icon-btn.off { color: var(--muted); opacity: .45; cursor: not-allowed; }
.icon-btn.busy { color: var(--warn); border-color: var(--warn); background: var(--warn-bg); cursor: progress; }
.icon-btn.busy svg { animation: spin 1.1s linear infinite; }
.icon-btn:disabled { cursor: not-allowed; }
.icon-btn.busy:disabled { cursor: progress; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .icon-btn.busy svg { animation: none; } }

.switch {
    width: 44px;
    height: 24px;
    padding: 0;
    flex: none;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--off-bg);
    cursor: pointer;
    transition: background .15s, border-color .15s;
}
.switch::before {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
    transition: transform .15s;
}
.switch[aria-checked="true"] { background: var(--ok); border-color: var(--ok); }
.switch[aria-checked="true"]::before { transform: translateX(20px); }
.switch:hover { filter: brightness(1.08); }
.switch:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .switch, .switch::before { transition: none; } }
