:root {
            --bg: #f3f6fb;
            --surface: #ffffff;
            --surface-soft: #f8fafc;
            --text: #172033;
            --muted: #667085;
            --border: #dce3ee;
            --border-strong: #c7d2e3;
            --primary: #1f4f8f;
            --primary-dark: #153b70;
            --primary-soft: #eaf2ff;
            --success: #18794e;
            --success-dark: #11633f;
            --warning: #c56a05;
            --warning-dark: #9b5203;
            --danger: #b42318;
            --shadow-sm: 0 2px 8px rgba(25, 44, 78, 0.06);
            --shadow-md: 0 12px 32px rgba(25, 44, 78, 0.11);
            --radius-sm: 10px;
            --radius: 16px;
            --radius-lg: 22px;
        }

        * { box-sizing: border-box; }

        html { scroll-behavior: smooth; }

        body {
            margin: 0;
            min-height: 100vh;
            background:
                radial-gradient(circle at top right, rgba(31, 79, 143, 0.10), transparent 30rem),
                var(--bg);
            color: var(--text);
            font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            line-height: 1.55;
        }

        .page-shell {
            width: min(1240px, calc(100% - 40px));
            margin: 30px auto 60px;
        }

        .app-header {
            position: relative;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 24px;
            padding: 30px 34px;
            margin-bottom: 22px;
            border-radius: var(--radius-lg);
            color: #fff;
            background: linear-gradient(135deg, #173b6c 0%, #245b9f 58%, #2d6fb9 100%);
            box-shadow: var(--shadow-md);
        }

        .app-header::after {
            content: "";
            position: absolute;
            width: 260px;
            height: 260px;
            right: -85px;
            top: -130px;
            border: 46px solid rgba(255,255,255,0.08);
            border-radius: 50%;
        }

        .app-header-content { position: relative; z-index: 1; }

        .app-kicker {
            margin: 0 0 6px;
            font-size: 0.78rem;
            font-weight: 800;
            letter-spacing: 0.12em;
            text-transform: uppercase;
            color: #cfe2ff;
        }

        .app-header h1 {
            margin: 0;
            font-size: clamp(1.75rem, 3vw, 2.65rem);
            line-height: 1.13;
            letter-spacing: -0.035em;
        }

        .app-subtitle {
            max-width: 720px;
            margin: 10px 0 0;
            color: rgba(255,255,255,0.84);
            font-size: 1rem;
        }

        .version-badge {
            position: relative;
            z-index: 1;
            flex: 0 0 auto;
            padding: 9px 13px;
            border: 1px solid rgba(255,255,255,0.32);
            border-radius: 999px;
            background: rgba(255,255,255,0.13);
            color: #fff;
            font-size: 0.78rem;
            font-weight: 800;
            letter-spacing: 0.04em;
            backdrop-filter: blur(8px);
        }

        form { margin: 0; }

        fieldset {
            min-width: 0;
            margin: 0 0 18px;
            padding: 22px 24px 24px;
            border: 1px solid var(--border);
            border-radius: var(--radius);
            background: var(--surface);
            box-shadow: var(--shadow-sm);
        }

        fieldset legend {
            padding: 0 9px;
            color: var(--primary-dark);
            font-size: 0.98rem;
            font-weight: 800;
        }

        fieldset p:first-of-type { margin-top: 0; }

        label {
            color: var(--text);
            font-weight: 600;
        }

        input[type="date"],
        input[type="text"],
        select {
            width: 100%;
            min-height: 44px;
            padding: 9px 12px;
            border: 1px solid var(--border-strong);
            border-radius: 9px;
            background: #fff;
            color: var(--text);
            font: inherit;
            outline: none;
            transition: border-color .18s ease, box-shadow .18s ease;
        }

        input[type="date"]:focus,
        input[type="text"]:focus,
        select:focus {
            border-color: #6c9bd1;
            box-shadow: 0 0 0 4px rgba(31, 79, 143, 0.11);
        }

        input[type="checkbox"] {
            width: 18px;
            height: 18px;
            margin: 0;
            accent-color: var(--primary);
            vertical-align: middle;
        }

        .date-fieldset {
            padding-top: 25px;
            border-color: #bcd0ea;
            background: linear-gradient(135deg, #f7faff 0%, #edf4ff 100%);
        }

        .date-row {
            display: grid;
            grid-template-columns: repeat(2, minmax(180px, 270px));
            gap: 16px;
            align-items: end;
        }

        .date-row label {
            display: block;
            margin-bottom: 6px;
            color: var(--primary-dark);
            font-size: 0.84rem;
        }

        .date-fieldset p {
            margin: 15px 0 0 !important;
            padding-top: 14px;
            border-top: 1px solid rgba(31,79,143,.14);
            color: var(--muted);
        }

        fieldset > .choice-label {
            display: inline-flex !important;
            align-items: center;
            gap: 9px;
            min-height: 42px;
            margin: 4px 10px 4px 0;
            padding: 9px 13px;
            border: 1px solid var(--border);
            border-radius: 10px;
            background: var(--surface-soft);
        }

        .section-title {
            display: flex;
            align-items: center;
            gap: 11px;
            margin: 31px 0 14px;
            padding: 0;
            border: 0;
            color: var(--primary-dark);
            font-size: 1.28rem;
            letter-spacing: -0.015em;
        }

        .section-title::before {
            content: "";
            width: 5px;
            height: 27px;
            border-radius: 999px;
            background: linear-gradient(var(--primary), #6ca0d8);
        }

        fieldset ul {
            display: grid;
            gap: 10px;
            margin: 14px 0 0;
            padding: 0 !important;
            list-style: none !important;
        }

        fieldset ul li {
            margin: 0 !important;
            padding: 12px 14px;
            border: 1px solid var(--border);
            border-radius: 11px;
            background: var(--surface-soft);
        }

        fieldset ul li label {
            display: grid !important;
            grid-template-columns: 20px minmax(0, 1fr);
            column-gap: 10px;
            width: 100% !important;
            cursor: pointer;
        }

        fieldset ul li label input { margin-top: 3px; }
        fieldset ul li label br { display: none; }
        fieldset ul li label span {
            grid-column: 2;
            margin-top: 4px;
            color: var(--muted) !important;
            font-size: 0.78rem;
            font-weight: 500;
            overflow-wrap: anywhere;
        }

        table.fonti-extra {
            width: 100%;
            margin-top: 15px;
            border-collapse: separate;
            border-spacing: 0;
            border: 1px solid var(--border);
            border-radius: 12px;
            overflow: hidden;
            background: #fff;
        }

        table.fonti-extra th,
        table.fonti-extra td {
            padding: 10px;
            border: 0;
            border-bottom: 1px solid var(--border);
            font-size: 0.84rem;
            vertical-align: middle;
        }

        table.fonti-extra th {
            background: #edf3fb;
            color: var(--primary-dark);
            font-size: 0.75rem;
            font-weight: 800;
            letter-spacing: .025em;
            text-transform: uppercase;
            text-align: left;
        }

        table.fonti-extra tr:last-child td { border-bottom: 0; }
        table.fonti-extra tbody tr:hover { background: #f8fbff; }
        table.fonti-extra td:first-child { width: 42px; text-align: center; font-weight: 800; }
        table.fonti-extra td:nth-child(2) { width: 64px; text-align: center; }
        table.fonti-extra input[type="text"],
        table.fonti-extra select { min-height: 38px; padding: 7px 9px; font-size: .82rem; }

        .section-intro {
            margin: -3px 0 14px;
            color: var(--muted);
            font-size: .9rem;
        }

        .url-box {
            padding: 13px 15px;
            margin: 0 0 19px;
            border: 1px dashed #aebdd0;
            border-radius: 11px;
            background: #f7f9fc;
            color: var(--muted);
            font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
            font-size: 0.77rem;
            overflow-wrap: anywhere;
        }

        button {
            appearance: none;
            border: 0;
            font-family: inherit;
            transition: transform .16s ease, box-shadow .16s ease, background-color .16s ease;
        }

        button:hover { transform: translateY(-1px); }
        button:active { transform: translateY(0); }
        button:focus-visible { outline: 3px solid rgba(51, 116, 187, .28); outline-offset: 2px; }

        .submit-btn,
        .csv-btn {
            display: inline-flex !important;
            align-items: center;
            justify-content: center;
            min-height: 44px;
            padding: 10px 16px;
            margin: 0 !important;
            border-radius: 10px;
            color: #fff;
            font-size: .88rem;
            font-weight: 800;
            cursor: pointer;
            box-shadow: 0 5px 13px rgba(25, 44, 78, .12);
        }

        .submit-btn { background: var(--primary); }
        .submit-btn:hover { background: var(--primary-dark); box-shadow: 0 7px 18px rgba(31,79,143,.24); }
        .csv-btn { background: var(--success); }
        .csv-btn:hover { background: var(--success-dark); box-shadow: 0 7px 18px rgba(24,121,78,.22); }

        #btn-deselect-audizioni {
            background: var(--warning) !important;
            color: #fff !important;
        }
        #btn-deselect-audizioni:hover { background: var(--warning-dark) !important; }

        .primary-actions,
        .selection-actions {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            padding: 16px;
            margin: 18px 0 0;
            border: 1px solid var(--border);
            border-radius: var(--radius);
            background: var(--surface);
            box-shadow: var(--shadow-sm);
        }

        .selection-actions {
            margin-top: 10px;
            background: #f8fafc;
        }

        .selection-actions .csv-btn:nth-child(2) { background: #5f6b7a; }
        .selection-actions .csv-btn:nth-child(2):hover { background: #475467; }

        .selection-summary {
            display: flex;
            align-items: center;
            min-height: 48px;
            margin-top: 10px;
            padding: 11px 15px;
            border: 1px solid #c8d8ed;
            border-radius: 11px;
            background: var(--primary-soft);
            color: var(--primary-dark);
            font-size: .9rem;
            font-weight: 800;
        }

        .error {
            margin: 16px 0;
            padding: 13px 15px;
            border: 1px solid #f1b8b3;
            border-radius: 10px;
            background: #fff1f0;
            color: var(--danger);
            font-weight: 700;
        }

        .diff-container {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 16px;
            margin: 18px 0 28px;
        }

        .diff-box {
            min-width: 0;
            padding: 18px;
            border: 1px solid #e6d8aa;
            border-radius: 13px;
            background: linear-gradient(135deg, #fffdf5, #fff8db);
            color: #5d4b14;
            font-size: .86rem;
            box-shadow: var(--shadow-sm);
        }

        .diff-box h3 {
            margin: 0 0 8px;
            color: #5d4b14;
            font-size: 1rem;
        }

        .diff-box ul { margin: 8px 0 0 19px; padding: 0; }

        .evento {
            position: relative;
            display: grid;
            grid-template-columns: minmax(0, 1fr);
            gap: 9px;
            margin: 0 0 14px;
            padding: 20px 22px;
            border: 1px solid var(--border);
            border-left: 5px solid #b8c5d8;
            border-radius: 14px;
            background: var(--surface);
            box-shadow: var(--shadow-sm);
            transition: border-color .18s ease, box-shadow .18s ease, transform .18s ease;
        }

        .evento:hover {
            transform: translateY(-2px);
            border-color: #b8c9df;
            box-shadow: 0 12px 28px rgba(25, 44, 78, .10);
        }

        .evento.is-selected {
            border-left-color: var(--primary);
            background: linear-gradient(90deg, #f5f9ff 0, #fff 24%);
        }

        .evento:has(input[data-default-unchecked="audizione"]) {
            border-left-color: var(--warning);
        }

        .csv-flag {
            order: 5;
            margin-top: 4px;
            padding-top: 12px;
            border-top: 1px solid var(--border);
            font-size: .82rem;
        }

        .csv-flag label {
            display: inline-flex;
            align-items: center;
            gap: 9px;
            cursor: pointer;
            color: var(--primary-dark);
        }

        .event-title {
            padding-right: 8px;
            color: #14213a;
            font-size: 1.06rem;
            line-height: 1.35;
        }

        .event-date {
            display: inline-flex;
            width: fit-content;
            padding: 5px 9px;
            border-radius: 999px;
            background: #edf3fb;
            color: var(--primary-dark);
            font-size: .78rem;
            font-weight: 800;
        }

        .event-description {
            color: #475467;
            font-size: .91rem;
        }

        .url {
            font-size: 0.78rem;
            overflow-wrap: anywhere;
        }

        .url a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 700;
        }

        .url a:hover { text-decoration: underline; }

        /* Pannello del log */
        #live-log-popup {
            position: fixed;
            top: 22px;
            right: 22px;
            z-index: 9999;
            width: min(430px, calc(100vw - 36px));
            max-height: 410px;
            overflow: hidden;
            padding: 14px;
            border: 1px solid rgba(255,255,255,.11);
            border-radius: 16px;
            background: rgba(12, 26, 49, .96);
            color: #b9f7cd;
            font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
            font-size: .76rem;
            box-shadow: 0 20px 55px rgba(5, 18, 40, .35);
            backdrop-filter: blur(12px);
        }

        #live-log-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 12px;
            margin-bottom: 10px;
        }

        #live-log-header span { color: #fff; font-family: inherit; font-weight: 800; }

        #live-log-close {
            width: 30px;
            height: 30px;
            padding: 0;
            border-radius: 8px;
            background: rgba(255,255,255,.09);
            color: #fff;
            cursor: pointer;
        }

        #live-log-loading {
            display: flex;
            align-items: center;
            margin-bottom: 9px;
            color: #cbfbd9;
        }

        #live-log-content {
            max-height: 285px;
            overflow: auto;
            padding: 10px;
            border: 1px solid rgba(255,255,255,.10);
            border-radius: 10px;
            background: rgba(0,0,0,.20);
        }

        .log-line {
            padding: 3px 0;
            white-space: normal;
            overflow-wrap: anywhere;
            border-bottom: 1px solid rgba(255,255,255,.035);
        }

        .spinner {
            width: 16px;
            height: 16px;
            margin-right: 8px;
            border: 2px solid #71dea0;
            border-top-color: transparent;
            border-radius: 50%;
            animation: spin .8s linear infinite;
        }

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

        @media (max-width: 820px) {
            .page-shell { width: min(100% - 24px, 1240px); margin-top: 14px; }
            .app-header { align-items: flex-start; flex-direction: column; padding: 25px 23px; }
            .date-row { grid-template-columns: 1fr 1fr; }
            .diff-container { grid-template-columns: 1fr; }
            table.fonti-extra { display: block; overflow-x: auto; white-space: nowrap; }
            #live-log-popup { top: 12px; right: 12px; }
        }

        @media (max-width: 560px) {
            .page-shell { width: min(100% - 16px, 1240px); }
            .app-header { border-radius: 16px; }
            .app-header h1 { font-size: 1.65rem; }
            .date-row { grid-template-columns: 1fr; }
            fieldset { padding: 18px 16px 20px; }
            .primary-actions, .selection-actions { padding: 12px; }
            .submit-btn, .csv-btn { width: 100%; }
            .evento { padding: 17px 16px; }
            #live-log-popup { width: calc(100vw - 16px); right: 8px; top: 8px; max-height: 55vh; }
        }

/* ======================================================
   V6 - componenti e ottimizzazione mobile
   ====================================================== */
body { overflow-x: hidden; }
img, svg { max-width: 100%; }
.date-help, .fieldset-help { color: var(--muted); font-size: .9rem; }
.fieldset-help { margin-top: 0; }
.source-list { font-size: .9rem; list-style: none; padding-left: 0; }
.choice-label { width: auto; }
.diff-label { margin: 4px 0 0; }

.app-toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    z-index: 10050;
    max-width: min(520px, calc(100vw - 28px));
    padding: 12px 16px;
    border-radius: 12px;
    background: #172033;
    color: #fff;
    font-size: .88rem;
    font-weight: 750;
    box-shadow: 0 18px 45px rgba(12, 26, 49, .28);
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 18px);
    transition: opacity .2s ease, transform .2s ease;
}
.app-toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

body.is-submitting .submit-btn[name="run"] { opacity: .78; cursor: wait; }
body.is-submitting .submit-btn[name="run"]::after {
    content: "";
    width: 15px;
    height: 15px;
    margin-left: 9px;
    border: 2px solid rgba(255,255,255,.7);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

@media (max-width: 700px) {
    body {
        background: #eef3f9;
        font-size: 15px;
        -webkit-text-size-adjust: 100%;
    }

    .page-shell {
        width: 100%;
        margin: 0 auto 42px;
        padding: 0 10px;
    }

    .app-header {
        margin: 0 -10px 14px;
        padding: max(22px, env(safe-area-inset-top)) 18px 22px;
        border-radius: 0 0 20px 20px;
        gap: 16px;
    }

    .app-header::after { width: 190px; height: 190px; right: -75px; top: -95px; }
    .app-header h1 { font-size: clamp(1.55rem, 8vw, 2rem); }
    .app-subtitle { font-size: .92rem; }
    .version-badge { padding: 7px 10px; font-size: .7rem; }

    fieldset {
        margin-bottom: 12px;
        padding: 17px 14px 18px;
        border-radius: 14px;
    }

    fieldset legend { max-width: calc(100vw - 62px); line-height: 1.25; }

    input[type="date"], input[type="text"], select {
        min-height: 48px;
        font-size: 16px; /* impedisce lo zoom automatico su iOS */
    }

    input[type="checkbox"] { width: 21px; height: 21px; flex: 0 0 21px; }
    fieldset > .choice-label { display: flex !important; width: 100% !important; margin-right: 0; }

    .section-title { margin: 24px 2px 12px; font-size: 1.12rem; }
    .section-title::before { height: 23px; }
    .section-intro { margin-left: 2px; margin-right: 2px; }
    .url-box { font-size: .72rem; }

    /* La tabella delle fonti extra diventa una sequenza di schede. */
    table.fonti-extra,
    table.fonti-extra tbody,
    table.fonti-extra tr,
    table.fonti-extra td { display: block; width: 100%; }

    table.fonti-extra { border: 0; background: transparent; overflow: visible; }
    table.fonti-extra thead {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0,0,0,0);
        white-space: nowrap;
        border: 0;
    }
    table.fonti-extra tbody { display: grid; gap: 11px; }
    table.fonti-extra tr {
        padding: 12px;
        border: 1px solid var(--border);
        border-radius: 12px;
        background: #fff;
        box-shadow: var(--shadow-sm);
    }
    table.fonti-extra td {
        display: grid;
        grid-template-columns: 92px minmax(0,1fr);
        align-items: center;
        gap: 10px;
        padding: 7px 0;
        border: 0;
        text-align: left !important;
        white-space: normal;
    }
    table.fonti-extra td::before {
        color: var(--muted);
        font-size: .69rem;
        font-weight: 800;
        letter-spacing: .035em;
        text-transform: uppercase;
    }
    table.fonti-extra td:nth-child(1)::before { content: "Fonte"; }
    table.fonti-extra td:nth-child(2)::before { content: "Usa"; }
    table.fonti-extra td:nth-child(3)::before { content: "URL"; }
    table.fonti-extra td:nth-child(4)::before { content: "Selettore"; }
    table.fonti-extra td:nth-child(5)::before { content: "ID / classe"; }
    table.fonti-extra td:first-child,
    table.fonti-extra td:nth-child(2) { width: 100%; text-align: left; }

    .primary-actions { display: grid; grid-template-columns: 1fr; padding: 11px; gap: 9px; }
    .selection-actions { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); padding: 11px; gap: 9px; }
    .selection-actions #btn-deselect-audizioni { grid-column: 1 / -1; }
    .submit-btn, .csv-btn { width: 100%; min-height: 48px; padding: 11px 13px; line-height: 1.25; }

    .selection-summary {
        position: sticky;
        bottom: max(8px, env(safe-area-inset-bottom));
        z-index: 30;
        min-height: 46px;
        margin: 10px 4px;
        box-shadow: 0 9px 26px rgba(25, 44, 78, .16);
    }

    .evento {
        margin-bottom: 11px;
        padding: 16px 14px;
        border-radius: 13px;
        gap: 8px;
    }
    .evento:hover { transform: none; }
    .event-title { padding-right: 0; font-size: 1rem; }
    .event-date { width: 100%; border-radius: 9px; line-height: 1.35; }
    .event-description { font-size: .89rem; line-height: 1.55; }
    .csv-flag label { align-items: flex-start; line-height: 1.35; }
    .url { font-size: .72rem; }

    .diff-box { padding: 15px; }

    /* Su mobile il log diventa un pannello dal basso. */
    #live-log-popup {
        top: auto;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        max-height: min(68vh, 520px);
        padding: 13px max(12px, env(safe-area-inset-right)) max(13px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
        border-radius: 18px 18px 0 0;
    }
    #live-log-content { max-height: calc(min(68vh, 520px) - 105px); }

    .app-toast { bottom: max(16px, env(safe-area-inset-bottom)); font-size: .82rem; }
}

@media (max-width: 430px) {
    .selection-actions { grid-template-columns: 1fr; }
    .selection-actions #btn-deselect-audizioni { grid-column: auto; }
    table.fonti-extra td { grid-template-columns: 78px minmax(0,1fr); }
    .app-kicker { font-size: .68rem; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

