/* ==========================================================================
   PolkasTarot — интерфейс в духе iOS.

   Опорные принципы:
     - мобильный экран первичен, настольный получает те же компоненты шире;
     - системная палитра и типографика iOS, светлая и тёмная темы;
     - безопасные зоны (вырез, домашняя полоска) учтены через env();
     - касаемые элементы не меньше 44 px.
   ========================================================================== */

:root {
    /* Палитра iOS, светлая тема */
    --bg:            #f2f2f7;   /* systemGroupedBackground */
    --surface:       #ffffff;
    --surface-sunk:  #f2f2f7;
    --fill:          rgba(120, 120, 128, 0.12);
    --fill-strong:   rgba(120, 120, 128, 0.2);

    --label:         #000000;
    --label-2:       rgba(60, 60, 67, 0.6);
    --label-3:       rgba(60, 60, 67, 0.3);
    --separator:     rgba(60, 60, 67, 0.18);

    --accent:        #007aff;   /* systemBlue */
    --accent-soft:   rgba(0, 122, 255, 0.12);
    --indigo:        #5856d6;
    --pink:          #ff2d55;

    --nav-bg:        rgba(249, 249, 249, 0.82);
    --sheet-bg:      #ffffff;
    --backdrop:      rgba(0, 0, 0, 0.4);

    --shadow-card:   0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.06);
    --shadow-sheet:  0 -1px 0 rgba(0, 0, 0, 0.05), 0 -8px 40px rgba(0, 0, 0, 0.18);

    --r-s:  10px;
    --r-m:  14px;
    --r-l:  20px;
    --r-sheet: 26px;

    --pad:   16px;
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg:           #000000;
        --surface:      #1c1c1e;
        --surface-sunk: #000000;
        --fill:         rgba(120, 120, 128, 0.24);
        --fill-strong:  rgba(120, 120, 128, 0.36);

        --label:        #ffffff;
        --label-2:      rgba(235, 235, 245, 0.6);
        --label-3:      rgba(235, 235, 245, 0.3);
        --separator:    rgba(84, 84, 88, 0.65);

        --accent:       #0a84ff;
        --accent-soft:  rgba(10, 132, 255, 0.22);
        --indigo:       #5e5ce6;
        --pink:         #ff375f;

        --nav-bg:       rgba(30, 30, 32, 0.8);
        --sheet-bg:     #1c1c1e;
        --backdrop:     rgba(0, 0, 0, 0.6);

        --shadow-card:  0 1px 2px rgba(0, 0, 0, 0.5);
        --shadow-sheet: 0 -8px 40px rgba(0, 0, 0, 0.6);
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--label);
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'SF Pro Display',
                 'Helvetica Neue', system-ui, 'Segoe UI', Roboto, sans-serif;
    font-size: 17px;              /* iOS body */
    line-height: 1.45;
    letter-spacing: -0.01em;
    -webkit-font-smoothing: antialiased;
    overflow-wrap: break-word;
}

button { font: inherit; color: inherit; cursor: pointer; }

img { max-width: 100%; display: block; }

/* --------------------------------------------------------------------------
   Навигация: закреплённая полоса с размытием, как в iOS
   -------------------------------------------------------------------------- */

.nav-bar {
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--nav-bg);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    backdrop-filter: saturate(180%) blur(20px);
    border-bottom: 0.5px solid var(--separator);
    padding-top: var(--safe-t);
}

.nav-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px calc(var(--pad) + var(--safe-r)) 8px calc(var(--pad) + var(--safe-l));
}

.nav-title {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.nav-action {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 36px;
    padding: 7px 14px;
    border: none;
    border-radius: 999px;
    background: var(--fill);
    color: var(--accent);
    font-size: 15px;
    font-weight: 600;
    transition: background 0.15s ease, transform 0.1s ease;
}

.nav-action:active { background: var(--fill-strong); transform: scale(0.96); }

/* Поле вопроса — в духе строки поиска iOS: скруглённая подложка, иконка
   слева, никакой рамки. */
.question-row {
    padding: 14px calc(var(--pad) + var(--safe-r)) 4px calc(var(--pad) + var(--safe-l));
}

.question-field {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    min-height: 38px;
    border-radius: 10px;
    background: var(--fill);
}

.question-icon { font-size: 14px; opacity: 0.65; }

.question-field input {
    flex: 1;
    min-width: 0;
    border: none;
    background: none;
    color: var(--label);
    /* 16px — иначе Safari на iPhone масштабирует страницу при фокусе. */
    font-size: 16px;
    font-family: inherit;
}

.question-field input::placeholder { color: var(--label-3); }
.question-field input:focus { outline: none; }
.question-field:focus-within { background: var(--fill-strong); }

/* Заданный вопрос над раскладом и ссылка на него. */
.spread-asked {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 6px 12px;
    margin: 0 0 18px;
    padding: 12px 14px;
    border-radius: 12px;
    background: var(--fill);
}

/* Вопрос и ссылка идут одной строкой: поле с вопросом стоит выше на той же
   странице, и разворачивать здесь второй абзац значит показать один текст
   дважды подряд. Перенос только если строка не влезла. */
.asked-text {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    font-size: 15px;
    font-style: italic;
    color: var(--label);
    overflow-wrap: anywhere;
}

.spread-asked > .text-btn { flex: 0 0 auto; }

.share-note { font-size: 13px; color: var(--label-2); word-break: break-all; }

/* Лента раскладов: горизонтальная прокрутка с подписями.
   Раньше здесь была сетка одинаковых кружков с одними эмодзи —
   угадать по ним расклад было невозможно. */
.spread-strip {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    /* Без scroll-padding привязка ставит первую плашку вплотную к краю,
       съедая левый отступ ленты. */
    scroll-padding-left: calc(var(--pad) + var(--safe-l));
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 2px calc(var(--pad) + var(--safe-r)) 12px calc(var(--pad) + var(--safe-l));
}

.spread-strip::-webkit-scrollbar { display: none; }

.spread-chip {
    flex: 0 0 auto;
    scroll-snap-align: start;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 38px;
    padding: 8px 15px;
    border: none;
    border-radius: 999px;
    background: var(--fill);
    color: var(--label);
    font-size: 15px;
    font-weight: 500;
    white-space: nowrap;
    transition: background 0.15s ease, transform 0.1s ease;
}

.spread-chip:active { background: var(--fill-strong); transform: scale(0.96); }
.chip-icon { font-size: 16px; line-height: 1; }

/* --------------------------------------------------------------------------
   Общая раскладка
   -------------------------------------------------------------------------- */

main {
    max-width: 900px;
    margin: 0 auto;
    padding: 0 calc(var(--pad) + var(--safe-l)) 32px calc(var(--pad) + var(--safe-r));
}

.section-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin: 28px 4px 10px;
}

.section-head h2 {
    margin: 0;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.text-btn {
    border: none;
    background: none;
    padding: 6px 2px;
    color: var(--accent);
    font-size: 15px;
    font-weight: 400;
}

.text-btn:active { opacity: 0.5; }

footer {
    padding: 8px calc(var(--pad) + var(--safe-l)) calc(20px + var(--safe-b));
    text-align: center;
    color: var(--label-3);
    font-size: 13px;
}

footer p { margin: 0; }

/* --------------------------------------------------------------------------
   Приветственный экран
   -------------------------------------------------------------------------- */

.welcome-screen { padding-top: 20px; }

.welcome-title {
    margin: 0 4px 6px;
    font-size: 34px;
    font-weight: 700;
    letter-spacing: -0.03em;
    line-height: 1.15;
}

.welcome-subtitle {
    margin: 0 4px 22px;
    color: var(--label-2);
    font-size: 17px;
}

/* Сгруппированный список — базовый строительный блок интерфейса iOS */
.inset-list {
    list-style: none;
    margin: 0;
    padding: 0;
    background: var(--surface);
    border-radius: var(--r-m);
    box-shadow: var(--shadow-card);
    overflow: hidden;
}

.inset-list:empty { display: none; }

.inset-list > li {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    min-height: 52px;
}

.inset-list > li + li { border-top: 0.5px solid var(--separator); }

.row-icon {
    flex: 0 0 auto;
    width: 30px;
    text-align: center;
    font-size: 22px;
    line-height: 1;
}

.row-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.row-title { font-size: 17px; font-weight: 500; }
.row-note { color: var(--label-2); font-size: 14px; line-height: 1.35; }

.empty-msg {
    color: var(--label-3);
    font-size: 15px;
    justify-content: center;
}

/* --------------------------------------------------------------------------
   Расклад
   -------------------------------------------------------------------------- */

.spread-main-title {
    margin: 24px 4px 4px;
    font-size: 28px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.spread-lede {
    margin: 0 4px 20px;
    color: var(--label-2);
    font-size: 15px;
}

/* Сетка позиций задаётся из spreads.js через переменные, а не inline-стилем:
   так CSS может перестроить её на узком экране. */
.spread-table {
    display: grid;
    grid-template-columns: var(--spread-cols, 1fr);
    grid-template-areas: var(--spread-areas);
    gap: 20px;
    justify-items: center;
    margin-bottom: 8px;
}

.spread-table.daily-layout { display: flex; justify-content: center; }

.card-item { width: 100%; max-width: 190px; text-align: center; }

.pos-label {
    margin-bottom: 8px;
    color: var(--label-2);
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.card-flipper { perspective: 1000px; }

.card-inner {
    position: relative;
    aspect-ratio: 350 / 600;
    transition: transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1);
    transform-style: preserve-3d;
}

.card-flipper.flipped .card-inner { transform: rotateY(180deg); }

.card-front,
.card-back {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    border-radius: var(--r-m);
    overflow: hidden;
    box-shadow: var(--shadow-card);
}

.card-back {
    transform: rotateY(180deg);
    background: linear-gradient(160deg, var(--indigo), var(--accent));
}

.card-img { width: 100%; height: 100%; object-fit: cover; }

.card-item.reversed .card-img { transform: rotate(180deg); }

.card-title-under {
    margin-top: 10px;
    font-size: 15px;
    font-weight: 600;
}

/* Толкование */

.interpretation-area { margin-top: 28px; }

.interpretation-area > h2 {
    margin: 0 4px 12px;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.info-block {
    background: var(--surface);
    border-radius: var(--r-m);
    box-shadow: var(--shadow-card);
    padding: 16px;
    margin-bottom: 14px;
}

.info-block h3 {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    margin: 0 0 12px;
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.info-block h3 small { font-size: 13px; font-weight: 600; }

.info-main-text > p { margin: 0 0 12px; }
.info-main-text strong { font-weight: 600; }

.advice-box {
    margin: 12px 0 0;
    padding: 12px 14px;
    background: var(--accent-soft);
    border-radius: var(--r-s);
    font-size: 16px;
}

.advice-box > strong { color: var(--accent); font-weight: 600; }
.advice-box p { margin: 6px 0 0; }

.info-sub-details { margin-top: 14px; }

.info-sub-details > p {
    margin: 0;
    padding: 12px 0;
    border-top: 0.5px solid var(--separator);
    font-size: 16px;
}

.info-sub-details strong {
    display: block;
    margin-bottom: 3px;
    color: var(--label-2);
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

/* --------------------------------------------------------------------------
   Галерея колоды
   -------------------------------------------------------------------------- */

.deck-gallery { padding-top: 4px; }

/* Фильтры — один ряд с прокруткой. Прежде они переносились на три строки
   и отжимали карты далеко вниз экрана. */
.filter-tabs {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none;
    margin: 0 calc(-1 * var(--pad)) 20px;
    padding: 2px var(--pad) 6px;
}

.filter-tabs::-webkit-scrollbar { display: none; }

.filter-tab {
    flex: 0 0 auto;
    min-height: 34px;
    padding: 7px 14px;
    border: none;
    border-radius: 999px;
    background: var(--fill);
    color: var(--label);
    font-size: 15px;
    font-weight: 500;
    white-space: nowrap;
    transition: background 0.15s ease, color 0.15s ease;
}

.filter-tab.active { background: var(--accent); color: #fff; }
.filter-tab:active { transform: scale(0.96); }

.deck-section-title {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    margin: 0 4px 12px;
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.deck-section-title .count {
    color: var(--label-2);
    font-size: 15px;
    font-weight: 400;
}

.deck-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
    gap: 14px;
}

.deck-card { cursor: pointer; }

.deck-card-image {
    aspect-ratio: 350 / 600;
    border-radius: var(--r-s);
    overflow: hidden;
    background: var(--fill);
    box-shadow: var(--shadow-card);
    transition: transform 0.15s ease;
}

.deck-card:active .deck-card-image { transform: scale(0.96); }
.deck-card-image img { width: 100%; height: 100%; object-fit: cover; }

.deck-card-name {
    margin-top: 7px;
    font-size: 13px;
    font-weight: 500;
    line-height: 1.25;
    text-align: center;
}

/* --------------------------------------------------------------------------
   Карточка карты: лист, выезжающий снизу
   -------------------------------------------------------------------------- */

.sheet-backdrop {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    background: var(--backdrop);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
    opacity: 0;
    visibility: hidden;
    /* При закрытии видимость гаснет только после затухания — иначе лист
       исчезал бы рывком. */
    transition: opacity 0.25s ease, visibility 0s linear 0.25s;
}

/* А при открытии — мгновенно. Раньше и здесь был плавный переход, и в
   первый кадр лист оставался visibility: hidden: focus() на кнопке закрытия
   молча не срабатывал, и фокус клавиатуры оставался позади открытого окна.
   Так были мертвы сразу две попытки его перенести — в UI.trapFocus и в
   Academy (academy.js, после enrich). */
.sheet-backdrop.active { opacity: 1; visibility: visible; transition: opacity 0.25s ease, visibility 0s; }

.sheet {
    position: relative;
    width: 100%;
    max-width: 640px;
    max-height: 92vh;
    display: flex;
    flex-direction: column;
    background: var(--sheet-bg);
    border-radius: var(--r-sheet) var(--r-sheet) 0 0;
    box-shadow: var(--shadow-sheet);
    transform: translateY(24px);
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.sheet-backdrop.active .sheet { transform: translateY(0); }

.sheet-grabber {
    width: 36px;
    height: 5px;
    margin: 8px auto 0;
    border-radius: 999px;
    background: var(--label-3);
    flex: 0 0 auto;
}

.sheet-close {
    position: absolute;
    top: 12px;
    right: 12px;
    /* Шапка карточки липкая и перекрывала бы кнопку. */
    z-index: 3;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 999px;
    background: var(--fill);
    color: var(--label-2);
    font-size: 14px;
    line-height: 1;
}

.sheet-close:active { background: var(--fill-strong); }

.sheet-body {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 4px 20px calc(24px + var(--safe-b));
}

/* Шапка карточки: миниатюра слева, название рядом — а не картинка
   во весь экран, из-за которой текст уезжал за пределы видимости. */
.modal-card-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 0 16px;
    position: sticky;
    top: 0;
    background: var(--sheet-bg);
    z-index: 1;
}

.modal-card-image {
    flex: 0 0 auto;
    width: 74px;
    aspect-ratio: 350 / 600;
    border-radius: var(--r-s);
    overflow: hidden;
    box-shadow: var(--shadow-card);
}

.modal-card-image img { width: 100%; height: 100%; object-fit: cover; }

.modal-card-info { min-width: 0; }

.modal-card-info h2 {
    margin: 0 0 4px;
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.card-type { color: var(--label-2); font-size: 14px; }

/* Соответствия карты: стихия, планета, знак. Ряд переносится, потому что на
   узком экране три чипа в строку не встают. */
.corr-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.corr-chip {
    display: inline-flex;
    align-items: baseline;
    gap: 5px;
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--fill);
    color: var(--label);
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
}

.corr-chip > b {
    color: var(--label-2);
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* В толковании расклада ряд идёт под заголовком карты и не должен спорить
   с ним по весу. */
.info-block .corr-row { margin: -4px 0 12px; }

.meaning-section { margin-bottom: 20px; }

.meaning-section > h3 {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0 0 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--label-2);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.orientation-badge {
    padding: 3px 9px;
    border-radius: 999px;
    background: var(--fill);
    color: var(--label-2);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0;
    text-transform: none;
}

.orientation-badge.direct { background: var(--accent-soft); color: var(--accent); }
.orientation-badge.reversed { background: rgba(255, 45, 85, 0.14); color: var(--pink); }

.modal-meanings .meaning-item,
.meaning-section .meaning-item {
    padding: 14px 16px;
    background: var(--surface-sunk);
    border-radius: var(--r-s);
    margin-bottom: 8px;
}

@media (prefers-color-scheme: dark) {
    .modal-meanings .meaning-item,
    .meaning-section .meaning-item { background: #2c2c2e; }
}

.meaning-item > strong {
    display: block;
    margin-bottom: 5px;
    font-size: 13px;
    font-weight: 600;
    color: var(--label-2);
}

.meaning-item p { margin: 0; font-size: 16px; }

/* Глубокие разделы карточки: свёрнуты, раскрываются по нажатию */
.deep-fields { margin-top: 2px; }

.deep-fields > summary {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 12px 16px;
    border-radius: var(--r-s);
    background: var(--surface-sunk);
    color: var(--accent);
    font-size: 15px;
    font-weight: 500;
    list-style: none;
    cursor: pointer;
}

.deep-fields > summary::-webkit-details-marker { display: none; }

.deep-fields > summary::after {
    content: '›';
    margin-left: auto;
    font-size: 20px;
    line-height: 1;
    color: var(--label-3);
    transform: rotate(90deg);
    transition: transform 0.2s ease;
}

.deep-fields[open] > summary::after { transform: rotate(-90deg); }
.deep-fields[open] > summary { margin-bottom: 8px; }
.deep-fields > summary:active { background: var(--fill); }

@media (prefers-color-scheme: dark) {
    .deep-fields > summary { background: #2c2c2e; }
}

.meaning-list {
    margin: 0;
    padding-left: 20px;
    font-size: 16px;
}

.meaning-list li { margin-bottom: 5px; }
.meaning-list li:last-child { margin-bottom: 0; }

/* --------------------------------------------------------------------------
   История
   -------------------------------------------------------------------------- */

#historyList > li { justify-content: space-between; }
#historyList small { display: block; color: var(--label-2); font-size: 13px; }

/* --------------------------------------------------------------------------
   Широкий экран: те же компоненты, больше воздуха
   -------------------------------------------------------------------------- */

@media (min-width: 700px) {
    .nav-title { font-size: 32px; }
    .deck-grid { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 18px; }
    .deck-card-name { font-size: 14px; }
    .sheet {
        align-self: center;
        max-height: 86vh;
        border-radius: var(--r-l);
    }
    .sheet-backdrop { align-items: center; }
    .modal-card-image { width: 96px; }
    .info-sub-details {
        display: grid;
        grid-template-columns: 1fr 1fr;
        column-gap: 20px;
    }
}

/* На узком экране многоколоночные расклады складываются в столбец:
   Кельтский крест в три колонки на телефоне нечитаем. */
@media (max-width: 699px) {
    .spread-table {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 24px;
    }
}

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