/**
 * منوی کشویی لایه‌به‌لایهٔ دسته‌ها + نوار تراشه‌ای زیردسته‌ها — فقط موبایل
 * (درخواست ۱۴۰۵/۰۹/۰۵). مارک‌آپ: renderCategoryDrawer / renderSubcategoryChips
 * در render_helpers.php؛ رفتار: assets/js/catalog-drawer.js.
 * دسکتاپ دست‌نخورده است (نوار دسته‌بندی و ستون آکاردئونی مثل قبل).
 */

/* ---------- منوی کشویی ---------- */
.cat-drawer[hidden] { display: none; }
.cat-drawer {
    position: fixed;
    inset: 0;
    z-index: 2000;
}
.cat-drawer-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    animation: cdFade .45s ease;
}
.cat-drawer-sheet {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    width: min(88vw, 380px);
    background: #fff;
    display: flex;
    flex-direction: column;
    box-shadow: -4px 0 24px rgba(0, 0, 0, .18);
    animation: cdSheetIn .45s cubic-bezier(.22, .61, .36, 1);
    overflow: hidden;
}
.cd-panel[hidden] { display: none; }
.cd-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}
.cd-panel.cd-in-fwd  { animation: cdFromLeft .38s cubic-bezier(.22, .61, .36, 1); }
.cd-panel.cd-in-back { animation: cdFromRight .38s cubic-bezier(.22, .61, .36, 1); }

.cd-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: var(--primary, #1e88e5);
    color: #fff;
    flex: none;
}
.cd-title {
    flex: 1;
    font-weight: 700;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.cd-icon-btn {
    width: 36px;
    height: 36px;
    flex: none;
    border: 0;
    border-radius: 8px;
    background: rgba(255, 255, 255, .15);
    color: inherit;
    font-size: 16px;
    cursor: pointer;
}
.cd-crumb {
    flex: none;
    padding: 8px 14px;
    font-size: 12px;
    color: #6b7280;
    border-bottom: 1px solid #eef0f3;
    background: #f8fafc;
    line-height: 1.9;
}
.cd-crumb button {
    border: 0;
    background: none;
    padding: 0;
    font: inherit;
    color: var(--primary, #1e88e5);
    cursor: pointer;
}
.cd-crumb-current { color: #374151; font-weight: 700; }

.cd-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.cd-list li { border-bottom: 1px solid #eef0f3; }
.cd-row {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 50px;
    padding: 10px 16px;
    border: 0;
    background: none;
    color: var(--text-main, #222);
    font: inherit;
    font-size: 14px;
    text-align: right;
    text-decoration: none;
    cursor: pointer;
}
.cd-row > i:first-child {
    width: 20px;
    text-align: center;
    color: #9aa3ad;
    flex: none;
}
.cd-text { flex: 1; min-width: 0; }
.cd-count {
    flex: none;
    min-width: 26px;
    padding: 1px 8px;
    border-radius: 10px;
    background: #f1f3f5;
    color: #5f6670;
    font-size: 11px;
    text-align: center;
}
/* ردیف دارای زیرمجموعه: لینک (کالاهای همین دسته) + کلید فلش (زیرمجموعه‌ها) */
.cd-has-sub { display: flex; align-items: stretch; }
.cd-has-sub > .cd-row { flex: 1; min-width: 0; }
.cd-sub {
    flex: none;
    width: 52px;
    border: 0;
    border-right: 1px solid #eef0f3;
    background: #f6f8fb;
    color: var(--primary, #1e88e5);
    font-size: 14px;
    cursor: pointer;
}
.cd-sub:active { background: #e6f0fd; }
.cd-row:active { background: #f1f5f9; }
.cd-row.cd-all { color: var(--primary, #1e88e5); font-weight: 700; background: #f3f8ff; }
.cd-row.cd-all > i:first-child { color: inherit; }
.cd-row.is-active { color: var(--primary, #1e88e5); font-weight: 700; }
.cd-row.is-active > i:first-child { color: inherit; }
.cd-row.is-empty .cd-text,
.cd-row.is-empty > i:first-child { opacity: .55; }

html.cd-lock, html.cd-lock body { overflow: hidden; }

@keyframes cdFade { from { opacity: 0; } }
@keyframes cdSheetIn { from { transform: translateX(100%); } }
@keyframes cdFromLeft { from { transform: translateX(-30%); opacity: 0; } }
@keyframes cdFromRight { from { transform: translateX(30%); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
    .cat-drawer-backdrop, .cat-drawer-sheet, .cd-panel { animation: none !important; }
}

/* ---------- نوار تراشه‌ای زیردسته‌ها ---------- */
.cat-chips {
    display: flex;
    flex-wrap: wrap; /* همهٔ تراشه‌ها دیده شوند (قبلاً اسکرول افقی بود و بقیه پنهان می‌ماند) */
    gap: 6px;
    padding: 2px 2px 8px;
    margin-bottom: 4px;
}
.cat-chips::-webkit-scrollbar { display: none; }
.cat-chip {
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 4px 10px;
    border: 1px solid #d7dbe0;
    border-radius: 16px;
    background: #fff;
    color: #2f353b;
    font: inherit;
    font-size: 12px;
    line-height: 1.7;
    text-decoration: none;
    cursor: pointer;
}
.cat-chip.is-active {
    background: #2f353b;
    border-color: #2f353b;
    color: #fff;
}
.cat-chip.is-empty:not(.is-active) { color: #9aa3ad; }
.cat-chip[hidden] { display: none; }
/* «+N بیشتر» / «کمتر»: اگر تراشه‌ها بیش از دو خط شوند (catalog-drawer.js) */
.cat-chip-more {
    border-style: dashed;
    color: var(--primary, #1e88e5);
    font-weight: 700;
}
.cat-chip-menu {
    background: #eaf3ff;
    border-color: #b9d6fb;
    color: var(--primary, #1e88e5);
    font-weight: 700;
}

/* ---------- سبک دوم: کاشی‌های آیکون‌دار (cd-style-grid، پیش‌نمایش با ?drawer=grid) ---------- */
.cd-style-grid .cd-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 12px;
    align-content: start;
    background: #f6f8fb;
}
.cd-style-grid .cd-list li { border: 0; }
.cd-style-grid .cd-list li:first-child { grid-column: 1 / -1; }
.cd-style-grid .cd-row:not(.cd-all) {
    position: relative;
    flex-direction: column;
    justify-content: center;
    gap: 8px;
    min-height: 118px;
    padding: 14px 8px 10px;
    border: 1px solid #e3e8ef;
    border-radius: 14px;
    background: #fff;
    text-align: center;
    font-size: 13px;
    line-height: 1.6;
}
.cd-style-grid .cd-row:not(.cd-all) > i:first-child {
    width: 50px;
    height: 50px;
    line-height: 50px;
    border-radius: 50%;
    background: #eaf3ff;
    color: var(--primary, #1e88e5);
    font-size: 22px;
    opacity: 1;
}
.cd-style-grid .cd-row:not(.cd-all) .cd-text { flex: none; }
.cd-style-grid .cd-row:not(.cd-all) .cd-count {
    position: absolute;
    top: 8px;
    left: 8px;
}
.cd-style-grid .cd-has-sub { display: block; position: relative; }
.cd-style-grid .cd-sub {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 30px;
    height: 30px;
    border: 1px solid #cfe2fb;
    border-radius: 50%;
    background: #eaf3ff;
    font-size: 12px;
}
.cd-style-grid .cd-row.cd-all {
    border: 1px solid #cfe2fb;
    border-radius: 12px;
}
.cd-style-grid .cd-row.is-active:not(.cd-all) {
    border-color: var(--primary, #1e88e5);
    box-shadow: 0 0 0 1px var(--primary, #1e88e5) inset;
}
.cd-style-grid .cd-row.is-active:not(.cd-all) > i:first-child {
    background: var(--primary, #1e88e5);
    color: #fff;
}
.cd-style-grid .cd-row.is-empty:not(.cd-all) > i:first-child {
    background: #f1f3f5;
    color: #9aa3ad;
}
.cd-style-grid .cd-row:not(.cd-all):active { background: #f3f8ff; }

/* فشرده‌سازی خودکار کاشی‌ها: catalog-drawer.js تا وقتی فهرست اسکرول دارد سطح
   بعدی (cd-d1 … cd-d4) را امتحان می‌کند؛ اگر در cd-d4 هم جا نشد، اسکرول می‌گیرد. */
.cd-style-grid .cd-list.cd-d1 { gap: 8px; }
.cd-style-grid .cd-list.cd-d1 .cd-row:not(.cd-all) { min-height: 94px; padding: 10px 6px 8px; gap: 6px; font-size: 12.5px; }
.cd-style-grid .cd-list.cd-d1 .cd-row:not(.cd-all) > i:first-child { width: 40px; height: 40px; line-height: 40px; font-size: 18px; }

.cd-style-grid .cd-list.cd-d2,
.cd-style-grid .cd-list.cd-d3 { grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 10px; }
.cd-style-grid .cd-list.cd-d4 { grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 8px; }
.cd-style-grid .cd-list.cd-d2 .cd-row:not(.cd-all) { min-height: 86px; padding: 18px 4px 6px; gap: 5px; font-size: 12px; line-height: 1.5; }
.cd-style-grid .cd-list.cd-d2 .cd-row:not(.cd-all) > i:first-child { width: 34px; height: 34px; line-height: 34px; font-size: 16px; }
.cd-style-grid .cd-list.cd-d3 .cd-row:not(.cd-all),
.cd-style-grid .cd-list.cd-d4 .cd-row:not(.cd-all) { min-height: 0; padding: 28px 3px 5px; gap: 4px; font-size: 11px; line-height: 1.45; border-radius: 10px; }
.cd-style-grid .cd-list.cd-d3 .cd-row:not(.cd-all) > i:first-child { width: 28px; height: 28px; line-height: 28px; font-size: 13px; }
.cd-style-grid .cd-list.cd-d4 .cd-row:not(.cd-all) > i:first-child { width: 24px; height: 24px; line-height: 24px; font-size: 12px; }
.cd-style-grid .cd-list:is(.cd-d2, .cd-d3, .cd-d4) .cd-row:not(.cd-all) .cd-count { top: 4px; left: 4px; min-width: 0; padding: 0 5px; font-size: 11px; }
.cd-style-grid .cd-list:is(.cd-d2, .cd-d3, .cd-d4) .cd-sub { top: 3px; right: 3px; width: 24px; height: 24px; font-size: 10px; }
.cd-style-grid .cd-list:is(.cd-d2, .cd-d3, .cd-d4) .cd-row.cd-all { min-height: 40px; padding: 6px 12px; font-size: 13px; }

/* تراشه‌ها با آیکون (همراه سبک دوم) */
.cat-chips-icons .cat-chip > i { color: var(--primary, #1e88e5); font-size: 12px; }
.cat-chips-icons .cat-chip.is-active > i { color: #fff; }

/* ---------- در موبایل نوار دسته و ستون آکاردئونی کنار گذاشته می‌شوند ---------- */
@media (max-width: 768px) {
    .category-menu-bar,
    .category-menu-bar.mobile-collapsible.open,
    .catalog-side {
        display: none !important;
    }
}
