:root {
  --bg: #101418;
  --panel: #182028;
  --panel2: #202a34;
  --text: #e8edf2;
  --muted: #a8b3be;
  --border: #2c3947;
  --accent: #6cb6ff;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, sans-serif;
}

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

/* --- Header --- */

header {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

label, .small {
  font-size: 13px;
  color: var(--muted);
}

button, select, input[type="range"] {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
}

button {
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- TTS state-driven button visibility --- */

header.tts-idle    #pauseBtn,
header.tts-idle    #resumeBtn,
header.tts-idle    #stopBtn   { display: none; }

header.tts-playing #speakBtn,
header.tts-playing #resumeBtn { display: none; }

header.tts-paused  #speakBtn,
header.tts-paused  #pauseBtn  { display: none; }

/* --- Progress bar --- */

#progressBar {
  height: 3px;
  background: var(--border);
  flex-shrink: 0;
}

#progressFill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.3s ease;
}

/* --- App layout (sidebar + main) --- */

#appLayout {
  flex: 1;
  display: flex;
  min-height: 0;
}

main {
  flex: 1;
  display: flex;
  min-height: 0;
}

main > .panel {
  flex: 1;
}

/* --- Edit mode --- */

main.editing {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

main.editing #editor {
  display: block;
  flex: 1;
  min-height: 0;
  order: -1;
  border-right: 1px solid var(--border);
}

/* --- Panels --- */

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
}

.panel:last-child {
  border-right: none;
}

.panel-title {
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

textarea {
  flex: 1;
  width: 100%;
  resize: none;
  border: none;
  outline: none;
  padding: 16px;
  background: #0e1318;
  color: var(--text);
  font: 14px/1.5 Consolas, Monaco, monospace;
}

/* --- Preview --- */

#preview {
  flex: 1;
  overflow: auto;
  padding: 24px;
  position: relative;
}

#preview h1, #preview h2, #preview h3, #preview h4, #preview h5, #preview h6 {
  margin-top: 1.3em;
  margin-bottom: 0.5em;
}

#preview p, #preview li, #preview blockquote {
  line-height: 1.6;
}

#preview a {
  color: var(--accent);
}

#preview code {
  background: #161d24;
  padding: 2px 5px;
  border-radius: 4px;
  font-family: Consolas, Monaco, monospace;
}

#preview pre {
  background: #161d24;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow: auto;
}

#preview pre code {
  background: transparent;
  padding: 0;
}

#preview blockquote {
  margin-left: 0;
  padding-left: 12px;
  border-left: 4px solid var(--accent);
  color: var(--muted);
}

#preview hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

#preview table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
}

#preview th, #preview td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

#preview th {
  background: var(--panel2);
}

#preview img {
  max-width: 100%;
}

/* --- Playlist sidebar --- */

#playlistPanel {
  width: 250px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#playlistPanel.hidden {
  display: none;
}

.playlist-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.playlist-title {
  font-size: 13px;
  color: var(--muted);
  font-weight: bold;
}

.playlist-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 18px;
  padding: 2px 6px;
  cursor: pointer;
  line-height: 1;
}

.playlist-close:hover {
  color: var(--text);
}

#playlistList {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.playlist-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  border-left: 3px solid transparent;
  transition: background 0.15s;
}

.playlist-item:hover {
  background: var(--panel2);
}

.playlist-item.active {
  color: var(--text);
  border-left-color: var(--accent);
  background: var(--panel2);
}

.playlist-item .file-icon {
  flex-shrink: 0;
  width: 16px;
  text-align: center;
}

.playlist-item .file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}


/* --- Panel tabs --- */

.panel-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.panel-tabs .tab {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--muted);
  font-size: 12px;
  padding: 8px 4px;
  cursor: pointer;
  text-align: center;
}

.panel-tabs .tab:hover {
  color: var(--text);
}

.panel-tabs .tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* --- Saved items --- */

#savedList {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

#savedList.hidden {
  display: none;
}

.saved-empty {
  padding: 16px 14px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.saved-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 14px;
  border-left: 3px solid transparent;
  transition: background 0.15s;
}

.saved-item:hover {
  background: var(--panel2);
}

.saved-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.saved-name {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saved-time {
  font-size: 11px;
  color: var(--muted);
}

.saved-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.saved-action {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--muted);
  font-size: 11px;
  padding: 3px 6px;
  cursor: pointer;
}

.saved-action:hover {
  color: var(--text);
  border-color: var(--accent);
}

/* --- Book dialog --- */

#bookDialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  min-width: 320px;
  max-width: 90vw;
}

#bookDialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.book-dialog-form {
  display: flex;
  flex-direction: column;
}

.book-dialog-title {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  color: var(--muted);
  font-weight: bold;
}

#bookDialogList {
  padding: 8px 0;
  max-height: 50vh;
  overflow-y: auto;
}

.book-dialog-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-left: 3px solid transparent;
  color: var(--text);
  padding: 10px 18px;
  font-size: 14px;
  cursor: pointer;
}

.book-dialog-item:hover {
  background: var(--panel2);
  border-left-color: var(--accent);
}

.book-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 0;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}

/* --- Status bar --- */

#status {
  padding: 8px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}

/* --- Groups --- */

.group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* --- File input styling --- */

.file-input-label {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  cursor: pointer;
}

.file-input-label:hover {
  border-color: var(--accent);
}

.file-input-label input[type="file"] {
  display: none;
}

/* --- Responsive --- */

@media (max-width: 900px) {
  main.editing {
    grid-template-columns: 1fr;
  }

  main.editing #editor {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 40vh;
  }

  .panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .panel:last-child {
    border-bottom: none;
  }

  #playlistPanel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    width: 280px;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.5);
  }
}
