:root {
  --bg: #080a0f;
  --bg-elev: #10131a;
  --bg-elev2: #171b24;
  --text: #f3f6fb;
  --muted: #98a2b3;
  --accent: #9aaeff;
  --accent-2: #d8f878;
  --danger: #ffa89e;
  --line: rgba(219, 230, 255, 0.12);
  --line-strong: rgba(219, 230, 255, 0.22);
  --radius: 16px;
  --tabbar-h: 64px;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: radial-gradient(circle at 88% -10%, rgba(154, 174, 255, 0.12), transparent 34%), var(--bg);
  color: var(--text);
  font-family: "Avenir Next", "Helvetica Neue", ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Nell'app installata su iOS, se scorre il documento la tabbar fissa si stacca
   dal fondo lasciando uno spazio: il documento resta fermo e scorre il body. */
html { height: 100%; overflow: hidden; }
body {
  height: 100%;
  overflow-y: auto;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}
body.reading-mode { padding-bottom: 0; background: #000; }
body.auth-mode { padding-bottom: 0; }

a { color: inherit; text-decoration: none; }
button { font: inherit; cursor: pointer; }
button:disabled { cursor: wait; opacity: 0.65; }
:focus-visible {
  outline: 3px solid var(--accent-2);
  outline-offset: 3px;
}

#app { max-width: 1080px; margin: 0 auto; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 18px) 18px 14px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(18px);
  border-bottom: 1px solid rgba(219, 230, 255, 0.08);
}
.topbar.col { flex-direction: column; align-items: stretch; }
.topbar.spread { justify-content: space-between; }
.topbar.back { padding-bottom: 8px; }
.brand { display: flex; align-items: center; gap: 9px; margin: 0; font-size: 24px; font-weight: 760; letter-spacing: -0.05em; }
.brand.small { font-size: 21px; }
.brand-mark { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 10px; background: var(--accent); color: #0a0d14; font-size: 17px; font-weight: 900; letter-spacing: 0; box-shadow: 0 8px 22px rgba(0, 0, 0, 0.28); }
.brand.small .brand-mark { width: 26px; height: 26px; border-radius: 8px; font-size: 15px; }
.brand-word { letter-spacing: -0.06em; }

.page { padding: 22px 18px 34px; }
.home-intro { padding: 18px 2px 22px; }
.home-intro h2 { max-width: 16ch; margin: 0; color: var(--text); font-size: clamp(30px, 7vw, 52px); line-height: 0.98; letter-spacing: -0.06em; }
.section-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.section-note { color: var(--muted); font-size: 12px; white-space: nowrap; letter-spacing: 0.02em; }

/* Card & griglie */
.shelf { margin-bottom: 34px; }
.shelf h2, .chapters-head h2 { font-size: 17px; letter-spacing: -0.02em; margin: 6px 2px 14px; }
.continue-shelf { padding: 18px 16px 16px; margin-inline: -2px; border: 1px solid var(--line); border-radius: 20px; background: linear-gradient(135deg, rgba(154, 174, 255, 0.1), rgba(16, 19, 26, 0.72) 58%); }
.row {
  display: flex;
  gap: 14px;
  overflow-x: auto;
  padding-bottom: 6px;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.row::-webkit-scrollbar { display: none; }
.row .card { flex: 0 0 130px; scroll-snap-align: start; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 24px 16px;
}

.card { display: flex; flex-direction: column; gap: 8px; position: relative; transition: transform 180ms var(--ease-out), opacity 180ms ease; }
.card:hover { transform: translateY(-3px); }
.cover {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-elev2);
  border: 1px solid var(--line);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.24);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-ph {
  width: 100%; height: 100%;
  display: grid; place-items: center;
  font-size: 34px; font-weight: 800; letter-spacing: -0.08em; color: var(--accent); opacity: 0.68;
}
.card-title {
  font-size: 13px;
  line-height: 1.25;
  font-weight: 650;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.resume {
  position: absolute;
  left: 6px; right: 6px; bottom: 34px;
  background: color-mix(in srgb, var(--accent) 92%, #11131a);
  color: #0b0e16;
  font-size: 10px; font-weight: 700;
  padding: 3px 6px; border-radius: 8px;
  text-align: center;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.new-chapters {
  position: absolute;
  top: 6px; right: 6px;
  background: var(--accent-2);
  color: #000;
  font-size: 10px; font-weight: 800;
  padding: 3px 7px; border-radius: 999px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  white-space: nowrap;
}

/* Dettaglio */
.detail-head { display: flex; gap: 18px; margin-bottom: 18px; }
.cover.big { flex: 0 0 130px; }
.detail-info { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.detail-info h1 { font-size: clamp(24px, 5vw, 34px); margin: 0; line-height: 1.02; letter-spacing: -0.05em; }
.muted { color: var(--muted); font-size: 14px; margin: 0; }
.muted strong { color: var(--text); }
.desc { max-width: 68ch; color: #d5dbea; font-size: 15px; line-height: 1.6; margin: 8px 0 26px; }

.tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
  font-size: 11px; font-weight: 600;
  background: rgba(154, 174, 255, 0.1); color: #c5cffd;
  padding: 4px 9px; border-radius: 999px;
}

.btn {
  align-self: flex-start;
  background: var(--bg-elev2);
  color: var(--text);
  border: 1px solid var(--line-strong);
  padding: 11px 16px;
  border-radius: 12px;
  font-weight: 650;
  transition: transform 180ms var(--ease-out), background-color 180ms ease, border-color 180ms ease, opacity 180ms ease;
}
.btn:hover:not(:disabled) { border-color: color-mix(in srgb, var(--accent) 60%, var(--line)); background: color-mix(in srgb, var(--bg-elev2) 78%, var(--accent) 22%); }
.btn:active { transform: scale(0.97); }
.primary-link { display: inline-flex; margin-top: 10px; background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.save.on { background: var(--accent); border-color: var(--accent); color: #0a0d15; }
.detail-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn.continue { background: var(--accent-2); border-color: var(--accent-2); color: #10140c; }
.btn.continue:disabled { background: var(--bg-elev2); border-color: var(--line); color: var(--muted); cursor: default; transform: none; }

/* Capitoli */
.chapters-head { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.chapters { display: flex; flex-direction: column; gap: 8px; }
.chapter {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: color-mix(in srgb, var(--bg-elev) 92%, var(--accent) 8%);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
  transition: background-color 180ms ease, border-color 180ms ease, transform 180ms var(--ease-out);
}
.chapter:hover { background: var(--bg-elev2); border-color: var(--line-strong); transform: translateY(-1px); }
.chapter.read { opacity: 0.6; }
.ch-main { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.ch-title { font-weight: 600; font-size: 15px; }
.chev { color: var(--muted); font-size: 22px; }
.badge {
  font-size: 11px; font-weight: 700;
  background: var(--accent); color: #fff;
  padding: 3px 8px; border-radius: 999px;
}

/* Ricerca e campi di testo */
.search-input {
  width: 100%;
  background: var(--bg-elev);
  border: 1px solid var(--line-strong);
  color: var(--text);
  padding: 13px 15px;
  border-radius: 14px;
  font-size: 16px;
  outline: none;
}
.search-input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(154, 174, 255, 0.12); }
.filter-row {
  display: flex; gap: 8px;
  overflow-x: auto;
  margin-bottom: 16px;
  padding-bottom: 2px;
  -webkit-overflow-scrolling: touch;
}
.filter-row::-webkit-scrollbar { display: none; }
.filter-row .chip { flex: 0 0 auto; white-space: nowrap; }
.chip {
  background: var(--bg-elev2);
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px; font-weight: 600;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.hint { color: var(--muted); text-align: center; padding: 40px 12px; }

/* Loading states preserve layout and make the requested operation legible. */
.loading-panel { display: grid; justify-items: center; gap: 14px; padding: 72px 20px; color: var(--muted); }
.loading-label { margin: 0; font-size: 14px; }
.skeleton-shelf { margin-bottom: 26px; }
.skeleton-shelf h2 { color: var(--muted); }
.skeleton-card { flex: 0 0 130px; }
.skeleton-cover, .skeleton-text, .skeleton-line { background: linear-gradient(100deg, var(--bg-elev) 20%, var(--bg-elev2) 45%, var(--bg-elev) 70%); background-size: 240% 100%; animation: shimmer 1.4s ease-in-out infinite; }
.skeleton-cover { aspect-ratio: 2 / 3; border-radius: var(--radius); }
.skeleton-text { height: 10px; border-radius: 999px; margin-top: 8px; }
.skeleton-text.short { width: 68%; }
.skeleton-line { width: 140px; height: 4px; border-radius: 999px; }
@keyframes shimmer { to { background-position: -240% 0; } }

/* Stati */
.errorbox { text-align: center; padding: 40px 16px; color: var(--muted); }
.error-title { color: var(--text); font-weight: 700; margin: 0 0 6px; }
.error-detail { margin: 0; line-height: 1.5; }
.errorbox .btn { margin: 12px auto 0; display: inline-block; }
.empty { text-align: center; padding: 70px 20px; }
.empty.compact { padding: 52px 20px; }
.empty-ico { display: grid; place-items: center; width: 56px; height: 56px; margin: 0 auto 14px; border: 1px solid var(--line-strong); border-radius: 18px; color: var(--accent); }
.empty-ico svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* Icon button */
.icon-btn {
  display: inline-grid; place-items: center;
  min-width: 38px; height: 38px; padding: 0 8px;
  background: var(--bg-elev2);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  color: var(--text);
  font-size: 20px; line-height: 1;
}
.icon-btn svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Tabbar — ancorata al bordo inferiore con pill attivo */
#tabbar {
  --tabbar-pad-b: max(6px, calc(env(safe-area-inset-bottom) - 8px));
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  height: calc(var(--tabbar-h) + var(--tabbar-pad-b) - 6px);
  display: flex;
  gap: 4px;
  padding: 6px 10px var(--tabbar-pad-b);
  background: color-mix(in srgb, var(--bg-elev) 94%, transparent);
  backdrop-filter: blur(22px) saturate(1.5);
  -webkit-backdrop-filter: blur(22px) saturate(1.5);
  border-top: 1px solid color-mix(in srgb, #fff 8%, transparent);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -10px 32px rgba(0, 0, 0, 0.36);
  z-index: 20;
  transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.32s ease;
}
#tabbar a {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px;
  color: var(--muted);
  font-size: 11px; font-weight: 600;
  border-radius: 16px;
  transition: color 0.25s ease, background 0.25s ease, transform 0.12s var(--ease-out);
}
#tabbar a:active { transform: scale(0.9); }
#tabbar a .ico {
  display: flex;
  transition: transform 0.28s var(--ease-out);
}
#tabbar a .ico svg {
  width: 23px; height: 23px; display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#tabbar a.active {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 15%, transparent);
}
#tabbar a.active .ico { transform: translateY(-2px) scale(1.12); }

/* Nel reader e nella schermata di accesso la tabbar non serve */
body.reading-mode #tabbar,
body.auth-mode #tabbar { display: none; }

/* Nascondere le barre nel reader con un tap */
body.chrome-hidden #tabbar {
  transform: translateY(100%);
  opacity: 0;
  pointer-events: none;
}
body.chrome-hidden .reader-bar {
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
}

/* Reader */
.reader-bar {
  position: fixed; top: 0; left: 0; right: 0;
  z-index: 30;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: color-mix(in srgb, #000 70%, transparent);
  backdrop-filter: blur(10px);
  transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.32s ease;
}
.reader-actions { display: flex; gap: 8px; }
.counter { font-size: 14px; font-weight: 700; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-stage { min-height: 100vh; background: #000; }
.reader-stage.vertical {
  display: flex; flex-direction: column; align-items: center;
  padding-top: 56px;
}
.reader-stage.vertical .page { width: 100%; max-width: 720px; height: auto; display: block; }
.reader-stage.vertical .page:not(.loaded) { min-height: 85vh; background: var(--bg-elev); }
.reader-stage.horizontal {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  padding: 56px 0 12px;
}
.reader-stage.horizontal .page { max-width: 100%; max-height: 100%; object-fit: contain; }
.tap-zone {
  position: fixed; top: 0; bottom: 0;
  width: 33%;
  background: transparent; border: none;
  z-index: 25;
}
.tap-zone.left { left: 0; }
.tap-zone.right { right: 0; }

/* Fine capitolo: capitolo precedente / successivo */
.chapter-nav {
  display: flex; gap: 10px;
  width: 100%; max-width: 720px;
  padding: 20px 14px calc(env(safe-area-inset-bottom) + 20px);
}
.chapter-nav:empty { display: none; }
.chapter-nav .btn {
  flex: 1; min-width: 0;
  align-self: stretch;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 12px 10px;
}
.chapter-nav .btn:disabled { opacity: 0.35; pointer-events: none; }
.chapter-nav .nav-label { font-size: 14px; }
.chapter-nav .nav-title {
  font-size: 12px; font-weight: 500; opacity: 0.8;
  max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.reader-stage.horizontal .chapter-nav {
  position: relative; z-index: 26;
  flex-direction: column;
  max-width: 360px; padding: 0 24px;
}

/* Accesso */
.auth {
  min-height: 100dvh;
  display: grid; align-content: center; justify-items: center; gap: 28px;
  padding: calc(env(safe-area-inset-top) + 32px) 18px calc(env(safe-area-inset-bottom) + 32px);
}
.auth-form {
  width: min(100%, 400px);
  display: flex; flex-direction: column; gap: 14px;
  padding: 24px 20px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 20px;
}
.auth-form h2 { margin: 0; font-size: 24px; letter-spacing: -0.04em; }
.auth-form .muted { margin-top: -8px; line-height: 1.45; }
.auth-form .btn.save { align-self: stretch; background: var(--accent); border-color: var(--accent); color: #0a0d15; }
.field { display: block; }
.setup-label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.link-btn { align-self: center; background: none; border: 0; padding: 6px; color: var(--accent); font-size: 14px; font-weight: 600; }
.form-status { margin: 0; font-size: 14px; font-weight: 600; line-height: 1.45; color: var(--muted); }
.form-status.error { color: var(--danger); }
.form-status:empty { display: none; }

/* Account */
.setup h1 { font-size: 24px; margin: 4px 0 8px; }
.setup .muted { font-size: 14px; line-height: 1.5; margin-bottom: 18px; }
.setup .form-status { margin-top: 14px; }
.settings-heading { font-size: 17px; letter-spacing: -0.02em; margin: 32px 0 8px; }
.settings-actions { display: flex; flex-direction: column; gap: 10px; }
.settings-actions .btn { align-self: stretch; text-align: center; }

@media (min-width: 640px) {
  .row .card { flex-basis: 150px; }
  .cover.big { flex-basis: 170px; }
  .page { padding-inline: 28px; }
}

@media (min-width: 900px) {
  #tabbar { left: 50%; right: auto; width: min(520px, calc(100% - 40px)); transform: translateX(-50%); border: 1px solid color-mix(in srgb, #fff 8%, transparent); border-bottom: 0; }
}

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