.layout-container {
    min-height: var(--app-dvh);
    display: flex;
    background: var(--bg-base, #FAFAF7);
    overflow-x: clip;
    --layout-primary: var(--primary-600);
    --layout-secondary: var(--primary-500);
}

/* Override BlazorBootstrap `main { height: 100vh }` — only body should scroll */
main.page-content,
.layout-container main {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
}

#layoutRoot.subscription-locked {
    position: relative;
}

#layoutRoot.subscription-locked::after {
    content: "";
    position: fixed;
    inset: 0;
    background: transparent;
    z-index: var(--z-modal);
    pointer-events: auto;
}

.modern-sidebar {
    width: var(--sidebar-width);
    height: var(--app-dvh);
    background: linear-gradient(180deg, #f6f4ed 0%, var(--bg-muted, #F1EFE8) 100%);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    top: 0;
    z-index: var(--z-sidebar);
    border-right: 0.5px solid var(--border-subtle, rgba(0,0,0,0.08));
    box-shadow: none;
    transition: transform 0.25s var(--ease-out, ease);
    overflow: hidden;
    overscroll-behavior-y: contain;
    touch-action: pan-y;
    scrollbar-width: thin;
    scrollbar-color: var(--border-default, rgba(0,0,0,0.15)) transparent;
    font-family: var(--font-sans, 'Inter', sans-serif);
}

.sidebar-header {
    padding: 14px var(--sidebar-header-pad-x);
    border-bottom: 0.5px solid var(--border-subtle, rgba(0,0,0,0.08));
    display: flex;
    align-items: center;
    gap: clamp(10px, calc(var(--section-gap) - 13px), 14px);
}

.logo-container {
    background: var(--bg-surface, #ffffff);
    padding: 6px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: none;
}

.logo-image {
    height: 30px;
    width: auto;
    object-fit: contain;
}

.brand-text {
    color: var(--text-primary, #1A1A18);
    font: 500 15px/1.2 var(--font-sans, 'Inter');
    letter-spacing: -0.01em;
}

.brand-subtitle {
    color: var(--text-tertiary, #888780);
    font: 400 11px/1.3 var(--font-sans, 'Inter');
    margin-top: 2px;
}

.layout-container .main-content {
    margin-left: var(--sidebar-width);
    width: calc(100% - var(--sidebar-width));
    flex: 1 1 auto;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    min-width: 0;
    min-height: var(--app-dvh);
    overflow: visible;
    transition: margin-left 0.3s ease;
}

#layoutRoot:not(.sidebar-open) .main-content,
.layout-container:not(.sidebar-open) .main-content {
    margin-left: 0;
    width: 100%;
}

.page-content {
    /* Override BlazorBootstrap global `main { height: 100vh; }` to avoid extra page scroll. */
    height: auto;
    min-height: max(0px, calc(var(--app-dvh) - var(--topbar-height) - var(--layout-footer-height)));
    min-width: 0;
    overflow: visible;
    overflow-y: visible;
    background: var(--bg-base, #FAFAF7);
    container-type: inline-size;
}

.page-content > * {
    min-width: 0;
    max-width: 100%;
}

.page-content > * > * {
    min-width: 0;
}

.page-content img,
.page-content video,
.page-content canvas,
.page-content svg,
.page-content iframe,
.page-content embed,
.page-content object {
    max-width: 100%;
}

.page-content :where(.container, .container-fluid, .row, [class^="col-"], [class*=" col-"]) {
    min-width: 0;
    max-width: 100%;
}

.page-content :where(.container, .container-fluid) {
    padding-left: max(16px, var(--page-gutter-x));
    padding-right: max(16px, var(--page-gutter-x));
}

.page-content :where(
    [class$="-page"],
    [class$="-shell"],
    [class$="-hero"],
    [class$="-grid"],
    [class$="-card"],
    [class$="-panel"],
    [class$="-content"],
    [class$="-header"],
    [class$="-body"],
    [class$="-toolbar"],
    [class$="-filters"],
    [class$="-actions"],
    [class$="-stats"],
    [class$="-meta"],
    [class$="-section"],
    [class$="-detail"],
    [class$="-list"],
    [class$="-table"],
    [class$="-module"],
    [class$="-topic"],
    [class$="-resource"],
    [class$="-result"],
    [class$="-video"],
    [class$="-vocabulary"],
    [class$="-tips"],
    [class$="-grammar"],
    [class$="-toeic"],
    [class$="-aptis"],
    [class$="-vstep"]
) {
    min-width: 0;
    max-width: 100%;
}

.page-content :where(
    [class^="toeic700-"],
    [class*=" toeic700-"],
    [class^="grammar-"],
    [class*=" grammar-"],
    [class^="tips-"],
    [class*=" tips-"],
    [class^="vocabulary-"],
    [class*=" vocabulary-"],
    [class^="results-"],
    [class*=" results-"],
    [class^="resource-"],
    [class*=" resource-"],
    [class^="video-"],
    [class*=" video-"],
    [class^="module-"],
    [class*=" module-"],
    [class^="part-"],
    [class*=" part-"],
    [class^="notify-"],
    [class*=" notify-"],
    [class^="about-"],
    [class*=" about-"],
    [class^="support-"],
    [class*=" support-"],
    [class^="terms-"],
    [class*=" terms-"],
    [class^="profile-"],
    [class*=" profile-"],
    [class^="settings-"],
    [class*=" settings-"],
    [class^="seo-landing__"],
    [class*=" seo-landing__"]
) {
    min-width: 0;
    max-width: 100%;
}

.page-content :where(
    .do-test-container,
    .test-wrapper,
    .test-header-bar,
    .progress-bar-section,
    .display-mode-bar,
    .part-navigation,
    .test-content,
    .question-container,
    .test-action-bar,
    .test-translation-bar,
    .results-table,
    .table-wrap,
    .resource-preview,
    .toeic700-language-table-wrap,
    .toeic700-reading-intro-table-wrap,
    .tips-key-table-wrap
) {
    min-width: 0;
    max-width: 100%;
}

.page-content :where(
    .test-info,
    .test-header-actions,
    .selection-step-header,
    .selection-toolbar,
    .selection-actions,
    .results-control-row,
    .results-filter-group,
    .results-search,
    .resource-section-header,
    .video-title-row,
    .hero-actions,
    .hero-tags,
    .topbar-community-links,
    .user-profile
) {
    min-width: 0;
}

.page-content :where(
    .results-table,
    .table-wrap,
    .toeic700-language-table-wrap,
    .toeic700-reading-intro-table-wrap,
    .tips-key-table-wrap,
    .resource-preview
) {
    overflow-x: auto;
}

.page-content :where(h1, h2, h3, h4, h5, h6, p, blockquote, li) {
    min-width: 0;
}

.page-content :where(
    .hero-title,
    .hero-lead,
    .module-options-title,
    .part-selection-title,
    .card-title,
    .resource-title,
    .video-title-row h2,
    .toeic700-card-title,
    .review-board-title
) {
    overflow-wrap: anywhere;
}

.layout-footer {
    flex-shrink: 0;
    background: #efede7;
    padding: 42px clamp(24px, 4vw, 56px) 20px;
    border-top: 1px solid var(--border-color);
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.dashboard-footer__grid {
    width: min(100%, 1240px);
    margin: 0 auto;
    display: grid;
    grid-template-columns: minmax(320px, 1.55fr) repeat(3, minmax(130px, .7fr));
    gap: clamp(28px, 4vw, 56px);
    align-items: start;
}

.dashboard-footer__brand-row {
    display: flex;
    align-items: center;
    gap: 11px;
    width: fit-content;
    color: var(--text-primary, #17251f);
    text-decoration: none;
}

.dashboard-footer__brand-mark {
    display: grid;
    width: 38px;
    height: 38px;
    place-items: center;
    border-radius: 11px;
    background: var(--layout-primary, #0e5a4b);
    color: #fff;
    box-shadow: 0 6px 16px rgba(14, 90, 75, .16);
    font-size: 1.3rem;
    font-weight: 800;
}

.dashboard-footer__brand-row strong,
.dashboard-footer__brand-row small {
    display: block;
}

.dashboard-footer__brand-row strong {
    font-size: 1rem;
    line-height: 1.15;
}

.dashboard-footer__brand-row small {
    margin-top: 2px;
    color: var(--text-secondary);
    font-size: .62rem;
    letter-spacing: .055em;
}

.dashboard-footer__brand > p {
    max-width: 480px;
    margin: 14px 0 18px;
    color: var(--text-secondary);
    font-size: .8rem;
    line-height: 1.65;
}

.dashboard-footer__business-card {
    max-width: 540px;
    padding: 16px 17px;
    border: 1px solid #d9ddd9;
    border-left: 4px solid var(--layout-primary, #0e5a4b);
    border-radius: 12px;
    background: rgba(255, 255, 255, .78);
    box-shadow: 0 5px 18px rgba(36, 52, 47, .045);
}

.dashboard-footer__business-card h2,
.dashboard-footer__column h2 {
    margin: 0;
    color: var(--text-primary, #17251f);
    font-size: .75rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.dashboard-footer__business-card h2 {
    margin-bottom: 10px;
    color: var(--layout-primary, #0e5a4b);
    font-size: .82rem;
}

.dashboard-footer__business-card p {
    margin: 0 0 7px;
    color: var(--text-secondary);
    font-size: .76rem;
    line-height: 1.55;
}

.dashboard-footer__business-card strong {
    color: var(--text-primary, #17251f);
}

.dashboard-footer__contact-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 18px;
    margin-top: 11px;
    padding-top: 11px;
    border-top: 1px solid #e3e6e3;
}

.dashboard-footer__contact-row a {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    color: var(--layout-primary, #0e5a4b);
    font-size: .76rem;
    font-weight: 800;
    text-decoration: none;
    transition: opacity .18s ease, transform .18s ease;
}

.dashboard-footer__contact-row a:hover {
    opacity: .75;
    transform: translateY(-1px);
}

.dashboard-footer__contact-row span {
    display: grid;
    width: 24px;
    height: 24px;
    place-items: center;
    border-radius: 7px;
    background: #e7f0ed;
    font-size: .78rem;
}

.dashboard-footer__column {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
}

.dashboard-footer__column h2 {
    margin: 4px 0 4px;
}

.dashboard-footer__column a,
.dashboard-footer__bottom a {
    color: var(--text-secondary);
    font-size: .76rem;
    line-height: 1.45;
    text-decoration: none;
    transition: color .18s ease, transform .18s ease;
}

.dashboard-footer__column a:hover,
.dashboard-footer__bottom a:hover {
    color: var(--layout-primary, #0e5a4b);
    font-weight: 700;
}

.dashboard-footer__column a:hover {
    transform: translateX(2px);
}

.dashboard-footer__bottom {
    width: min(100%, 1240px);
    margin: 30px auto 0;
    padding-top: 17px;
    border-top: 1px solid #dedbd2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    color: #858d89;
    font-size: .68rem;
}

.dashboard-footer__bottom > div {
    display: flex;
    flex-wrap: wrap;
    gap: 17px;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: var(--z-sidebar-overlay);
    pointer-events: none;
}

#layoutRoot:not(.sidebar-open) .modern-sidebar,
.layout-container:not(.sidebar-open) .modern-sidebar {
    transform: translateX(-100%);
}

#layoutRoot.sidebar-open .top-navbar,
.layout-container.sidebar-open .top-navbar {
    z-index: var(--z-topbar-elevated);
}

/* Keep mobile drawer hidden until JS resolves first viewport state. */
#layoutRoot.sidebar-open:not(.layout-ready) .modern-sidebar {
    transform: translateX(-100%);
}

#layoutRoot.sidebar-open:not(.layout-ready) .sidebar-overlay {
    display: none;
}

#blazor-error-ui {
    background: var(--danger-gradient);
    bottom: 0;
    box-shadow: 0 -1px 10px rgba(0, 0, 0, 0.2);
    display: none;
    left: 0;
    padding: 15px 20px;
    position: fixed;
    width: 100%;
    z-index: var(--z-blazor-error);
    color: white;
    font-weight: 500;
}

#blazor-error-ui .reload {
    color: white;
    margin-left: 15px;
    text-decoration: underline;
}

#blazor-error-ui .dismiss {
    cursor: pointer;
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.5rem;
}

.modern-sidebar::-webkit-scrollbar {
    width: 6px;
}

.modern-sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.modern-sidebar::-webkit-scrollbar-thumb {
    background: var(--border-default, rgba(0, 0, 0, 0.15));
    border-radius: 3px;
}

.modern-sidebar::-webkit-scrollbar-thumb:hover {
    background: var(--border-strong, rgba(0, 0, 0, 0.25));
}

@media (max-width: 1024px) {
    .layout-footer {
        padding: 34px 24px 18px;
    }

    .dashboard-footer__grid {
        grid-template-columns: minmax(280px, 1.4fr) repeat(2, minmax(140px, 1fr));
    }

    .dashboard-footer__column:last-child {
        grid-column: 2 / -1;
    }
}

@media (max-width: 900px),
       (max-width: 1180px) and (pointer: coarse),
       (max-width: 1180px) and (hover: none) {
    #layoutRoot.sidebar-open .sidebar-overlay,
    .layout-container.sidebar-open .sidebar-overlay,
    .sidebar-overlay.active {
        display: block;
        pointer-events: auto;
    }

    .modern-sidebar {
        width: min(96vw, 360px);
        max-width: 360px;
        z-index: var(--z-sidebar-mobile);
    }

    .layout-container .main-content {
        margin-left: 0;
        width: 100%;
    }
}

@media (max-width: 768px) {
    .layout-footer {
        padding: 30px 20px 16px;
    }

    .dashboard-footer__grid {
        grid-template-columns: 1fr 1fr;
        gap: 28px 20px;
    }

    .dashboard-footer__brand {
        grid-column: 1 / -1;
    }

    .dashboard-footer__column:last-child {
        grid-column: auto;
    }

    .dashboard-footer__bottom {
        align-items: flex-start;
        flex-direction: column;
        gap: 10px;
    }
}

@media (max-width: 440px) {
    .dashboard-footer__grid {
        grid-template-columns: 1fr;
    }

    .dashboard-footer__brand,
    .dashboard-footer__column:last-child {
        grid-column: auto;
    }
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .layout-container {
        background: var(--bg-base, #101312);
    }

    :root:not([data-theme="light"]) .modern-sidebar {
        background: linear-gradient(180deg, var(--bg-surface, #171D1A) 0%, var(--bg-base, #101312) 100%);
        color: var(--text-primary, #F4F6F3);
        border-right-color: var(--border-subtle, rgba(255,255,255,0.08));
    }

    :root:not([data-theme="light"]) .sidebar-header,
    :root:not([data-theme="light"]) .layout-footer {
        background: var(--bg-surface, #171D1A);
        border-color: var(--border-subtle, rgba(255,255,255,0.08));
    }

    :root:not([data-theme="light"]) .logo-container {
        background: var(--bg-elevated, #222923);
        border: 1px solid var(--border-subtle, rgba(255,255,255,0.08));
    }
}

[data-theme="dark"] .layout-container {
    background: var(--bg-base, #101312);
}

[data-theme="dark"] .modern-sidebar {
    background: linear-gradient(180deg, var(--bg-surface, #171D1A) 0%, var(--bg-base, #101312) 100%);
    color: var(--text-primary, #F4F6F3);
    border-right-color: var(--border-subtle, rgba(255,255,255,0.08));
}

[data-theme="dark"] .sidebar-header,
[data-theme="dark"] .layout-footer {
    background: var(--bg-surface, #171D1A);
    border-color: var(--border-subtle, rgba(255,255,255,0.08));
}

[data-theme="dark"] .logo-container {
    background: var(--bg-elevated, #222923);
    border: 1px solid var(--border-subtle, rgba(255,255,255,0.08));
}
