/* Liste CRM – stile condiviso da tutte le viste */

:root {
    --bg: #121212;
    --surface: #1c1c1c;
    --surface-2: #262626;
    --line: #343434;
    --text: #e0e0e0;
    --muted: #9c9c9c;
    --green: #4caf50;
    --green-deep: #388e3c;
    --green-ink: #1b5e20;
    --accent-text: #81c784;
    --cta-bg: #4caf50;
    --cta-text: #0c1f0d;
    --cta-hover: #66bb6a;
    --sel-bg: rgba(76, 175, 80, 0.13);
    --amber: #e6b450;
    --red: #ef8a80;
    --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
    --bar-h: 60px;
    --crm-head-h: 52px;
}

:root[data-theme="light"] {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --surface-2: #edf3ed;
    --line: #d9ddd9;
    --text: #2b2b2b;
    --muted: #666666;
    --accent-text: #2e7d32;
    --cta-bg: #2e7d32;
    --cta-text: #ffffff;
    --cta-hover: #1b5e20;
    --sel-bg: rgba(76, 175, 80, 0.14);
    --amber: #9a6700;
    --red: #c62828;
    --shadow: 0 12px 32px rgba(27, 94, 32, 0.14);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    scroll-padding-top: calc(var(--bar-h) + 16px);
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "Archivo", "Segoe UI", system-ui, sans-serif;
    font-size: 15px;
    line-height: 1.45;
}

h1,
h2,
h3 {
    margin: 0;
    font-stretch: 112%;
    font-weight: 700;
    line-height: 1.15;
}

p {
    margin: 0;
}

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
}

input[type="checkbox"],
input[type="radio"] {
    accent-color: var(--green);
}

input[type="checkbox"] {
    width: 17px;
    height: 17px;
    margin: 0;
    cursor: pointer;
}

:focus-visible {
    outline: 2px solid var(--accent-text);
    outline-offset: 2px;
}

.num {
    font-variant-numeric: tabular-nums;
    font-stretch: 92%;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* Pulsanti */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 38px;
    padding: 0 14px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
}

.btn:hover {
    border-color: var(--accent-text);
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn:disabled:hover {
    border-color: var(--line);
}

.btn-primary {
    border-color: transparent;
    background: var(--cta-bg);
    color: var(--cta-text);
    font-weight: 700;
}

.btn-primary:hover {
    border-color: transparent;
    background: var(--cta-hover);
}

.btn-primary:disabled:hover {
    background: var(--cta-bg);
}

.btn-danger {
    color: var(--red);
}

.btn-danger:hover {
    border-color: var(--red);
}

.btn-small {
    min-height: 30px;
    padding: 0 10px;
    font-size: 13px;
}

.btn-block {
    width: 100%;
    min-height: 46px;
    font-size: 16px;
}

.link-btn {
    padding: 0;
    border: 0;
    background: none;
    color: var(--muted);
    font-size: 13px;
    text-decoration: underline;
    text-underline-offset: 3px;
    cursor: pointer;
}

.link-btn:hover {
    color: var(--text);
}

/* Barra superiore */

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 16px;
    min-height: var(--bar-h);
    padding: 8px 24px;
    background: var(--bg);
    border-bottom: 1px solid var(--line);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    margin-right: auto;
    color: var(--text);
    font-size: 18px;
    font-weight: 700;
    font-stretch: 112%;
    text-decoration: none;
}

.brand svg {
    color: var(--green);
}

.sync {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sync-label {
    color: var(--muted);
    font-size: 13px;
}

.segmented {
    display: inline-flex;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
}

.segmented label {
    position: relative;
    cursor: pointer;
}

.segmented input {
    position: absolute;
    opacity: 0;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    cursor: pointer;
}

.segmented span {
    display: block;
    padding: 5px 12px;
    border-radius: 5px;
    font-size: 14px;
    font-weight: 500;
    color: var(--muted);
}

.segmented input:checked + span {
    background: var(--surface-2);
    color: var(--accent-text);
    box-shadow: inset 0 0 0 1px var(--green-deep);
}

.segmented input:focus-visible + span {
    outline: 2px solid var(--accent-text);
    outline-offset: 2px;
}

/* Struttura pagina: contenuto + pannello selezione */

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    gap: 28px;
    align-items: start;
    max-width: 1520px;
    margin: 0 auto;
    padding: 24px 24px 80px;
}

.page-head {
    margin-bottom: 20px;
}

.page-head h1 {
    font-size: 30px;
}

.page-head p {
    margin-top: 6px;
    color: var(--muted);
}

.tools {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.search {
    flex: 1 1 240px;
    max-width: 380px;
    min-height: 38px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--surface);
}

.search::placeholder {
    color: var(--muted);
}

.crm-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.crm-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px 4px 4px;
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--text);
    font-size: 13px;
    text-decoration: none;
}

.crm-chip:hover {
    border-color: var(--accent-text);
}

.crm-chip .chip-count {
    color: var(--accent-text);
    font-weight: 700;
}

.crm-chip .chip-count:empty {
    display: none;
}

.crm-badge {
    display: inline-grid;
    place-items: center;
    min-width: 22px;
    height: 22px;
    padding: 0 5px;
    border-radius: 4px;
    background: var(--green-ink);
    color: #e8f5e9;
    font-size: 12px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.banner {
    margin-bottom: 16px;
    padding: 12px 14px;
    border: 1px solid var(--red);
    border-radius: 8px;
    color: var(--red);
}

.banner[hidden] {
    display: none;
}

/* Sezione di un CRM */

.crm {
    margin-top: 28px;
}

.crm-head {
    position: sticky;
    top: var(--bar-h);
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 12px;
    height: var(--crm-head-h);
    background: var(--bg);
}

.crm-head h2 {
    display: flex;
    align-items: baseline;
    gap: 8px;
    font-size: 18px;
    min-width: 0;
}

.crm-head h2 small {
    overflow: hidden;
    color: var(--muted);
    font-size: 13px;
    font-weight: 400;
    font-stretch: 100%;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.crm-head .crm-count {
    margin-left: auto;
    color: var(--muted);
    font-size: 13px;
    white-space: nowrap;
}

.crm-head .crm-count strong {
    color: var(--accent-text);
}

.lists {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
}

.lists th {
    position: sticky;
    top: calc(var(--bar-h) + var(--crm-head-h));
    z-index: 4;
    padding: 9px 12px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--line);
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    text-align: left;
}

.lists th:first-child {
    border-top-left-radius: 9px;
}

.lists th:last-child {
    border-top-right-radius: 9px;
}

.lists td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
}

.lists tbody tr:last-child td {
    border-bottom: 0;
}

.lists th.c-num,
.lists td.c-num {
    text-align: right;
}

.lists .c-check {
    width: 42px;
    padding-right: 0;
}

.lists .c-check input {
    display: block;
}

.lists tbody tr {
    cursor: pointer;
}

.lists tbody tr:hover td {
    background: var(--surface-2);
}

.lists tbody tr.is-selected td {
    background: var(--sel-bg);
}

.lists tbody tr.is-selected td:first-child {
    box-shadow: inset 3px 0 0 var(--green);
}

.lists tbody tr.is-locked {
    cursor: default;
    color: var(--muted);
}

/* Righe informative del CRM: niente casella, niente aspetto cliccabile */
.lists .c-check input[hidden] {
    display: none;
}

.lists tbody tr.is-locked:hover td {
    background: none;
}

.lists tbody tr[hidden] {
    display: none;
}

.c-name {
    font-weight: 500;
    word-break: break-word;
}

.row-meta {
    display: none;
}

.c-new {
    width: 120px;
    font-weight: 700;
}

.meter {
    display: block;
    height: 3px;
    margin-top: 4px;
    margin-left: auto;
    width: 72px;
    border-radius: 2px;
    background: var(--line);
    overflow: hidden;
}

.meter i {
    display: block;
    height: 100%;
    background: var(--green);
}

tr.is-low .c-new {
    color: var(--amber);
}

tr.is-low .meter i {
    background: var(--amber);
}

.crm-empty {
    padding: 18px 16px;
    border: 1px dashed var(--line);
    border-radius: 10px;
    color: var(--muted);
}

.no-match {
    margin-top: 28px;
    color: var(--muted);
}

.no-match[hidden] {
    display: none;
}

/* Pannello selezione */

.tray {
    position: sticky;
    top: calc(var(--bar-h) + 24px);
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - var(--bar-h) - 48px);
    background: var(--surface);
    border: 1px solid var(--line);
    border-top: 3px solid var(--green);
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.tray-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px 12px;
    border-bottom: 1px solid var(--line);
}

.tray-head h2 {
    font-size: 17px;
}

.tray-summary {
    margin-top: 2px;
    color: var(--muted);
    font-size: 13px;
}

.tray-summary strong {
    color: var(--accent-text);
}

.tray-toggle {
    display: none;
    margin-left: auto;
}

.tray-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: 4px 0;
}

.tray-empty {
    padding: 20px 16px;
    color: var(--muted);
    font-size: 14px;
}

.tray-group {
    padding: 10px 16px 6px;
}

.tray-group + .tray-group {
    border-top: 1px solid var(--line);
}

.tray-group-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.tray-group-head a {
    color: var(--text);
    font-weight: 700;
    text-decoration: none;
}

.tray-group-head a:hover {
    text-decoration: underline;
}

.tray-group-head .link-btn {
    margin-left: auto;
}

.tray-items {
    margin: 0;
    padding: 0;
    list-style: none;
}

.tray-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto 26px;
    align-items: center;
    gap: 8px;
    padding: 4px 0 4px 30px;
    font-size: 14px;
}

.tray-item.is-new {
    animation: tray-in 0.5s ease-out;
}

@keyframes tray-in {
    from {
        background: var(--sel-bg);
        transform: translateX(8px);
    }
    to {
        background: transparent;
        transform: none;
    }
}

.tray-item-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tray-item-new {
    color: var(--muted);
    font-size: 13px;
}

.tray-item-new.is-low {
    color: var(--amber);
}

.tray-remove {
    display: grid;
    place-items: center;
    width: 26px;
    height: 26px;
    padding: 0;
    border: 0;
    border-radius: 4px;
    background: none;
    color: var(--muted);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
}

.tray-remove:hover {
    background: var(--surface-2);
    color: var(--red);
}

.tray-foot {
    padding: 12px 16px 16px;
    border-top: 1px solid var(--line);
}

.tray-totals {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    color: var(--muted);
    font-size: 13px;
}

.tray-totals strong {
    color: var(--text);
}

.tray-foot .link-btn {
    display: block;
    margin: 10px auto 0;
}

/* Finestra blacklist */

dialog {
    width: min(520px, calc(100vw - 32px));
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}

dialog::backdrop {
    background: rgba(0, 0, 0, 0.6);
}

.dialog-head {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 18px 20px 0;
}

.dialog-head h2 {
    font-size: 20px;
}

.dialog-head p {
    margin-top: 4px;
    color: var(--muted);
    font-size: 14px;
}

.dialog-head .tray-remove {
    margin-left: auto;
}

.dialog-body {
    padding: 16px 20px 20px;
}

.field-label {
    display: flex;
    justify-content: space-between;
    margin-bottom: 6px;
    font-size: 14px;
    font-weight: 500;
}

.field-label span {
    color: var(--muted);
    font-weight: 400;
}

textarea {
    display: block;
    width: 100%;
    min-height: 120px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--bg);
    resize: vertical;
}

.check-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 4px 12px;
    margin: 18px 0 16px;
    padding: 0;
    border: 0;
}

.check-grid legend {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin: 0 0 6px;
    padding: 0;
    font-size: 14px;
    font-weight: 500;
}

.check-line {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    cursor: pointer;
}

.form-status {
    margin-top: 12px;
    font-size: 14px;
}

.form-status:empty {
    display: none;
}

.form-status.is-error {
    color: var(--red);
}

.form-status.is-ok {
    color: var(--accent-text);
}

/* Attesa sincronizzazione */

.overlay {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: grid;
    place-items: center;
    padding: 24px;
    background: rgba(0, 0, 0, 0.7);
}

.overlay[hidden] {
    display: none;
}

.overlay-box {
    width: min(440px, 100%);
    padding: 24px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.overlay-box h2 {
    font-size: 20px;
}

.overlay-box p {
    margin-top: 8px;
    color: var(--muted);
}

.progress {
    height: 4px;
    margin-top: 18px;
    border-radius: 2px;
    background: var(--line);
    overflow: hidden;
}

.progress i {
    display: block;
    width: 35%;
    height: 100%;
    border-radius: 2px;
    background: var(--green);
    animation: progress 1.4s ease-in-out infinite;
}

@keyframes progress {
    from {
        transform: translateX(-100%);
    }
    to {
        transform: translateX(290%);
    }
}

/* Pagina opzioni di rigenerazione */

.options h1 {
    font-size: 28px;
}

.options > p {
    margin-top: 6px;
    color: var(--muted);
    max-width: 62ch;
}

.back-link {
    display: inline-block;
    margin-bottom: 14px;
    color: var(--muted);
    font-size: 14px;
}

.back-link:hover {
    color: var(--text);
}

.group {
    margin: 22px 0 0;
    padding: 6px 16px 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--surface);
}

.group legend {
    padding: 0 8px;
    margin-left: -8px;
    color: var(--accent-text);
    font-size: 14px;
    font-weight: 700;
}

.group .check-line {
    padding: 7px 0;
}

.group .check-line + .check-line,
.group .sub-options + .check-line {
    border-top: 1px solid var(--line);
}

.sub-options {
    margin: 0 0 6px 8px;
    padding-left: 18px;
    border-left: 2px solid var(--green-deep);
}

.group .sub-options .check-line {
    padding: 5px 0;
    border-top: 0;
    font-size: 14px;
}

.master-line {
    margin-top: 22px;
    font-weight: 700;
}

.date-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 0 4px;
}

.date-row select,
.date-row input {
    min-height: 38px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--bg);
    color-scheme: dark;
}

:root[data-theme="light"] .date-row select,
:root[data-theme="light"] .date-row input {
    color-scheme: light;
}

.date-row input:disabled {
    opacity: 0.45;
}

.hint {
    margin-top: 8px;
    color: var(--muted);
    font-size: 13px;
}

.tray-item.is-readonly {
    grid-template-columns: minmax(0, 1fr) auto;
}

@media (min-width: 1001px) {
    /* Qui a restare fisso è il contenitore: riepilogo e invio scorrono insieme */
    .options-layout .side {
        position: sticky;
        top: calc(var(--bar-h) + 24px);
    }

    .options-layout .tray {
        position: static;
    }
}

/* Accesso con PIN */

.login {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 24px;
}

.login-box {
    width: min(380px, 100%);
    padding: 28px;
    border: 1px solid var(--line);
    border-top: 3px solid var(--green);
    border-radius: 10px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.login-box .brand {
    margin-bottom: 22px;
}

.login-box h1 {
    font-size: 24px;
}

.login-box p {
    margin-top: 6px;
    color: var(--muted);
}

.pin-input {
    display: block;
    width: 100%;
    margin: 20px 0 14px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--bg);
    font-size: 26px;
    font-weight: 700;
    letter-spacing: 10px;
    text-align: center;
}

.pin-input:focus {
    border-color: var(--green);
}

/* Schermi stretti */

@media (max-width: 1000px) {
    .layout {
        grid-template-columns: minmax(0, 1fr);
        padding: 20px 16px 150px;
    }

    .topbar {
        position: static;
        flex-wrap: wrap;
        padding: 10px 16px;
    }

    .crm-head {
        position: static;
    }

    .lists th {
        position: static;
    }

    .tray {
        position: fixed;
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 30;
        max-height: 80vh;
        border-radius: 12px 12px 0 0;
        border-left: 0;
        border-right: 0;
        border-bottom: 0;
    }

    .tray-toggle {
        display: inline-flex;
    }

    .tray:not(.is-open) .tray-body,
    .tray:not(.is-open) .tray-totals,
    .tray:not(.is-open) .tray-foot .link-btn {
        display: none;
    }

    .tray-head {
        padding: 10px 16px;
    }

    .tray:not(.is-open) .tray-head {
        border-bottom: 0;
    }

    .tray:not(.is-open) .tray-foot {
        padding-top: 0;
        border-top: 0;
    }

    /* Pagina opzioni: riepilogo in alto, invio fisso in basso */
    .options-layout .side {
        display: contents;
    }

    .options-layout .tray {
        position: static;
        order: -1;
        max-height: 260px;
        border: 1px solid var(--line);
        border-top: 3px solid var(--green);
        border-radius: 10px;
        box-shadow: none;
    }

    .options-layout {
        display: flex;
        flex-direction: column;
    }

    .options-layout .submit-box {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 30;
        margin: 0;
        padding: 12px 16px 16px;
        border-top: 1px solid var(--line);
        background: var(--surface);
    }
}

@media (max-width: 720px) {
    .sync {
        order: 3;
        width: 100%;
        flex-wrap: wrap;
    }

    .lists,
    .lists tbody {
        display: block;
    }

    .lists thead {
        display: none;
    }

    .lists tbody tr {
        display: grid;
        grid-template-columns: 38px minmax(0, 1fr) auto;
        align-items: center;
        border-bottom: 1px solid var(--line);
    }

    .lists tbody tr:last-child {
        border-bottom: 0;
    }

    .lists td {
        border-bottom: 0;
    }

    /* In griglia le celle non riempiono la riga: lo sfondo passa alla riga */
    .lists tbody tr.is-selected {
        background: var(--sel-bg);
        box-shadow: inset 3px 0 0 var(--green);
    }

    .lists tbody tr.is-selected td,
    .lists tbody tr.is-selected td:first-child,
    .lists tbody tr:hover td {
        background: none;
        box-shadow: none;
    }

    .lists td.c-extra {
        display: none;
    }

    .lists .c-new {
        width: auto;
    }

    .row-meta {
        display: block;
        color: var(--muted);
        font-size: 12px;
        font-weight: 400;
    }

    .crm-head {
        flex-wrap: wrap;
        height: auto;
        padding: 8px 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    .tray-item.is-new,
    .progress i {
        animation: none;
    }

    .progress i {
        width: 100%;
        opacity: 0.6;
    }
}
