@layer page {
    :root {
        --media-pesata-accent: #e4c5ff;
        --media-pesata-bg: hsl(from var(--media-pesata-accent) h s 95%);

        --media-pesata-accent-2: #ffe4c5;
        --media-pesata-accent-3: #c5ffe4;

        --card-base: var(--media-pesata-accent);
    }

    main {
        justify-self: center;
        background: var(--media-pesata-bg);

        display: flex;
        flex-direction: column;
        align-items: center;

        padding: 4.5rem 3rem;
        gap: 4.5rem;

        @media screen and (max-width: 1024px) {
            padding: 3rem 1rem;
            gap: 3rem;

            .card {
                width: 100%;
            }
        }
    }

    .media-pesata-container {
        max-width: 72rem;
        margin: 0 auto;
        padding: 2rem;

        @media screen and (max-width: 768px) {
            padding: 0;
        }
    }

    h1 {
        text-align: center;
        margin-bottom: 1rem;
        color: var(--color-primary);
    }

    p {
        text-align: center;
        margin-bottom: 2rem;
        color: var(--color-text-secondary);
    }

    .card {
        align-content: start;
        gap: 1rem;
    }

    button {
        display: grid;
        grid-auto-flow: column;
        place-content: center;
        place-items: center;

        padding: 0.25rem 1rem;
        gap: 0.5rem;
        font-size: 16px;

        border: 2px solid #222;
        border-radius: 4px;
        box-shadow: 2px 2px 0 0 #222;

        transition: all 64ms linear;
        text-decoration: none;
        color: #222;

        cursor: pointer;

        &:hover {
            transform: translate(-1px, -1px);
            box-shadow: 3px 3px 0 0 #222;
        }

        &:active {
            transform: translate(1px, 1px);
            box-shadow: 1px 1px 0 0 #222;
        }
    }

    .media-pesata-app {
        max-width: 1200px;
        margin: 0 auto;
        padding: 2rem;
        min-height: 100vh;

        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: repeat(7, auto);

        align-items: start;
        gap: 2rem;

        > .wide {
            grid-column: 1 / -1;
        }

        @media screen and (max-width: 1024px) {
            padding: 0;

            grid-template-columns: 1fr;
            gap: 1rem;
        }
    }

    .media-pesata-title {
        text-align: center;
        color: var(--palette-black);
        margin-bottom: 2rem;
        font-family: var(--font-display);
        font-weight: 700;
        font-size: 2.5rem;
    }

    /* Selezione tipo studente */
    .student-type-selector {
        background: #fff;
        border: var(--border-large);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 1.5rem;
        margin-bottom: 2rem;

        display: grid;
        place-content: center;

        h2 {
            margin-bottom: 1rem;
            color: var(--palette-black);
            font-family: var(--font-display);
            font-weight: 600;
            font-size: 1.5rem;
            text-align: center;
        }
    }

    /* .radio-group {
        display: flex;
        gap: 1.5rem;
        justify-content: center;

        label {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-size: 1.1rem;
            cursor: pointer;
            padding: 0.5rem 1rem;
            border: 2px solid var(--palette-black);
            border-radius: 6px;
            background: #fff;
            font-family: var(--font-secondary);
            font-weight: 600;
            transition: all 64ms linear;
            box-shadow: 2px 2px 0 0 var(--palette-black);
        }

        label:hover {
            transform: translate(-1px, -1px);
            box-shadow: 3px 3px 0 0 var(--palette-black);
        }

        label:active {
            transform: translate(1px, 1px);
            box-shadow: 1px 1px 0 0 var(--palette-black);
        }

        input[type='radio'] {
            width: 18px;
            height: 18px;
            margin: 0;
            accent-color: #007bff;
        }
    } */

    .compound-button {
        display: flex;
        border: 3px solid #222;
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 #222;
        overflow: hidden;
        width: fit-content;

        button {
            border: none;
            border-radius: 0;
            box-shadow: none;
            margin: 0;
            flex: 1;
            min-width: 120px;
            padding: 0.5rem;

            &:not(:last-child) {
                border-right: 3px solid #222;
            }

            &:hover {
                transform: none;
                box-shadow: none;
                background: #f0f0f0;
            }

            &:active {
                transform: none;
                box-shadow: none;
                background: #e0e0e0;
            }

            &.active {
                background: hsl(from var(--media-pesata-accent) h s 75%);
                color: #f4fef7;

                &:hover {
                    background: hsl(from var(--media-pesata-accent) h s 85%);
                }

                &:active {
                    background: hsl(from var(--media-pesata-accent) h s 95%);
                }
            }
        }
    }

    /* Counter CFU */
    .cfu-counter {
        background: hsl(from var(--media-pesata-accent-2) h s 75%);
        border: var(--border-large);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 1rem;
        text-align: center;

        &.error {
            background: #ffd8d8;
            border-color: #dc3545;
        }
    }

    .cfu-counter h3 {
        margin: 0;
        color: var(--palette-black);
        font-family: var(--font-display);
        font-weight: 600;
        font-size: 1.3rem;
    }

    .error-message {
        color: #d63031;
        font-weight: bold;
        margin: 0;
        font-family: var(--font-secondary);
    }

    .error-message {
        background: #ff6b7a;
        border: 2px solid var(--palette-black);
        border-radius: 6px;
        padding: 1rem;
        color: var(--palette-black);
        font-weight: 600;
    }

    .error-message p {
        margin: 0;
        text-align: center;
    }

    @media (max-width: 768px) {
        .main-content {
            grid-template-columns: 1fr;
        }
    }

    /* Sezione selezione corsi */
    .course-selection {
        background: #fff;
        border: var(--border-large);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 1.5rem;
    }

    .course-selection h2 {
        margin-bottom: 1.5rem;
        color: var(--palette-black);
        font-family: var(--font-display);
        font-weight: 600;
        font-size: 1.5rem;
    }

    .course-category {
        display: grid;
        gap: 0.5rem;

        > button {
            padding: 0.25rem 0.75rem 0.25rem 0.5rem;
            font-size: 20px;

            place-content: stretch;
            place-items: stretch;
        }

        .course-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 0.5rem;

            padding: 0.25rem;

            border-radius: 0.25rem;
            overflow-y: auto;
            max-height: 50vh;

            position: relative;

            /* bottom linear gradient for scroll */
            &::after {
                content: '';
                position: sticky;
                bottom: -0.25rem;
                left: 0;
                right: 0;
                height: 1rem;
                margin-top: calc(-1rem - 0.25rem);

                background: linear-gradient(to top, #0004, transparent);
            }

            .course-button {
                display: grid;
                grid-template-rows: auto auto;
                padding: 0.5rem;
                gap: 0.25rem;
                justify-content: stretch;
                justify-items: start;

                border: 2px solid var(--palette-black);
                border-radius: 4px;
                background: #f8f9fa;
                cursor: pointer;
                transition: all 64ms linear;
                text-align: left;
                font-family: var(--font-secondary);
                box-shadow: 2px 2px 0 0 var(--palette-black);

                &:hover:not(:disabled) {
                    background: var(--homepage-projects-bg);
                    transform: translate(-1px, -1px);
                    box-shadow: 3px 3px 0 0 var(--palette-black);
                }

                &:active:not(:disabled) {
                    transform: translate(1px, 1px);
                    box-shadow: 1px 1px 0 0 var(--palette-black);
                }

                &:disabled {
                    opacity: 0.5;
                    cursor: not-allowed;
                    background: #e0e0e0;
                }

                > .course-name {
                    font-weight: 600;
                    font-size: 0.9rem;
                    line-height: 1.3;
                    color: var(--palette-black);
                }

                > .course-cfu {
                    font-size: 0.8rem;
                    color: #666;
                    font-weight: bold;
                    /* font-family: var(--font-mono); */
                }
            }

            @media screen and (590px <= width <= 1024px) {
                &::after {
                    display: none;
                }
            }
        }
    }

    /* Materia personalizzata */
    .custom-course {
        margin-top: 1rem;
        padding-top: 1.5rem;
        border-top: 3px solid var(--palette-black);
    }

    .custom-course h3 {
        margin-bottom: 1rem;
        color: var(--palette-black);
        font-family: var(--font-display);
        font-weight: 600;
        font-size: 1.2rem;
    }

    .add-custom-btn {
        background: #1e6733;
        color: #f4fef7;
        border: 3px solid var(--palette-black);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 0.75rem 1.5rem;
        cursor: pointer;
        font-size: 0.9rem;
        font-weight: 600;
        font-family: var(--font-secondary);
        transition: all 64ms linear;
    }

    .add-custom-btn:hover {
        background: #2b8b47;
        transform: translate(-1px, -1px);
        box-shadow: 5px 5px 0 0 var(--palette-black);
    }

    input {
        padding: 0.25rem 0.5rem;
        font-size: 14px;

        border: 2px solid #222;
        border-radius: 4px;
        box-shadow: 2px 2px 0 0 #222;

        background: #fff;
        font-family: var(--font-primary);
    }

    .custom-form {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;

        input[type='text'] {
            flex: 2;
            min-width: 200px;
        }

        input[type='number'] {
            flex: 0 0 80px;
        }
    }

    .confirm-btn,
    .cancel-btn {
        padding: 0.5rem 1rem;
        border: 2px solid var(--palette-black);
        border-radius: 4px;
        cursor: pointer;
        font-size: 0.9rem;
        font-weight: 600;
        font-family: var(--font-secondary);
        transition: all 64ms linear;
    }

    .confirm-btn {
        background: #28a745;
        color: white;
        box-shadow: 2px 2px 0 0 var(--palette-black);
    }

    .confirm-btn:hover {
        background: #218838;
        transform: translate(-1px, -1px);
        box-shadow: 3px 3px 0 0 var(--palette-black);
    }

    .confirm-btn:active {
        transform: translate(1px, 1px);
        box-shadow: 1px 1px 0 0 var(--palette-black);
    }

    .cancel-btn {
        background: #6c757d;
        color: white;
        box-shadow: 2px 2px 0 0 var(--palette-black);
    }

    .cancel-btn:hover {
        background: #5a6268;
        transform: translate(-1px, -1px);
        box-shadow: 3px 3px 0 0 var(--palette-black);
    }

    .cancel-btn:active {
        transform: translate(1px, 1px);
        box-shadow: 1px 1px 0 0 var(--palette-black);
    }

    /* Sezione corsi selezionati */
    .selected-courses {
        background: #fff;
        border: var(--border-large);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 1.5rem;
    }

    .selected-courses h2 {
        margin-bottom: 1.5rem;
        color: var(--palette-black);
        font-family: var(--font-display);
        font-weight: 600;
        font-size: 1.5rem;
    }

    .no-courses {
        text-align: center;
        color: #666;
        font-style: italic;
        padding: 2rem;
        font-family: var(--font-secondary);
    }

    .courses-list {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;

        /* @media screen and (max-width: 1024px) {
            gap: 0;

            .course-item {
                &:first-child:not(:last-child) {
                    border-bottom-left-radius: 0;
                    border-bottom-right-radius: 0;
                }

                &:last-child:not(:first-child) {
                    border-top-left-radius: 0;
                    border-top-right-radius: 0;

                    border-top: none;
                }

                &:not(:first-child, :last-child) {
                    border-radius: 0;
                    border-top: none;
                }
            }
        } */
    }

    .course-item {
        display: grid;
        grid-template-rows: auto 1fr;
        grid-template-columns: 1fr auto auto;
        align-items: center;

        gap: 0.5rem 1rem;
        padding: 0.5rem 1rem 0.5rem 0.5rem;
        background: #fff;
        border: 2px solid var(--palette-black);
        border-radius: 4px;
        transition: all 64ms linear;

        > .course-name {
            grid-column: 1 / 2;
            grid-row: 1 / 2;

            font-weight: 600;
            font-size: 0.9rem;
            color: var(--palette-black);
            font-family: var(--font-secondary);
        }

        > .course-cfu {
            grid-column: 1 / 2;
            grid-row: 2 / 3;

            font-size: 0.8rem;
            font-weight: bold;
            color: #666;
            /* font-family: var(--font-mono); */
        }

        > .tall {
            grid-row: 1 / -1;
        }

        .course-grade {
            display: grid;
            grid-auto-flow: column;
            place-content: center;

            gap: 0.5rem;

            input[type='number'] {
                min-width: 4.25rem;
            }
        }

        > .actions {
            grid-column: -2 / -1;
        }

        .lode-checkbox {
            display: flex;
            align-items: center;
            gap: 0.25rem;
            font-size: 0.8rem;
            cursor: pointer;
            font-weight: 600;
            font-family: var(--font-secondary);

            &.disabled {
                opacity: 0.5;
                cursor: not-allowed;
            }
        }

        .remove-btn {
            background: #d63031;
            color: white;
            width: 1.75rem;
            height: 1.75rem;
            font-size: 20px;
            padding: 0;

            margin-top: 4px;

            &:hover {
                background: #b71c1c;
            }
        }

        @media screen and (max-width: 1024px) {
            justify-items: start;
            align-items: center;
            gap: 0.5rem;

            grid-template-columns: 1fr auto;
            grid-template-rows: auto auto;

            padding: 0.5rem;

            .course-name {
                display: grid;
                place-content: center;
                place-items: center;

                grid-column: 1 / 2;
                grid-row: 1 / 2;
            }

            .course-cfu {
                display: grid;
                justify-self: center;
                align-self: start;

                grid-column: 2 / 3;
                grid-row: 1 / 2;
            }

            .course-grade {
                grid-row: 2 / 3;
            }

            .actions {
                grid-column: 2 / 3;
                grid-row: 2 / 3;

                justify-self: end;
            }
        }
    }

    .calculate-button {
        background: #1e6733;
        color: #f4fef7;
        border: 3px solid var(--palette-black);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 1rem 2rem;
        font-size: 1.2rem;
        font-weight: 600;
        font-family: var(--font-secondary);
        cursor: pointer;
        transition: all 64ms linear;
        margin-bottom: 2rem;

        &:hover {
            background: #2b8b47;
            transform: translate(-1px, -1px);
            box-shadow: 5px 5px 0 0 var(--palette-black);
        }

        &:active {
            transform: translate(2px, 2px);
            box-shadow: 2px 2px 0 0 var(--palette-black);
        }
    }

    .results {
        background: #fff;
        border: 3px solid var(--palette-black);
        border-radius: 6px;
        box-shadow: 4px 4px 0 0 var(--palette-black);
        padding: 2rem;
        margin-top: 1rem;

        > h2 {
            margin-bottom: 1.5rem;
            color: var(--palette-black);
            font-family: var(--font-display);
            font-weight: 600;
            font-size: 1.6rem;
            text-align: center;
        }

        .results-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 1rem;
        }

        @media screen and (max-width: 1024px) {
            padding: 1rem;
        }
    }

    /* Ordine degli elementi: il massimo voto di laurea al centro */
    .result-item:nth-child(4) {
        order: -1;
        grid-column: 1 / -1;
        justify-self: center;
        max-width: 500px;
        width: 100%;
    }

    .result-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem;
        background: hsl(from var(--media-pesata-accent-3) h s 75%);
        border: 2px solid var(--palette-black);
        border-radius: 4px;
        font-family: var(--font-secondary);
        font-weight: 600;
        font-size: 1rem;
    }

    .result-item.highlight {
        background: hsl(from var(--media-pesata-accent-2) h s 75%);
        font-weight: bold;
        font-size: 1.2rem;
    }

    .result-item .label {
        font-weight: 600;
        color: var(--palette-black);
        font-size: 0.9rem;
        line-height: 1.2;
    }

    .result-item .value {
        font-weight: bold;
        font-size: 1.1rem;
        font-family: var(--font-mono);
        color: var(--palette-black);
    }

    .pass-fail-badge {
        background: var(--color-accent);
        color: white;
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 0.8rem;
        font-weight: bold;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .lode-badge {
        color: var(--palette-black);
        font-weight: bold;
        margin-left: 0.5rem;
    }

    /* Responsive */
    /* @media (max-width: 768px) {
        .course-grid {
            grid-template-columns: 1fr;
        }

        .course-item {
            flex-direction: row;
            flex-wrap: wrap;
            align-items: center;
            gap: 0.5rem;
            padding: 0.75rem;
        }

        .course-info {
            flex: 1 1 100%;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            gap: 0.5rem;
        }

        .course-grade {
            flex: 1 1 auto;
            justify-content: flex-start;
            gap: 0.5rem;
        }

        .course-grade input[type='number'] {
            width: 70px;
        }

        .remove-btn {
            width: 28px;
            height: 28px;
            font-size: 1rem;
            flex-shrink: 0;
        }

        .custom-form {
            flex-direction: column;
        }

        .results-grid {
            grid-template-columns: 1fr;
        }

        .result-item:nth-child(4) {
            order: -1;
            grid-column: 1;
            justify-self: center;
            max-width: 100%;
        }
    } */

    /* Layout ultra-compatto per schermi molto piccoli */
    /* @media (max-width: 480px) {
        .course-item {
            padding: 0.5rem;
            gap: 0.25rem;
        }

        .course-info {
            flex: 1 1 100%;
            margin-bottom: 0.25rem;
        }

        .course-info .course-name {
            font-size: 0.85rem;
        }

        .course-info .course-cfu {
            font-size: 0.75rem;
        }

        .course-grade {
            flex: 1 1 auto;
            gap: 0.25rem;
        }

        .course-grade input[type='number'] {
            width: 65px;
            padding: 0.375rem;
            font-size: 0.85rem;
        }

        .lode-checkbox {
            font-size: 0.75rem;
        }

        .remove-btn {
            width: 26px;
            height: 26px;
            font-size: 0.9rem;
        }
    } */

    .section-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1rem;
    }

    .reset-btn {
        background: #d63031;
        color: white;
        border: 2px solid var(--palette-black);
        padding: 0.5rem 1rem;
        border-radius: 4px;
        font-size: 0.9rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 64ms linear;
        box-shadow: 2px 2px 0 0 var(--palette-black);
    }

    .reset-btn:hover {
        background: #b71c1c;
        transform: translate(-1px, -1px);
        box-shadow: 3px 3px 0 0 var(--palette-black);
    }

    .reset-btn:active {
        transform: translate(1px, 1px);
        box-shadow: 1px 1px 0 0 var(--palette-black);
    }

    .cfu-info {
        font-size: 0.9rem;
        color: #666;
        margin: 0.25rem 0 0 0;
        font-weight: normal;
    }

    .error-text {
        color: #d63031;
        font-weight: 600;
        margin: 0.5rem 0 0 0;
    }

    /* Category headers collapsible */
    .category-header {
        width: 100%;
        background: #e4c5ff;
        border: 2px solid var(--palette-black);
        border-radius: 4px;
        padding: 0.5rem 1rem;
        cursor: pointer;
        display: flex;
        justify-content: space-between;
        align-items: center;
        transition: all 64ms linear;
        margin-bottom: 0.5rem;
        min-height: 36px;
        box-shadow: 1px 1px 0 0 var(--palette-black);
    }

    .category-header:hover {
        background: #d9b3ff;
        transform: translate(-1px, -1px);
        box-shadow: 2px 2px 0 0 var(--palette-black);
    }

    .category-header:active {
        transform: translate(0px, 0px);
        box-shadow: 1px 1px 0 0 var(--palette-black);
    }

    .category-header h3 {
        margin: 0;
        font-family: var(--font-display);
        font-weight: 600;
        color: var(--palette-black);
        font-size: 1rem;
    }

    .toggle-icon {
        font-size: 1rem;
        color: var(--palette-black);
        transition: transform 150ms ease;
        user-select: none;
    }

    .toggle-icon.expanded {
        transform: rotate(90deg);
    }

    /* Calculate button */
    .calculate-section {
        display: grid;
        place-content: center;
        padding: 2rem 0 1rem;

        button {
            font-size: 20px;
            padding: 1rem;

            /* border 3px */
            border: 3px solid var(--palette-black);
            border-radius: 6px;
            box-shadow: 4px 4px 0 0 var(--palette-black);

            &:hover {
                transform: translate(-1px, -1px);
                box-shadow: 5px 5px 0 0 var(--palette-black);
            }

            &:active {
                transform: translate(2px, 2px);
                box-shadow: 2px 2px 0 0 var(--palette-black);
            }

            background: hsl(from var(--media-pesata-accent-2) h s 75%);
        }
    }

    .calculate-btn {
        background: #1e6733;
        color: #f4fef7;
        border: var(--border-large);
        border-radius: 6px;
        padding: 0.75rem 1.5rem;
        font-size: 1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 64ms linear;
        font-family: var(--font-primary);
        display: inline-block;
        box-shadow: 4px 4px 0 0 var(--palette-black);
    }

    .calculate-btn:hover {
        background: #2b8b47;
        transform: translate(-1px, -1px);
        box-shadow: 5px 5px 0 0 var(--palette-black);
    }

    .calculate-btn:active {
        transform: translate(2px, 2px);
        box-shadow: 2px 2px 0 0 var(--palette-black);
    }

    /* Info section */
    .info-section {
        background: var(--color-accent-light);
        border: var(--border-large);
        border-radius: 8px;
        padding: 1.5rem;
        margin: 2rem 0;
    }

    .info-section h3 {
        margin-top: 0;
        margin-bottom: 1rem;
        color: var(--palette-black);
        font-family: var(--font-display);
        font-weight: 600;
    }

    .info-content h4 {
        color: var(--palette-black);
        font-weight: 600;
        margin-top: 1rem;
        margin-bottom: 0.5rem;
    }

    .info-content ul {
        margin-bottom: 1rem;
        padding-left: 1.5rem;
    }

    .info-content li {
        margin-bottom: 0.5rem;
        line-height: 1.4;
    }

    /* Gestione testi lunghi su schermi più piccoli */
    @media (max-width: 480px) {
        .result-item {
            flex-direction: column;
            align-items: flex-start;
            gap: 0.5rem;
        }

        .result-item .label {
            font-size: 0.85rem;
        }

        .result-item .value {
            align-self: flex-end;
            font-size: 1.2rem;
        }
    }
}

@layer utility {
    @media screen and (max-width: 1024px) {
        .card {
            > .h-flex {
                flex-direction: column;
                align-items: start;

                > .spacer {
                    display: none;
                }
            }
        }
    }
}
