:root {
    --bg: #f3f4f8;
    --surface: #ffffff;
    --surface2: #f1f2f6;
    --border: #e1e4ec;
    --text: #1d1f27;
    --text2: #6b7089;

    --accent: #2a398e;
    --accent-hover: #1f2a68;
    --accent-dim: rgba(42, 57, 142, 0.10);

    --danger: #b91c1c;
    --success: #166534;

    --radius: 8px;
    --shadow: 0 2px 8px rgba(20, 24, 40, 0.06);
}

body { font-family: system-ui, sans-serif; margin: 0; color: var(--text); background: var(--bg); }

/* ===== Top bar: brand + (mobile) sidebar toggle on the left, account area on the right ===== */
.topbar { background: transparent; border-bottom: 1px solid var(--border); padding: 8px 20px; display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.topbar-left { display: flex; align-items: center; gap: 10px; }
.sidebar-toggle { display: none; background: none; border: none; color: var(--text); font-size: 1.3rem; line-height: 1; cursor: pointer; padding: 4px; }
.sidebar-toggle:hover { background: var(--surface2); border-radius: 6px; }
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; }
.brand:hover { text-decoration: none; }
.brand-icon { height: 44px; width: auto; }
.brand-wordmark { height: 22px; width: auto; }

.topbar-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.topbar-actions a, .topbar-actions button { color: var(--text); text-decoration: none; background: none; border: none; font: inherit; font-weight: 600; font-size: 0.95rem; cursor: pointer; padding: 0; }
.topbar-actions a:hover, .topbar-actions button:hover { color: var(--accent); text-decoration: underline; }
.nav-user-menu { position: relative; }
.nav-user-menu > summary { display: inline-block; color: var(--text); font-weight: 600; font-size: 0.95rem; cursor: pointer; list-style: none; }
.nav-user-menu > summary:hover { color: var(--accent); text-decoration: underline; }
.nav-user-menu > summary::-webkit-details-marker { display: none; }
.nav-user-menu > summary::after { content: " ▾"; }
.nav-user-menu[open] > summary::after { content: " ▴"; }
.nav-user-menu-list { position: absolute; top: 100%; right: 0; margin-top: 8px; display: flex; flex-direction: column; gap: 2px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; min-width: 200px; z-index: 20; }
.nav-user-menu-list a, .nav-user-menu-list button { display: block; width: 100%; text-align: left; padding: 6px 8px; border-radius: 4px; }
.nav-user-menu-list a { color: var(--text); }
.nav-user-menu-list a:hover { background: var(--surface2); color: var(--accent); text-decoration: none; }
.nav-user-menu-list form { margin: 0; max-width: none; }
.nav-balance { background: var(--accent-dim); color: var(--accent); border: 1px solid rgba(26, 68, 120, 0.25); border-radius: 999px; padding: 6px 14px !important; }
.nav-balance:hover { background: rgba(26, 68, 120, 0.16); color: var(--accent-hover); }
.nav-balance.active { background: rgba(26, 68, 120, 0.2); border-color: rgba(26, 68, 120, 0.45); }

/* ===== App shell: sticky left sidebar + content ===== */
.app-shell { display: flex; align-items: flex-start; }
.sidebar-backdrop { display: none; }
.sidebar { flex: 0 0 220px; width: 220px; box-sizing: border-box; background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px; position: sticky; top: 0; align-self: flex-start; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar-section-title { margin: 16px 0 6px; padding: 0 12px; font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text2); }
.sidebar-link { display: block; padding: 9px 12px; border-radius: var(--radius); color: var(--text); text-decoration: none; font-size: 0.92rem; font-weight: 500; }
.sidebar-link:hover { background: var(--surface2); text-decoration: none; }
.sidebar-link.active { background: var(--accent); color: #fff; font-weight: 600; }

@media (max-width: 899px) {
    .sidebar-toggle { display: inline-flex; align-items: center; justify-content: center; }
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; z-index: 80;
        width: min(260px, 82vw); border-right: 1px solid var(--border);
        transform: translateX(-100%); transition: transform 0.2s ease; overflow-y: auto;
    }
    .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg, 0 16px 40px rgba(20, 24, 40, 0.18)); }
    .sidebar-backdrop.open { display: block; position: fixed; inset: 0; z-index: 75; background: rgba(15, 18, 30, 0.4); }
}

/* Область контента — на всю доступную ширину экрана (без максимальной ширины), адаптивно сужается
   вместе с viewport; боковая панель (sidebar) отъедает фиксированные 220px слева. */
main { flex: 1; min-width: 0; margin: 24px; }

/* Карточка страницы: выделенная шапка (как .view-content/.modal-header в img/style.css) + тело.
   Без overflow:hidden — иначе position:sticky у вложенных элементов (например, фильтров
   Marketplace) перестаёт работать относительно окна прокрутки; скруглённые углы шапки достигаются
   явным border-radius на .page-panel-header, а не обрезкой контейнера. */
.page-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.page-panel-header { padding: 1.1rem 1.5rem; background: var(--surface2); border-bottom: 1px solid var(--border); border-radius: var(--radius) var(--radius) 0 0; }
.page-panel-header h1 { margin: 0; font-size: 1.3rem; }
.page-panel-header p { margin: 6px 0 0; color: var(--text2); font-size: 0.9rem; }
.page-panel-body { padding: 1.5rem; }
.page-panel-body > :first-child { margin-top: 0; }
.page-panel-body > :last-child { margin-bottom: 0; }
form { display: flex; flex-direction: column; gap: 10px; max-width: 480px; margin-bottom: 24px; }
form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
form input, form textarea, form select { padding: 8px; border: 1px solid var(--border); border-radius: 6px; font: inherit; background: var(--surface); color: var(--text); }
form input[type="file"] { padding: 4px 0; border: none; }
button { align-self: flex-start; padding: 8px 16px; border: none; border-radius: var(--radius); background: var(--accent); color: white; cursor: pointer; }
button:hover { background: var(--accent-hover); }
button:disabled { opacity: 0.6; cursor: default; }
button:disabled:hover { background: var(--accent); }
table { width: 100%; border-collapse: collapse; margin-bottom: 24px; }
th, td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); }
.error { color: #b91c1c; background: #fee2e2; padding: 8px 12px; border-radius: 6px; }
.success { color: #166534; background: #dcfce7; padding: 8px 12px; border-radius: 6px; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; margin-bottom: 24px; }
.card { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 4px; font-size: 1.1rem; }
.card-meta { color: var(--text2); font-size: 0.85rem; margin: 0 0 8px; }
.card-actions { display: flex; align-items: center; gap: 12px; margin-top: auto; padding-top: 12px; flex-wrap: wrap; }
.card-actions form { margin-bottom: 0; }

.button, a.button { display: inline-block; padding: 8px 16px; border-radius: var(--radius); background: var(--accent); color: white; text-decoration: none; }
.button:hover, a.button:hover { background: var(--accent-hover); text-decoration: none; }

.card-comments-link { color: var(--accent); }
.card-comments-link:hover { color: var(--accent-hover); }

.dashboard-links { display: flex; flex-wrap: wrap; gap: 8px 20px; margin-top: 16px; }
.dashboard-links a { color: var(--accent); font-weight: 600; text-decoration: none; }
.dashboard-links a:hover { color: var(--accent-hover); text-decoration: underline; }

.comment-thread { margin-top: 32px; }
.comment-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.comment-item { border-bottom: 1px solid #e5e7eb; padding-bottom: 12px; }
.comment-item p { margin: 0 0 4px; }
.comment-meta { color: #6b7280; font-size: 0.85rem; }

.chat-user, .chat-assistant { border-radius: 8px; padding: 8px 12px; border-bottom: none; }
.chat-user { background: #f0f0f0; }
.chat-assistant { background: #eaf3ff; }

.markdown-body :first-child { margin-top: 0; }
.markdown-body :last-child { margin-bottom: 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { margin: 12px 0 6px; }
.markdown-body ul, .markdown-body ol { margin: 6px 0; padding-left: 1.4em; }
.markdown-body code { background: #e5e7eb; border-radius: 3px; padding: 1px 4px; font-size: 0.9em; }
.markdown-body pre { background: #1f2937; color: #f9fafb; padding: 10px; border-radius: 6px; overflow-x: auto; }
.markdown-body pre code { background: none; padding: 0; color: inherit; }

.warning { color: #a15c00; font-size: 0.85rem; margin: 4px 0 0; }

.rating-stars-wrap { display: inline-flex; align-items: center; gap: 6px; font-size: 0.9rem; color: #6b7280; }
.rating-stars { position: relative; display: inline-block; font-size: 1.1rem; line-height: 1; white-space: nowrap; }
.rating-stars-empty { color: #d1d5db; }
.rating-stars-filled { position: absolute; top: 0; left: 0; overflow: hidden; white-space: nowrap; color: #f59e0b; }
.rating-summary { display: flex; align-items: center; }
.rating-widget { margin-top: 32px; }
.rating-breakdown { list-style: none; margin: 0 0 16px; padding: 0; color: #6b7280; font-size: 0.85rem; display: flex; flex-direction: column; gap: 2px; }

/* Единый виджет "отображение + оценка": визуал — та же дорожка .rating-stars/-empty/-filled, что и в
   read-only сводке (см. _RatingStars), только крупнее и с прозрачными хит-зонами (label) поверх
   каждой трети для наведения/клика — заливкой .rating-stars-filled управляет JS (см. _TemplateRatingWidget). */
.star-rating {
    /* align-self: flex-start — обязателен. form { display:flex; flex-direction:column } задан
       глобально (см. правило "form" выше) и по умолчанию (align-items: stretch) растягивает ЛЮБОЙ
       прямой потомок формы, включая этот div, на всю ширину формы (max-width: 480px), независимо
       от display:inline-block здесь — тогда видимые звёзды (по факту содержимого) занимают только
       часть этой ширины слева, а хит-зоны второй/третьей звезды (трети от растянутых 480px)
       уезжают далеко правее реальных звёзд. align-self:flex-start возвращает ширину по контенту. */
    position: relative; display: inline-block; align-self: flex-start; height: 2rem; line-height: 2rem;
}
.star-rating .rating-stars { font-size: 2rem; }
.star-rating input { position: absolute; opacity: 0; width: 1px; height: 1px; overflow: hidden; }
/* top:0 + height явным значением, а не bottom:0 — bottom:0 требует, чтобы у контейнера
   (.star-rating) была definite height по спецификации CSS2.1 10.6.4; inline-block с auto-высотой
   такой не считается, из-за чего хит-зоны 2-й и 3-й звезды схлопывались и наведение всегда
   попадало только на первую (см. явную height: 2rem на .star-rating выше — тот же размер). */
.star-rating label { position: absolute; top: 0; height: 2rem; width: 33.333%; cursor: pointer; }
.star-rating input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 1px; border-radius: 2px; }
.star-rating-caption { min-height: 1.2em; margin: 4px 0 12px; color: #374151; font-size: 0.9rem; }

.better-template-field { position: relative; display: flex; flex-direction: column; gap: 4px; max-width: 480px; }
.autocomplete-list { list-style: none; margin: 0; padding: 4px 0; position: absolute; top: 100%; left: 0; right: 0; background: #fff; border: 1px solid #e5e7eb; border-radius: 4px; max-height: 220px; overflow-y: auto; z-index: 10; box-shadow: 0 4px 10px rgba(0,0,0,0.08); }
.autocomplete-list li { padding: 6px 10px; cursor: pointer; }
.autocomplete-list li:hover, .autocomplete-list li:focus { background: var(--accent-dim); }
.autocomplete-empty { padding: 6px 10px; color: #6b7280; font-size: 0.85rem; cursor: default; }

/* Marketplace */
.marketplace-hero { margin-bottom: 20px; }
.marketplace-subtitle { margin: 0 0 4px; font-size: 1.05rem; font-weight: 600; color: var(--text); }
.marketplace-lead { margin: 0 0 10px; max-width: 720px; color: var(--text2); }
.marketplace-workflow { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 0 0 18px; color: var(--text2); font-size: 0.82rem; }
.marketplace-workflow span[aria-hidden] { color: var(--border); }

.marketplace-search { position: relative; max-width: 720px; margin-bottom: 16px; }
.marketplace-search-icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); opacity: 0.55; pointer-events: none; }
.marketplace-search input { box-sizing: border-box; width: 100%; padding: 14px 16px 14px 42px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); font-size: 1.05rem; box-shadow: var(--shadow); }
.marketplace-search input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }

.marketplace-categories { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.category-chip { flex: 0 0 auto; padding: 7px 14px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: var(--text); font-size: 0.88rem; font-weight: 600; white-space: nowrap; cursor: pointer; }
.category-chip:hover { background: var(--surface); border-color: var(--accent); color: var(--accent); }
.category-chip.active { border-color: var(--accent); background: var(--accent-dim); color: var(--accent); }

.marketplace-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 16px; padding: 10px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.marketplace-count { color: var(--text2); font-size: 0.9rem; }
.marketplace-count strong { color: var(--text); }
.marketplace-toolbar-right { display: flex; align-items: center; gap: 12px; }
.marketplace-sort { display: flex; align-items: center; gap: 6px; color: var(--text2); font-size: 0.88rem; }
.marketplace-sort select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); font: inherit; }
.filters-toggle { display: none; padding: 7px 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); font-weight: 600; cursor: pointer; }
.filters-toggle:hover { background: var(--surface); border-color: var(--accent); color: var(--accent); }

.marketplace-layout { display: grid; grid-template-columns: 240px minmax(0, 1fr); align-items: start; gap: 20px; }
.filter-sidebar-backdrop { display: none; }
.filter-sidebar { position: sticky; top: 16px; display: flex; flex-direction: column; gap: 14px; padding: 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.filter-sidebar-header { display: none; align-items: center; justify-content: space-between; }
.filter-sidebar-header h2 { margin: 0; font-size: 1rem; }
.filter-sidebar-close { border: none; background: none; color: var(--text2); font-size: 1.4rem; line-height: 1; cursor: pointer; }
.filter-sidebar-close:hover { background: var(--surface2); color: var(--text); }
.filter-group { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0 0 14px; border: none; border-bottom: 1px solid var(--border); }
.filter-group:last-of-type { padding-bottom: 0; border-bottom: none; }
.filter-group legend { padding: 0 0 6px; color: var(--text2); font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }
.filter-checkbox { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; cursor: pointer; }
.filter-checkbox input { margin: 0; }
.filters-reset { align-self: flex-start; padding: 6px 0; border: none; background: none; color: var(--accent); font-weight: 600; cursor: pointer; }
.filters-reset:hover { background: none; color: var(--accent-hover); text-decoration: underline; }

/* auto-fill вместо фиксированного числа колонок — сетка сама подстраивается под ширину экрана */
.card-grid.marketplace-grid { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.marketplace-empty { padding: 32px 0; color: var(--text2); text-align: center; }

.template-card { justify-content: space-between; }
.template-card-category { display: inline-block; margin-bottom: 8px; padding: 3px 8px; border-radius: 4px; background: var(--accent-dim); color: var(--accent); font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }
.template-card-title { margin: 0 0 8px; font-size: 1.05rem; line-height: 1.3; }
.template-card-title a { color: var(--text); text-decoration: none; }
.template-card-title a:hover { color: var(--accent); }
.template-card-description { display: -webkit-box; overflow: hidden; margin: 0 0 10px; color: var(--text2); font-size: 0.88rem; line-height: 1.4; -webkit-box-orient: vertical; -webkit-line-clamp: 3; }
.template-card-meta { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; color: var(--text2); font-size: 0.85rem; }
.template-card-downloads { white-space: nowrap; }
.template-card-author { margin: 0; color: var(--text2); font-size: 0.82rem; }
.template-card-footer { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.template-card-price { font-size: 1.2rem; font-weight: 700; color: var(--text); }
.template-card-price.is-free { color: var(--success); }
.template-card-cta { padding: 8px 16px; }

@media (max-width: 1023px) {
    .marketplace-layout { grid-template-columns: 1fr; }
    .filters-toggle { display: inline-block; }
    .filter-sidebar-header { display: flex; }
    .filter-sidebar {
        position: fixed; top: 0; right: 0; bottom: 0; z-index: 60;
        width: min(320px, 86vw); border-radius: 0;
        transform: translateX(100%); transition: transform 0.2s ease; overflow-y: auto;
    }
    .filter-sidebar.open { transform: translateX(0); }
    .filter-sidebar-backdrop.open { display: block; position: fixed; inset: 0; z-index: 50; background: rgba(15, 18, 30, 0.4); }
}

@media (max-width: 599px) {
    .marketplace-toolbar { flex-direction: column; align-items: stretch; }
    .marketplace-toolbar-right { justify-content: space-between; }
}
