/*
  Page Layout System v2
  Controls: body[data-layout="1col|2col"]
  Structure:
    .page-layout-wrapper (flex container)
      .main-with-sidebar (main content, flex:1)
      .page-sidebar (sidebar, 280px)
*/

/* ===== Main Layout Wrapper (flex) ===== */
.page-layout-wrapper {
    display: flex;
    gap: 24px;
    max-width: 1200px;  /* 默认=标准(standard)，让wide有实际效果 */
    margin: 0 auto;
    padding: 20px 16px;
    align-items: flex-start;
    /* 防止内部元素（标签云、宽表格、长文本）撑破容器宽度 */
    overflow-x: hidden;
    box-sizing: border-box;
}

/* 主内容区 —— 弹性填充剩余空间，严格不溢出 */
.main-with-sidebar {
    flex: 1;
    min-width: 0;          /* 关键：允许 flex 子项收缩到小于内容宽度 */
    max-width: 100%;      /* 不超过父容器 */
    overflow-x: hidden;   /* 裁剪超宽子元素（标签云/grid/代码块） */
    box-sizing: border-box;
}

/* 侧边栏 —— 固定宽度，在页面宽度内部 */
.page-sidebar {
    width: 280px;
    flex-shrink: 0;
    overflow-x: hidden;
    box-sizing: border-box;
}

/* Page width from admin settings */
body[data-width="narrow"] .page-layout-wrapper,
body[data-width="narrow"] .home-v2 { max-width: 760px; }
body[data-width="standard"] .page-layout-wrapper,
body[data-width="standard"] .home-v2 { max-width: 1100px; }
body[data-width="wide"] .page-layout-wrapper,
body[data-width="wide"] .home-v2 { max-width: 1440px; }
body[data-width="full"] .page-layout-wrapper,
body[data-width="full"] .home-v2 { max-width: 100%; padding-left:20px;padding-right:20px; }

/* Sidebar hidden via data-sidebar=0 or toggle */
body[data-sidebar="0"] .page-sidebar,
body.sidebar-force-hide .page-sidebar {
    display: none !important;
}
body[data-sidebar="0"] .main-with-sidebar,
body.sidebar-force-hide .main-with-sidebar {
    max-width: 100%;
    flex: none;
    width: 100%;
}
body[data-sidebar="0"] .page-layout-wrapper,
body.sidebar-force-hide .page-layout-wrapper {
    display: block;
}

/* 1-column: hide sidebar, content full width */
body[data-layout="1col"] .page-sidebar {
    display: none !important;
}

body[data-layout="1col"] .main-with-sidebar {
    max-width: 100%;
    margin: 0 auto;
    flex: none;
}

body[data-layout="1col"] .page-layout-wrapper {
    display: block;
    max-width: 100%;
    padding: 20px 16px;
}

body[data-layout="1col"] .home-v2 {
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
}

/* Article detail specific */
body[data-layout="1col"] .article-detail-layout {
    max-width: 100%;
    margin: 0 auto;
}

body:not([data-layout="1col"]) .article-detail-layout {
    max-width: 100%;
}

/* Filter card / grid pages (article list, encyclopedia, library, album) */
body:not([data-layout="1col"]) .main-with-sidebar > .filter-card,
body:not([data-layout="1col"]) .main-with-sidebar > .article-grid,
body:not([data-layout="1col"]) .main-with-sidebar > .encyclopedia-list,
body:not([data-layout="1col"]) .main-with-sidebar > .library-grid,
body:not([data-layout="1col"]) .main-with-sidebar > .waterfall-full {
    max-width: 100%;
}

/* 筛选卡在两栏布局下不要过宽 */
body:not([data-layout="1col"]) .main-with-sidebar > .filter-card {
    max-width: 860px;
}

/* Footer adjustment when sidebar exists */
body:not([data-layout="1col"]) .footer--with-sidebar {
    /* footer already handles this via JS */
}

/* Sidebar inner styling */
.page-sidebar .sidebar-inner {
    position: sticky;
    top: 80px;
    max-height: calc(100vh - 100px);
    overflow-y: auto;
    scrollbar-width: none;
}
.page-sidebar .sidebar-inner::-webkit-scrollbar {
    display: none;
}

/* Sidebar widget base */
.sidebar-widget {
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color, #eee);
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 16px;
}
.sidebar-widget:last-child {
    margin-bottom: 0;
}
.widget-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-primary, #333);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--color-primary, #913500);
}
.widget-title svg {
    color: var(--color-primary, #913500);
    flex-shrink: 0;
}

/* Widget list styles */
.widget-list {
    list-style: none;
    padding: 0;
    margin: 0;
}
.widget-list-text li {
    padding: 4px 0;
    border-bottom: 1px solid rgba(0,0,0,0.04);
}
.widget-list-text li:last-child { border-bottom: none; }
.wlt-rank {
    font-size: 11px;
    color: #999;
    margin-right: 6px;
}
.wlt-title {
    font-size: 13px;
    color: var(--color-text-secondary, #666);
    text-decoration: none;
    transition: color 0.2s;
}
.wlt-title:hover {
    color: var(--color-primary, #913500);
}

/* Widget article list (with cover) */
.widget-list-article li {
    display: flex;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid rgba(0,0,0,0.04);
}
.widget-list-article li:last-child { border-bottom: none; }
.wla-cover {
    width: 60px;
    height: 44px;
    border-radius: 6px;
    overflow: hidden;
    flex-shrink: 0;
}
.wla-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.wla-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.wla-title {
    font-size: 13px;
    color: var(--color-text-primary, #333);
    text-decoration: none;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.wla-title:hover { color: var(--color-primary, #913500); }
.wla-views {
    font-size: 11px;
    color: #999;
    display: flex;
    align-items: center;
    gap: 3px;
}

/* Widget book list */
.widget-list-book li {
    display: flex;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid rgba(0,0,0,0.04);
}
.widget-list-book li:last-child { border-bottom: none; }
.wlb-cover {
    width: 40px;
    height: 56px;
    border-radius: 4px;
    overflow: hidden;
    flex-shrink: 0;
}
.wlb-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.wlb-cover-placeholder {
    width: 40px;
    height: 56px;
    background: linear-gradient(135deg, #f0e8dc, #e0d4c4);
    border-radius: 4px;
    flex-shrink: 0;
}
.wlb-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.wlb-title {
    font-size: 13px;
    color: var(--color-text-primary, #333);
    text-decoration: none;
    line-height: 1.4;
}
.wlb-author {
    font-size: 11px;
    color: #999;
}

/* Widget tags cloud */
.widget-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
.widget-tag {
    display: inline-block;
    padding: 3px 10px;
    background: #f5f5f5;
    border-radius: 14px;
    font-size: 12px;
    color: #666;
    text-decoration: none;
    transition: all 0.2s;
}
.widget-tag:hover {
    background: var(--color-primary, #913500);
    color: #fff;
}

/* ===== Widget Areas (content_top / content_bottom / page_bottom) ===== */

/* content_top: inside main-with-sidebar, above filter */
.widget-area-top {
    margin-bottom: 20px;
}

/* content_bottom: inside main-with-sidebar, below list/pagination */
.widget-area-bottom {
    margin-top: 20px;
}

/* page_bottom: full width, below page-layout-wrapper (across sidebar) */
.widget-area-page-bottom {
    max-width: 1200px;  /* 默认=standard */
    margin: 0 auto;
    padding: 20px 16px 0;
    width: 100%;
}

body[data-width="narrow"] .widget-area-page-bottom { max-width: 760px; }
body[data-width="standard"] .widget-area-page-bottom { max-width: 1100px; }
body[data-width="wide"] .widget-area-page-bottom { max-width: 1440px; }
body[data-width="full"] .widget-area-page-bottom { max-width: 100%; padding-left:20px;padding-right:20px; }

/* Widget area inner: same card style as sidebar widgets */
.widget-area-inner {
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.widget-area-top .sidebar-widget,
.widget-area-bottom .sidebar-widget {
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color, #eee);
    border-radius: 10px;
    padding: 16px;
}
.widget-area-page-bottom .widget-area-inner {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 16px;
}
.widget-area-page-bottom .sidebar-widget {
    flex: 1;
    min-width: 220px;
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color, #eee);
    border-radius: 10px;
    padding: 16px;
}

/* ===== Sidebar Fixed / Float Behavior ===== */
.page-sidebar.sidebar-fixed .sidebar-inner {
    position: sticky;
    top: 80px;
    max-height: calc(100vh - 100px);
    overflow-y: auto;
    scrollbar-width: none;
}
.page-sidebar.sidebar-fixed .sidebar-inner::-webkit-scrollbar {
    display: none;
}

.page-sidebar.sidebar-float .sidebar-inner {
    position: static;
}

/* Sidebar toggle button */
.sidebar-toggle-btn {
    display: none;
    position: fixed;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 900;
    width: 42px; height: 42px;
    border-radius: 50%;
    background: #f0f0f0;
    color: #666;
    border: none;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.12);
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.2s;
}
.sidebar-toggle-btn:hover { background: #e0e0e0; color: #333; }

/* When sidebar is hidden */
.page-sidebar.sidebar-hidden {
    margin-right: -280px !important;
    opacity: 0.3;
    pointer-events: none;
    transition: margin-right 0.35s ease, opacity 0.35s ease;
}
.page-sidebar:not(.sidebar-hidden) {
    transition: margin-right 0.35s ease, opacity 0.35s ease;
}
.main-with-sidebar.has-sidebar-hidden {
    max-width: 100% !important;
    flex: none;
    width: 100%;
    transition: max-width 0.35s ease;
}

/* Show toggle button on desktop */
@media (min-width: 1025px) {
    .sidebar-toggle-btn {
        display: flex;
    }
}

/* Mobile: sidebar overlay */
@media (max-width: 1024px) {
    .page-sidebar {
        position: fixed;
        top: 0;
        right: -100%;
        width: 280px;
        height: 100vh;
        z-index: 2000;
        background: #fff;
        box-shadow: -4px 0 20px rgba(0,0,0,0.15);
        overflow-y: auto;
        padding-top: 60px;
        transition: right 0.3s ease;
        margin-right: 0 !important;
    }
    .page-sidebar.sidebar-open { right: 0; }
    .sidebar-toggle-btn { display: flex; }
    body.sidebar-overlay-open { overflow: hidden; }
}

/* ===== Page Top Widget Area (full width, above layout wrapper) ===== */
.widget-area-page-top {
    max-width: 1200px;  /* 默认=standard */
    margin: 0 auto;
    padding: 12px 16px 0;
}

/* Filter card inside page_top widget - full width */
.widget-area-page-top .filter-card {
    max-width: 100%;
    margin: 0 0 12px 0;
}

/* ===== 防溢出保护（全局兜底） ===== */

/* 确保 main-content 内所有直接子元素不超出 */
.main-with-sidebar > * {
    max-width: 100%;
    box-sizing: border-box;
}

/* 筛选栏 / 标签云 / 表单行 —— 允许换行，禁止撑出 */
.filter-row,
.filter-card,
.cat-filter,
.tag-cloud,
.content-list {
    max-width: 100%;
    overflow-wrap: break-word;
    word-break: break-word;
}

/* Grid 布局容器：防止子项 min-width 撑破 */
.card-grid,
.enc-list,
.enc-list > a,
.encyclopedia-list-layout {
    max-width: 100%;
}
.card-grid {
    width: 100%;
}

/* 图片和媒体元素不撑破容器 */
.main-with-sidebar img,
.main-with-sidebar video,
.main-with-sidebar iframe {
    max-width: 100%;
    height: auto;
}

/* body 层面防止水平滚动条（保留竖向） */
html, body {
    overflow-x: hidden;
}

