:root {
    --bg: #ffffff;
    --fg: #1f1f1f;
    --link: #0b57d0;
    --muted: color-mix(in srgb, var(--fg) 60%, var(--bg));
    --line: color-mix(in srgb, var(--fg) 14%, var(--bg));
    --panel: color-mix(in srgb, var(--fg) 5%, var(--bg));
    --ui-font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: light;
}
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--fg);
    font-family: var(--ui-font);
    -webkit-tap-highlight-color: transparent;
}
button {
    font: inherit;
    color: inherit;
    background: none;
    border: 0;
    padding: 0;
    cursor: pointer;
}
a { color: var(--link); }
[hidden] { display: none !important; }

.icon-button {
    display: inline-grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
}
.icon-button:hover { background: var(--panel); }
.icon-button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Libreria */
.library-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top));
    border-bottom: 1px solid var(--line);
}
.library-header h1 { margin: 0; font-size: 1.4rem; font-weight: 650; }
.library {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 24px 16px;
    padding: 20px 16px max(20px, env(safe-area-inset-bottom));
    max-width: 1200px;
    margin: 0 auto;
}
.book { display: block; text-decoration: none; color: inherit; min-width: 0; }
.book-cover {
    position: relative;
    aspect-ratio: 2 / 3;
    border-radius: 4px;
    background: var(--panel);
    border: 1px solid var(--line);
    overflow: hidden;
    display: grid;
    place-items: center;
    padding: 8px;
    text-align: center;
    font-size: .85rem;
    color: var(--muted);
}
.offline-badge {
    position: absolute;
    left: 6px;
    bottom: 6px;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--bg);
    color: var(--fg);
    font-size: .7rem;
    font-weight: 600;
    box-shadow: 0 1px 4px rgb(0 0 0 / .25);
}
.offline .book:not(.available-offline) { opacity: .4; cursor: not-allowed; }
.book-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.book-cover:has(img) { padding: 0; }
.book-title { margin: 8px 0 2px; font-size: .9rem; font-weight: 600; line-height: 1.25; overflow-wrap: anywhere; }
.book-author { margin: 0; font-size: .8rem; color: var(--muted); }
.message { padding: 48px 16px; text-align: center; color: var(--muted); }

/* Pannello impostazioni */
.settings {
    position: fixed;
    z-index: 20;
    top: calc(56px + env(safe-area-inset-top));
    right: 12px;
    width: min(320px, calc(100vw - 24px));
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgb(0 0 0 / .18);
    padding: 16px;
}
.settings h2 { margin: 0 0 8px; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.settings section + section { margin-top: 16px; }
.choices { display: flex; flex-wrap: wrap; gap: 8px; }
.choice {
    flex: 1 1 0;
    min-width: 64px;
    padding: 10px 8px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--panel);
}
.choice[aria-pressed="true"] { border-color: var(--link); box-shadow: inset 0 0 0 1px var(--link); }
.choice.swatch { color: var(--sw-fg); background: var(--sw-bg); }
.size-row { display: flex; align-items: center; gap: 12px; }
.size-row output { flex: 1; text-align: center; font-variant-numeric: tabular-nums; }
.size-row .choice { flex: 0 0 56px; font-weight: 600; }

/* Lettore */
.reader { display: grid; grid-template-rows: auto 1fr auto; height: 100%; height: 100dvh; }
.reader-bar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    padding-top: max(4px, env(safe-area-inset-top));
    min-width: 0;
}
.reader-title { flex: 1; min-width: 0; margin: 0 4px; font-size: .9rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-stage { position: relative; min-height: 0; }
foliate-view { display: block; width: 100%; height: 100%; }
.page-turn {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 48px;
    z-index: 5;
    display: grid;
    place-items: center;
    color: var(--muted);
    opacity: 0;
    transition: opacity .15s;
}
.page-turn:hover, .page-turn:focus-visible { opacity: 1; }
.page-turn.prev { left: 0; }
.page-turn.next { right: 0; }
.page-turn svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2; }
.reader-footer {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 16px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    font-size: .75rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}
.reader-footer span:first-child { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

/* Indice */
.toc-backdrop { position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / .35); }
.toc {
    position: fixed;
    z-index: 31;
    inset: 0 auto 0 0;
    width: min(360px, 88vw);
    background: var(--bg);
    border-right: 1px solid var(--line);
    overflow-y: auto;
    padding: max(16px, env(safe-area-inset-top)) 0 max(16px, env(safe-area-inset-bottom));
}
.toc h2 { margin: 0 16px 8px; font-size: 1rem; }
.toc ol { list-style: none; margin: 0; padding: 0; }
.toc ol ol { padding-left: 16px; }
.toc a { display: block; padding: 8px 16px; color: inherit; text-decoration: none; font-size: .9rem; line-height: 1.3; }
.toc a:hover { background: var(--panel); }
.toc a[aria-current="true"] { color: var(--link); font-weight: 600; }

/* Barra della libreria */
.toolbar { display: flex; gap: 2px; }
.icon-button[aria-pressed="true"] { background: var(--panel); color: var(--link); }
.offline #upload-button, .offline #manage-button { opacity: .4; pointer-events: none; }
.status {
    margin: 12px 16px 0;
    padding: 10px 14px;
    border-radius: 8px;
    background: var(--panel);
    border: 1px solid var(--line);
    font-size: .9rem;
    max-width: 1168px;
}
@media (min-width: 1232px) { .status { margin-inline: auto; } }
.status.error { border-color: #c0392b; }

/* Cancellazione */
.delete-button {
    display: none;
    position: absolute;
    top: 6px;
    right: 6px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    place-items: center;
    background: #c0392b;
    color: #fff;
    box-shadow: 0 1px 4px rgb(0 0 0 / .3);
}
.delete-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }
.managing .delete-button { display: grid; }
.managing .book { cursor: default; }

/* Accesso */
.login {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 24px 16px;
}
.login img { border-radius: 16px; }
.login h1 { margin: 0 0 8px; font-size: 1.6rem; }
.login form { width: min(340px, 100%); display: grid; gap: 14px; }
.login label { display: grid; gap: 6px; font-size: .85rem; color: var(--muted); }
.login input {
    font: inherit;
    font-size: 1rem;
    color: var(--fg);
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
}
.login input:focus { outline: 2px solid var(--link); outline-offset: -1px; }
.form-error { margin: 0; color: #c0392b; font-size: .9rem; }
.primary {
    padding: 12px;
    border-radius: 8px;
    background: var(--link);
    color: var(--bg);
    font-weight: 600;
}
.primary:disabled { opacity: .6; }
