/* =========================================================================
   Мини-приложение «Отчёты» — стили
   Цвета берутся из темы Telegram (--tg-theme-*), поэтому интерфейс
   автоматически выглядит родным и в светлой, и в тёмной теме.

   Основной режим работы — смартфон, поэтому вёрстка рассчитана на тач:
   крупные тач-таргеты, перенос элементов вместо горизонтального скролла,
   учёт безопасных зон (safe-area) и реальной высоты вьюпорта Telegram.
   ========================================================================= */

:root {
    --bg: var(--tg-theme-bg-color, #ffffff);
    --text: var(--tg-theme-text-color, #1c1c1e);
    --hint: var(--tg-theme-hint-color, #8a8a8e);
    --link: var(--tg-theme-link-color, #2ea6ff);
    --button: var(--tg-theme-button-color, #2ea6ff);
    --button-text: var(--tg-theme-button-text-color, #ffffff);
    --secondary-bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
    --section-bg: var(--tg-theme-section-bg-color, var(--tg-theme-secondary-bg-color, #f2f2f7));
    --destructive: var(--tg-theme-destructive-text-color, #e53935);
    --border: rgba(120, 120, 128, 0.24);
    --radius: 12px;

    /* Отступ экрана (тянется под ширину устройства) и размер тач-таргета */
    --pad: clamp(14px, 4.2vw, 18px);
    --tap: 48px;

    color-scheme: light;
}

body.theme-dark {
    color-scheme: dark; /* системные календари/клавиатуры тоже станут тёмными */
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
    /* чтобы сфокусированное поле не пряталось под кнопкой «Отправить» */
    scroll-padding-bottom: 96px;
}

body {
    min-height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    font-size: 16px;
    line-height: 1.45;
    background: var(--bg);
    color: var(--text);
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: none;
}

.hidden {
    display: none !important;
}

/* -------------------------------------------------------------------------
   Каркас экранов
   --app-height проставляет script.js (реальная высота вьюпорта Telegram),
   если переменной нет — работают фолбэки 100vh / 100dvh.
   ------------------------------------------------------------------------- */
.app {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 560px;          /* аккуратно на планшетах и в Telegram Desktop */
    margin: 0 auto;
    min-height: var(--app-height, 100vh);
    min-height: var(--app-height, 100dvh);
}

.screen {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: var(--app-height, 100vh);
    min-height: var(--app-height, 100dvh);
    padding-top: calc(var(--pad) + env(safe-area-inset-top, 0px));
    padding-bottom: calc(var(--pad) + env(safe-area-inset-bottom, 0px));
    padding-left: max(var(--pad), env(safe-area-inset-left, 0px));
    padding-right: max(var(--pad), env(safe-area-inset-right, 0px));
    animation: screen-in 0.18s ease-out;
}

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

    to {
        opacity: 1;
        transform: none;
    }
}

.screen--center {
    justify-content: center;
    align-items: center;
    text-align: center;
    gap: 10px;
}

/* Контент не растягивается: кнопка «Отправить» стоит сразу под формой.
   Если контент длиннее экрана, sticky-футер сам прилипнет к низу вьюпорта. */
.content {
    flex: 0 0 auto;
    min-width: 0;
}

/* -------------------------------------------------------------------------
   Шапка с названием отчёта
   ------------------------------------------------------------------------- */
.header {
    padding: 0 2px 14px;
}

/* Тип отчёта — единственный элемент шапки */
.header__title {
    font-size: clamp(20px, 5.8vw, 24px);
    font-weight: 600;
    line-height: 1.25;
    overflow-wrap: break-word;
}

/* -------------------------------------------------------------------------
   Карточка формы
   ------------------------------------------------------------------------- */
.card {
    background: var(--section-bg);
    border-radius: var(--radius);
    padding: var(--pad);
}

/* --- Быстрые пресеты периода: чипы переносятся по строкам, без скролла --- */
.presets {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 14px;
}

.chip {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;             /* удобный тач-таргет */
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
    line-height: 1.15;
    white-space: nowrap;
    cursor: pointer;
    touch-action: manipulation;   /* без задержки нажатия и двойного зума */
    user-select: none;
    -webkit-user-select: none;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.chip:active {
    transform: scale(0.97);
}

.chip--active {
    background: var(--button);
    border-color: transparent;
    color: var(--button-text);
}

/* --- Поля ввода --- */
.fields {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.field {
    min-width: 0;
}

.field__label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--hint);
}

.field__input {
    display: block;
    width: 100%;
    min-width: 0;
    min-height: var(--tap);
    padding: 12px 14px;
    font-family: inherit;
    font-size: 16px; /* 16px и больше — iOS не зумит страницу при фокусе */
    line-height: 1.3;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field__input:focus {
    border-color: var(--button);
    box-shadow: 0 0 0 3px rgba(46, 166, 255, 0.18);
}

/* Поле даты: нативный вид НЕ отключаем — так системный календарь надёжно
   открывается и корректно рисуется и на iOS, и на Android */
.field__input[type="date"] {
    cursor: pointer;
    touch-action: manipulation;
    padding-right: 8px;
}

.field__input[type="date"]::-webkit-datetime-edit {
    padding: 0;
}

.field__input[type="date"]::-webkit-calendar-picker-indicator {
    opacity: 0.55;
    padding: 6px;
    cursor: pointer;
}

body.theme-dark .field__input[type="date"]::-webkit-calendar-picker-indicator {
    filter: invert(1);
}

.field__input[type="text"],
.field__input[type="number"] {
    -webkit-appearance: none;
    appearance: none;
}

.field__error {
    display: none;
    margin-top: 6px;
    font-size: 13px;
    color: var(--destructive);
}

.field--error .field__input {
    border-color: var(--destructive);
}

.field--error .field__error {
    display: block;
}

/* --- Сводка по выбранному периоду --- */
.summary {
    margin: 12px 2px 0;
    font-size: 14px;
    color: var(--hint);
}

/* --- Сообщение об ошибке отправки --- */
.form-error {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    margin-top: 12px;
    padding: 12px 14px;
    border-radius: 10px;
    background: rgba(229, 57, 53, 0.12);
    font-size: 14px;
    color: var(--destructive);
}

.form-error__retry {
    border: none;
    background: none;
    padding: 6px 0;
    font-family: inherit;
    font-size: 14px;
    font-weight: 600;
    color: var(--link);
    text-decoration: underline;
    cursor: pointer;
    touch-action: manipulation;
}

/* -------------------------------------------------------------------------
   Кнопки
   ------------------------------------------------------------------------- */
.footer {
    position: sticky;
    bottom: 0;
    z-index: 5;
    margin-top: 14px;
    padding-top: 12px;
    /* градиент должен перекрывать контент по всей ширине экрана,
       поэтому компенсируем боковые отступы .screen */
    margin-left: calc(var(--pad) * -1);
    margin-right: calc(var(--pad) * -1);
    padding-left: max(var(--pad), env(safe-area-inset-left, 0px));
    padding-right: max(var(--pad), env(safe-area-inset-right, 0px));
    background: linear-gradient(to top, var(--bg) 78%, transparent);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    min-height: 52px;
    padding: 14px 18px;
    border: none;
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 17px;
    font-weight: 600;
    line-height: 1.2;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    transition: opacity 0.15s ease, transform 0.1s ease;
}

.btn:active {
    transform: scale(0.985);
}

.btn[disabled] {
    opacity: 0.55;
    pointer-events: none;
}

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

.btn--ghost {
    background: transparent;
    color: var(--link);
}

.btn--block {
    width: 100%;
}

.btn:focus-visible,
.chip:focus-visible,
.field__input:focus-visible {
    outline: 2px solid var(--link);
    outline-offset: 2px;
}

/* -------------------------------------------------------------------------
   Служебные экраны (загрузка / успех / ошибка)
   ------------------------------------------------------------------------- */
.spinner {
    width: 30px;
    height: 30px;
    border: 3px solid var(--border);
    border-top-color: var(--button);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}

.spinner--small {
    width: 18px;
    height: 18px;
    border-width: 2px;
    border-color: rgba(255, 255, 255, 0.4);
    border-top-color: currentColor;
}

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

.status-icon {
    font-size: clamp(44px, 14vw, 56px);
    line-height: 1;
}

.screen--center h2 {
    font-size: clamp(18px, 5.2vw, 20px);
    font-weight: 600;
}

.status-text {
    max-width: 34em;
    font-size: 15px;
    color: var(--hint);
    overflow-wrap: break-word;
}

.status-hint {
    font-size: 13px;
    color: var(--hint);
}

.loading-text {
    font-size: 14px;
    color: var(--hint);
}

.status-actions {
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 100%;
    max-width: 340px;
    margin-top: 8px;
}

/* -------------------------------------------------------------------------
   Debug-панель (localhost или ?debug=1)
   Размещена сверху и сворачивается тапом, чтобы не закрывать кнопку
   «Отправить» на смартфоне.
   ------------------------------------------------------------------------- */
.debug-panel {
    position: fixed;
    top: 6px;
    left: 8px;
    right: 8px;
    max-width: 460px;
    z-index: 999;
    max-height: 38%;
    overflow: auto;
    padding: 8px 10px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.78);
    color: #f1f1f1;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 11px;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.debug-panel__toggle {
    display: block;
    margin-bottom: 4px;
    color: #8ee6ff;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.debug-panel--collapsed {
    max-height: 30px;
    overflow: hidden;
}

.debug-panel--collapsed .debug-panel__toggle {
    margin-bottom: 0;
}

/* -------------------------------------------------------------------------
   Узкие смартфоны (320–380 px)
   ------------------------------------------------------------------------- */
@media (max-width: 380px) {
    :root {
        --pad: 12px;
        --tap: 46px;
    }

    .card {
        padding: 12px;
    }

    .presets {
        gap: 6px;
        margin-bottom: 12px;
    }

    .chip {
        min-height: 42px;
        padding: 0 12px;
        font-size: 13px;
    }

    .fields {
        gap: 12px;
    }

    .btn {
        font-size: 16px;
    }
}

/* -------------------------------------------------------------------------
   Низкие экраны (небольшие телефоны, открытая клавиатура)
   ------------------------------------------------------------------------- */
@media (max-height: 640px) {
    .header {
        padding-bottom: 10px;
    }

    .card {
        padding: 12px;
    }

    .presets {
        margin-bottom: 12px;
    }

    .chip {
        min-height: 40px;
    }

    .footer {
        padding-top: 10px;
    }

    .btn {
        min-height: 48px;
        padding: 12px 18px;
    }

    .status-icon {
        font-size: 40px;
    }
}

/* -------------------------------------------------------------------------
   Телефон в горизонтальной ориентации: поля в две колонки
   ------------------------------------------------------------------------- */
@media (orientation: landscape) and (max-height: 520px) {
    .header {
        padding-bottom: 8px;
    }

    .fields {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 10px;
    }

    .field {
        flex: 1 1 44%;
        min-width: 150px;
    }

    .presets {
        gap: 6px;
        margin-bottom: 10px;
    }

    .status-icon {
        font-size: 34px;
    }
}

/* -------------------------------------------------------------------------
   Планшеты и десктоп
   ------------------------------------------------------------------------- */
@media (min-width: 640px) {
    .presets {
        gap: 10px;
    }

    .chip {
        font-size: 15px;
    }
}

/* -------------------------------------------------------------------------
   Уважение к системной настройке «уменьшить анимацию»
   ------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {

    .screen {
        animation: none;
    }

    .spinner {
        animation-duration: 2s;
    }
}
