/* WrytaEngine — Shared Component Styles */

/* ============================================ */
/* FORM ELEMENTS                                */
/* ============================================ */

.form-group {
  margin-bottom: 12px;
}

.form-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-input,
.form-textarea,
.form-select {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 0.875rem;
  transition: border-color var(--transition-fast);
}

.form-input:focus,
.form-textarea:focus,
.form-select:focus {
  border-color: var(--accent-primary);
  outline: none;
  box-shadow: 0 0 0 2px rgba(91, 155, 213, 0.15);
}

.form-input::placeholder,
.form-textarea::placeholder {
  color: var(--text-muted);
}

.form-textarea {
  min-height: 80px;
  line-height: 1.5;
}

.form-select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23a0a0c0' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

/* ============================================ */
/* BUTTONS                                      */
/* ============================================ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.3px;
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent-primary);
  color: #fff;
  border: 1px solid var(--accent-primary);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--border-light);
}

.btn-success {
  background: var(--success);
  color: #fff;
  border: 1px solid var(--success);
}

.btn-success:hover:not(:disabled) {
  background: #5cc497;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
  border: 1px solid var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: #e05565;
}

.btn-warning {
  background: var(--warning);
  color: #1a1a2e;
  border: 1px solid var(--warning);
}

.btn-warning:hover:not(:disabled) {
  background: #deb85a;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid transparent;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-sm {
  padding: 4px 10px;
  font-size: 0.75rem;
}

.btn-lg {
  padding: 12px 24px;
  font-size: 0.9375rem;
}

/* Generate Chapter button — deliberately half-sized */
#btn-generate {
  padding: 6px 12px;
  font-size: 0.75rem;
}

.btn-block {
  width: 100%;
}

.btn-icon {
  padding: 6px;
  min-width: 32px;
  min-height: 32px;
  border-radius: var(--radius-sm);
}

/* ============================================ */
/* CHAPTER INPUT SECTION (Main Panel)           */
/* ============================================ */

.chapter-input-area {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 16px;
  flex-shrink: 0;
}

.chapter-input-area .chapter-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.chapter-input-area .chapter-counter {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.chapter-input-area textarea {
  width: 100%;
  min-height: 80px;
  max-height: 200px;
  padding: 34px 44px 10px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 0.9rem;
  line-height: 1.6;
  resize: vertical;
  font-family: var(--font-sans);
}

.chapter-input-area textarea:focus {
  border-color: var(--accent-primary);
  outline: none;
}

/* Wizard button floating inside the prompt box (top-right corner) */
.chapter-input-wizard {
  position: absolute;
  top: 2px;
  right: 8px;
  z-index: 2;
  font-size: 0.72rem;
  padding: 3px 10px;
  opacity: 0.9;
  background: var(--bg-tertiary);
  border-color: var(--border-color);
}
.chapter-input-wizard:hover {
  opacity: 1;
}

.chapter-input-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  align-items: center;
}

/* Step guide badges (1-8 sequence) */
.step-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 13px;
  height: 13px;
  min-width: 13px;
  border-radius: 50%;
  background: var(--accent-primary);
  color: #fff;
  font-size: 0.52rem;
  font-weight: 700;
  line-height: 1;
  vertical-align: middle;
  margin-right: 4px;
  flex-shrink: 0;
  box-shadow: 0 0 0 1.5px rgba(0, 0, 0, 0.3);
}
.step-badge-corner {
  position: absolute;
  top: -6px;
  left: -6px;
  z-index: 3;
  margin-right: 0;
}

/* ============================================ */
/* CHAPTER OUTPUT (contentEditable)             */
/* ============================================ */

.chapter-output-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 300px;
}

.chapter-output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.chapter-output-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.chapter-output-actions {
  display: flex;
  gap: 6px;
}

.chapter-output-editor {
  flex: 1;
  padding: 24px 28px;
  background: #f5f0e8;
  border: 1px solid #d4c5a9;
  border-radius: var(--radius-md);
  color: #1a1a1a;
  font-family: 'Courier New', 'Courier Prime', 'IBM Plex Mono', 'Source Code Pro', 'Consolas', 'Liberation Mono', monospace;
  font-size: 0.75rem;
  line-height: 1.85;
  overflow-y: auto;
  min-height: 400px;
  outline: none;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.chapter-output-editor:focus {
  border-color: #8b7355;
}

.chapter-output-editor:empty::before {
  content: 'Your generated chapter will appear here...';
  color: #999;
  font-style: italic;
  font-family: var(--font-sans);
}

.chapter-output-editor p {
  margin-bottom: 1em;
}

.chapter-output-editor h2,
.chapter-output-editor h3 {
  margin: 1.2em 0 0.6em;
  color: var(--accent-primary);
}

/* Selection styling for chapter output */
.chapter-output-editor ::selection {
  background: rgba(139, 115, 85, 0.25);
  color: #1a1a1a;
}

/* ============================================ */
/* CHAPTER HISTORY LIST                         */
/* ============================================ */

.chapter-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chapter-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-fast);
  font-size: 0.8125rem;
}

.chapter-list-item:hover {
  background: var(--bg-hover);
}

.chapter-list-item.active {
  background: var(--bg-active);
  border-left: 2px solid var(--accent-primary);
}

.chapter-list-item .chapter-num {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  min-width: 28px;
}

.chapter-list-item .chapter-info {
  flex: 1;
  min-width: 0;
}

.chapter-list-item .chapter-name {
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chapter-list-item .chapter-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.chapter-list-item .chapter-badge {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 10px;
  font-weight: 600;
}

.chapter-badge.approved {
  background: rgba(76, 175, 132, 0.15);
  color: var(--success);
}

.chapter-badge.draft {
  background: rgba(212, 168, 67, 0.15);
  color: var(--warning);
}

/* ============================================ */
/* REWRITE TOOLS (Right Sidebar)                */
/* ============================================ */

.rewrite-section {
  padding: 16px;
  border-bottom: 1px solid var(--border-color);
}

.rewrite-section:last-child {
  border-bottom: none;
}

.rewrite-actions-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.rewrite-action-btn {
  padding: 8px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 500;
  text-align: center;
  transition: all var(--transition-fast);
}

.rewrite-action-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--accent-primary);
}

.rewrite-action-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.rewrite-action-btn .icon {
  display: block;
  font-size: 1.1rem;
  margin-bottom: 2px;
}

/* Tone selector */
.tone-selector {
  margin-top: 10px;
}

.tone-selector .form-select {
  margin-top: 4px;
}

/* Selection info */
.selection-info {
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 8px;
  background: var(--bg-primary);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  font-family: var(--font-mono);
  word-break: break-all;
  max-height: 60px;
  overflow-y: auto;
}

.selection-info.empty {
  font-style: italic;
  color: var(--text-muted);
}

/* ============================================ */
/* CONTINUITY & CHARACTER STATES               */
/* ============================================ */

.notes-editor {
  width: 100%;
  min-height: 100px;
  padding: 10px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 0.8125rem;
  line-height: 1.6;
  resize: vertical;
  font-family: var(--font-sans);
}

.notes-editor:focus {
  border-color: var(--accent-primary);
  outline: none;
}

.notes-editor.small {
  min-height: 60px;
}

/* Continuity Notes panel — data shown in blue */
#continuity-notes {
  color: var(--accent-primary);
}

#continuity-notes::placeholder {
  color: var(--text-muted);
  opacity: 1;
}


/* ── State Ledger (Character States panel) ─────────────────── */

.state-ledger {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.state-ledger-entry {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 6px 8px;
  box-sizing: border-box;
  /* Each INDIVIDUAL chapter-state card is capped at ~half its previous height
     and scrolls internally — long states no longer blow out the whole
     Character States panel (2026-08-21). */
  max-height: 130px;
  overflow-y: auto;
  overflow-y: overlay;
  -webkit-overflow-scrolling: touch;
}

.state-ledger-title {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--accent-primary);
  letter-spacing: 0.3px;
  margin-bottom: 4px;
}

.state-ledger-body {
  width: 100%;
  min-height: 38px;   /* halved (was 76px) so the individual cards stay compact (2026-08-21) */
  font-family: var(--font-sans);
  font-size: 0.7rem;
  line-height: 1.5;
  color: var(--text-secondary);
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 6px 8px;
  box-sizing: border-box;
  white-space: pre-wrap;
  word-wrap: break-word;
  cursor: default;
  user-select: text;
}

.state-ledger-empty {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-style: italic;
}

/* States panel container — the ledger cards sit on a silver panel */
.right-tab-panel[data-panel="states"] {
  background: #c0c0c0;
  border-radius: 6px;
  padding: 8px;
}

/* Characters panel container — same silver panel treatment as States */
.right-tab-panel[data-panel="characters"] {
  background: #c0c0c0;
  border-radius: 6px;
  padding: 8px;
}


/* Right sidebar panels — only the active tab's panel is shown */
.right-tab-panel {
  display: none;
}

.right-tab-panel.active {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ── Right sidebar vertical tab layout ─────────────────────── */

.right-sidebar-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  overflow-y: overlay;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
}

.right-tab-rail {
  width: 104px;
  min-width: 104px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 0 12px;
  background: var(--bg-primary);
  border-left: 1px solid var(--border-color);
  overflow-y: auto;
  overflow-y: overlay;
}

.right-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 10px 4px;
  background: transparent;
  border: none;
  border-left: 3px solid transparent;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  text-align: center;
  cursor: pointer;
  transition: all var(--transition-fast);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.right-tab .tab-icon {
  font-size: 1.1rem;
  line-height: 1;
}

.right-tab .tab-icon .tab-icon-img {
  display: inline-block;
  width: 18px;
  height: 18px;
  object-fit: contain;
  vertical-align: middle;
}

.right-tab .tab-icon .tab-icon-img.tab-icon-img-lg {
  width: 24px;
  height: 24px;
}

.right-tab:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.right-tab.active {
  background: var(--bg-active);
  color: var(--accent-primary);
  border-left-color: var(--accent-primary);
}

.right-tab-panel {
  display: none;
}

.right-tab-panel.active {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 8px;
}

.right-tab-panel.active .notes-editor {
  flex: 1;
  min-height: 120px;
  resize: none;
}

.notes-hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ── Character tree (Characters panel) ─────────────────────── */

.char-tree {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.char-node {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  overflow: hidden;
}

.char-node-header {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 7px 8px;
  background: transparent;
  border: none;
  cursor: pointer;
  text-align: left;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
}

.char-node-header:hover {
  background: var(--bg-hover);
}

.char-chevron {
  font-size: 0.7rem;
  color: var(--text-muted);
  width: 12px;
  flex-shrink: 0;
}

.char-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.char-badge {
  flex-shrink: 0;
  font-size: 0.62rem;
  font-weight: 700;
  color: var(--accent-primary);
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 0 6px;
  line-height: 1.5;
}

.char-body {
  padding: 2px 8px 8px 20px;
  border-top: 1px solid var(--border-color);
}

.char-node.collapsed .char-body {
  display: none;
}

.char-group-label {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent-primary);
  margin: 6px 0 2px;
}

.char-trait {
  position: relative;
  padding-left: 10px;
  margin: 2px 0;
  font-size: 0.7rem;
  line-height: 1.45;
  color: var(--text-secondary);
}

.char-trait::before {
  content: '└';
  position: absolute;
  left: 0;
  color: var(--text-muted);
  opacity: 0.5;
}

.char-empty {
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1.5;
  padding: 8px;
  background: var(--bg-tertiary);
  border: 1px dashed var(--border-color);
  border-radius: 6px;
}

/* ── Collapsible entry tree (generic — Notes, Traits, Places, Objects, States) ── */

.entry-tree {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.entry-node {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  overflow: hidden;
}

.entry-node-header {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 7px 8px;
  background: transparent;
  border: none;
  cursor: pointer;
  text-align: left;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
}

.entry-node-header:hover {
  background: var(--bg-hover);
}

.entry-chevron {
  font-size: 0.7rem;
  color: var(--text-muted);
  width: 12px;
  flex-shrink: 0;
}

.entry-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-body {
  padding: 4px 8px 8px 20px;
  border-top: 1px solid var(--border-color);
}

.entry-node.collapsed .entry-body {
  display: none;
}

.entry-body-text {
  font-size: 0.7rem;
  line-height: 1.5;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-wrap: break-word;
}

.entry-empty {
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1.5;
  padding: 8px;
  background: var(--bg-tertiary);
  border: 1px dashed var(--border-color);
  border-radius: 6px;
}
.notes-editor[readonly] {
  color: var(--text-secondary);
  cursor: default;
}

.lock-hint {
  color: var(--warning);
}


.right-tab-generating {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius-md);
  font-size: 0.68rem;
  white-space: nowrap;
  color: var(--text-secondary);
  flex-shrink: 0;
}

/* ============================================ */
/* LOADING / GENERATING INDICATOR              */
/* ============================================ */

.generating-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg-tertiary);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius-md);
  color: var(--accent-primary);
  font-size: 0.72rem;
  font-weight: 500;
  white-space: nowrap;
  flex-wrap: nowrap;
}

/* ── STOP BUTTON + STALL ALERT (2026-09-18) ──
   The indicator was a single nowrap row, so a Stop button and a whole banner needed somewhere to live.
   Wrapping is enabled here and every direct-child div (the progress-bar wrapper, and the stall banner)
   claims a full row of its own: the quill + label + Stop button stay on the first line, the bar keeps its
   place under them, and the alert appears as a full-width strip inside the same block. */
.generating-indicator {
  flex-wrap: wrap;
}

.generating-indicator > div {
  flex-basis: 100%;
}

/* Always available while a run is live — not only once a stall is declared. */
.gen-stop-btn {
  flex-shrink: 0;
  margin-left: auto;
}

.gen-stall-alert {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  margin-top: 6px;
  border-radius: var(--radius-sm);
  background: rgba(220, 53, 69, 0.12);
  border: 1px solid rgba(220, 53, 69, 0.55);
  color: var(--text-primary);
  font-size: 0.7rem;
  font-weight: 500;
  white-space: normal;
  text-align: left;
}

.gen-stall-alert span {
  flex: 1;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--accent-dim);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Animated writing quill icon ── */
/* Replaced by logos/quill7.gif animated GIF */

/* ============================================ */
/* SETTINGS PANEL                               */
/* ============================================ */

.settings-group {
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
}

.settings-group:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.settings-group h4 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

/* API key input with show/hide */
.api-key-wrapper {
  display: flex;
  gap: 4px;
}

.api-key-wrapper .form-input {
  flex: 1;
}

.api-key-wrapper .btn-icon {
  flex-shrink: 0;
}

/* ============================================ */
/* EMPTY STATE                                 */
/* ============================================ */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
}

.empty-state .icon {
  font-size: 2.5rem;
  margin-bottom: 12px;
  opacity: 0.5;
}

.empty-state .title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.empty-state .desc {
  font-size: 0.8125rem;
  max-width: 280px;
}

/* ============================================ */
/* SIDEBAR TABS                                 */
/* ============================================ */

.sidebar-tabs {
  display: flex;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-tab {
  flex: 1;
  padding: 8px 12px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  font-family: var(--font-sans);
}

.sidebar-tab:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.sidebar-tab.active {
  color: var(--accent-primary);
  border-bottom-color: var(--accent-primary);
}

.sidebar-tab-content {
  /* content shown/hidden via JS */
}

/* ============================================ */
/* OUTLINE LIST (Edit Project tab)              */
/* ============================================ */

.outline-item {
  transition: background var(--transition-fast);
}

.outline-item.generated:hover {
  background: var(--bg-hover) !important;
}

.outline-item.active {
  border-color: var(--accent-primary) !important;
}

/* ============================================ */
/* PDF VIEWER POPUP                             */
/* ============================================ */

.pdf-viewer-overlay {
  position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
  height: 100dvh;
  background: rgba(0, 0, 0, 0.75); z-index: 10000;
  display: flex; align-items: center; justify-content: center;
  animation: pdfFadeIn 0.2s ease;
}
@keyframes pdfFadeIn { from { opacity: 0; } to { opacity: 1; } }

.pdf-viewer-modal {
  background: var(--bg-primary, #1a1a2e);
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 8px;
  width: 95vw; max-width: 1100px; height: 92vh;
  height: 92dvh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

.pdf-viewer-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px;
  background: var(--bg-secondary, #16213e);
  border-bottom: 1px solid var(--border-color, #2a2a4a);
  flex-shrink: 0;
}

.pdf-viewer-title {
  font-size: 0.95rem; font-weight: 600;
  color: var(--text-primary); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
  max-width: 50%;
}

.pdf-viewer-actions {
  display: flex; gap: 8px; flex-shrink: 0;
}

.pdf-viewer-body {
  flex: 1; display: flex; overflow: hidden;
  background: #525659; /* dark gray — matches Adobe Reader */
}

.pdf-viewer-iframe {
  width: 100%; height: 100%; border: none;
}

/* ============================================ */
/* STORY WIZARD MODAL                          */
/* ============================================ */

.sw-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.65); z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  animation: swFadeIn 0.2s ease;
}
@keyframes swFadeIn { from { opacity: 0; } to { opacity: 1; } }

.sw-modal {
  background: var(--bg-primary, #1a1a2e);
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 12px; width: 620px; max-width: 95vw; max-height: 90vh;
  max-height: 90dvh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.sw-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 24px; border-bottom: 1px solid var(--border-color, #2a2a4a);
}
.sw-header h2 { margin: 0; font-size: 1.15rem; color: var(--accent-primary); }
.sw-close {
  background: none; border: none; font-size: 1.5rem; color: var(--text-muted);
  cursor: pointer; padding: 0 4px; line-height: 1;
}
.sw-close:hover { color: var(--text-primary); }

.sw-steps {
  display: flex; align-items: center; justify-content: center; gap: 0;
  padding: 16px 24px; border-bottom: 1px solid var(--border-color, #2a2a4a);
}
.sw-step-dot { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.sw-dot-circle {
  width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: var(--bg-secondary, #16213e); border: 2px solid var(--border-color, #2a2a4a);
  color: var(--text-muted); font-size: 0.75rem; font-weight: 600;
  transition: all 0.25s;
}
.sw-step-dot.active .sw-dot-circle { background: var(--accent-primary); border-color: var(--accent-primary); color: #fff; }
.sw-step-dot.done .sw-dot-circle { background: var(--success); border-color: var(--success); color: #fff; }
.sw-dot-label { font-size: 0.65rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.sw-step-dot.active .sw-dot-label { color: var(--accent-primary); }
.sw-step-dot.done .sw-dot-label { color: var(--success); }
.sw-step-line { width: 40px; height: 2px; background: var(--border-color, #2a2a4a); margin: 0 4px 20px 4px; }
.sw-step-dot.done + .sw-step-line { background: var(--success); }

.sw-body { padding: 24px; overflow-y: auto; flex: 1; min-height: 250px; }
.sw-field { display: flex; flex-direction: column; gap: 8px; }
.sw-label { font-size: 0.95rem; font-weight: 600; color: var(--text-primary); }
.sw-hint { font-size: 0.8rem; color: var(--text-muted); margin: 0 0 4px 0; }
.sw-textarea {
  width: 100%; background: var(--bg-secondary, #16213e); border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 6px; color: var(--text-primary); padding: 10px 12px; font-size: 0.875rem;
  font-family: inherit; resize: vertical; min-height: 60px; box-sizing: border-box;
}
.sw-textarea:focus { outline: none; border-color: var(--accent-primary); }
.sw-input {
  width: 100%; background: var(--bg-secondary, #16213e); border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 6px; color: var(--text-primary); padding: 8px 10px; font-size: 0.85rem;
  font-family: inherit; box-sizing: border-box;
}
.sw-input:focus { outline: none; border-color: var(--accent-primary); }
.sw-select {
  width: 100%; background: var(--bg-secondary, #16213e); border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 6px; color: var(--text-primary); padding: 8px 10px; font-size: 0.85rem;
  font-family: inherit; cursor: pointer; box-sizing: border-box;
}
.sw-select:focus { outline: none; border-color: var(--accent-primary); }
.sw-char-count { font-size: 0.7rem; color: var(--text-muted); text-align: right; }

.sw-character-card {
  display: flex; flex-direction: column; gap: 6px;
  background: var(--bg-secondary, #16213e); border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 8px; padding: 12px; margin-bottom: 8px; position: relative;
}
.sw-remove-char {
  position: absolute; top: 8px; right: 8px;
  background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 0.875rem;
  padding: 2px 6px; border-radius: 4px;
}
.sw-remove-char:hover { color: #e74c3c; background: rgba(231,76,60,0.1); }
.sw-instruct-textarea { min-height: 60px; }

.sw-footer {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 24px; border-top: 1px solid var(--border-color, #2a2a4a);
}
.sw-footer .btn { min-width: 100px; }

/* ============================================ */
/* ADMIN CONSOLE                               */
/* ============================================ */

.admin-overlay {
  position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
  height: 100dvh;
  background: rgba(0, 0, 0, 0.75); z-index: 20000;
  display: flex; align-items: center; justify-content: center;
  animation: adminFadeIn 0.2s ease;
}
@keyframes adminFadeIn { from { opacity: 0; } to { opacity: 1; } }

.admin-modal {
  background: var(--bg-primary, #1a1a2e);
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: 12px;
  width: 95vw; max-width: 1100px; height: 90vh;
  height: 90dvh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

.admin-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 20px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--accent-primary);
  flex-shrink: 0;
}
.admin-header h2 {
  margin: 0; font-size: 1.1rem; color: var(--accent-primary);
  display: flex; align-items: center; gap: 8px;
}
.admin-close {
  background: none; border: none; font-size: 1.3rem; color: var(--text-muted);
  cursor: pointer; padding: 4px 8px; line-height: 1; border-radius: 4px;
  transition: all 0.15s;
}
.admin-close:hover { color: #fff; background: rgba(255,255,255,0.1); }

.admin-tabs {
  display: flex; flex-shrink: 0;
  background: var(--bg-secondary, #16213e);
  border-bottom: 1px solid var(--border-color, #2a2a4a);
}
.admin-tab {
  flex: 1; padding: 12px 16px;
  background: transparent; border: none; border-bottom: 2px solid transparent;
  color: var(--text-muted); font-size: 0.8rem; font-weight: 600;
  cursor: pointer; transition: all 0.15s; font-family: var(--font-sans);
  white-space: nowrap;
}
.admin-tab:hover { color: var(--text-primary); background: rgba(255,255,255,0.03); }
.admin-tab.active { color: var(--accent-primary); border-bottom-color: var(--accent-primary); }

.admin-body {
  flex: 1; overflow-y: auto; padding: 20px;
}

.admin-loading {
  text-align: center; padding: 60px 20px; color: var(--text-muted); font-size: 0.9rem;
}
.admin-error {
  text-align: center; padding: 40px 20px; color: var(--danger); font-weight: 600;
}

/* Admin Sections */
.admin-section { margin-bottom: 24px; }
.admin-section h3 {
  font-size: 0.9rem; font-weight: 600; color: var(--text-primary);
  margin-bottom: 12px; padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color, #2a2a4a);
}
.admin-section h4 {
  font-size: 0.85rem; font-weight: 600; color: var(--text-primary); margin-bottom: 10px;
}
.admin-hint {
  font-size: 0.75rem; color: var(--text-muted); margin-bottom: 14px;
  line-height: 1.5;
}
.admin-section-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px;
}
.admin-section-header h3 { margin-bottom: 0; padding-bottom: 0; border-bottom: none; }

/* Stats Grid */
.admin-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.admin-stat-card {
  background: var(--bg-secondary, #16213e);
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: var(--radius-md); padding: 14px;
  text-align: center;
}
.admin-stat-card.highlight {
  border-color: var(--accent-primary);
}
.admin-stat-card.cost {
  border-color: #d4a843;
}
.admin-stat-value {
  font-size: 1.5rem; font-weight: 700; color: var(--text-primary);
}
.admin-stat-card.cost .admin-stat-value { color: #d4a843; }
.admin-stat-label {
  font-size: 0.65rem; color: var(--text-muted); text-transform: uppercase;
  letter-spacing: 0.5px; margin-top: 4px;
}

/* Info Grid */
.admin-info-grid {
  display: flex; flex-direction: column; gap: 8px;
}
.admin-info-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 12px; background: var(--bg-secondary, #16213e);
  border-radius: var(--radius-sm);
}
.admin-info-label {
  font-size: 0.8rem; color: var(--text-secondary); font-weight: 500;
}
.admin-info-value {
  font-size: 0.8rem; color: var(--text-primary); font-weight: 600;
}

/* Table */
.admin-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: var(--radius-md);
}
.admin-table {
  width: 100%; border-collapse: collapse; font-size: 0.78rem;
}
.admin-table thead {
  background: var(--bg-tertiary, #0f3460);
}
.admin-table th {
  padding: 10px 12px; text-align: left; font-weight: 600;
  color: var(--text-secondary); text-transform: uppercase;
  font-size: 0.68rem; letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border-color, #2a2a4a);
  white-space: nowrap;
}
.admin-table td {
  padding: 8px 12px; border-bottom: 1px solid var(--border-color, #2a2a4a);
  color: var(--text-primary); white-space: nowrap;
}
.admin-table tbody tr:hover {
  background: var(--bg-hover, rgba(255,255,255,0.03));
}
.admin-table tbody tr:last-child td { border-bottom: none; }
.admin-actions { display: flex; gap: 6px; }
.admin-cost-positive { color: #d4a843 !important; font-weight: 600; font-family: var(--font-mono); }

/* Badges (plan / status / flags) used in Users list + User Detail */
.admin-badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 0.65rem; font-weight: 700; letter-spacing: 0.3px;
  line-height: 1.5; white-space: nowrap; vertical-align: middle;
}
.admin-badge.b-premium { background: rgba(212,168,67,0.15); color: #d4a843; border: 1px solid rgba(212,168,67,0.4); }
.admin-badge.b-basic { background: rgba(96,165,250,0.12); color: #60a5fa; border: 1px solid rgba(96,165,250,0.35); }
.admin-badge.b-free { background: rgba(255,255,255,0.06); color: var(--text-muted); border: 1px solid var(--border-color, #2a2a4a); }
.admin-badge.b-active { background: rgba(74,222,128,0.12); color: #4ade80; border: 1px solid rgba(74,222,128,0.35); }
.admin-badge.b-cancelled { background: rgba(248,113,113,0.12); color: #f87171; border: 1px solid rgba(248,113,113,0.35); }
.admin-badge.b-pastdue { background: rgba(251,146,60,0.12); color: #fb923c; border: 1px solid rgba(251,146,60,0.35); }
.admin-badge.b-admin { background: rgba(167,139,250,0.15); color: #a78bfa; border: 1px solid rgba(167,139,250,0.4); }
.admin-badge.b-verified { background: rgba(74,222,128,0.12); color: #4ade80; border: 1px solid rgba(74,222,128,0.35); }
.admin-badge.b-unverified { background: rgba(251,146,60,0.1); color: #fb923c; border: 1px solid rgba(251,146,60,0.3); }

.admin-mono { font-family: var(--font-mono, monospace); font-size: 0.72rem; }
.admin-muted { color: var(--text-muted); }
.admin-subtext { display: block; font-size: 0.68rem; color: var(--text-muted); margin-top: 2px; }
.admin-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.admin-chip {
  padding: 6px 12px; background: var(--bg-secondary, #16213e);
  border: 1px solid var(--border-color, #2a2a4a); border-radius: var(--radius-sm);
  font-size: 0.72rem; color: var(--text-secondary);
}
.admin-chip strong { color: var(--text-primary); }
.admin-section h3 .admin-badge { margin-left: 8px; }

/* Spend Period Tabs (sub-tabs) */
.admin-spend-tabs {
  display: flex; gap: 4px; margin-bottom: 14px;
}
.admin-spend-tab {
  padding: 6px 14px;
  background: var(--bg-secondary, #16213e);
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: var(--radius-sm); color: var(--text-muted);
  font-size: 0.75rem; font-weight: 500; cursor: pointer; transition: all 0.15s;
  font-family: var(--font-sans);
}
.admin-spend-tab:hover { color: var(--text-primary); border-color: var(--text-muted); }
.admin-spend-tab.active {
  background: var(--accent-primary); color: #000; border-color: var(--accent-primary);
}

/* User Form Grid */
.admin-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

/* Admin Nav Button */
.btn-admin-nav {
  background: transparent;
  border: 1px dashed var(--border-color);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  font-size: 0.68rem;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font-sans);
}
.btn-admin-nav:hover {
  border-color: var(--accent-primary);
  color: var(--accent-primary);
  background: rgba(91, 155, 213, 0.08);
}

/* ============================================ */
/* PREMIUM BOX (Cover + Audiobook)              */
/* ============================================ */

.premium-box {
  position: relative;
  display: flex;
  flex-direction: row;
  gap: 12px;
  border: 1.5px solid #d4a843;
  border-radius: var(--radius-lg, 12px);
  padding: 18px 14px 14px 14px;
  margin-bottom: 16px;
  background: linear-gradient(135deg,
    rgba(212, 168, 67, 0.05) 0%,
    rgba(212, 168, 67, 0.01) 100%);
  background-color: var(--bg-secondary, #1a1a2e);
}

.premium-box-label {
  position: absolute;
  top: -0.65em;
  left: 14px;
  background: var(--bg-secondary, #1a1a2e);
  color: #d4a843;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 0 8px;
  line-height: 1;
}

.premium-col {
  flex: 1;
  min-width: 0;
}

.premium-col-cover {
  flex: 1.1;
}

.premium-col-audio {
  flex: 0.9;
}

.premium-divider {
  width: 1px;
  align-self: stretch;
  background: var(--border-color, #2a2a4a);
  flex-shrink: 0;
}

.premium-section-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 2px;
}

/* ============================================ */
/* AUDIOBOOK DIALOG                             */
/* ============================================ */

.audiobook-dialog {
  background: var(--bg-secondary, #16213e);
  border: 1px solid var(--border-color, #2a2a4a);
  border-radius: var(--radius-lg, 12px);
  padding: 16px;
  margin-bottom: 16px;
}

.audiobook-dialog-inner {
  max-width: 100%;
}

/* Range slider styling */
.form-range {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: var(--border-color, #2a2a4a);
  border-radius: 3px;
  outline: none;
  margin: 6px 0;
}

.form-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-primary, #5b9bd5);
  cursor: pointer;
  border: 2px solid var(--bg-primary, #0f0f23);
  box-shadow: 0 0 0 2px rgba(91, 155, 213, 0.3);
}

.form-range::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-primary, #5b9bd5);
  cursor: pointer;
  border: 2px solid var(--bg-primary, #0f0f23);
  box-shadow: 0 0 0 2px rgba(91, 155, 213, 0.3);
}

/* ============================================ */
/* MOBILE RESPONSIVE ADJUSTMENTS                */
/* ============================================ */

@media (max-width: 768px) {
  /* Buttons: larger touch targets */
  .btn {
    padding: 10px 14px;
    font-size: 0.85rem;
    min-height: 44px;
  }

  .btn-sm {
    padding: 8px 12px;
    font-size: 0.78rem;
    min-height: 36px;
  }

  .btn-icon {
    min-width: 36px;
    min-height: 36px;
    padding: 8px;
  }

  /* Form elements: larger for touch */
  .form-input,
  .form-textarea,
  .form-select {
    padding: 10px 12px;
    font-size: 0.9rem;
    min-height: 44px;
  }

  /* Sidebar tabs: larger tap targets */
  .sidebar-tab {
    padding: 12px 8px;
    font-size: 0.78rem;
    min-height: 44px;
  }

  /* Chapter list items: easier to tap */
  .chapter-list-item {
    padding: 12px 10px;
    min-height: 44px;
  }

  /* Chapter input area */
  .chapter-input-area textarea {
    min-height: 60px;
    font-size: 1rem;
  }

  .chapter-input-actions {
    flex-wrap: wrap;
    gap: 6px;
  }

  .chapter-input-actions .btn {
    flex: 1 1 auto;
    min-width: 100px;
  }

  /* Chapter output editor: reduced padding */
  .chapter-output-editor {
    padding: 14px 16px;
    font-size: 0.95rem;
    min-height: 300px;
  }

  .chapter-output-header {
    flex-wrap: wrap;
    gap: 8px;
  }

  .chapter-output-actions {
    flex-wrap: wrap;
  }

  /* Premium box: stack vertically on mobile */
  .premium-box {
    flex-direction: column;
  }

  .premium-divider {
    width: auto;
    height: 1px;
    align-self: auto;
  }

  .premium-col {
    width: 100%;
  }

  /* Sidebar: tighter padding */
  .sidebar-scroll {
    padding: 8px;
  }

  /* Admin: responsive stats grid */
  .admin-stats-grid {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 8px;
  }

  .admin-stat-card {
    padding: 10px;
  }

  .admin-stat-value {
    font-size: 1.2rem;
  }

  .admin-form-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .btn {
    padding: 8px 12px;
    font-size: 0.8rem;
    min-height: 44px;
  }

  .chapter-output-editor {
    padding: 10px 12px;
    font-size: 0.9rem;
    min-height: 250px;
  }

  .chapter-input-area {
    padding: 12px;
  }

  .chapter-input-actions {
    flex-direction: column;
  }

  .chapter-input-actions .btn {
    width: 100%;
  }

  .rewrite-actions-grid {
    grid-template-columns: 1fr 1fr;
    gap: 4px;
  }

  /* Story wizard: reduce header padding */
  .sw-header {
    padding: 12px 16px;
  }

  .sw-body {
    padding: 16px;
  }

  .sw-footer {
    padding: 12px 16px;
  }

  /* Chapter stats bar: stack on narrow phones */
  .chapter-stats-bar {
    gap: 6px;
    padding: 6px 8px;
    font-size: 0.65rem;
  }

  .chapter-stats-bar .btn {
    padding: 2px 6px;
    font-size: 0.6rem;
    min-height: 22px;
  }

  /* Chapter output actions: wrap buttons compactly on narrow phones */
  .chapter-output-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .chapter-output-actions {
    flex-wrap: wrap;
  }

  .chapter-output-actions .btn-sm {
    padding: 3px 7px;
    font-size: 0.6rem;
    min-height: 26px;
    gap: 2px;
    letter-spacing: 0;
  }
}

/* ── Guaranteed iPhone targeting via JS body class ──────────── */
/* (bypasses Safari cache + media query viewport quirks)         */

body.is-phone .chapter-output-header {
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

body.is-phone .chapter-output-actions {
  flex-wrap: wrap;
}

body.is-phone .chapter-output-actions .btn-sm {
  padding: 3px 7px !important;
  font-size: 0.6rem !important;
  min-height: 26px !important;
  gap: 2px;
  letter-spacing: 0;
}

/* ============================================ */
/* CHAPTER STATS BAR + INLINE ANALYSIS          */
/* ============================================ */

.chapter-stats-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  padding: 4px 14px;
  background: #2a2a3a;
  border-bottom: 1px solid #3a3a5a;
  margin-bottom: 0;
  align-items: center;
  min-height: 26px;
  font-size: 0.7rem;
  color: #a0a0c0;
}

.stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  white-space: nowrap;
}

.stat-pill.ai-pill {
  font-weight: 600;
  cursor: default;
  color: var(--text-secondary);
}

.stat-pill.ai-pill.ai-green { color: #4caf50; }
.stat-pill.ai-pill.ai-yellow { color: #f0ad4e; }
.stat-pill.ai-pill.ai-red { color: #d9534f; }

/* ── Inline Analysis Results ──────────────────────────────── */

.inline-analysis {
  padding: 12px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 8px;
  max-height: 400px;
  overflow-y: auto;
}

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

/* ── Analysis Dropdowns ──────────────────────────────────── */

.analysis-dropdown {
  margin-bottom: 4px;
  border-radius: 6px;
  overflow: hidden;
}

.analysis-dropdown-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  background: var(--bg-tertiary);
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--text-secondary);
  user-select: none;
  transition: background 0.15s;
}

.analysis-dropdown-header:hover {
  background: var(--bg-hover);
}

.dropdown-arrow {
  font-size: 0.6rem;
  transition: transform 0.2s;
}

.analysis-dropdown.open .dropdown-arrow {
  transform: rotate(180deg);
}

.analysis-dropdown-body {
  display: none;
  padding: 6px 10px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-top: none;
  border-radius: 0 0 6px 6px;
}

.analysis-dropdown.open .analysis-dropdown-body {
  display: block;
}

.analysis-issue-click {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  padding: 4px 0;
  font-size: 0.8rem;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.15s;
}

.analysis-issue-click:hover {
  background: rgba(91, 155, 213, 0.08);
}

.issue-marker {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-top: 6px;
  flex-shrink: 0;
}

.overused-tag {
  font-size: 0.75rem;
  padding: 2px 10px;
  background: rgba(91, 155, 213, 0.08);
  border: 1px solid rgba(91, 155, 213, 0.2);
  border-radius: 12px;
}

.ai-flag-tag {
  font-size: 0.72rem;
  padding: 3px 10px;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: 12px;
  color: var(--text-secondary);
}

/* Admin tabs: smaller */
.admin-tab {
  padding: 10px 8px;
  font-size: 0.7rem;
}

.analysis-stats {
  grid-template-columns: repeat(3, 1fr);
}

/* ── Inline Search Bar ───────────────────────────────────── */

.inline-search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--accent-primary);
  border-radius: 8px;
  margin-bottom: 8px;
}

.inline-search-bar input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 0.85rem;
  outline: none;
  padding: 4px 0;
}

.inline-search-bar input::placeholder {
  color: var(--text-muted);
}

.search-nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

mark.search-highlight {
  background: rgba(255, 193, 7, 0.3);
  color: var(--text-primary);
  border-radius: 2px;
  padding: 0 1px;
}

mark.search-highlight-current {
  background: rgba(245, 158, 11, 0.6);
  border-radius: 2px;
  padding: 0 1px;
}

/* ============================================ */
/* CHAPTER ANALYSIS PANEL                       */
/* ============================================ */

.chapter-analysis-panel {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  margin-top: 12px;
  overflow: hidden;
  animation: analysisSlideIn 0.25s ease-out;
}

@keyframes analysisSlideIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.analysis-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-primary);
}

.analysis-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  transition: color var(--transition-fast);
}

.analysis-close:hover {
  color: var(--danger);
}

.analysis-stats {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-color);
}

.analysis-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.analysis-stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent-primary);
}

.analysis-stat-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-top: 2px;
}

.analysis-issue-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-color);
}

.analysis-issue-count {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-right: 4px;
}

.analysis-issue-tag {
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
}

.analysis-issue-tag.adverb { background: rgba(240, 173, 78, 0.15); color: #f0ad4e; }
.analysis-issue-tag.passive { background: rgba(217, 83, 79, 0.15); color: #d9534f; }
.analysis-issue-tag.cliche { background: rgba(91, 192, 222, 0.15); color: #5bc0de; }
.analysis-issue-tag.long-sentence { background: rgba(91, 155, 213, 0.15); color: #5b9bd5; }
.analysis-issue-tag.short-sentence { background: rgba(162, 132, 194, 0.15); color: #a284c2; }

.analysis-section {
  padding: 10px 14px;
}

.analysis-section + .analysis-section {
  border-top: 1px solid var(--border-color);
}

.analysis-section-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.analysis-overused {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.analysis-overused-word {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  padding: 2px 10px;
  background: rgba(91, 155, 213, 0.08);
  border: 1px solid rgba(91, 155, 213, 0.2);
  border-radius: 12px;
  color: var(--text-primary);
}

.analysis-overused-count {
  font-size: 0.65rem;
  color: var(--text-muted);
  font-weight: 600;
}

.analysis-issues-list {
  max-height: 300px;
  overflow-y: auto;
}

.analysis-issue-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 7px 10px;
  margin: 2px 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-fast);
  border-left: 3px solid transparent;
}

.analysis-issue-item:hover {
  background: var(--bg-tertiary);
}

.analysis-issue-click:hover {
  background: var(--bg-tertiary);
}

.analysis-issue-item.adverb { border-left-color: #f0ad4e; }
.analysis-issue-item.passive { border-left-color: #d9534f; }
.analysis-issue-item.cliche { border-left-color: #5bc0de; }
.analysis-issue-item.long_sentence { border-left-color: #5b9bd5; }
.analysis-issue-item.short_sentence { border-left-color: #a284c2; }

.analysis-issue-icon {
  font-size: 0.85rem;
  flex-shrink: 0;
  width: 20px;
  text-align: center;
}

.analysis-issue-body {
  font-size: 0.78rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.analysis-issue-body strong {
  color: var(--text-primary);
  font-size: 0.7rem;
}

/* ============================================ */
/* LANGUAGE SELECTOR (flag dropdown)            */
/* ============================================ */

.lang-selector { position: relative; flex-shrink: 0; }
.lang-btn {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 6px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-color); background: var(--bg-tertiary);
  cursor: pointer; color: var(--text-primary); font-size: 0.75rem;
  font-family: var(--font-sans); line-height: 1;
}
.lang-btn:hover { border-color: var(--border-light); background: var(--bg-hover); }
.lang-btn svg { width: 20px; height: 15px; border-radius: 2px; display: block; }
.lang-caret { font-size: 0.6rem; color: var(--text-muted); }
.lang-menu {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 60;
  min-width: 150px; background: var(--bg-secondary);
  border: 1px solid var(--border-color); border-radius: var(--radius-md);
  box-shadow: var(--shadow-md); padding: 4px; display: flex; flex-direction: column; gap: 2px;
}
.lang-menu[hidden] { display: none; }
.lang-option {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 7px 8px; border: none; background: transparent; border-radius: var(--radius-sm);
  color: var(--text-primary); font-size: 0.8rem; cursor: pointer; font-family: var(--font-sans);
  text-align: left;
}
.lang-option svg { width: 20px; height: 15px; border-radius: 2px; flex-shrink: 0; }
.lang-option:hover { background: var(--bg-hover); }
/**
 * WrytaEngine — Help Overlay Component
 *
 * Adds a floating "?" help button. On click, darkens the screen and shows
 * labelled balloons pointing to the main UI areas (replacing the old 1-8
 * numbered step badges).
 */

/* ── Help button ────────────────────────────── */
#help-btn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 350;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent-primary);
  color: #fff;
  border: 2px solid var(--accent-hover);
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 12px rgba(0,0,0,0.5);
  transition: transform 0.2s, background 0.2s;
  line-height: 1;
  font-family: var(--font-sans);
}
#help-btn:hover {
  transform: scale(1.1);
  background: var(--accent-hover);
}
#help-btn.active {
  background: var(--danger);
  border-color: var(--danger);
}

/* ── Overlay dimmer ─────────────────────────── */
#help-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: transparent;
  display: none;
}
#help-overlay.visible {
  display: block;
}

/* Dim panels around the spotlight hole — lighter, barely blurred */
.help-shade {
  position: absolute;
  background: rgba(8, 8, 20, 0.38);
  backdrop-filter: blur(1px);
  -webkit-backdrop-filter: blur(1px);
}

/* Spotlight ring around the active target */
#help-spotlight {
  position: fixed;
  z-index: 305;
  border: 2px solid var(--accent-primary);
  border-radius: 10px;
  pointer-events: none;
  display: none;
  box-shadow: 0 0 0 3px rgba(91, 155, 213, 0.25);
  transition: left 160ms ease, top 160ms ease, width 160ms ease, height 160ms ease;
}
#help-spotlight.visible {
  display: block;
}

/* Balloon nav row */
.help-balloon-main {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  flex: 1;
}
.help-balloon-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 5px;
}
.help-nav-btn {
  background: var(--accent-primary);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 0.58rem;
  font-weight: 600;
  padding: 3px 7px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.help-nav-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}
.help-nav-btn:disabled {
  opacity: 0.45;
  cursor: default;
}
.help-nav-next {
  margin-left: 0;
}
.help-progress {
  font-size: 0.58rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── Comic speech-bubble balloon ────────────── */
.help-balloon {
  position: fixed;
  z-index: 310;
  box-sizing: border-box;
  background: var(--bg-secondary);
  border: 2px solid var(--accent-primary);
  border-radius: 12px;
  border-width: 3px;
  padding: 12px 18px;
  min-width: 220px;
  max-width: 250px;
  box-shadow: 0 3px 14px rgba(0,0,0,0.5);
  display: none;
  pointer-events: auto;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
}
.help-balloon.visible {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}
.help-balloon-num {
  flex-shrink: 0;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--accent-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.55rem;
  font-weight: 700;
  line-height: 1;
}
.help-balloon-text {
  font-size: 0.66rem;
  color: var(--text-primary);
  line-height: 1.3;
  min-width: 0;
}
.help-progress {
  flex: 1 1 auto;
  min-width: 0;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Comic speech-bubble tail (two-layer triangle) ── */
.help-balloon-tail-outer {
  position: absolute;
  width: 0;
  height: 0;
  z-index: 0;
}
.help-balloon-tail-inner {
  position: absolute;
  width: 0;
  height: 0;
  z-index: 1;
}

/* Tail on the LEFT side (balloon to the right of target) */
/* Outlined comic tails — outer = accent triangle, inner = fill triangle.
   Both anchored flush to the padding box so the mouth merges with the body. */
.help-balloon-tail-outer,
.help-balloon-tail-inner {
  position: absolute;
  width: 0;
  height: 0;
}

/* Tail on the LEFT side, pointing left */
.help-balloon-tail-outer.arrow-left {
  left: -14px;
  border-top: 14px solid transparent;
  border-bottom: 14px solid transparent;
  border-right: 14px solid var(--accent-primary);
}
.help-balloon-tail-inner.arrow-left {
  left: -14px;
  border-top: 11px solid transparent;
  border-bottom: 11px solid transparent;
  border-right: 14px solid var(--bg-secondary);
}

/* Tail on the RIGHT side, pointing right */
.help-balloon-tail-outer.arrow-right {
  right: -14px;
  border-top: 14px solid transparent;
  border-bottom: 14px solid transparent;
  border-left: 14px solid var(--accent-primary);
}
.help-balloon-tail-inner.arrow-right {
  right: -14px;
  border-top: 11px solid transparent;
  border-bottom: 11px solid transparent;
  border-left: 14px solid var(--bg-secondary);
}

/* Tail pointing DOWN from the bottom edge */
.help-balloon-tail-outer.arrow-down {
  bottom: -14px;
  border-left: 14px solid transparent;
  border-right: 14px solid transparent;
  border-top: 14px solid var(--accent-primary);
}
.help-balloon-tail-inner.arrow-down {
  bottom: -14px;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-top: 14px solid var(--bg-secondary);
}

/* Tail pointing UP from the top edge */
.help-balloon-tail-outer.arrow-up {
  top: -14px;
  border-left: 14px solid transparent;
  border-right: 14px solid transparent;
  border-bottom: 14px solid var(--accent-primary);
}
.help-balloon-tail-inner.arrow-up {
  top: -14px;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 14px solid var(--bg-secondary);
}

/* ── Responsive ──────────────────────────────── */
@media (max-width: 768px) {
  .help-balloon {
    min-width: 190px;
    max-width: 220px;
    padding: 10px 14px;
  }
  .help-balloon-text { font-size: 0.6rem; }
  #help-btn {
    width: 36px; height: 36px;
    font-size: 1rem;
    bottom: 62px;
  }
}
.lang-option.active { color: var(--accent-primary); font-weight: 600; }

/* ============================================ */
/* ADMIN — SERVER MONITOR TAB                   */
/* ============================================ */

.admin-tab { position: relative; }
.admin-alert-dot {
  position: absolute;
  top: 5px;
  right: 7px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--warning);
  box-shadow: 0 0 6px rgba(212, 168, 67, 0.8);
}
.admin-alert-dot.dot-warn { background: var(--warning); }
.admin-alert-dot.dot-crit {
  background: var(--danger);
  box-shadow: 0 0 8px rgba(212, 74, 90, 0.9);
  animation: mon-pulse 1.2s ease-in-out infinite;
}
@keyframes mon-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.4); opacity: 0.7; }
}

.mon-banners { margin-bottom: 16px; }
.mon-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  margin-bottom: 8px;
  font-size: 14px;
  border: 1px solid;
}
.mon-banner-crit {
  background: rgba(212, 74, 90, 0.12);
  border-color: rgba(212, 74, 90, 0.5);
  color: #f87171;
}
.mon-banner-crit strong { color: #f87171; letter-spacing: 0.4px; }
.mon-banner-warn {
  background: rgba(212, 168, 67, 0.1);
  border-color: rgba(212, 168, 67, 0.45);
  color: var(--warning);
}
.mon-banner-warn strong { color: var(--warning); letter-spacing: 0.4px; }
.mon-banner-ok {
  background: rgba(76, 175, 132, 0.08);
  border-color: rgba(76, 175, 132, 0.35);
  color: var(--success);
}

.mon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
}
.mon-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}
.mon-card-warn { border-color: rgba(212, 168, 67, 0.5); }
.mon-card-crit { border-color: rgba(212, 74, 90, 0.6); background: rgba(212, 74, 90, 0.06); }
.mon-card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.mon-card-title { font-size: 13px; font-weight: 600; color: var(--text-secondary); }
.mon-state {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 2px 7px;
  border-radius: 10px;
  white-space: nowrap;
}
.mon-state.crit { background: rgba(212, 74, 90, 0.15); color: #f87171; border: 1px solid rgba(212, 74, 90, 0.45); }
.mon-state.warn { background: rgba(212, 168, 67, 0.12); color: var(--warning); border: 1px solid rgba(212, 168, 67, 0.4); }
.mon-gauge {
  height: 9px;
  border-radius: 5px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  overflow: hidden;
}
.mon-gauge-fill { height: 100%; border-radius: 5px; transition: width 400ms ease; }
.mon-fill-ok { background: var(--success); }
.mon-fill-warn { background: var(--warning); }
.mon-fill-crit { background: var(--danger); }
.mon-card-detail {
  margin-top: 7px;
  font-size: 12px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.mon-charts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 12px;
}
.mon-chart-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}
.mon-chart-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}
.mon-chart { width: 100%; height: 64px; display: block; }
.mon-chart-empty {
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
  padding: 20px 0;
}

.mon-info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}
.mon-kv { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.mon-kv td { padding: 4px 0; border-bottom: 1px dashed var(--border-color); }
.mon-kv tr:last-child td { border-bottom: none; }
.mon-kv td:first-child { color: var(--text-muted); padding-right: 10px; white-space: nowrap; }
.mon-kv td:last-child { color: var(--text-primary); text-align: right; font-family: var(--font-mono); }

.mon-range { display: flex; gap: 6px; }
.mon-range-btn { cursor: pointer; }
.mon-range-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent-primary);
  color: var(--text-primary);
}

/* T9 (2026-09-04): loud red shipped-corrupt banner — surfaces scenes that shipped
   corrupt after retries (the inline "> ⚠ Scene N" blockquote marker was missed by
   the user inside wall-of-text prose, Ch10 Kettle Lake). */
.corrupt-banner {
  margin: 0 0 12px 0;
  padding: 12px 16px;
  border: 2px solid #d32f2f;
  border-radius: var(--radius-lg, 10px);
  background: rgba(211, 47, 47, 0.14);
  color: #ff6b6b;
  font-weight: 700;
  font-size: 0.95rem;
  box-shadow: 0 0 0 4px rgba(211, 47, 47, 0.08);
}

.corrupt-banner-item {
  margin-top: 6px;
  padding: 6px 10px;
  background: rgba(211, 47, 47, 0.18);
  border-radius: 6px;
  font-weight: 500;
  font-size: 0.8rem;
  color: var(--text-primary, #eee);
}
