/* Дизайн-система Mini App.
   Цвета берутся из темы Telegram, поэтому светлая и тёмная схемы получаются
   из одного набора правил, а не из двух разных вёрсток. */

:root {
    /* Палитра: значения Telegram с запасными вариантами для браузера */
    --bg: var(--tg-theme-bg-color, #ffffff);
    --bg-secondary: var(--tg-theme-secondary-bg-color, #f2f2f7);
    --surface: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
    --text: var(--tg-theme-text-color, #0f0f0f);
    --text-muted: var(--tg-theme-hint-color, #8a8a8e);
    --accent: var(--tg-theme-button-color, #2f8ee0);
    --accent-text: var(--tg-theme-button-text-color, #ffffff);
    --link: var(--tg-theme-link-color, #2f8ee0);
    --danger: var(--tg-theme-destructive-text-color, #e5484d);
    --success: #2ea043;
    --warning: #d98c00;
    --separator: color-mix(in srgb, var(--text-muted) 24%, transparent);

    /* Шкала отступов: только эти значения, без произвольных чисел */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 28px;

    --radius-sm: 8px;
    --radius-md: 10px;
    --radius-lg: 14px;

    --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
    --text-lg: 20px;
    --text-md: 16px;
    --text-sm: 14px;
    --text-xs: 12px;

    --tap-target: 44px;
    --transition: 140ms ease;

    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --tabbar-height: 56px;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow-x: hidden; /* горизонтального скролла быть не должно ни на одном экране */
}

body {
    font-family: var(--font);
    font-size: var(--text-md);
    line-height: 1.4;
    color: var(--text);
    background: var(--bg-secondary);
    -webkit-font-smoothing: antialiased;
    overscroll-behavior-y: contain;
}

button, input, textarea, select {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

/* ------------------------------------------------------------------ */
/* Каркас                                                              */
/* ------------------------------------------------------------------ */
.app {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    padding-top: var(--safe-top);
}

.screen {
    flex: 1;
    padding: var(--space-3) var(--space-4);
    padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-5));
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
    animation: screen-in 160ms ease-out;
}

@keyframes screen-in {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: none; }
}

.screen-title {
    font-size: var(--text-lg);
    font-weight: 600;
    margin: var(--space-2) 0 var(--space-4);
    letter-spacing: -0.2px;
}

.screen-subtitle {
    color: var(--text-muted);
    font-size: var(--text-sm);
    margin: calc(var(--space-3) * -1) 0 var(--space-4);
}

/* ------------------------------------------------------------------ */
/* Секции и строки списка                                              */
/* ------------------------------------------------------------------ */
.section {
    background: var(--surface);
    border-radius: var(--radius-md);
    overflow: hidden;
    margin-bottom: var(--space-4);
}

.section-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-2);
    padding: 0 var(--space-1) var(--space-2);
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    color: var(--text-muted);
}

.row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    min-height: var(--tap-target);
    padding: var(--space-3) var(--space-4);
    background: none;
    border: none;
    text-align: left;
    cursor: pointer;
    transition: background var(--transition);
}

.row + .row {
    border-top: 1px solid var(--separator);
}

.row:active {
    background: color-mix(in srgb, var(--text-muted) 12%, transparent);
}

.row[disabled] {
    opacity: 0.5;
    cursor: default;
}

.row-body {
    flex: 1;
    min-width: 0; /* иначе длинные названия ломают строку */
}

.row-title {
    font-size: var(--text-md);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.row-sub {
    font-size: var(--text-sm);
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.row-aside {
    color: var(--text-muted);
    font-size: var(--text-sm);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    max-width: 55%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Строка без перехода: показывает значение, но никуда не ведёт. */
.row.is-static {
    cursor: default;
}

.row.is-static:active {
    background: none;
}

/* Подпись, которой нужно несколько строк (подсказки в переключателях). */
.row-sub.is-wrap {
    white-space: normal;
    overflow: visible;
}

/* Действие внутри строки списка: «Убрать» у получателя. */
.row-action {
    flex-shrink: 0;
    min-height: var(--tap-target);
    padding: 0 var(--space-2);
    border: none;
    background: none;
    color: var(--danger);
    font-size: var(--text-sm);
    cursor: pointer;
}

.chevron {
    width: 8px;
    height: 8px;
    border-right: 2px solid var(--text-muted);
    border-bottom: 2px solid var(--text-muted);
    transform: rotate(-45deg);
    opacity: 0.6;
    flex-shrink: 0;
}

/* ------------------------------------------------------------------ */
/* Метрики                                                             */
/* ------------------------------------------------------------------ */
.metrics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
    gap: var(--space-2);
    margin-bottom: var(--space-4);
}

.metric {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    min-width: 0;
}

.metric-value {
    font-size: var(--text-lg);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

.metric-label {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.metric-value.is-danger { color: var(--danger); }
.metric-value.is-success { color: var(--success); }

/* ------------------------------------------------------------------ */
/* Значки состояния                                                    */
/* ------------------------------------------------------------------ */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: var(--text-xs);
    font-weight: 500;
    padding: 3px 8px;
    border-radius: 100px;
    background: color-mix(in srgb, var(--text-muted) 16%, transparent);
    color: var(--text-muted);
    white-space: nowrap;
}

.badge.is-live {
    background: color-mix(in srgb, var(--success) 16%, transparent);
    color: var(--success);
}

.badge.is-off {
    background: color-mix(in srgb, var(--text-muted) 14%, transparent);
}

.badge.is-error {
    background: color-mix(in srgb, var(--danger) 14%, transparent);
    color: var(--danger);
}

.dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    flex-shrink: 0;
}

.badge.is-live .dot {
    animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.35; }
}

/* ------------------------------------------------------------------ */
/* Кнопки                                                              */
/* ------------------------------------------------------------------ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--tap-target);
    padding: 0 var(--space-4);
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: var(--accent-text);
    font-size: var(--text-md);
    font-weight: 500;
    cursor: pointer;
    transition: opacity var(--transition), transform var(--transition);
    width: 100%;
}

.btn:active { transform: scale(0.985); }
.btn[disabled] { opacity: 0.5; cursor: default; transform: none; }

.btn.is-secondary {
    background: var(--surface);
    color: var(--text);
}

.btn.is-danger {
    background: color-mix(in srgb, var(--danger) 14%, transparent);
    color: var(--danger);
}

.btn-row {
    display: flex;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
}

/* ------------------------------------------------------------------ */
/* Формы                                                               */
/* ------------------------------------------------------------------ */
.field {
    padding: var(--space-3) var(--space-4);
}

.field + .field { border-top: 1px solid var(--separator); }

.field-label {
    display: block;
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-bottom: var(--space-1);
}

.input, .textarea {
    width: 100%;
    min-height: var(--tap-target);
    padding: var(--space-2) 0;
    border: none;
    background: none;
    outline: none;
    font-size: var(--text-md);
}

.textarea {
    min-height: 108px;
    resize: vertical;
    line-height: 1.45;
    font-family: inherit;
}

.input:focus-visible, .textarea:focus-visible {
    outline: none;
}

.field:focus-within .field-label { color: var(--link); }

.field-error {
    color: var(--danger);
    font-size: var(--text-sm);
    margin-top: var(--space-1);
}

.switch {
    position: relative;
    width: 48px;
    height: 28px;
    flex-shrink: 0;
    border-radius: 100px;
    background: color-mix(in srgb, var(--text-muted) 32%, transparent);
    border: none;
    cursor: pointer;
    transition: background var(--transition);
    padding: 0;
}

/* Сам переключатель невысокий, но попасть по нему пальцем нужно уверенно:
   невидимая зона расширяет область нажатия до комфортных 44px. */
.switch::before {
    content: "";
    position: absolute;
    top: -8px;
    bottom: -8px;
    left: -8px;
    right: -8px;
}

.switch::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #fff;
    transition: transform var(--transition);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }

.segmented {
    display: flex;
    gap: 2px;
    padding: 2px;
    background: color-mix(in srgb, var(--text-muted) 12%, transparent);
    border-radius: var(--radius-sm);
    margin: var(--space-1) 0;
}

.segmented button {
    flex: 1;
    min-height: 36px;
    border: none;
    border-radius: 6px;
    background: none;
    font-size: var(--text-sm);
    cursor: pointer;
    transition: background var(--transition);
    padding: 0 var(--space-1);
}

.segmented button[aria-pressed="true"] {
    background: var(--surface);
    font-weight: 500;
}

/* ------------------------------------------------------------------ */
/* Состояния: загрузка, пусто, ошибка                                  */
/* ------------------------------------------------------------------ */
.state {
    text-align: center;
    padding: var(--space-6) var(--space-4);
    color: var(--text-muted);
}

.state-title {
    color: var(--text);
    font-weight: 500;
    margin-bottom: var(--space-1);
}

.state-text {
    font-size: var(--text-sm);
    margin-bottom: var(--space-4);
}

.state .btn { width: auto; min-width: 160px; margin: 0 auto; }

.skeleton {
    background: color-mix(in srgb, var(--text-muted) 14%, transparent);
    border-radius: var(--radius-sm);
    animation: shimmer 1.1s ease-in-out infinite;
}

.skeleton-row {
    height: 56px;
    margin-bottom: 1px;
}

.skeleton-title {
    height: 26px;
    width: 45%;
    margin: var(--space-2) 0 var(--space-4);
}

/* Пояснение под секцией: почему так и что будет дальше. */
.note {
    color: var(--text-muted);
    font-size: var(--text-sm);
    line-height: 1.45;
    padding: 0 var(--space-1);
    margin: calc(var(--space-2) * -1) 0 var(--space-4);
}

.field-hint {
    color: var(--text-muted);
    font-size: var(--text-xs);
    margin-top: var(--space-1);
}

/* Заметное сообщение: что мешает запуску или какая была ошибка. */
.callout {
    border-radius: var(--radius-md);
    padding: var(--space-3) var(--space-4);
    margin-bottom: var(--space-4);
    background: var(--surface);
    border-left: 3px solid var(--text-muted);
}

.callout.is-warning {
    border-left-color: var(--warning);
}

.callout.is-error {
    border-left-color: var(--danger);
}

.callout-title {
    font-size: var(--text-sm);
    font-weight: 500;
    margin-bottom: var(--space-1);
}

.callout.is-error .callout-title {
    color: var(--danger);
}

.callout-text {
    font-size: var(--text-sm);
    color: var(--text-muted);
    word-break: break-word;
}

.callout-list {
    margin: 0;
    padding-left: var(--space-4);
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.callout-list li + li {
    margin-top: 2px;
}

@keyframes shimmer {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid color-mix(in srgb, currentColor 30%, transparent);
    border-top-color: currentColor;
    border-radius: 50%;
    animation: spin 700ms linear infinite;
    display: inline-block;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------------ */
/* Прогресс                                                            */
/* ------------------------------------------------------------------ */
.progress {
    height: 3px;
    border-radius: 100px;
    background: color-mix(in srgb, var(--text-muted) 20%, transparent);
    overflow: hidden;
    margin-top: var(--space-2);
}

.progress-bar {
    height: 100%;
    background: var(--accent);
    border-radius: inherit;
    transition: width 300ms ease;
}

/* ------------------------------------------------------------------ */
/* Логи                                                                */
/* ------------------------------------------------------------------ */
.logs {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    font-size: var(--text-xs);
    line-height: 1.6;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    max-height: 60vh;
    overflow-y: auto;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.log-line {
    white-space: pre-wrap;
    word-break: break-word;
    padding: 2px 0;
}

.log-line.is-error { color: var(--danger); }

/* ------------------------------------------------------------------ */
/* Нижняя навигация                                                    */
/* ------------------------------------------------------------------ */
.tabbar {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0;
    width: 100%;
    /* На широком экране Telegram Desktop навигация держится той же колонки,
       что и контент, иначе кнопки разъезжаются по краям монитора. */
    max-width: 720px;
    display: flex;
    background: var(--surface);
    border-top: 1px solid var(--separator);
    padding-bottom: var(--safe-bottom);
    z-index: 10;
}

.tab {
    flex: 1;
    min-height: var(--tabbar-height);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    border: none;
    background: none;
    color: var(--text-muted);
    font-size: 11px;
    cursor: pointer;
    transition: color var(--transition);
    padding: var(--space-1) 2px;
}

.tab[aria-selected="true"] { color: var(--accent); }
.tab svg { width: 22px; height: 22px; }

/* ------------------------------------------------------------------ */
/* Всплывающее уведомление                                             */
/* ------------------------------------------------------------------ */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-3));
    transform: translateX(-50%) translateY(8px);
    max-width: calc(100vw - var(--space-5) * 2);
    background: var(--text);
    color: var(--bg);
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition), transform var(--transition);
    z-index: 20;
}

.toast.is-visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.is-error { background: var(--danger); color: #fff; }

/* ------------------------------------------------------------------ */
/* Адаптив                                                             */
/* ------------------------------------------------------------------ */
@media (max-width: 359px) {
    /* iPhone SE и подобные: ужимаем, но не ломаем сетку */
    :root { --space-4: 12px; --text-lg: 18px; }
    .metrics { grid-template-columns: repeat(2, 1fr); }
    .tab { font-size: 10px; }
}

@media (min-width: 720px) {
    .screen { padding-left: var(--space-5); padding-right: var(--space-5); }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ------------------------------------------------------------------ */
/* Выбор чата-источника                                                */
/* ------------------------------------------------------------------ */
.row-avatar {
    width: 34px;
    height: 34px;
    flex-shrink: 0;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    background: color-mix(in srgb, var(--accent) 14%, transparent);
}

/* ------------------------------------------------------------------ */
/* Карточки сообщений: выбираем взглядом, а не по номеру               */
/* ------------------------------------------------------------------ */
.msg-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
}

.msg {
    display: block;
    width: 100%;
    text-align: left;
    background: var(--surface);
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    padding: var(--space-3);
    cursor: pointer;
    transition: border-color var(--transition), transform var(--transition);
}

.msg:active {
    transform: scale(0.995);
    border-color: color-mix(in srgb, var(--accent) 45%, transparent);
}

.msg.is-picked {
    border-color: var(--accent);
}

.msg-head {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-bottom: var(--space-1);
}

.msg-id {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.msg-tag {
    font-size: var(--text-xs);
    padding: 1px 6px;
    border-radius: 100px;
    background: color-mix(in srgb, var(--text-muted) 16%, transparent);
    color: var(--text-muted);
}

.msg-time {
    margin-left: auto;
    font-size: var(--text-xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.msg-text {
    font-size: var(--text-sm);
    line-height: 1.45;
    color: var(--text);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

/* ------------------------------------------------------------------ */
/* Загрузка файла                                                      */
/* ------------------------------------------------------------------ */
.dropzone {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    min-height: 132px;
    margin-bottom: var(--space-4);
    padding: var(--space-4);
    border: 1px dashed color-mix(in srgb, var(--text-muted) 40%, transparent);
    border-radius: var(--radius-lg);
    background: var(--surface);
    cursor: pointer;
    transition: border-color var(--transition), opacity var(--transition);
}

.dropzone:active {
    border-color: var(--accent);
}

.dropzone.is-busy {
    opacity: 0.6;
    pointer-events: none;
}

.dropzone-icon {
    font-size: 26px;
    line-height: 1;
}

.dropzone-title {
    font-size: var(--text-md);
    font-weight: 500;
}

.dropzone-hint {
    font-size: var(--text-xs);
    color: var(--text-muted);
}

.upload {
    background: var(--surface);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    margin-bottom: var(--space-4);
}

.upload-row {
    display: flex;
    align-items: baseline;
    gap: var(--space-2);
    margin-bottom: var(--space-2);
}

.upload-name {
    flex: 1;
    min-width: 0;
    font-size: var(--text-sm);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.upload-size {
    font-size: var(--text-xs);
    color: var(--text-muted);
    flex-shrink: 0;
}
