:root {
    --bg: var(--tg-theme-bg-color, #f2f3f5);
    --text: var(--tg-theme-text-color, #1c1c1e);
    --hint: var(--tg-theme-hint-color, #8a8a8e);
    --card: var(--tg-theme-secondary-bg-color, #ffffff);
    --accent: var(--tg-theme-button-color, #2ea6ff);
    --accent-text: var(--tg-theme-button-text-color, #ffffff);
    --border: rgba(128, 128, 128, 0.18);
    --ok: #2ecc71;
    --warn: #e0a800;
    --err: #ff3b30;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.45;
}

#app {
    max-width: 640px;
    margin: 0 auto;
    padding: 16px 14px calc(78px + env(safe-area-inset-bottom, 0px));
}

.app-header { padding: 8px 4px 4px; }
.title-row { display: flex; align-items: center; gap: 10px; }
.app-header h1 { font-size: 22px; margin: 0; letter-spacing: 0.3px; }
.subtitle { margin: 2px 0 0; color: var(--hint); font-size: 13px; }

.card {
    background: var(--card);
    border-radius: 16px;
    padding: 16px;
    margin-top: 14px;
    border: 1px solid var(--border);
}
.card h2 { font-size: 15px; margin: 0 0 10px; }
.hint { color: var(--hint); font-size: 13px; margin: 0 0 12px; }
.hint.center { text-align: center; margin: 8px 0 0; }
.muted { color: var(--hint); font-weight: 400; }

.screen { animation: fade 0.18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.kv { font-size: 13px; }
.kv-row { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-top: 1px solid var(--border); }
.kv-row:first-child { border-top: none; }
.kv-row > span:last-child { color: var(--hint); text-align: right; }

.link { color: var(--accent); font-size: 13px; font-weight: 600; text-decoration: none; white-space: nowrap; align-self: center; }

.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field .meta { font-size: 11px; color: var(--hint); margin-bottom: 6px; }
.field input, .field textarea {
    width: 100%;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 14px;
    font-family: inherit;
    resize: vertical;
}
.field textarea { min-height: 70px; word-break: break-all; }
.field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); }

.btn {
    -webkit-appearance: none;
    appearance: none;
    border: none;
    border-radius: 12px;
    padding: 12px 16px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    width: 100%;
    transition: opacity 0.15s ease;
}
.btn:active { opacity: 0.7; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.btn[disabled] { opacity: 0.5; cursor: default; }

#screen-settings .btn-primary { margin-top: 4px; }

.badge {
    font-size: 12px;
    font-weight: 600;
    padding: 3px 9px;
    border-radius: 999px;
}
.badge-ok { background: rgba(46, 204, 113, 0.15); color: var(--ok); }
.badge-err { background: rgba(255, 59, 48, 0.15); color: var(--err); }
.badge-muted { background: rgba(138, 138, 142, 0.15); color: var(--hint); }

.result {
    margin-top: 12px;
    padding: 12px;
    border-radius: 10px;
    background: var(--bg);
    font-size: 13px;
}
.result.ok { border-left: 3px solid var(--ok); }
.result.err { border-left: 3px solid var(--err); }
.result table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.result td { padding: 3px 4px; border-top: 1px solid var(--border); vertical-align: top; }

.exports { margin-bottom: 14px; }
.export-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 0;
    border-top: 1px solid var(--border);
    font-size: 13px;
}
.export-row:first-child { border-top: none; }
.export-row .meta { color: var(--hint); font-size: 12px; }

.actions-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.actions-grid .btn { padding: 10px; font-size: 13px; }

.jobs { font-size: 13px; }
.job-row { padding: 8px 0; border-top: 1px solid var(--border); }
.job-row:first-child { border-top: none; }
.job-row .job-head { display: flex; justify-content: space-between; gap: 8px; }
.job-row .job-name { font-weight: 600; }
.job-row .job-meta { color: var(--hint); font-size: 12px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.dot.success { background: var(--ok); }
.dot.failed { background: var(--err); }
.dot.running { background: var(--warn); }
.dot.skipped { background: var(--hint); }

.gate { text-align: center; padding: 60px 20px; color: var(--hint); }
.gate-icon { font-size: 42px; margin-bottom: 12px; }

/* Full-width native toast with countdown */
.toast {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 60;
    background: var(--card);
    color: var(--text);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.16);
    padding-top: env(safe-area-inset-top, 0px);
    overflow: hidden;
    animation: toastIn 0.22s ease;
}
@keyframes toastIn { from { transform: translateY(-100%); } to { transform: none; } }
.toast-row { display: flex; align-items: center; gap: 10px; padding: 12px 14px; }
.toast-ic {
    flex: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 700;
    color: #fff;
    background: var(--hint);
}
.toast-success .toast-ic { background: var(--ok); }
.toast-error .toast-ic { background: var(--err); }
.toast-info .toast-ic { background: var(--accent); }
.toast-text { flex: 1; font-size: 14px; line-height: 1.35; }
.toast-x {
    flex: none;
    background: none;
    border: none;
    color: var(--hint);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    padding: 0 4px;
}
.toast-progress { height: 3px; background: rgba(128, 128, 128, 0.15); }
.toast-progress i { display: block; height: 100%; width: 100%; transform-origin: left center; }
.toast-success .toast-progress i { background: var(--ok); }
.toast-error .toast-progress i { background: var(--err); }
.toast-info .toast-progress i { background: var(--accent); }
@keyframes toastbar { from { transform: scaleX(1); } to { transform: scaleX(0); } }

/* Toggle switch */
.sw { position: relative; display: inline-block; width: 46px; height: 28px; flex: none; }
.sw input { position: absolute; opacity: 0; width: 0; height: 0; }
.sw-track {
    position: absolute;
    inset: 0;
    background: rgba(128, 128, 128, 0.3);
    border-radius: 999px;
    transition: background 0.18s ease;
}
.sw-track::before {
    content: "";
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.18s ease;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.sw input:checked + .sw-track { background: var(--ok); }
.sw input:checked + .sw-track::before { transform: translateX(18px); }

/* People list (recipients + access) */
.people { display: flex; flex-direction: column; }
.person {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 0;
    border-top: 1px solid var(--border);
}
.person:first-child { border-top: none; }
.person-info { min-width: 0; }
.person-name { font-weight: 600; font-size: 14px; word-break: break-word; }

.person-block { padding: 12px 0; border-top: 1px solid var(--border); }
.person-block:first-child { border-top: none; }
.owner-tag { margin-top: 6px; font-size: 12px; color: var(--ok); font-weight: 600; }
.req-badge {
    display: inline-block;
    margin-top: 6px;
    font-size: 11px;
    font-weight: 600;
    color: var(--warn);
    background: rgba(224, 168, 0, 0.15);
    padding: 2px 8px;
    border-radius: 999px;
}

/* Permission chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
    display: inline-flex;
    align-items: center;
    font-size: 12px;
    font-weight: 600;
    padding: 6px 11px;
    border-radius: 999px;
    border: 1px solid var(--border);
    color: var(--hint);
    cursor: pointer;
    user-select: none;
    transition: all 0.15s ease;
}
.chip input { display: none; }
.chip.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.ok-check { color: var(--ok); font-weight: 700; }
.export-name { font-weight: 600; }
.rep-table td:nth-child(2) { text-align: center; width: 28px; }
.rep-table td:nth-child(3) { color: var(--hint); white-space: nowrap; text-align: right; }

/* Icon action buttons */
.row-actions { display: flex; gap: 6px; align-items: center; flex: none; }
.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--accent);
    cursor: pointer;
    padding: 0;
    transition: background 0.15s ease;
}
.icon-btn:active { background: rgba(46, 166, 255, 0.12); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

/* Expandable job rows */
.job-row { cursor: pointer; }
.job-row .job-head .chev { width: 16px; height: 16px; fill: none; stroke: var(--hint); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; transition: transform 0.18s ease; }
.job-row.open .job-head .chev { transform: rotate(180deg); }
.job-sub { font-size: 12px; color: var(--hint); margin-top: 2px; }
.st-success { color: var(--ok); font-weight: 600; }
.st-failed { color: var(--err); font-weight: 600; }
.st-skipped { color: var(--hint); font-weight: 600; }
.st-running { color: var(--warn); font-weight: 600; }
.job-details { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--border); }
.job-msg {
    margin-top: 8px;
    background: var(--bg);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 12px;
    line-height: 1.4;
    white-space: pre-wrap;
    word-break: break-word;
}

.btn-sm { padding: 8px 12px; font-size: 13px; }
.owner-btn { margin-top: 10px; }
.danger-btn { color: var(--err); border-color: rgba(255, 59, 48, 0.45); margin-top: 8px; }

.select {
    width: 100%;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 14px;
    font-family: inherit;
}
.csv-auth-field { margin-top: 14px; }

.ip-list { margin-bottom: 10px; }
.ip-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin-bottom: 8px;
}
.ip-main { flex: 1; min-width: 0; }
.ip-meta {
    font-size: 12px;
    color: var(--muted);
    margin-top: 4px;
    line-height: 1.35;
}
.ip-value {
    font-size: 15px;
    font-weight: 600;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    word-break: break-all;
}
.ip-add-row {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-top: 10px;
}
.ip-new-input {
    flex: 1;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 14px;
    font-family: inherit;
}
.icon-btn-danger { color: var(--err); border-color: rgba(255, 59, 48, 0.35); }
.schedule-jobs { margin: 12px 0; }

.job-log-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}
.job-date-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--muted);
}
.input-date {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 10px;
    font-size: 14px;
    font-family: inherit;
}

.server-disk { margin-bottom: 4px; }
.server-storage { display: flex; flex-direction: column; gap: 16px; }
.storage-block {
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}
.storage-block:last-child { border-bottom: none; padding-bottom: 0; }
.storage-title { font-weight: 600; margin-bottom: 8px; }
.storage-slider-label {
    display: block;
    font-size: 13px;
    color: var(--muted);
    margin: 10px 0 6px;
}
.storage-slider {
    width: 100%;
    accent-color: var(--accent);
}
.storage-stats { margin: 10px 0; }
.storage-apply-btn { margin-top: 8px; }
.ok-note { color: var(--ok); margin-top: 8px; }
.disk-row { margin-bottom: 4px; }
.disk-head {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    margin-bottom: 6px;
}
.disk-pct { font-weight: 600; color: var(--accent); }
.disk-track {
    height: 8px;
    background: var(--border);
    border-radius: 4px;
    overflow: hidden;
}
.disk-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), #5ac8fa);
    border-radius: 4px;
}
.line-chart,
.line-chart-host {
    width: 100%;
    margin-top: 8px;
}
.line-chart-box {
    display: flex;
    gap: 4px;
    align-items: flex-start;
}
.line-chart-yaxis {
    width: 26px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 88px;
    font-size: 10px;
    line-height: 1;
    color: var(--muted);
    text-align: right;
    padding-right: 3px;
    user-select: none;
}
.line-chart-yaxis span::after {
    content: "%";
    font-size: 9px;
    opacity: 0.75;
}
.line-chart-plot {
    flex: 1;
    min-width: 0;
    position: relative;
    touch-action: none;
}
.line-chart-svg {
    width: 100%;
    height: 88px;
    display: block;
    overflow: visible;
}
.line-chart-xaxis {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    line-height: 1.2;
    color: var(--muted);
    margin-top: 3px;
    padding: 0 1px;
    user-select: none;
}
.line-chart-xaxis span::after {
    content: "ч";
    font-size: 9px;
    opacity: 0.7;
}
.line-chart-grid {
    stroke: var(--border);
    stroke-width: 1;
    stroke-dasharray: 3 3;
    opacity: 0.55;
}
.line-chart-area {
    fill: rgba(0, 122, 255, 0.12);
    stroke: none;
}
.line-chart-line {
    fill: none;
    stroke: var(--chart-stroke, var(--accent));
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
}
.line-chart-cross {
    stroke-width: 1;
    stroke-dasharray: 4 3;
    opacity: 0.85;
}
.line-chart-dot {
    stroke: #fff;
    stroke-width: 1.5;
}
.line-chart-tip {
    position: absolute;
    z-index: 3;
    transform: translate(-50%, calc(-100% - 6px));
    pointer-events: none;
    background: rgba(30, 30, 30, 0.92);
    color: #fff;
    font-size: 11px;
    line-height: 1.3;
    padding: 4px 8px;
    border-radius: 6px;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.line-chart-tip.hidden {
    display: none;
}
.metric-legend { margin-top: 8px; }

.tab.hidden { display: none !important; }

/* Bottom tab bar */
.tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    background: var(--card);
    border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    z-index: 40;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
}
.tab {
    flex: 1;
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 6px 0;
    color: var(--hint);
    font-size: 11px;
    font-weight: 600;
    transition: color 0.15s ease;
}
.tab svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.9;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.tab.active { color: var(--accent); }
.tab:active { opacity: 0.6; }

/* Registration */
.register { padding: 24px 8px; }
.register-card { text-align: center; }
.register-icon { font-size: 46px; margin-bottom: 6px; }
.register-card h2 { font-size: 18px; margin: 0 0 8px; }
.register-card .hint { text-align: center; margin-bottom: 16px; }
.register-user {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 16px;
    font-size: 14px;
}
.reg-name { font-weight: 600; }
.register-error, #register-error { text-align: left; margin-top: 12px; }

/* Profile */
.profile { font-size: 13px; }
.profile .reg-name { margin-bottom: 8px; font-size: 15px; }

/* Audit history */
.audit { font-size: 13px; }
.audit-row { padding: 10px 0; border-top: 1px solid var(--border); }
.audit-row:first-child { border-top: none; }
.audit-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.audit-key { font-weight: 600; }
.audit-diff { margin-top: 4px; font-size: 12px; word-break: break-all; }
.audit-diff .old { color: var(--hint); text-decoration: line-through; }
.audit-diff .new { color: var(--text); }

.hidden { display: none !important; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
