* {
    box-sizing: border-box;
}

:root {
    --bg: #f5f7fa;
    --surface: #ffffff;
    --text: #182230;
    --muted: #667085;
    --border: #e4e7ec;
    --primary: #240d3b;
    --primary-hover: #837294;
    --soft: #eef4fb;
    --radius: 16px;
}

html,
body {
    margin: 0;
    min-height: 100%;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family:
        Inter,
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        Roboto,
        Arial,
        sans-serif;
}

button,
input,
select {
    font: inherit;
}

button,
a {
    -webkit-tap-highlight-color: transparent;
}

.container {
    width: min(1240px, calc(100% - 40px));
    margin: 0 auto;
}


/* HEADER */

.main-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    /*padding: 46px 0 40px;*/
}

.eyebrow {
    color: var(--primary);
    font-size: 13px;
    font-weight: 800;
    letter-spacing: .08em;
    text-transform: uppercase;
    margin-bottom: 10px;
}

.main-header h1 {
    margin: 0 0 10px;
    font-size: clamp(34px, 5vw, 50px);
    line-height: 1.05;
    letter-spacing: -.03em;
}

.main-header p {
    margin: 0;
    max-width: 700px;
    color: var(--muted);
    font-size: 17px;
    line-height: 1.6;
}


/* SEARCH */

.search-panel {
    margin-top: 28px;
    padding: 24px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 8px 30px rgba(16, 24, 40, .04);
}

.search-grid {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 18px;
}

.field {
    min-width: 0;
}

.field label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 700;
}

input,
select {
    width: 100%;
    height: 54px;
    padding: 0 15px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid #cfd5df;
    border-radius: 10px;
    outline: none;
    transition:
        border-color .15s ease,
        box-shadow .15s ease;
}

input:focus,
select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(23, 63, 115, .10);
}

.search-input-wrap {
    position: relative;
}

.search-input-wrap input {
    padding-right: 50px;
}

.clear-input {
    position: absolute;
    top: 50%;
    right: 9px;
    transform: translateY(-50%);

    width: 36px;
    height: 36px;

    border: 0;
    border-radius: 50%;

    background: transparent;
    color: var(--muted);

    cursor: pointer;
    font-size: 25px;
    line-height: 1;

    visibility: hidden;
}

.clear-input:hover {
    background: #f2f4f7;
}


/* TOOLBAR */

.results-toolbar {
    min-height: 68px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
}

.result-count {
    color: var(--muted);
    font-size: 15px;
}

.clear-filters {
    border: 0;
    padding: 8px 0;

    background: transparent;
    color: var(--primary);

    font-weight: 700;
    cursor: pointer;
}


/* CARDS */

.poster-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.poster-card {
    display: grid;
    grid-template-columns: 110px minmax(0, 1fr) auto;
    align-items: center;
    gap: 24px;

    padding: 23px;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;

    transition:
        transform .12s ease,
        box-shadow .12s ease,
        border-color .12s ease;
}

.poster-card:hover {
    transform: translateY(-1px);
    border-color: #d0d5dd;
    box-shadow: 0 8px 24px rgba(16, 24, 40, .06);
}

.poster-code {
    font-size: 15px;
    font-weight: 800;
    color: var(--primary);
    align-self: flex-start;
    padding-top: 3px;
}

.poster-content {
    min-width: 0;
}

.poster-content h2 {
    margin: 0 0 10px;
    font-size: 19px;
    line-height: 1.4;
    letter-spacing: -.01em;
}

.poster-author-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 11px;
}

.poster-author {
    color: #344054;
    font-weight: 600;
}

.poster-country {
    color: var(--muted);
}

.poster-country::before {
    content: "•";
    margin-right: 8px;
}

.poster-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.topic-badge {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    padding: 5px 10px;

    border-radius: 999px;

    background: var(--soft);
    color: var(--primary);

    font-size: 12px;
    font-weight: 800;
}

.poster-action {
    align-self: center;
}

.view-button,
.open-pdf-button,
.back-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;

    min-height: 46px;
    padding: 0 18px;

    border: 0;
    border-radius: 10px;

    background: var(--primary);
    color: #fff;

    text-decoration: none;
    font-weight: 700;

    cursor: pointer;
}

.view-button:hover,
.open-pdf-button:hover,
.back-button:hover {
    background: var(--primary-hover);
}

.view-button.unavailable {
    background: #98a2b3;
}


/* STATUS */

.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;

    padding: 26px;

    color: var(--muted);
}

.spinner {
    width: 22px;
    height: 22px;

    border: 3px solid #d0d5dd;
    border-top-color: var(--primary);
    border-radius: 50%;

    animation: spin .8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.no-results,
.error-box {
    padding: 70px 30px;
    text-align: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
}

.no-results-icon {
    font-size: 52px;
    color: #98a2b3;
    line-height: 1;
    margin-bottom: 15px;
}

.no-results h2 {
    margin: 0 0 8px;
}

.no-results p {
    margin: 0;
    color: var(--muted);
}

.error-box {
    color: #b42318;
}

.hidden {
    display: none !important;
}

.load-more-wrap {
    display: flex;
    justify-content: center;
    padding: 28px 0 50px;
}

.load-more {
    min-height: 48px;
    min-width: 160px;

    padding: 0 20px;

    border: 1px solid var(--border);
    border-radius: 10px;

    background: var(--surface);
    color: var(--text);

    font-weight: 700;
    cursor: pointer;
}


/* =========================================
   PDF VIEWER
========================================= */

.viewer-page {
    width: 100%;
    height: 100dvh;
    min-height: 100dvh;

    overflow: hidden;

    background: #111827;
}


.viewer-shell {
    width: 100%;
    height: 100dvh;
    min-height: 100dvh;

    display: flex;
    flex-direction: column;
}


.viewer-header {
    flex-shrink: 0;

    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;

    gap: 20px;

    min-height: 92px;

    padding:
        calc(14px + env(safe-area-inset-top))
        calc(20px + env(safe-area-inset-right))
        14px
        calc(20px + env(safe-area-inset-left));

    background: #fff;

    border-bottom: 1px solid var(--border);

    z-index: 100;
}


.viewer-content {
    flex: 1;

    min-width: 0;
    min-height: 0;

    overflow: hidden;
}


.desktop-pdf-viewer {
    width: 100%;
    height: 100%;
}


.pdf-frame {
    display: block;

    width: 100%;
    height: 100%;

    border: 0;

    background: #525659;
}

.mobile-pdf-viewer {
    display: none;
}

.pdf-missing {
    height: 100%;
    padding: 40px;

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

    text-align: center;
    background: var(--bg);
}

.pdf-missing-icon {
    width: 70px;
    height: 70px;

    display: flex;
    align-items: center;
    justify-content: center;

    border-radius: 50%;

    background: #fee4e2;
    color: #b42318;

    font-size: 36px;
    font-weight: 900;
}

.pdf-missing h1 {
    margin-bottom: 8px;
}

.pdf-missing p {
    max-width: 620px;
    margin-top: 0;

    color: var(--muted);
    line-height: 1.6;
}

.missing-file {
    margin: 12px 0 28px;
    padding: 12px 16px;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
}

.back-button.large {
    min-width: 180px;
}


/* RESPONSIVE */

@media (max-width: 900px) {

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

    .poster-card {
        grid-template-columns: 90px minmax(0, 1fr);
    }

    .poster-action {
        grid-column: 2;
    }

    .viewer-header {
        grid-template-columns: auto minmax(0, 1fr);
    }

    .open-pdf-button {
        /*display: none;*/
    }

}

@media (max-width: 640px) {

    .container {
        width: min(100% - 24px, 1240px);
    }

    .main-header {
        padding: 30px 0;
    }

    .main-header p {
        font-size: 15px;
    }

    .search-panel {
        margin-top: 16px;
        padding: 16px;
    }

    .poster-card {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
        padding: 18px;
    }

    .poster-code {
        padding: 0;
    }

    .poster-action {
        width: 100%;
    }

    .view-button {
        width: 100%;
    }

    .viewer-header {
        min-height: auto;
        padding: 10px;
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .viewer-meta {
        order: -1;
    }

    .viewer-title {
        font-size: 14px;
    }

    .viewer-subtitle {
        font-size: 12px;
    }

    .back-button {
        min-height: 40px;
    }

}

@media (max-width: 768px) {

    /*
     * Su iPhone lasciamo scrollare la pagina,
     * non un iframe interno.
     */

    html,
    body {
        width: 100%;
        min-height: 100%;
    }


    body.viewer-page {
        height: auto;
        min-height: 100dvh;

        overflow-x: hidden;
        overflow-y: auto;

        background: #e9eaec;
    }


    .viewer-shell {
        height: auto;
        min-height: 100dvh;

        display: block;
    }


    /*
     * HEADER
     */

    .viewer-header {
        position: sticky;
        top: 0;

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

        gap: 6px;

        min-height: 0;

        padding:
            calc(8px + env(safe-area-inset-top))
            12px
            10px;

        background: rgba(255, 255, 255, .98);

        z-index: 1000;

        box-shadow:
            0 2px 10px rgba(0, 0, 0, .08);
    }


    .viewer-meta {
        order: 1;
    }


    .viewer-code {
        margin-bottom: 3px;

        font-size: 11px;
    }


    .viewer-title {
        white-space: normal;

        overflow: visible;
        text-overflow: initial;

        font-size: 14px;
        line-height: 1.25;

        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;

        overflow: hidden;
    }


    .viewer-subtitle {
        margin-top: 4px;

        white-space: normal;

        font-size: 11px;
        line-height: 1.3;

        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;

        overflow: hidden;
    }


    .back-button {
        order: 0;

        align-self: flex-start;

        min-height: 36px;

        padding: 0 12px;

        margin-bottom: 2px;

        font-size: 12px;
    }


    .open-pdf-button {
        display: none;
    }


    /*
     * CONTENT
     */

    .viewer-content {
        width: 100%;

        overflow: visible;

        min-height: 0;
    }


    /*
     * Via l'iframe su mobile
     */

    .desktop-pdf-viewer {
        display: none;
    }


    /*
     * PDF.js
     */

    .mobile-pdf-viewer {
        display: block;

        width: 100%;

        padding:
            10px
            10px
            calc(20px + env(safe-area-inset-bottom));
    }


    .pdf-pages {
        width: 100%;

        display: flex;
        flex-direction: column;

        align-items: center;

        gap: 12px;
    }


    .pdf-page {
        display: block;

        max-width: 100%;
        height: auto;

        background: #fff;

        box-shadow:
            0 2px 10px rgba(0, 0, 0, .18);
    }


    .pdf-loading {
        padding: 50px 20px;

        text-align: center;

        color: #667085;

        font-size: 14px;
    }


    .pdf-error {
        margin: 20px;

        padding: 25px;

        background: #fff;

        border-radius: 10px;

        color: #b42318;

        text-align: center;
    }

}
