:root {
    --bg: #14151a;
    --bg-2: #1c1e26;
    --bg-3: #262932;
    --line: #313542;
    --text: #eceef3;
    --muted: #9aa0ae;
    --accent: #ffb43a;
    --accent-2: #4f8cff;
    --green: #34c759;
    --danger: #ff5468;
    --radius: 12px;
    --shadow: 0 6px 24px rgba(0,0,0,.35);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
a { color: inherit; text-decoration: none; cursor: pointer; }
button { font-family: inherit; cursor: pointer; }

/* ---------- Topbar ---------- */
#topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 20px;
    padding: 10px 18px;
    background: rgba(20,21,26,.92);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: .3px; cursor: pointer; white-space: nowrap; }
.brand span { color: var(--accent); }
#mainnav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
#mainnav a {
    padding: 7px 13px; border-radius: 20px; color: var(--muted); font-size: 14px; font-weight: 600;
}
#mainnav a:hover { color: var(--text); background: var(--bg-3); }
#mainnav a.active { color: #1b1400; background: var(--accent); }
.topright { display: flex; align-items: center; gap: 12px; }
.minutes { font-size: 13px; color: var(--accent); font-weight: 700; white-space: nowrap; }
.user { font-size: 13px; color: var(--muted); }
.btn-ghost {
    background: transparent; border: 1px solid var(--line); color: var(--muted);
    border-radius: 8px; padding: 6px 12px; font-size: 13px;
}
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }

/* ---------- Layout ---------- */
#app { max-width: 1200px; margin: 0 auto; padding: 22px 18px 80px; }
.page-title { font-size: 24px; font-weight: 700; margin: 6px 0 18px; }
.section-title { font-size: 15px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; margin: 22px 0 12px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.muted { color: var(--muted); }
.center-msg { text-align: center; color: var(--muted); padding: 60px 20px; }

/* ---------- Buttons ---------- */
.btn {
    background: var(--accent); color: #1b1400; border: none; border-radius: 8px;
    padding: 9px 16px; font-weight: 700; font-size: 14px;
}
.btn:hover { filter: brightness(1.06); }
.btn.secondary { background: var(--bg-3); color: var(--text); }
.btn.danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
.btn.small { padding: 6px 11px; font-size: 13px; }
.btn.tiny { padding: 5px 8px; font-size: 12px; font-weight: 600; border-radius: 6px; }
.btn:disabled { opacity: .5; cursor: default; }

/* Azioni sulle card film */
.movie-actions { display: flex; gap: 6px; margin-top: 8px; }
.movie-actions .m-seen { flex: 1 1 auto; }
.badge-corner.todo { background: rgba(120,110,90,.85); }

/* ---------- Filtri ---------- */
.tabs { display: flex; gap: 6px; margin-bottom: 16px; flex-wrap: wrap; }
.tab { padding: 7px 14px; border-radius: 20px; background: var(--bg-2); color: var(--muted); font-size: 13px; font-weight: 600; border: 1px solid var(--line); }
.tab.active { background: var(--bg-3); color: var(--text); border-color: var(--muted); }

/* ---------- Griglia poster ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 16px; }
.card { background: var(--bg-2); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); transition: transform .12s ease, border-color .12s; }
.card:hover { transform: translateY(-3px); border-color: var(--muted); }
.poster { position: relative; aspect-ratio: 2/3; background: var(--bg-3); display: flex; align-items: center; justify-content: center; }
.poster img { width: 100%; height: 100%; object-fit: cover; display: block; }
.poster .ph { font-size: 13px; color: var(--muted); padding: 10px; text-align: center; font-weight: 600; }
.fav-star { position: absolute; top: 6px; right: 8px; font-size: 16px; filter: drop-shadow(0 1px 2px rgba(0,0,0,.6)); }
.badge-corner { position: absolute; top: 6px; left: 6px; background: rgba(0,0,0,.7); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 20px; }
.card-body { padding: 9px 10px 11px; }
.card-title { font-size: 13px; font-weight: 600; line-height: 1.25; margin-bottom: 7px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 2.5em; }
.progress { height: 5px; background: var(--bg-3); border-radius: 3px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--green); }
.progress-label { font-size: 11px; color: var(--muted); margin-top: 5px; }

/* ---------- Dettaglio serie ---------- */
/* Banner full-width in cima: esce dai margini di #app fino ai bordi del viewport. */
.detail-banner {
    position: relative;
    margin-left: calc(50% - 50vw);
    margin-right: calc(50% - 50vw);
    margin-top: -22px; /* annulla il padding-top di #app */
    height: 280px;
    overflow: hidden;
    background: var(--bg-3);
    display: flex;
    align-items: flex-end;
    margin-bottom: 22px;
}
.detail-banner-img {
    position: absolute; inset: 0;
    background-size: cover;
    background-position: center 30%;
    /* leggera desaturazione + scurita per far risaltare il testo */
    filter: saturate(1.05);
}
/* Sfumatura dal basso per la leggibilita' del titolo. */
.detail-banner::after {
    content: ""; position: absolute; inset: 0;
    background: linear-gradient(to top, rgba(20,21,26,.96) 0%, rgba(20,21,26,.55) 35%, rgba(20,21,26,.15) 70%, rgba(20,21,26,0) 100%);
}
.detail-banner.no-img { background: linear-gradient(135deg, var(--bg-3), var(--bg-2)); }
.detail-back {
    position: absolute; top: 14px; left: max(18px, calc(50vw - 600px + 18px)); z-index: 2;
    color: #fff; font-size: 14px; font-weight: 600;
    background: rgba(0,0,0,.45); padding: 6px 12px; border-radius: 20px;
    backdrop-filter: blur(4px);
}
.detail-banner-title {
    position: relative; z-index: 1;
    width: 100%; max-width: 1200px; margin: 0 auto;
    padding: 18px 18px 20px;
}
.detail-banner-title h1 { font-size: 32px; margin: 0 0 8px; text-shadow: 0 2px 12px rgba(0,0,0,.6); }
.detail-meta { color: #d6dae2; font-size: 14px; margin-bottom: 0; text-shadow: 0 1px 6px rgba(0,0,0,.6); }
.detail-info { }
.detail-overview { color: #c7ccd6; line-height: 1.5; margin-bottom: 18px; max-width: 640px; margin-top: 14px; }
.detail-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.detail-tagline { font-style: italic; color: var(--muted); margin-top: 10px; }

/* Azioni sotto le card di ricerca */
.search-actions { display: flex; gap: 6px; margin-top: 8px; }
.search-actions .add-btn { flex: 1 1 auto; }
.search-actions .btn { text-align: center; }

/* Stagioni nell'anteprima TMDB */
.td-seasons { display: flex; flex-direction: column; gap: 10px; padding: 0 16px 20px; }
.td-season { display: flex; gap: 12px; align-items: center; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px; }
.td-season img { width: 46px; height: 69px; object-fit: cover; border-radius: 6px; flex: 0 0 auto; }
.td-season-ph { width: 46px; height: 69px; display: flex; align-items: center; justify-content: center; background: var(--bg-3); border-radius: 6px; font-size: 10px; color: var(--muted); text-align: center; flex: 0 0 auto; padding: 2px; }
.td-season-info { display: flex; flex-direction: column; gap: 3px; }
.td-season-info span { font-size: 12px; }
.wtw-tag { display: inline-flex; align-items: center; gap: 6px; background: var(--bg-3); border: 1px solid var(--line); border-radius: 8px; padding: 6px 11px; font-size: 13px; margin-top: 4px; }

/* ---------- Prossimo episodio (barra orizzontale) ---------- */
.next-ep {
    display: flex; align-items: center; gap: 14px;
    background: linear-gradient(135deg, var(--bg-3), var(--bg-2));
    border: 1px solid var(--line); border-left: 4px solid var(--accent);
    border-radius: var(--radius); padding: 12px 16px; margin-bottom: 22px;
}
.next-ep-label {
    font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
    color: var(--accent); white-space: nowrap; flex-shrink: 0;
}
.next-ep-main { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.next-ep-num { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; flex-shrink: 0; }
.next-ep-name { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.next-ep-btn { flex-shrink: 0; }
.next-ep.done { border-left-color: var(--green); }
.next-ep.done .next-ep-name { color: var(--green); }

/* ---------- Extra / Speciali ---------- */
.season-extra .season-head h3 { color: var(--muted); }
.extra-tag {
    font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em;
    color: var(--muted); background: var(--bg-3); border: 1px solid var(--line);
    border-radius: 20px; padding: 2px 8px; margin-left: 8px; vertical-align: middle; white-space: nowrap;
}

.season { margin-bottom: 12px; }
.season-head {
    display: flex; align-items: center; gap: 10px;
    width: 100%; text-align: left;
    background: transparent; border: none; border-bottom: 1px solid var(--line);
    padding: 12px 4px; margin-bottom: 6px; cursor: pointer; color: inherit;
}
.season-head:hover { background: var(--bg-2); }
.season-head h3 { margin: 0; font-size: 17px; flex: 1; }
.season-head .s-count { color: var(--muted); font-size: 13px; font-weight: 600; }
.season-chevron { color: var(--muted); font-size: 14px; flex-shrink: 0; transition: transform .18s ease; display: inline-block; }
.season.open > .season-head .season-chevron { transform: rotate(90deg); }
/* Corpo dell'accordion: collassato di default, aperto con .open */
.season-body { display: none; }
.season.open > .season-body { display: block; }
.ep-row { display: flex; align-items: center; gap: 12px; padding: 9px 6px; border-radius: 8px; }
.ep-row:hover { background: var(--bg-2); }
.ep-check { width: 22px; height: 22px; border-radius: 6px; border: 2px solid var(--line); background: transparent; flex-shrink: 0; display: flex; align-items: center; justify-content: center; color: #fff; font-size: 13px; }
.ep-check.on { background: var(--green); border-color: var(--green); }
.ep-num { color: var(--muted); font-size: 13px; width: 46px; flex-shrink: 0; font-variant-numeric: tabular-nums; }
.ep-name { flex: 1; font-size: 14px; }
.ep-date { color: var(--muted); font-size: 12px; }
.ep-through { font-size: 12px; color: var(--accent-2); }

/* ---------- To watch ---------- */
.tw-item { display: flex; align-items: center; gap: 14px; margin-bottom: 10px; }
.tw-item .next-ep-label { display: none; }
.tw-poster { flex-shrink: 0; display: block; line-height: 0; }
.tw-item img, .tw-item .ph { width: 48px; height: 72px; border-radius: 6px; object-fit: cover; background: var(--bg-3); }
.tw-item .ph { display: flex; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); text-align: center; padding: 4px; line-height: 1.2; }
.tw-info { flex: 1; min-width: 0; }
.tw-show { font-weight: 700; display: inline-block; }
.tw-ep { color: var(--muted); font-size: 13px; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tw-ep .next-ep-num { color: var(--accent-2); font-weight: 600; }

/* ---------- Auth ---------- */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth-card { background: var(--bg-2); border: 1px solid var(--line); border-radius: 16px; padding: 32px; width: 100%; max-width: 380px; box-shadow: var(--shadow); }
.auth-card h1 { text-align: center; margin: 0 0 6px; }
.auth-card .sub { text-align: center; color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input {
    width: 100%; padding: 11px 13px; border-radius: 9px; border: 1px solid var(--line);
    background: var(--bg); color: var(--text); font-size: 15px;
}
.field input:focus { outline: none; border-color: var(--accent-2); }
.auth-switch { text-align: center; margin-top: 16px; font-size: 14px; color: var(--muted); }
.auth-switch a { color: var(--accent-2); font-weight: 600; }
.form-error { color: var(--danger); font-size: 13px; margin-bottom: 12px; text-align: center; min-height: 18px; }

/* ---------- Search ---------- */
.search-bar { display: flex; gap: 10px; margin-bottom: 8px; }
.search-bar input { flex: 1; padding: 12px 15px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg-2); color: var(--text); font-size: 15px; }
.search-bar input:focus { outline: none; border-color: var(--accent-2); }
.seg { display: inline-flex; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 18px; }
.seg button { background: transparent; border: none; color: var(--muted); padding: 9px 18px; font-weight: 600; font-size: 14px; }
.seg button.active { background: var(--accent); color: #1b1400; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--bg-3); color: var(--text); padding: 11px 20px; border-radius: 10px; box-shadow: var(--shadow); border: 1px solid var(--line); z-index: 50; font-size: 14px; }

/* ---------- Loader ---------- */
.spinner { width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 60px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 720px) {
    /* ----- Header: solo brand + info utente, compatto ----- */
    #topbar {
        gap: 10px;
        padding: 10px 14px;
        padding-top: max(10px, env(safe-area-inset-top));
        /* Niente backdrop-filter su mobile: creerebbe un containing block
           che ancorerebbe la bottom-nav (position:fixed) al topbar invece che al viewport. */
        backdrop-filter: none;
        background: var(--bg);
    }
    .brand { font-size: 17px; }
    .brand span { display: inline; }
    .topright { margin-left: auto; gap: 10px; }
    .minutes { font-size: 12px; }
    .user { display: none; } /* poco spazio: nascondi username su mobile */
    .btn-ghost { padding: 6px 11px; font-size: 12px; }

    /* ----- Navigazione: bottom bar fissa (pattern mobile) ----- */
    #mainnav {
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
        display: flex; gap: 0; flex-wrap: nowrap;
        background: rgba(20,21,26,.96);
        backdrop-filter: blur(10px);
        border-top: 1px solid var(--line);
        padding: 6px 4px;
        padding-bottom: max(6px, env(safe-area-inset-bottom));
    }
    #mainnav a {
        flex: 1 1 0; min-width: 0;
        display: flex; flex-direction: column; align-items: center; gap: 2px;
        padding: 6px 2px; border-radius: 10px;
        font-size: 10px; font-weight: 600; line-height: 1;
        color: var(--muted); background: transparent;
        overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    }
    /* Icone Material Design come mask (ereditano il colore del testo). */
    #mainnav a::before {
        content: "";
        width: 24px; height: 24px;
        background-color: currentColor;
        -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
        -webkit-mask-position: center; mask-position: center;
        -webkit-mask-size: contain; mask-size: contain;
    }
    /* video_library */
    #mainnav a[data-nav="library"]::before {
        -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M4 6H2v14c0 1.1.9 2 2 2h14v-2H4V6zm16-4H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-9 12V6l7 4-7 4z'/%3E%3C/svg%3E");
        mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M4 6H2v14c0 1.1.9 2 2 2h14v-2H4V6zm16-4H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-9 12V6l7 4-7 4z'/%3E%3C/svg%3E");
    }
    /* live_tv */
    #mainnav a[data-nav="towatch"]::before {
        -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M21 6h-7.59l3.29-3.29L16 2l-4 4-4-4-.71.71L10.59 6H3c-1.1 0-2 .89-2 2v12c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V8c0-1.11-.9-2-2-2zm0 14H3V8h18v12zM9 10v8l7-4z'/%3E%3C/svg%3E");
        mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M21 6h-7.59l3.29-3.29L16 2l-4 4-4-4-.71.71L10.59 6H3c-1.1 0-2 .89-2 2v12c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V8c0-1.11-.9-2-2-2zm0 14H3V8h18v12zM9 10v8l7-4z'/%3E%3C/svg%3E");
    }
    /* movie */
    #mainnav a[data-nav="movies"]::before {
        -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M18 4l2 4h-3l-2-4h-2l2 4h-3l-2-4H8l2 4H7L5 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V4h-4z'/%3E%3C/svg%3E");
        mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M18 4l2 4h-3l-2-4h-2l2 4h-3l-2-4H8l2 4H7L5 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V4h-4z'/%3E%3C/svg%3E");
    }
    /* list */
    #mainnav a[data-nav="lists"]::before {
        -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z'/%3E%3C/svg%3E");
        mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z'/%3E%3C/svg%3E");
    }
    /* search */
    #mainnav a[data-nav="search"]::before {
        -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z'/%3E%3C/svg%3E");
        mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z'/%3E%3C/svg%3E");
    }
    #mainnav a:hover { background: transparent; color: var(--muted); }
    #mainnav a.active { background: transparent; color: var(--accent); }

    /* ----- Layout: spazio in fondo per la bottom bar ----- */
    #app { padding: 16px 12px calc(78px + env(safe-area-inset-bottom)); }
    /* Il toast va sopra la bottom bar */
    .toast { bottom: calc(80px + env(safe-area-inset-bottom)); }
    .page-title { font-size: 21px; }
    .grid { grid-template-columns: repeat(auto-fill, minmax(105px, 1fr)); gap: 12px; }

    /* ----- Dettaglio serie: banner piu' basso su mobile ----- */
    .detail-banner { height: 200px; margin-top: -16px; margin-bottom: 18px; }
    .detail-banner-title { padding: 14px 14px 16px; }
    .detail-banner-title h1 { font-size: 24px; }
    .detail-back { top: 12px; left: 12px; }
    .detail-actions .btn { flex: 1 1 auto; }

    /* ----- Prossimo episodio ----- */
    .next-ep { flex-wrap: wrap; gap: 8px 12px; padding: 12px 14px; }
    .next-ep-label { width: 100%; }
    .next-ep-btn { margin-left: auto; }

    /* ----- Righe episodio + "segna fino a qui" ----- */
    .ep-row {
        flex-wrap: wrap;
        gap: 6px 10px;
        padding: 10px 6px;
    }
    .ep-num { width: auto; }
    /* nome + link occupano il resto della prima riga */
    .ep-name { flex: 1 1 auto; min-width: 0; font-size: 14px; }
    /* la data va a capo, allineata sotto */
    .ep-date { width: 100%; padding-left: 34px; font-size: 11px; }
    /* "segna fino a qui" diventa un chip tappabile, va a capo se serve */
    .ep-through {
        display: inline-block;
        margin-top: 2px;
        padding: 3px 9px;
        background: var(--bg-3);
        border: 1px solid var(--line);
        border-radius: 20px;
        font-size: 12px;
        white-space: nowrap;
    }
}
