/* ------------------------------------------------------------------ база */

:root {
    --bg:        #0d1622;
    --surface:   #16202f;
    --surface-2: #1d2836;
    --chip:      #1e2836;
    --accent:    #2f8fff;
    --text:      #ffffff;
    --muted:     #7d8b9e;
    --pink:      #ef4b7b;
    --radius:    16px;
    --gap:       10px;
    --pad:       12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, sans-serif;
    font-size: 15px;
    line-height: 1.3;
    overscroll-behavior-y: none;
}

body {
    /* вебапп: тянемся во всю ширину устройства, на десктопе — колонка по центру */
    max-width: 560px;
    margin: 0 auto;
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
img { display: block; max-width: 100%; }

/* --------------------------------------------------------------- шапка */

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: calc(10px + env(safe-area-inset-top)) 0 10px var(--pad);
    background: var(--bg);
}

.icon-btn {
    flex: 0 0 auto;              /* поиск и сердечко не уезжают при свайпе */
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: var(--chip);
    color: var(--text);
    transition: background .15s;
}

.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 21px; height: 21px; }

/* лента категорий — единственное, что скроллится вбок */
.chips {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    padding-right: var(--pad);
    mask-image: linear-gradient(to right, #000 calc(100% - 22px), transparent);
}

.chips::-webkit-scrollbar { display: none; }

.chip {
    flex: 0 0 auto;
    height: 42px;
    padding: 0 20px;
    border-radius: 21px;
    background: var(--chip);
    color: var(--text);
    font-size: 16px;
    font-weight: 500;
    white-space: nowrap;
    transition: background .15s, color .15s;
}

.chip.is-active { background: var(--accent); color: #fff; }

/* --------------------------------------------------- строка со счётчиком */

.subbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px var(--pad) 12px;
}

.total {
    color: var(--muted);
    font-size: 15px;
    font-weight: 500;
}

/* ---------------------------------------------------------------- сетка */

.grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--gap);
    padding: 0 var(--pad);
}

.card {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, .05);
    border-radius: var(--radius);
    padding: 8px 8px 10px;
}

.thumb {
    position: relative;
    border-radius: 11px;
    overflow: hidden;
    background: var(--surface-2);
    aspect-ratio: 1 / 1;
}

.thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.views {
    position: absolute;
    top: 7px;
    left: 7px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 24px;
    padding: 0 9px;
    border-radius: 12px;
    background: rgba(10, 16, 24, .68);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    color: #fff;
    font-size: 13px;
    font-weight: 500;
}

.views svg { width: 15px; height: 15px; }

.card-title {
    margin: 10px 2px 3px;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.card-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 0 2px;
}

.card-count {
    color: var(--muted);
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.fav {
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    color: var(--pink);
}

.fav svg { width: 23px; height: 23px; }

/* ---------------------------------------------------------------- пусто */

.empty {
    margin: 40px var(--pad);
    color: var(--muted);
    text-align: center;
}

/* мелкие телефоны: карточки чуть плотнее */
@media (max-width: 340px) {
    :root { --gap: 8px; --pad: 10px; }
    .chip { padding: 0 16px; font-size: 15px; }
}

/* -------------------------------------------------------- модалка пака */

.card { cursor: pointer; }
.thumb img { transition: transform .18s; }
.card:active .thumb img { transform: scale(.96); }

/* display:flex ниже перебил бы атрибут hidden — гасим явно */
[hidden] { display: none !important; }

.sheet-wrap {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    /* отступ со всех сторон вдвое больше, чем у каталога */
    padding: calc(var(--pad) * 2 + env(safe-area-inset-top))
             calc(var(--pad) * 2)
             calc(var(--pad) * 2 + env(safe-area-inset-bottom));
}

.sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(4, 8, 14, .62);
    backdrop-filter: blur(14px) saturate(.85);
    -webkit-backdrop-filter: blur(14px) saturate(.85);
    animation: fade .18s ease-out;
}

.sheet {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    /* уже каталога на два его отступа с каждой стороны */
    max-width: calc(560px - var(--pad) * 4);
    margin: 0 auto;
    /* всегда во весь экран за вычетом отступов, независимо от содержимого */
    flex: 1 1 auto;
    min-height: 0;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, .07);
    border-radius: 22px;
    overflow: hidden;
    animation: rise .22s cubic-bezier(.2, .8, .3, 1);
}

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(22px); opacity: 0; } }

.sheet-head {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 18px 16px 12px;
}

.sheet-head-text { flex: 1 1 auto; min-width: 0; }

.sheet-title {
    margin: 0;
    font-size: 21px;
    font-weight: 700;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sheet-sub {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 14px;
}

.sheet-close {
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--muted);
}

.sheet-close svg { width: 20px; height: 20px; }
.sheet-close:active { background: #26313f; }

.sheet-body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    padding: 4px 16px 8px;
    align-content: start;
    scrollbar-width: none;
}

.sheet-body::-webkit-scrollbar { display: none; }

.sheet-cell {
    aspect-ratio: 1 / 1;
    border-radius: 12px;
    background: var(--surface-2);
    padding: 6px;
}

.sheet-cell img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.sheet-foot {
    flex: 0 0 auto;
    padding: 12px 16px 14px;
}

.sheet-add {
    display: block;
    height: 54px;
    line-height: 54px;
    border-radius: 15px;
    background: var(--accent);
    color: #fff;
    font-size: 17px;
    font-weight: 600;
    text-align: center;
    text-decoration: none;
}

.sheet-add:active { background: #2a7fe4; }

body.is-locked { overflow: hidden; }

@media (min-width: 561px) {
    .sheet-wrap { justify-content: center; }
}

/* ---------------------------------------------------------------- поиск */

/* иконка и поле — один блок: свёрнутый он ровно с круглую кнопку,
   развёрнутый растягивается на всю шапку */
.search {
    /* свёрнутый — ровный круг 42px, флекс его не сжимает */
    flex: 0 0 auto;
    width: 42px;
    min-width: 0;
    display: flex;
    align-items: center;
    height: 42px;
    border-radius: 21px;
    background: var(--chip);
    overflow: hidden;
    /* анимируем именно width — flex-grow не интерполируется и давал рывок */
    transition: width .3s cubic-bezier(.2, .8, .3, 1);
}

/* тот же блок растягивается на всю шапку, иконка остаётся внутри слева */
.topbar.is-search .search {
    width: calc(100% - var(--pad));
}

/* в режиме поиска сердечко и лента категорий уходят */
.topbar.is-search .icon-btn,
.topbar.is-search .chips { display: none; }

.search-btn {
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    color: var(--text);
}

.search-btn svg { width: 21px; height: 21px; }
.topbar.is-search .search-btn { cursor: default; }

.search-input {
    flex: 1 1 auto;
    min-width: 0;
    width: 0;
    height: 100%;
    padding: 0;
    border: 0;
    background: none;
    color: var(--text);
    font-size: 16px;
    outline: none;
}

.search-input::placeholder { color: var(--muted); }
.search-input::-webkit-search-cancel-button { display: none; }

.search-close {
    flex: 0 0 auto;
    width: 34px;
    height: 42px;
    display: grid;
    place-items: center;
    color: var(--muted);
}

.search-close svg { width: 18px; height: 18px; }

/* -------------------------------------------------- загрузка в попапе */

.sheet-loader {
    position: absolute;
    left: 0;
    right: 0;
    top: 74px;
    bottom: 80px;
    display: grid;
    place-items: center;
}


.spinner {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: conic-gradient(from 0deg, rgba(255, 255, 255, 0) 0deg,
                                          rgba(255, 255, 255, .12) 120deg,
                                          #ffffff 360deg);
    -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3.5px), #000 0);
            mask: radial-gradient(farthest-side, transparent calc(100% - 3.5px), #000 0);
    animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(1turn); } }

@media (prefers-reduced-motion: reduce) {
    .spinner { animation-duration: 2s; }
}

/* ------------------------------------------------------------- избранное */

/* сердечко в шапке подсвечивается так же, как активная категория */
.icon-btn.is-on { background: var(--accent); color: #fff; }

/* сердечко на карточке: контур -> заливка */
.fav.is-on svg path { fill: currentColor; }

/* ---------------------------------------------------------------- реклама */

.adv {
    grid-column: 1 / -1;          /* во всю ширину сетки */
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, .05);
}

.adv img {
    width: 100%;
    height: auto;
    display: block;
}

/* ------------------------------------------------------------- страницы */

.pager {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 3px;
    border-radius: 17px;
    background: var(--chip);
}

.pager:empty { display: none; }

.pager button {
    min-width: 28px;
    height: 28px;
    padding: 0 8px;
    border-radius: 14px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 500;
}

.pager button.is-active {
    background: var(--surface-2);
    color: var(--text);
}

.pager-bottom {
    width: fit-content;
    margin: 16px auto 0;
}
