/**
 * alg-template.css - Styles for the redesigned algorithm template page
 * Layout: Top section with case details, scrollable grid below
 */

/* ============================================================================
   Container Layout
   ============================================================================ */

.algs-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 52px);
    /* Account for navbar */
    max-height: calc(100vh - 52px);
    overflow: hidden;
    padding: 0.75rem;
    gap: 0.5rem;
}

/* ============================================================================
   Header Section
   ============================================================================ */

.algs-header {
    flex-shrink: 0;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.75rem 1rem;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}

/* Allow absolute-centering of the header-case block */
.algs-header .header-content {
    position: relative;
}

.algs-header .header-title {
    flex: 0 0 auto;
}

.algs-header .header-actions {
    flex: 0 0 auto;
}

.algs-header .header-case {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    z-index: 2;
}

/* Mobile: fall back to normal flow so layout remains stackable */
@media (max-width: 767.98px) {
    .algs-header .header-case {
        position: static;
        transform: none;
        left: auto;
        top: auto;
        justify-content: center;
        width: auto;
    }

    .algs-header .header-title,
    .algs-header .header-actions {
        flex: 0 0 auto;
    }
}

.header-title {
    display: flex;
    align-items: center;
}

.header-title h1 {
    font-size: 1.25rem;
    font-weight: 700;
    margin: 0;
}

.header-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.breadcrumb-nav {
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border-color);
}

.breadcrumb-nav .breadcrumb {
    font-size: 0.875rem;
}

.breadcrumb-nav .breadcrumb-item.active {
    color: var(--text-color) !important;
}

.breadcrumb-nav .breadcrumb-item a {
    color: var(--primary-color, #0d6efd);
}

/* Match /algs modal card layout for subset selection */
#subsetModal .modal-dialog {
    display: flex;
    justify-content: center;
    pointer-events: none;
}

#subsetModal .modal-content {
    display: inline-block;
    pointer-events: auto;
    min-width: calc(4 * 180px + 3 * 1rem + 8rem);
    max-width: 95vw;
    padding-left: 2rem;
    padding-right: 2rem;
}

#subsetModal .modal-body>.row {
    justify-content: center;
}

#subsetModal .algset-card {
    position: relative;
}

/* Keep subset modal layout consistent with /algs page */
#subsetModal .algset-card {
    width: 120px;
    min-height: 140px;
    margin: 0 auto;
}

@media (max-width: 991.98px) {
    #subsetModal #subsetGrid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
        gap: 0.5rem;
    }

    #subsetModal #subsetGrid>div {
        width: auto !important;
        max-width: none !important;
        flex: none !important;
        padding-left: 0;
        padding-right: 0;
    }

    #subsetModal #subsetGrid .algset-card {
        width: 100%;
        max-width: 120px;
    }
}

@media (min-width: 992px) {
    #subsetModal #subsetGrid {
        display: flex !important;
        flex-wrap: wrap;
        justify-content: center;
        align-items: flex-start;
        gap: 0.5rem;
    }

    #subsetModal #subsetGrid>div {
        display: flex;
        justify-content: center;
        align-items: stretch;
        width: auto;
        padding-left: 0;
        padding-right: 0;
    }
}

@media (max-width: 900px) {
    #subsetModal .modal-content {
        min-width: 0;
        padding-left: 1rem;
        padding-right: 1rem;
    }
}

/* ============================================================================
   Case Details Section (Top Panel)
   ============================================================================ */

.case-details-section {
    flex-shrink: 0;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.4rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    /* Prevent the section from collapsing so small that content becomes invisible */
    min-height: 220px;
}

/* Row 1: Case Header */
.case-header-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}

.case-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0;
}

.case-title .badge {
    font-size: 0.75rem;
}

.add-alg-btn {
    padding: 0.25rem 0.5rem;
}

/* Row 2: Preview Area with Navigation */
.case-preview-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    min-height: 140px;
}

/* Make the preview row a positioning context for the hitzone overlay */
.case-preview-row {
    position: relative;
}

/* Case Navigation Previews */
.case-nav-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    position: relative;
    flex: 0 0 14%;
    min-width: 80px;
    max-width: 160px;
    padding: 0.75rem;
    border-radius: 0.5rem;
    transition: all 0.2s ease;
    background: var(--card-bg);
    border: 1px solid transparent;
}

.case-nav-preview:hover:not(.disabled) {
    background: rgba(var(--primary-rgb, 13, 110, 253), 0.1);
    border-color: var(--primary-color, #0d6efd);
}

.case-nav-preview:hover:not(.disabled) .nav-preview-canvas {
    transform: scale(1.15);
}

.case-nav-preview.disabled {
    opacity: 0.3;
    cursor: not-allowed;
    pointer-events: none;
}

.nav-preview-canvas {
    width: 90px;
    height: 90px;
    transition: transform 0.2s ease;
}

/* Hide the small chevron overlay icons on the side previews */
.case-nav-preview .nav-overlay {
    display: none;
}

/* Enlarge the nav preview container so the images have room */
.case-nav-preview {
    min-width: 100px;
    max-width: 200px;
}

.nav-overlay {
    position: absolute;
    bottom: 4px;
    font-size: 0.75rem;
    color: var(--muted-text, #6c757d);
}

/* Main Preview Area */
.main-preview-area {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    flex-wrap: nowrap;
    width: 100%;
}

.preview-image-container {
    display: flex;
    align-items: center;
    justify-content: center;
}

.case-preview-canvas {
    width: 120px;
    height: 120px;
}

.preview-player-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1 1 auto;
}

.cube-player-wrapper {
    width: 176px;
    height: 176px;
    min-width: 160px;
    min-height: 160px;
    margin: 0 auto;
}

/* Remove any ring/outline/box-shadow on player hover — we'll use the hitzone hover instead */
.player-wrapper:focus,
.player-wrapper:hover,
.cube-player-wrapper:focus,
.cube-player-wrapper:hover {
    outline: none;
    box-shadow: none;
    border: none;
}

/* Preview hitzone covers the central region between the left/right nav previews.
   On hover it shows a soft edge that fades toward the center. */
.preview-hitzone {
    position: absolute;
    left: 14%;
    right: 14%;
    top: 0;
    bottom: 0;
    z-index: 3;
    /* make non-interactive so it doesn't capture hover intended for side previews */
    pointer-events: none;
    transition: background 220ms ease, opacity 220ms ease;
    background: transparent;
}

/* Hover gradient moved to the side preview containers (.case-nav-preview) so
   hovering the left/right cubes shows the soft outer-edge fade. */
.case-nav-preview:hover:not(.disabled) {
    /* transparent near the cube, darker at the outer edge */
    background: radial-gradient(circle at center, transparent 45%, rgba(0, 0, 0, 0.08) 100%);
}

@media (prefers-color-scheme: dark) {
    .case-nav-preview:hover:not(.disabled) {
        background: radial-gradient(circle at center, transparent 45%, rgba(255, 255, 255, 0.04) 100%);
    }
}

/* Player info toggle and popup */
.player-wrapper {
    position: relative;
}

.player-info-toggle {
    position: absolute;
    top: 6px;
    right: 6px;
    background: rgba(0, 0, 0, 0.06);
    border: 1px solid rgba(0, 0, 0, 0.06);
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--muted-text, #6c757d);
    transition: background 0.12s, color 0.12s;
}

.player-info-toggle:hover {
    background: rgba(var(--primary-rgb, 13, 110, 253), 0.08);
    color: var(--primary-color, #0d6efd);
}

.player-info-popup {
    position: absolute;
    top: 44px;
    right: 6px;
    width: 260px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    padding: 0.5rem;
    border-radius: 0.4rem;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
    display: none;
    z-index: 40;
    font-size: 0.9rem;
}

/* Show popup on hover of toggle or popup itself */
.player-info-toggle:hover+.player-info-popup,
.player-info-popup:hover,
.player-info-popup.show {
    display: block;
}

/* Ensure the popup is usable on small screens */
@media (max-width: 576px) {
    .player-info-popup {
        right: 2px;
        left: 2px;
        width: auto;
        top: 46px;
    }
}

.preview-disclaimer {
    flex-basis: 100%;
    text-align: center;
    font-size: 0.75rem;
    color: var(--muted-text, #6c757d);
    padding: 0.25rem;
    background: rgba(var(--info-rgb, 13, 202, 240), 0.1);
    border-radius: 0.25rem;
}

/* Row 3: Algorithm Cards with Pagination */
.algorithm-row {
    display: flex;
    align-items: stretch;
    gap: 0.5rem;
    min-height: 56px;
}

.alg-nav-btn {
    flex-shrink: 0;
    width: 36px;
    min-width: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--text-color);
}

.alg-nav-btn:hover:not(:disabled) {
    background: rgba(var(--primary-rgb, 13, 110, 253), 0.1);
    border-color: var(--primary-color, #0d6efd);
    color: var(--primary-color, #0d6efd);
}

.alg-nav-btn:disabled,
.alg-nav-btn.disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* Algorithm Cards Container */
.algorithm-cards-container {
    flex: 1;
    display: flex;
    gap: 0.4rem;
    align-items: stretch;
}

/* Layout variations based on number of algorithms */
.algorithm-cards-container.alg-cards-1 {
    justify-content: center;
}

.algorithm-cards-container.alg-cards-2 {
    justify-content: space-around;
}

.algorithm-cards-container.alg-cards-3 {
    justify-content: space-between;
}

/* Individual Algorithm Card */
.algorithm-card {
    flex: 1;
    max-width: calc(33.333% - 0.33rem);
    min-width: 150px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.25rem 0.35rem 0.5rem;
    /* increase bottom padding slightly to accommodate meta */
    display: flex;
    flex-direction: column;
    transition: all 0.2s ease;
    cursor: pointer;
}

.algorithm-cards-container.alg-cards-1 .algorithm-card {
    max-width: 300px;
}

.algorithm-cards-container.alg-cards-2 .algorithm-card {
    max-width: calc(50% - 0.25rem);
}

.algorithm-card:hover {
    border-color: var(--primary-color, #0d6efd);
    box-shadow: 0 2px 8px var(--shadow);
    /* Do not change background on hover; provide click feedback instead */
    background: transparent;
}

.algorithm-card-content {
    display: flex;
    flex-direction: column;
    height: auto;
    justify-content: flex-start;
    align-items: flex-start;
    gap: 0.04rem;
    /* keep notation and meta grouped near the top */
}

.algorithm-card .alg-notation {
    /* Prevent the notation area from stretching the card vertically */
    font-family: 'Roboto Mono', monospace;
    font-weight: 500;
    font-size: 0.9rem;
    line-height: 1.25;
    word-break: break-word;
    cursor: pointer;
    padding: 0.02rem 0.06rem;
    border-radius: 0.25rem;
    transition: background-color 0.2s;
    /* Allow notation to grow so long algorithms are fully visible */
    max-height: none;
    overflow: visible;
    white-space: normal;
}

.algorithm-card .alg-notation:hover {
    background: transparent !important;
}

/* Ensure algorithm notation never shows hover background from other rules */
.alg-notation,
.algorithm-card .alg-notation,
.alg-label {
    background: transparent !important;
}

/* Prevent text hover from triggering noticeable hover visuals; keep card hover subtle */
.algorithm-card:hover .alg-notation {
    background: transparent !important;
}

/* Visual feedback class applied briefly on click */
.algorithm-card-clicked {
    transform: translateY(-1px) scale(0.998);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
}

.alg-meta {
    display: block;
    position: relative;
    margin-top: 0;
    padding: 0.12rem 0 0;
    /* small top padding */
    min-height: 1.15rem;
    /* allow area to grow when notation wraps */
    height: auto;
    width: 100%;
}

.algorithm-card .alg-usercount {
    font-size: 0.72rem;
    color: var(--muted-text, #6c757d);
    margin: 0;
    line-height: 1;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    white-space: nowrap;
    /* keep counts on a single line */
    text-align: center;
    padding: 0 0.2rem;
    box-sizing: border-box;
    max-width: calc(100% - 40px);
    /* leave room for the state button */
    overflow: hidden;
    text-overflow: ellipsis;
}

.algorithm-card .alg-state-btn {
    width: 28px;
    height: 28px;
    min-width: 28px;
    padding: 0;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s;
    position: absolute;
    right: 0.12rem;
    top: 50%;
    transform: translateY(-50%);
    /* vertically center inside the meta area */
}

/* Make the state button look like the case toggle: white circular surface with subtle border.
   Coloring of the icon or border is driven by the card state class when logged in. */
.algorithm-card .alg-state-btn {
    background: #ffffff;
    border: 1px solid var(--border-color);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    color: var(--muted-text, #6c757d);
}

#page-context-data[data-logged-in="true"]~.algs-container .alg-card.alg-card-known .alg-state-btn {
    color: #28a745;
    border-color: rgba(40, 167, 69, 0.25);
}

#page-context-data[data-logged-in="true"]~.algs-container .alg-card.alg-card-learning .alg-state-btn {
    color: #ffc107;
    border-color: rgba(255, 193, 7, 0.25);
}

#page-context-data[data-logged-in="true"]~.algs-container .alg-card.alg-card-unknown .alg-state-btn {
    color: #dc3545;
    border-color: rgba(220, 53, 69, 0.25);
}

.algorithm-card .alg-state-btn:hover {
    /* Avoid moving the button on hover; provide subtle feedback instead */
    transform: translateY(-50%);
    filter: brightness(0.98);
}

/* ============================================================================
   No Selection Placeholder
   ============================================================================ */

.no-selection-section {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    min-height: 150px;
}

/* ============================================================================
   Grid Section (Scrollable)
   ============================================================================ */

.grid-section {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.75rem;
    /* Keep at least ~3 card rows visible before internal scrolling */
    min-height: 252px;
}

/* Override base alg-grid styles for better horizontal layout */
.grid-section .alg-grid {
    --alg-grid-gap: 0.9rem;
    display: grid;
    /* More columns since we have full width now */
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: var(--alg-grid-gap);
    padding: var(--alg-grid-gap);
    box-sizing: border-box;
    justify-items: stretch;
    align-content: start;
    max-height: none;
    overflow: visible;
}

/* Stronger rules to avoid conflicts with legacy styles.css which
   defines .alg-grid as flex and .alg-card with fixed width/height. */
.grid-section .alg-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)) !important;
    gap: var(--alg-grid-gap) !important;
    padding: var(--alg-grid-gap) !important;
}

.grid-section .alg-grid>.alg-card {
    /* Let each grid item size itself within the column and avoid
       fixed widths from older global rules. */
    width: auto !important;
    height: auto !important;
    min-width: 0 !important;
    min-height: 0 !important;
    box-sizing: border-box !important;
}

/* Ensure inner content doesn't overflow its grid cell */
.grid-section .alg-grid>.alg-card .alg-card-inner {
    width: 100% !important;
}

/* Styles for legacy .alg-card elements used in templates */
.alg-card {
    display: block;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.5rem;
    text-decoration: none;
    color: inherit;
    transition: all 0.16s ease;
}

.alg-card .alg-card-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

/* Ensure the small canvas inside grid cards is centered horizontally */
.alg-card .alg-card-inner canvas.cube-canvas,
.alg-card .alg-card-inner .cube-canvas {
    display: block;
    margin: 0 auto;
}

/* Reduce vertical spacing for grid card label and usercount to eliminate gaps */
.alg-card .alg-label {
    margin: 0;
    padding: 0 0.1rem;
    font-family: 'Roboto Mono', monospace;
    font-size: 0.84rem;
    line-height: 1.08;
    max-height: 3.2em;
    overflow: hidden;
}

/* Add a bit more vertical spacing between the image and the label */
.alg-card .alg-label {
    margin-top: 0.35rem;
}

.alg-card .alg-usercount {
    margin: 0;
    padding: 0;
    font-size: 0.75rem;
    color: var(--muted-text, #6c757d);
}

.alg-card:hover {
    /* Keep card hover visual (shadow & border) but do not shift the whole card
       because that moves absolutely-positioned controls. Move only the inner
       content so controls (state button) remain stable. */
    box-shadow: 0 4px 12px var(--shadow);
    border-color: var(--primary-color, #0d6efd);
}

.alg-card:hover .alg-card-inner {
    transform: translateY(-2px);
    transition: transform 0.16s ease;
}

/* Algorithm card state colors - must come after base .alg-card styles */
.alg-card.alg-card-known,
.alg-card.alg-card-learning,
.alg-card.alg-card-unknown {
    /* placeholder so rules are grouped; actual coloring is scoped below */
}

/* Only apply colored card backgrounds when the user is logged in. The
   hidden `#page-context-data` element includes a data attribute we can use. */
#page-context-data[data-logged-in="true"]~.algs-container .alg-card.alg-card-known {
    border-color: #28a745 !important;
    background-color: rgba(40, 167, 69, 0.15) !important;
}

#page-context-data[data-logged-in="true"]~.algs-container .alg-card.alg-card-learning {
    border-color: #ffc107 !important;
    background-color: rgba(255, 193, 7, 0.2) !important;
}

#page-context-data[data-logged-in="true"]~.algs-container .alg-card.alg-card-unknown {
    border-color: #dc3545 !important;
    background-color: rgba(220, 53, 69, 0.15) !important;
}

.alg-card.selected-card {
    border-color: #0d6efd !important;
    box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.25) !important;
}

/* Scrollbar styling for grid */
.grid-section::-webkit-scrollbar {
    width: 6px;
}

.grid-section::-webkit-scrollbar-track {
    background: transparent;
}

.grid-section::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

.grid-section::-webkit-scrollbar-thumb:hover {
    background: var(--muted-text);
}

/* ============================================================================
   Responsive Adjustments
   ============================================================================ */

/* Medium screens (tablets) */
@media (max-width: 991.98px) {
    .algs-container {
        padding: 0.5rem;
    }

    .case-preview-row {
        min-height: 120px;
    }

    .case-nav-preview {
        width: 60px;
        min-width: 60px;
    }

    .nav-preview-canvas {
        width: 40px;
        height: 40px;
    }

    .case-preview-canvas {
        width: 100px;
        height: 100px;
    }

    .cube-player-wrapper {
        width: 140px;
        height: 140px;
    }

    .algorithm-card {
        min-width: 120px;
    }

    .algorithm-card .alg-notation {
        font-size: 0.85rem;
    }
}

/* Small screens (phones) */
@media (max-width: 767.98px) {
    .algs-container {
        height: calc(100dvh - 52px);
        max-height: calc(100dvh - 52px);
        overflow: hidden;
        padding: 0.5rem;
    }

    .header-title h1 {
        font-size: 1.1rem;
    }

    /* Keep left/right navigation on phones to preserve vertical space
       and allow more room for the algorithm grid. */
    .case-preview-row {
        flex-direction: row;
        align-items: center;
        min-height: auto;
    }

    .case-nav-preview {
        /* side previews stay compact on phones */
        flex: 0 0 14%;
        min-width: 60px;
        max-width: 120px;
        padding: 0.5rem;
    }

    .case-nav-preview.prev-case {
        order: 0;
    }

    .main-preview-area {
        /* center the main preview while keeping navs on the sides */
        flex-direction: row;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        flex: 1 1 auto;
    }

    .preview-image-container,
    .preview-player-container {
        display: flex;
        justify-content: center;
    }

    .case-preview-canvas {
        width: 100px;
        height: 100px;
    }

    /* Slightly smaller cube player on phones */
    .cube-player-wrapper {
        width: 120px;
        height: 120px;
        min-width: 120px;
        min-height: 120px;
    }

    .algorithm-row {
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 0.25rem;
    }

    .algorithm-cards-container {
        flex-wrap: nowrap;
        overflow-x: auto;
    }

    .algorithm-card {
        /* One algorithm card per page on phones for readability */
        flex: 0 0 100%;
        width: 100%;
        max-width: none;
        min-width: 0;
        box-sizing: border-box;
    }

    .grid-section {
        flex: 1 1 auto;
        min-height: 222px;
        max-height: none;
        overflow-y: auto;
    }

    .grid-section .alg-grid {
        --alg-grid-gap: 0.32rem;
        /* Fit three slimmer cards on phones */
        grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
        padding: var(--alg-grid-gap) !important;
        gap: var(--alg-grid-gap) !important;
    }

    .grid-section .alg-grid>.alg-card {
        padding: 0.2rem;
        border-radius: 0.35rem;
        width: 100% !important;
        max-width: 125px;
        justify-self: center;
    }

    .grid-section .alg-card .alg-card-inner .cube-canvas,
    .grid-section .alg-card .alg-card-inner canvas.cube-canvas {
        width: 50px;
        height: 50px;
    }

    .grid-section .alg-card .alg-label {
        font-size: 0.66rem;
        line-height: 1.02;
        margin-top: 0.12rem;
        max-height: 2.05em;
    }
}

/* Very small screens */
@media (max-width: 479.98px) {
    .header-actions {
        justify-content: center;
        width: 100%;
    }

    .case-title {
        font-size: 1rem;
    }

    .case-preview-canvas {
        width: 80px;
        height: 80px;
    }

    .cube-player-wrapper {
        width: 100px;
        height: 100px;
        min-width: 100px;
        min-height: 100px;
    }

    .algorithm-card {
        /* Keep one algorithm card per page on very small phones too */
        flex: 0 0 100%;
        width: 100%;
        max-width: none;
        min-width: 0;
        box-sizing: border-box;
        padding-left: 0.4rem;
        padding-right: 0.4rem;
    }

    .grid-section .alg-grid {
        /* Keep three columns on very small screens too */
        grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
        --alg-grid-gap: 0.24rem;
    }

    .grid-section .alg-grid>.alg-card {
        padding: 0.15rem;
        max-width: 112px;
    }

    .grid-section .alg-card .alg-card-inner .cube-canvas,
    .grid-section .alg-card .alg-card-inner canvas.cube-canvas {
        width: 44px;
        height: 44px;
    }

    .grid-section .alg-card .alg-label {
        font-size: 0.61rem;
    }
}

/* ============================================================================
   Landscape / Short-screen layout (e.g. phone in landscape orientation)
   Keep most scrolling inside the grid, but allow limited page scroll when
   needed so top content and grid never get clipped.
   ============================================================================ */

@media (max-height: 500px) {
    .algs-container {
        height: auto;
        min-height: calc(100dvh - 52px);
        max-height: none;
        overflow: visible;
    }

    .grid-section {
        flex: 0 0 auto;
        height: 222px;
        overflow-y: auto;
        overflow-x: hidden;
        max-height: 222px;
        min-height: 222px;
    }

    /* Compact cube player in landscape to reclaim vertical space */
    .cube-player-wrapper {
        width: 100px;
        height: 100px;
        min-width: 100px;
        min-height: 100px;
    }

    .case-details-section {
        min-height: 200px;
    }

    .case-preview-row {
        min-height: 110px;
    }

    .nav-preview-canvas {
        width: 50px;
        height: 50px;
    }
}

/* ============================================================================
   Dark Mode Adjustments
   ============================================================================ */

@media (prefers-color-scheme: dark) {
    .case-nav-preview:hover:not(.disabled) {
        background: rgba(255, 255, 255, 0.1);
    }

    .algorithm-card .alg-notation:hover {
        background: rgba(255, 255, 255, 0.1);
    }

    .alg-nav-btn:hover:not(:disabled) {
        background: rgba(255, 255, 255, 0.1);
    }

    .breadcrumb-nav .breadcrumb-item.active {
        color: rgba(255, 255, 255, 0.88);
    }
}

[data-theme="dark"] .breadcrumb-nav .breadcrumb-item.active {
    color: rgba(255, 255, 255, 0.88);
}

:root:not([data-theme="dark"]) .breadcrumb-nav .breadcrumb-item.active,
[data-theme="light"] .breadcrumb-nav .breadcrumb-item.active {
    color: var(--text-color) !important;
}

/* ============================================================================
   Animations
   ============================================================================ */

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.case-details-section:not(.d-none) {
    animation: fadeIn 0.2s ease-out;
}

/* Card hover animation for grid */
.grid-section .alg-card {
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.grid-section .alg-card:hover {
    transform: translateY(-2px);
}

/* ============================================================================
   Print Styles
   ============================================================================ */

@media print {
    .algs-container {
        height: auto;
        max-height: none;
        overflow: visible;
    }

    .header-actions,
    .alg-nav-btn,
    .case-nav-preview,
    .cube-player-wrapper,
    .preview-disclaimer {
        display: none !important;
    }

    .grid-section {
        max-height: none;
        overflow: visible;
    }
}