/* === Reset e base === */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: #112233;
  color: #eee;
  font-family: sans-serif;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: row;
}

/* === Sidebar === */
#sidebar {
  width: 200px;
  background: #112233;
  border-right: 1px solid #444;
  padding: 10px;
  overflow-y: auto;
  flex-shrink: 0;
  z-index: 50; /* sotto l'header */
}

.book-button {
  display: block;
  margin-bottom: 6px;
  padding: 8px 12px;
  background: #334455;
  border: none;
  width: 100%;
  text-align: left;
  color: #eee;
  cursor: pointer;
  border-radius: 6px;
  font-size: 14px;
  transition: background 0.15s ease;
}

.book-button:hover {
  background: #445566;
}

/* Stati speciali sidebar */
.book-button.missing {
  background: #554433;
  color: orange;
  cursor: not-allowed;
}

/* Quando l'utente NON è loggato, il libro è totalmente disabilitato */
.book-button.locked {
  background: #444444;
  color: #888888;
  cursor: not-allowed;
  opacity: 0.6;
}

/* Quando il libro può essere sbloccato o acquistato (Stato Attivo/Cliccabile) */
.book-button.obtainable {
  background: #445566; /* Un blu/grigio più acceso rispetto a locked */
  color: #ffcc00;      /* Testo color oro/giallo per evidenziare l'azione */
  cursor: pointer;     /* Il cursore diventa la manina classica dei link */
  transition: background 0.15s ease;
}

.book-button.obtainable:hover {
  background: #556677; /* Schiarisce leggermente al passaggio del mouse */
  color: #fff;
}

.book-button.available {
  background: #334455;
}


/* === Main layout === */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}


/* === Header e Allineamento Elementi (Desktop) === */
#header {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  background: #223344;
  border-bottom: 1px solid #444;
  flex-shrink: 0;
  position: relative;
  z-index: 100;
}

#header > div {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
}

#bookTitle {
  font-weight: bold;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  max-width: 35%;
  margin-right: auto;
}


/* === Stile Pulsanti Header (Home, Sidebar, Temi, Frecce e Font) === */
#header button, 
#header .arrow, 
#homeBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  margin: 0;
  cursor: pointer;
  background: #334455;
  color: #eee;
  border: none;
  border-radius: 50%;
  font-family: inherit;
  font-size: 14px;
  font-weight: bold;
  text-decoration: none;
  transition: background-color 0.15s ease, transform 0.1s ease;
  vertical-align: middle;
  outline: none;
  flex-shrink: 0;
}

/* Allineamento icone immagini (home e frecce) */
#header button img, 
#header .arrow img, 
#homeBtn img {
  display: block;
  height: 18px;
  width: auto;
  pointer-events: none;
}

/* Hover & Active per tutti i pulsanti dell'header */
#header button:hover, 
#header .arrow:hover, 
#homeBtn:hover {
  background: #445566;
  color: #fff;
}

#header button:active, 
#header .arrow:active, 
#homeBtn:active {
  transform: scale(0.92);
}


/* === Selettori (TOC e Scelta Font) === */
#toc, 
#fontSelect {
  background: #334455;
  color: #eee;
  border: 1px solid #445566;
  padding: 0 6px;
  border-radius: 4px;
  height: 34px;
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  outline: none;
  vertical-align: middle;
  transition: border-color 0.15s ease, background-color 0.15s ease;
  flex-shrink: 0;
  box-sizing: border-box;
}

#toc:hover, 
#fontSelect:hover {
  background: #445566;
  border-color: #556677;
}

#toc {
  max-width: 150px;
}

#fontSelect {
  max-width: 95px;
}


/* === Viewer desktop === */
#viewer-wrap {
  flex: 1 1 auto;
  display: flex;
  justify-content: center;
  align-items: stretch;
  padding: 24px;
  box-sizing: border-box;
  width: 100%;
  height: calc(100vh - 50px);
}

#viewer {
  width: 100%;
  max-width: 1200px;
  height: 100%;
  min-width: 0;
  background: #FFFFFF;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  position: relative;
  display: flex;
  flex-direction: column;
}

#viewer iframe, #viewer > div {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  border: 0; margin: 0; padding: 0;
  box-sizing: border-box; flex: 1 1 auto;
}

/* Permette al JS di gestire lo swipe orizzontale su iOS senza che Safari lo blocchi */
#viewer, #viewer iframe {
  touch-action: pan-y pinch-zoom;
}

/* Spread divider */
#viewer.spread::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: 50%;
  width: 12px; transform: translateX(-6px);
  background: radial-gradient(
    ellipse at center,
    rgba(0, 0, 0, 0.15) 0%,
    rgba(0, 0, 0, 0.05) 40%,
    rgba(0, 0, 0, 0) 100%
  );
  pointer-events: none; z-index: 10;
}

/* === Mobile: Header e layout due righe ben proporzionato === */
body.mobile #header {
  position: fixed;
  top: env(safe-area-inset-top, 0);
  left: 0; right: 0;
  height: auto;
  min-height: 80px;
  padding: 8px;
  z-index: 100;
  background: rgba(34,51,68,0.98);
  border-bottom: 1px solid rgba(68,68,68,0.6);
  transform: translateY(-100%);
  transition: transform 220ms ease;
  box-sizing: border-box;
  padding-top: max(6px, env(safe-area-inset-top));
  padding-left: max(8px, env(safe-area-inset-left));
  padding-right: max(8px, env(safe-area-inset-right));
}

/* Layout flessibile a 2 righe su Mobile */
body.mobile #header > div {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  width: 100%;
}

/* Pulsanti compatti su Mobile */
body.mobile #header button, 
body.mobile #header .arrow, 
body.mobile #homeBtn {
  width: 28px;
  height: 28px;
  font-size: 11px;
  flex-shrink: 0;
}

/* Titolo compatto in riga 1 */
body.mobile #bookTitle {
  max-width: 36%;
  font-size: 12px;
  margin-right: 0;
}

/* Selettori adatti al viewport mobile */
body.mobile #toc {
  max-width: 36%;
  width: 36%;
  height: 32px;
  font-size: 11px;
 -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
}

body.mobile #fontSelect {
  max-width: 28%;
  width: 28%;
  height: 32px;
  font-size: 11px;
 -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
}

/* Stato visibile su mobile */
body.mobile.header-visible #header {
  transform: translateY(0);
}

/* Viewer full e adattamento sotto l'header */
body.mobile #viewer-wrap {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 0;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
  padding: 0;
}

@supports not (height: 100dvh) {
  body.mobile.header-visible #viewer-wrap {
    height: calc(100vh - (env(safe-area-inset-top, 0) + 80px) - env(safe-area-inset-bottom, 0px));
  }
  body.mobile #viewer-wrap {
    height: calc(100vh - env(safe-area-inset-bottom, 0px));
  }
}

body.mobile.header-visible #viewer-wrap {
  top: calc(env(safe-area-inset-top, 0) + 80px);
  height: calc(100dvh - (env(safe-area-inset-top, 0) + 80px));
}

/* TAP ZONE (Solo per Mobile) */
#tap-zone-top {
  display: none; /* Disattivata su PC */
}

body.mobile #tap-zone-top {
  display: block;
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 60px;
  z-index: 999;
  background: transparent;
  pointer-events: auto;
}

body.mobile.header-visible #tap-zone-top {
  pointer-events: none;
  opacity: 0;
}