/* Base + Layout */
* { box-sizing: border-box; }

body {
  font-family: 'Segoe UI', sans-serif;
  margin: 0;
  background: #fefefe;
  display: flex;
  flex-direction: row;
}

/* Sidebar */
#sidebar {
  width: 320px;
  background: #fafafa;
  border-right: 1px solid #ddd;
  padding: 20px 10px 120px 20px;
  overflow-y: auto;
  position: fixed;
  top: 0; bottom: 0; left: 0;
  transition: left 0.3s ease;
  z-index: 1000;
}

#sessionList, #promptHistory, #chapterIndex {
  list-style: none;
  padding: 0;
  font-size: 0.9rem;
  margin-bottom: 12px;
}

/* Lista sessioni con altezza massima fissa */
#sessionList {
  max-height: 325px;
  overflow-y: auto;
}

/* Stile scrollbar per lista sessioni */
#sessionList::-webkit-scrollbar {
  width: 6px;
}

#sessionList::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

#sessionList::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

#sessionList::-webkit-scrollbar-thumb:hover {
  background: #a1a1a1;
}

#sessionList li,
#promptHistory li,
#chapterIndex li {
  margin-bottom: 6px;
  cursor: pointer;
  padding: 4px;
}

#sessionList li.active { font-weight: bold; color: #0066cc; }
#sessionList li:hover,
#promptHistory li:hover,
#chapterIndex li:hover {
  background: #eee;
}

/* Indice capitoli */
#chapterIndex {
  max-height: 225px;
  overflow-y: auto;
}

/* Stile scrollbar per indice capitoli */
#chapterIndex::-webkit-scrollbar {
  width: 6px;
}

#chapterIndex::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

#chapterIndex::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

#chapterIndex::-webkit-scrollbar-thumb:hover {
  background: #a1a1a1;
}

#chapterIndex li {
  font-size: 0.85rem;
  padding: 3px 8px;
  margin-bottom: 3px;
  border-left: 3px solid transparent;
  transition: all 0.2s ease;
}

#chapterIndex li:hover {
  border-left-color: #0066cc;
  background: #f0f8ff;
}

#chapterIndex li.story-title {
  font-weight: bold;
  color: #333;
  border-left-color: #ff6b35;
  background: #fff5f0;
}

#chapterIndex li.chapter-item {
  padding-left: 16px;
  color: #666;
}

#chapterIndex li.empty-message {
  font-style: italic;
  color: #999;
  cursor: default;
  padding: 8px;
  text-align: center;
}

/* Buttons */
#newSessionBtn,
#exportJsonBtn,
#exportMdBtn,
#importJsonBtn,
#installBtn {
  width: 100%;
  padding: 6px 10px;
  font-size: 0.9rem;
  cursor: pointer;
}

/* Main Area */
main {
  margin-left: 340px;
  flex-grow: 1;
  padding: 20px;
  padding-bottom: 180px;
}

#mainHeader {
  position: sticky;
  top: 0;
  background: #fefefe;
  border-bottom: 1px solid #ddd;
  padding: 0px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.03);
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Header actions container */
.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-btn {
  background: transparent;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.header-btn:hover {
  background: rgba(0,0,0,0.1);
  transform: scale(1.1);
}

/* Story content */
#markdownStory {
  margin-top: 20px;
  line-height: 1.6;
  text-align: justify;
  padding-left: 8px;
  padding-right: 12px;
}

/* Titoli dei capitoli non giustificati */
#markdownStory h1,
#markdownStory h2,
#markdownStory h3,
#markdownStory h4,
#markdownStory h5,
#markdownStory h6 {
  text-align: left;
}

/* Titolo e sottotitolo del libro sempre centrati */
#markdownStory .book-title,
#markdownStory .book-subtitle {
  text-align: center;
}

.story-block.highlight,
.chapter-title.highlight {
  animation: highlightFade 2s ease-in-out;
}

.story-separator {
  margin: 50px 0;
  border-top: 1px dashed #bbb;
}

.stats-inline {
  margin-top: 8px;
  font-size: 0.85rem;
  color: #777;
}

@keyframes highlightFade {
  0% { background-color: #ffffcc; }
  100% { background-color: transparent; }
}

/* Form - inline layout */
form {
  position: fixed;
  bottom: 0;
  left: 320px;
  width: calc(100% - 320px);
  background: #fff;
  padding: 12px 20px;
  display: flex;
  gap: 12px;
  align-items: center;
  border-top: 1px solid #ddd;
  box-shadow: 0 -2px 5px rgba(0,0,0,0.05);
  z-index: 1000;
}

/* Submit button as icon only - standard style */
#submitBtn {
  /*background: #007bff;*/
  border: none;
  border-radius: 0px;
  padding: 12px 16px;
  font-size: 1.2rem;
  cursor: pointer;
  color: white;
  transition: all 0.2s ease;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  border: 1px solid #000;
}

#submitBtn:hover {
  /*background: #0056b3;*/
  transform: translateY(-1px);
}

#submitBtn:active {
  transform: translateY(0);
  animation: pulse 0.3s ease-out;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

/* Settings sidebar */
.settings-sidebar {
  position: fixed;
  top: 0;
  right: -340px;
  width: 320px;
  height: 100%;
  background: #fafafa;
  box-shadow: -2px 0 12px rgba(0,0,0,0.08);
  border-left: 1px solid #ddd;
  z-index: 2000;
  transition: right 0.3s cubic-bezier(.4,0,.2,1);
  display: flex;
  flex-direction: column;
}
.settings-sidebar.open {
  right: 0;
}

.settings-header {
  flex-shrink: 0;
  padding: 20px 20px 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  font-size: 1.1rem;
  font-weight: 600;
}

.settings-content {
  flex: 1;
  overflow-y: auto;
  padding: 0 20px;
}

.settings-sidebar label {
  font-size: 0.97rem;
  color: #444;
  margin-bottom: 2px;
  margin-top: 8px;
  font-weight: 500;
  display: block;
}
.settings-sidebar select {
  width: 100%;
  font-size: 0.97rem;
  padding: 8px;
  margin-bottom: 8px;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #f7f7fa;
  color: #222;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
}
body.darkmode .settings-sidebar {
  background: #1a1a1a;
  color: #eee;
  border-left-color: #444;
}
body.darkmode .settings-sidebar label {
  color: #e0e0e0;
}
body.darkmode .settings-sidebar select {
  background: #333;
  color: #e0e0e0;
  border: 1px solid #555;
}

/* Settings bottom actions - Tour Guide & Install App Buttons */
.settings-bottom-actions {
  flex-shrink: 0;
  padding: 16px 20px 20px 20px;
  border-top: 1px solid #e0e0e0;
  background: #fafafa;
  display: flex;
  gap: 8px;
}

.action-btn {
  flex: 1;
  padding: 12px 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: center;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tour-btn {
  background: linear-gradient(135deg, #4CAF50, #45a049);
  color: white;
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.3);
  margin: 0;
}

.tour-btn:hover {
  background: linear-gradient(135deg, #45a049, #3d8b40);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.4);
}

.install-btn {
  background: linear-gradient(135deg, #2196F3, #1976D2);
  color: white;
  box-shadow: 0 2px 8px rgba(33, 150, 243, 0.3);
  margin: 0;
}

.install-btn:hover {
  background: linear-gradient(135deg, #1976D2, #1565C0);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.4);
}

.action-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

/* Dark mode styles for action buttons */
body.darkmode .settings-bottom-actions {
  border-top-color: #444;
  background: #1a1a1a;
}

body.darkmode .tour-btn {
  background: linear-gradient(135deg, #4CAF50, #45a049);
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.2);
}

body.darkmode .tour-btn:hover {
  background: linear-gradient(135deg, #45a049, #3d8b40);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

body.darkmode .install-btn {
  background: linear-gradient(135deg, #2196F3, #1976D2);
  box-shadow: 0 2px 8px rgba(33, 150, 243, 0.2);
}

body.darkmode .install-btn:hover {
  background: linear-gradient(135deg, #1976D2, #1565C0);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
}

/* Responsive: Stack buttons on very narrow screens */
@media (max-width: 400px) {
  .settings-bottom-actions {
    flex-direction: column;
    gap: 8px;
  }
}

form label {
  font-size: 0.97rem;
  color: #444;
  margin-bottom: 2px;
  margin-top: 8px;
  font-weight: 500;
}
body.darkmode form select {
  background: #222;
  color: #eee;
  border: 1px solid #444;
}


button {
  padding: 8px 16px;
  font-size: 1rem;
  cursor: pointer;
}

/* Responsive */
@media (max-width: 768px) {
  body { flex-direction: column; }

  #sidebar {
    left: -100%;
    width: 80%;
    max-width: 320px;
    z-index: 1500; /* Aumentato per essere sopra al form */
  }

  #sidebar.visible { left: 0; }

  #toggleSidebar { display: inline-block; }

  main {
    margin-left: 0;
    padding-bottom: 160px;
  }

  /* Form mantiene layout inline anche su mobile */
  form {
    left: 0;
    width: 100%;
    padding: 12px 16px;
    gap: 8px;
  }

  /* Textarea e pulsante sulla stessa riga */
  textarea#inputText {
    flex: 1;
    min-height: 44px;
    max-height: 44px;
    font-size: 16px; /* Previene zoom su iOS */
  }

  #submitBtn {
    min-width: 44px;
    padding: 12px;
    font-size: 1.1rem;
  }

  /* Header actions responsive */
  .header-actions {
    gap: 8px;
  }

  .header-btn {
    font-size: 1.2rem;
    padding: 4px 6px;
  }

  #voiceProgressCancel, #voiceProgressPause {
    width: auto;
  }
}

.renameSessionBtn, .deleteSessionBtn {
    padding: 0px 0px;
    float: right;
    width: auto;
}

#toggleSidebar {
    float: right;
    width: auto;
}

#sessionList li {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#sessionList li span {
  flex-grow: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.renameSessionBtn, .deleteSessionBtn {
  margin-left: 6px;
  font-size: 0.8rem;
  padding: 2px 4px;
  background: transparent;
  border: none;
  cursor: pointer;
}

#toggleSidebar {
  display: none;
}

#toggleSidebar {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 4px 8px;
  transition: transform 0.2s ease;
}

#toggleSidebar:hover {
  transform: scale(1.2);
}


@media (max-width: 768px) {
  #toggleSidebar {
    display: inline-block;
  }

  /* Global voice progress bar responsive styles */
  .global-voice-progress {
    padding: 10px 12px;
    font-size: 0.9rem;
  }

  .voice-progress-info {
    font-size: 0.8rem;
    gap: 8px;
  }

  .voice-progress-info span {
    min-width: auto;
    flex: 1;
  }

  #voiceCurrentText {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60%;
  }
}

.story-cover {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  margin: 20px auto 40px;
  box-shadow: 0 0 12px rgba(0, 0, 0, 0.08);
}

.book-title {
  font-size: 2.6em;
  font-weight: 700;
  font-family: 'Georgia', serif;
  text-align: center;
  margin-top: 40px;
  margin-bottom: 10px;
  color: #222;
}

.book-subtitle {
  font-size: 1.3em;
  font-style: italic;
  font-family: 'Georgia', serif;
  text-align: center;
  color: #666;
  margin-bottom: 20px;
}

textarea#inputText {
  flex: 1;
  padding: 12px;
  font-size: 1rem;
  font-family: inherit;
  line-height: 1.4;
  resize: none;
  overflow-y: auto;
  height: 50px;
  min-height: 50px;
  max-height: 50px;
}

.story-loading.fade-in {
  opacity: 0;
  animation: fadeIn 1s ease-in-out forwards, blinkLoop 2s ease-in-out infinite;
}

@keyframes fadeIn {
  to { opacity: 1; }
}

@keyframes blinkLoop {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

body.darkmode {
  background: #1a1a1a;
  color: #eee;
}

body.darkmode #sidebar {
  background: #111;
  border-right: 1px solid #333;
}

body.darkmode #storyCounter {
  color: #999;
}

body.darkmode main,
body.darkmode form {
  background: #1a1a1a;
}

body.darkmode #mainHeader {
  background: #1a1a1a;
  border-bottom: 1px solid #333;
  box-shadow: none;
}

body.darkmode .header-btn:hover {
  background: rgba(255,255,255,0.1);
}

/* Dark mode for submit button */
body.darkmode #submitBtn {
  background: #4da6ff;
}

body.darkmode #submitBtn:hover {
  background: #0066cc;
}

body.darkmode textarea,
body.darkmode input,
body.darkmode button {
  background: #222;
  color: #eee;
  border: 1px solid #444;
}

body.darkmode .story-loading {
  color: #bbb;
}

body.darkmode .book-title {
  color: #f5f5f5;
}

body.darkmode .book-subtitle {
  color: #bbb;
}

.book-title {
  transition: color 0.3s ease, text-shadow 0.3s ease;
}

.book-subtitle {
  transition: color 0.3s ease, text-shadow 0.3s ease;
}

/* Toggle */
.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-right: 10px;
  margin-bottom: 10px;
}

.sidebar-header h3 {
  margin: 0;
}

#storyCounter {
  font-size: 0.8rem;
  color: #666;
  font-weight: normal;
}

#themeToggle {
  background: transparent;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  transition: transform 0.2s ease;
  width:auto;
}

#themeToggle:hover {
  transform: scale(1.2);
}

body.darkmode #themeToggle {
  color: #ffd95a;
}

.export-buttons {
  display: flex;
  gap: 8px;
  margin: 6px 0;
}

.export-buttons button {
  flex: 1;
  font-size: 0.85rem;
  padding: 6px 10px;
}

body.darkmode #sessionList li:hover,
body.darkmode #promptHistory li:hover,
body.darkmode #chapterIndex li:hover {
  background: #333;
  color: #fff;
}

/* Stile scrollbar per dark mode */
body.darkmode #sessionList::-webkit-scrollbar-track {
  background: #2a2a2a;
}

body.darkmode #sessionList::-webkit-scrollbar-thumb {
  background: #555;
}

body.darkmode #sessionList::-webkit-scrollbar-thumb:hover {
  background: #666;
}

body.darkmode #chapterIndex::-webkit-scrollbar-track {
  background: #2a2a2a;
}

body.darkmode #chapterIndex::-webkit-scrollbar-thumb {
  background: #555;
}

body.darkmode #chapterIndex::-webkit-scrollbar-thumb:hover {
  background: #666;
}

body.darkmode #chapterIndex li.story-title {
  background: #2a1f1a;
  border-left-color: #ff8c42;
  color: #fff;
}

body.darkmode #chapterIndex li:hover {
  border-left-color: #4da6ff;
  background: #1a2a3a;
}

#toastContainer {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
}

.toast {
  background-color: #333;
  color: #fff;
  padding: 12px 18px;
  margin-top: 10px;
  border-radius: 6px;
  font-size: 0.95rem;
  opacity: 0;
  transform: translateY(20px);
  transition: all 0.4s ease;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.leggiStoryBtn {
  background-color: transparent;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.leggiStoryBtn:hover {
  transform: scale(1.2);
}

#voiceSelect {
  width: 100%;
  font-size: 0.97rem;
  padding: 8px;
  margin-bottom: 8px;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #f7f7fa;
  color: #222;
}
body.darkmode #voiceSelect {
  background: #222;
  color: #eee;
  border: 1px solid #444;
}

.vocalControls button {
  background: transparent;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.vocalControls button:hover {
  transform: scale(1.2);
}

.voice-progress-container {
  height: 6px;
  background: #eee;
  border-radius: 3px;
  overflow: hidden;
  margin-top: 12px;
}

.voice-progress-bar {
  height: 100%;
  width: 0%;
  background: #007bff;
  transition: width 0.25s linear;
}

body.darkmode .voice-progress-container {
  background: #444;
}

body.darkmode .voice-progress-bar {
  background: #66bfff;
}

/* Barra di progresso vocale globale */
.global-voice-progress {
  width: 100%;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  transition: all 0.3s ease;
}

.global-voice-progress.hidden {
  display: none;
}

.voice-progress-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.voice-progress-info span {
  font-size: 0.9rem;
  color: #333;
  font-weight: 500;
}

.voice-progress-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

#voiceProgressCancel, #voiceProgressPause {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: #dc3545;
  padding: 4px;
  border-radius: 4px;
  transition: background 0.2s ease;
}

#voiceProgressCancel:hover, #voiceProgressPause:hover {
  background: #f8f9fa;
}

/* Dark mode per barra globale */
body.darkmode .global-voice-progress {
  background: #2a2a2a;
  border-color: #555;
}

body.darkmode .voice-progress-info span {
  color: #fff;
}

body.darkmode #voiceProgressCancel {
  color: #ff6b6b;
}

/* AI Audio Progress Bar - Clickable timeline */
#globalVoiceProgress .voice-progress-container {
  cursor: pointer;
  position: relative;
}

#globalVoiceProgress .voice-progress-container:hover .voice-progress-bar {
  opacity: 0.8;
  transition: opacity 0.2s ease;
}

/* Visual feedback for clickable progress bar */
#globalVoiceProgress[data-ai-audio="true"] .voice-progress-container {
  cursor: pointer;
}

#globalVoiceProgress[data-ai-audio="true"] .voice-progress-container:hover {
  background-color: rgba(0,0,0,0.05);
  border-radius: 4px;
}

body.darkmode #globalVoiceProgress[data-ai-audio="true"] .voice-progress-container:hover {
  background-color: rgba(255,255,255,0.1);
}

body.darkmode #voiceProgressCancel:hover {
  background: #333;
}

.voice-status {
  font-size: 0.85em;
  color: #666;
  font-style: italic;
  text-align: center;
  margin-top: 8px;
}

.darkmode .voice-status {
  color: #aaa;
}

/* Controlli vocali per i capitoli */
.chapter-voice-btn {
  display: inline-block;
  margin-right: 8px;
  font-size: 1.1em;
  cursor: pointer;
  user-select: none;
}

/* AI Audio button styling */
.ai-audio-btn {
  display: inline-block;
  margin-right: 8px;
  font-size: 1.1em;
  cursor: pointer;
  user-select: none;
  opacity: 0.8;
  transition: opacity 0.2s ease;
}

.ai-audio-btn:hover {
  opacity: 1;
  transform: scale(1.1);
}

.chapter-title {
  position: relative;
  display: flex;
  align-items: center;
  margin: 20px 0 10px 0;
  font-size: 1.4rem;
  font-weight: bold;
}

.chapter-title .chapter-voice-btn {
  margin-right: 8px;
}

.chapter-title .ai-audio-btn {
  margin-right: 8px;
}


/* Stili per la modalità dark */

/* Stili per il titolo principale */


/* Stili per genere e sottogenere unificati */
.book-genre-info {
  font-size: 0.9rem;
  color: #666;
  margin: 8px 0;
  text-align: center;
  font-style: italic;
}

.darkmode .book-genre-info {
  color: #aaa;
}

/* Simplified styles for play buttons */
.chapter-voice-btn {
  all: unset;
  font-size: 1em;
  cursor: pointer;
  color: inherit;
}

.ai-audio-btn {
  all: unset;
  font-size: 1em;
  cursor: pointer;
  color: inherit;
  opacity: 0.8;
}

.ai-audio-btn:hover {
  opacity: 1;
}

[data-story-title="true"] .chapter-voice-btn {
  all: unset;
  font-size: 1em;
  cursor: pointer;
  color: inherit;
}

[data-story-title="true"] .ai-audio-btn {
  all: unset;
  font-size: 1em;
  cursor: pointer;
  color: inherit;
  opacity: 0.8;
}


#closeStorySettings {
  width: auto;
}

/* Onboarding responsive instructions */
.mobile-instructions {
  display: none;
}

.desktop-instructions {
  display: inline;
}

@media (max-width: 768px) {
  .mobile-instructions {
    display: inline;
  }
  
  .desktop-instructions {
    display: none;
  }
}

