/* ================================================================
   INKWELL STUDIO — Premium Dark Editorial Design System
   ================================================================ */

:root {
  /* Core Palette — Warm Dark */
  --bg:          #0d0c0b;
  --surface:     #171512;
  --surface-2:   #1f1d1a;
  --surface-3:   #272421;
  --surface-hover: #2e2b27;
  --border:      #2a2725;
  --border-2:    #333028;

  /* Typography */
  --text:        #e9e4dd;
  --text-2:      #b0a99e;
  --text-3:      #7a726a;
  --text-4:      #4a4540;

  /* Accent — Warm Gold */
  --accent:      #c9921f;
  --accent-2:    #e0a82b;
  --accent-3:    #f0be50;
  --accent-dim:  rgba(201,146,31,0.12);
  --accent-dim2: rgba(201,146,31,0.2);

  /* Semantic */
  --success:     #4fa87c;
  --danger:      #d95555;
  --danger-dim:  rgba(217,85,85,0.12);
  --info:        #5a8fd4;

  /* Typography */
  --font-ui:      'DM Sans', system-ui, sans-serif;
  --font-display: 'Playfair Display', Georgia, serif;
  --font-reading: 'EB Garamond', Georgia, serif;

  /* Radius */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --r-xl: 22px;

  /* Shadows */
  --shadow-sm:  0 1px 3px rgba(0,0,0,0.4);
  --shadow-md:  0 4px 16px rgba(0,0,0,0.5);
  --shadow-lg:  0 8px 32px rgba(0,0,0,0.6);
  --shadow-modal: 0 24px 64px rgba(0,0,0,0.8);
  --glow-accent: 0 0 20px rgba(201,146,31,0.15);

  /* Sidebar */
  --sidebar-w: 240px;
  --topbar-h: 52px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { height: 100%; }

body {
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-4); }

/* Selection */
::selection { background: var(--accent-dim2); color: var(--accent-3); }

/* ================================================================
   APP LAYOUT
   ================================================================ */

.app-root {
  display: flex;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

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

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(3px);
  z-index: 200;
  opacity: 0;
  transition: opacity 0.25s;
}
.sidebar-overlay.active { display: block; opacity: 1; }

.sidebar {
  width: var(--sidebar-w);
  height: 100%;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 300;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 0;
  overflow: hidden;
}

/* Brand */
.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.4rem 1.25rem 1.2rem;
  border-bottom: 1px solid var(--border);
}
.brand-icon {
  width: 36px;
  height: 36px;
  background: var(--accent-dim);
  border: 1px solid var(--accent-dim2);
  border-radius: var(--r-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-2);
  font-size: 1rem;
  flex-shrink: 0;
}
.brand-name {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.1;
}
.brand-sub {
  font-size: 0.68rem;
  color: var(--text-3);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* Sidebar Nav */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.65rem 0.9rem;
  border-radius: var(--r-md);
  background: none;
  border: none;
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
.nav-item:hover {
  background: var(--surface-3);
  color: var(--text);
  transform: translateX(2px);
}
.nav-item.active {
  background: var(--accent-dim);
  color: var(--accent-2);
  font-weight: 600;
}
.nav-item i { width: 16px; text-align: center; font-size: 0.9rem; }

.nav-divider {
  margin: 1rem 0.5rem 0.5rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-4);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* Recent Volumes */
.recent-volumes { display: flex; flex-direction: column; gap: 0.1rem; }
.recent-empty { font-size: 0.82rem; color: var(--text-4); padding: 0.5rem 0.9rem; }
.recent-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.9rem;
  border-radius: var(--r-md);
  cursor: pointer;
  background: none;
  border: none;
  width: 100%;
  text-align: left;
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  transition: background 0.15s, color 0.15s;
  overflow: hidden;
}
.recent-item:hover { background: var(--surface-3); color: var(--text); }
.recent-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.recent-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }

/* Sidebar Footer */
.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.api-status-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.5rem;
  font-size: 0.78rem;
  color: var(--text-3);
}
.status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-4);
  flex-shrink: 0;
}
.status-dot.online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.offline { background: var(--text-4); }
.settings-btn { margin-top: 0; }

/* ================================================================
   MAIN WRAPPER
   ================================================================ */

.main-wrapper {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

/* Mobile Topbar */
.topbar {
  display: none;
  height: var(--topbar-h);
  align-items: center;
  padding: 0 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 1rem;
  flex-shrink: 0;
  z-index: 100;
}
.topbar-btn {
  background: none; border: none; color: var(--text-2);
  font-size: 1.1rem; cursor: pointer; padding: 0.4rem;
  border-radius: var(--r-sm); transition: color 0.15s;
}
.topbar-btn:hover { color: var(--text); }
.topbar-brand {
  display: flex; align-items: center; gap: 0.5rem;
  color: var(--accent-2); font-family: var(--font-display);
  font-size: 1rem; font-weight: 600;
}
.topbar-right { margin-left: auto; }

/* ================================================================
   VIEWS SYSTEM
   ================================================================ */

.views {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.view {
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
  display: flex;
  flex-direction: column;
  transition: opacity 0.2s ease;
}
.view.active {
  opacity: 1;
  pointer-events: all;
  visibility: visible;
}
.view.slide-in {
  animation: slideIn 0.25s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
.view.slide-out {
  animation: slideOut 0.2s ease forwards;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateX(12px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes slideOut {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(-12px); }
}

/* ================================================================
   DASHBOARD VIEW
   ================================================================ */

.view-scroll { flex: 1; overflow-y: auto; }
.view-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2.5rem 2.5rem 4rem;
}

.dashboard-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 2.5rem;
  gap: 1rem;
}
.display-title {
  font-family: var(--font-display);
  font-size: 2.2rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
}
.display-sub {
  font-size: 0.95rem;
  color: var(--text-3);
  margin-top: 0.3rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 0.75rem;
}

/* Stats Row */
.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin-bottom: 2.5rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  position: relative;
  overflow: hidden;
  transition: border-color 0.2s, transform 0.2s;
}
.stat-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--border-2);
}
.stat-card:hover {
  border-color: var(--border-2);
  transform: translateY(-1px);
}
.stat-card.highlight::before {
  background: linear-gradient(90deg, var(--accent), var(--accent-3));
}
.stat-card.highlight { border-color: var(--accent-dim2); }
.stat-icon {
  font-size: 1.1rem;
  color: var(--text-3);
  margin-bottom: 0.5rem;
}
.stat-card.highlight .stat-icon { color: var(--accent-2); }
.stat-val {
  font-family: var(--font-display);
  font-size: 1.9rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1;
}
.stat-card.highlight .stat-val { color: var(--accent-3); }
.stat-lbl {
  font-size: 0.78rem;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Section Header */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 1rem;
}
.section-title {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
}

/* Search */
.search-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 0.5rem 0.9rem;
  min-width: 200px;
  transition: border-color 0.2s;
}
.search-wrap:focus-within { border-color: var(--accent); }
.search-wrap i { color: var(--text-3); font-size: 0.85rem; flex-shrink: 0; }
.search-wrap input {
  background: none; border: none; outline: none;
  font-family: var(--font-ui); font-size: 0.88rem;
  color: var(--text); width: 100%;
}
.search-wrap input::placeholder { color: var(--text-3); }
.search-wrap.sm { min-width: 130px; padding: 0.35rem 0.75rem; }

/* Library Grid */
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1.25rem;
}

/* Book Card */
.book-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.2,0,0,1), box-shadow 0.2s, border-color 0.2s;
  display: flex;
  flex-direction: column;
}
.book-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-lg);
  border-color: var(--border-2);
}
.book-cover {
  height: 130px;
  position: relative;
  display: flex;
  align-items: flex-end;
  padding: 1rem;
  overflow: hidden;
}
.book-cover::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0.6) 100%);
}
.book-cover-genre {
  position: relative;
  z-index: 1;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: rgba(255,255,255,0.75);
  background: rgba(0,0,0,0.3);
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  backdrop-filter: blur(4px);
}
.book-cover-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -60%);
  font-size: 2.5rem;
  color: rgba(255,255,255,0.12);
  z-index: 0;
}
.book-info {
  padding: 1rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.book-card-title {
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.book-card-meta {
  font-size: 0.78rem;
  color: var(--text-3);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.2rem;
}
.book-card-meta i { font-size: 0.72rem; }
.book-card-actions {
  display: flex;
  gap: 0.25rem;
  padding: 0.5rem 0.75rem 0.75rem;
  opacity: 0;
  transition: opacity 0.2s;
}
.book-card:hover .book-card-actions { opacity: 1; }

/* Empty States */
.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}
.empty-state.hidden { display: none; }
.empty-glyph {
  font-size: 3.5rem;
  color: var(--text-4);
  margin-bottom: 0.5rem;
  opacity: 0.5;
}
.empty-state h3 {
  font-family: var(--font-display);
  font-size: 1.4rem;
  color: var(--text-2);
}
.empty-state p { color: var(--text-3); max-width: 340px; line-height: 1.6; }

/* ================================================================
   BOOK VIEW
   ================================================================ */

.book-view-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.book-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.25rem 2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 70px;
}
.btn-back {
  width: 34px; height: 34px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--text-2);
  cursor: pointer;
  font-size: 0.9rem;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.btn-back:hover { background: var(--surface-3); color: var(--text); border-color: var(--border-2); }

.book-title-group { flex: 1; min-width: 0; }
.editable-title {
  background: none;
  border: none;
  outline: none;
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--text);
  width: 100%;
  line-height: 1.2;
}
.editable-title::placeholder { color: var(--text-4); }
.editable-title.sm { font-size: 1rem; font-family: var(--font-ui); font-weight: 500; }

.book-chips-row {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.35rem;
  flex-wrap: wrap;
}
.bchip {
  font-size: 0.73rem;
  color: var(--text-3);
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0.2rem 0.6rem;
}

.book-hdr-actions {
  display: flex;
  gap: 0.75rem;
  flex-shrink: 0;
}

/* Book Workspace */
.book-workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 380px;
  overflow: hidden;
}

/* Chapters Panel */
.chapters-panel {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.panel-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 0.75rem;
}
.panel-title {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.chapter-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.chapter-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: all 0.15s;
  user-select: none;
  position: relative;
}
.chapter-item:hover {
  background: var(--surface-2);
  border-color: var(--border-2);
}
.chapter-item.drag-over {
  border-color: var(--accent);
  background: var(--accent-dim);
}
.chapter-item.dragging {
  opacity: 0.4;
}
.chap-drag-handle {
  color: var(--text-4);
  font-size: 0.8rem;
  cursor: grab;
  padding: 0 0.1rem;
  flex-shrink: 0;
}
.chap-drag-handle:active { cursor: grabbing; }
.chap-num {
  font-size: 0.72rem;
  color: var(--text-4);
  min-width: 22px;
  text-align: right;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}
.chap-info { flex: 1; min-width: 0; }
.chap-title {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chap-meta {
  font-size: 0.73rem;
  color: var(--text-3);
  margin-top: 0.1rem;
}
.chap-actions {
  display: flex;
  gap: 0.15rem;
  opacity: 0;
  transition: opacity 0.15s;
}
.chapter-item:hover .chap-actions { opacity: 1; }

.empty-sm {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 3rem 1.5rem;
  color: var(--text-3);
  font-size: 0.88rem;
  text-align: center;
}
.empty-sm.hidden { display: none; }
.empty-sm i { font-size: 2rem; color: var(--text-4); opacity: 0.5; }

/* Notes Panel */
.notes-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
}
.notes-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.ntab {
  flex: 1;
  padding: 0.9rem 0.5rem;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-3);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.ntab:hover { color: var(--text-2); }
.ntab.active { color: var(--accent-2); border-bottom-color: var(--accent); }

.npane { flex: 1; overflow: hidden; display: none; }
.npane.active { display: flex; }
.notes-ta {
  flex: 1;
  width: 100%;
  background: none;
  border: none;
  outline: none;
  padding: 1.25rem 1.5rem;
  font-family: var(--font-ui);
  font-size: 0.88rem;
  line-height: 1.75;
  color: var(--text-2);
  resize: none;
}
.notes-ta::placeholder { color: var(--text-4); }

.notes-foot {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.genre-sel {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  padding: 0.45rem 0.75rem;
  outline: none;
}
.genre-sel option { background: var(--surface); }

/* ================================================================
   EDITOR VIEW
   ================================================================ */

.editor-view {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Editor Bar */
.editor-bar {
  height: 54px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  gap: 0.75rem;
  flex-shrink: 0;
  z-index: 10;
}
.ebar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}
.ebar-bc {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  overflow: hidden;
}
.bc-book {
  font-size: 0.82rem;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}
.bc-sep {
  font-size: 0.65rem;
  color: var(--text-4);
  flex-shrink: 0;
}

.pane-tabs-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
}
.pane-tabs {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 3px;
  gap: 2px;
}
.ptab {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.85rem;
  border-radius: 7px;
  border: none;
  background: none;
  font-family: var(--font-ui);
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-3);
  cursor: pointer;
  transition: all 0.15s;
}
.ptab:hover { color: var(--text-2); }
.ptab.active {
  background: var(--surface-3);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.ptab i { font-size: 0.78rem; }
.ptab span { display: block; }

.ebar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
}
.wc-wrap {
  font-size: 0.78rem;
  color: var(--text-3);
  white-space: nowrap;
}
.save-badge {
  font-size: 0.75rem;
  color: var(--success);
  display: flex;
  align-items: center;
  gap: 0.3rem;
  white-space: nowrap;
}
.save-badge.hidden { display: none; }

/* Markdown Toolbar */
.md-toolbar {
  height: 40px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 0.75rem;
  gap: 0.25rem;
  flex-shrink: 0;
  overflow-x: auto;
  /* Smooth scroll on iOS */
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}
.md-toolbar::-webkit-scrollbar { height: 0; }
/* All toolbar children must never shrink — key for responsive */
.tbgrp { display: flex; gap: 0.15rem; flex-shrink: 0; }
.tbsep { width: 1px; height: 20px; background: var(--border); margin: 0 0.35rem; flex-shrink: 0; }
.tbtn {
  padding: 0.3rem 0.6rem;
  border-radius: var(--r-sm);
  border: none;
  background: none;
  color: var(--text-3);
  font-family: var(--font-ui);
  font-size: 0.82rem;
  font-weight: 500;
  flex-shrink: 0;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}
.tbtn:hover { background: var(--surface-3); color: var(--text); }
.accent-tbtn { color: var(--accent-2) !important; }
.accent-tbtn:hover { background: var(--accent-dim) !important; }

/* Editor Workspace */
.editor-workspace {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.epane {
  flex: 1;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.epane:last-child { border-right: none; }

/* Write Pane */
.epane-write { background: var(--surface); }
#markdown-editor {
  flex: 1;
  padding: 2.5rem 3rem;
  border: none;
  resize: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 1.06rem;
  line-height: 1.9;
  caret-color: var(--accent-2);
}
#markdown-editor::placeholder { color: var(--text-4); line-height: 2; }

/* Preview Pane */
.epane-preview { background: var(--bg); }
.preview-scroller { flex: 1; overflow-y: auto; }
.novel-body {
  max-width: 62ch;
  margin: 0 auto;
  padding: 2.5rem 2rem;
  font-family: var(--font-reading);
  font-size: 1.15rem;
  line-height: 2.1;
  color: var(--text-2);
}
.novel-body h1, .novel-body h2, .novel-body h3 {
  font-family: var(--font-display);
  color: var(--text);
  margin-top: 2.5rem;
  margin-bottom: 1rem;
  font-weight: 600;
}
.novel-body h1 { font-size: 1.9rem; }
.novel-body h2 { font-size: 1.45rem; }
.novel-body h3 { font-size: 1.15rem; }
.novel-body p {
  margin-bottom: 1.4em;
  text-indent: 1.5em;
}
.novel-body p:first-of-type { text-indent: 0; }
.novel-body blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 1.5rem;
  margin: 1.5rem 0;
  font-style: italic;
  color: var(--text-3);
}
.novel-body hr {
  border: none;
  text-align: center;
  margin: 2.5rem auto;
  color: var(--text-3);
  font-size: 1.1rem;
  letter-spacing: 0.4em;
}
.novel-body hr::after { content: '✦ ✦ ✦'; }
.novel-body strong { color: var(--text); font-weight: 600; }
.novel-body em { color: var(--text-2); }
.novel-body code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1em 0.4em;
  font-size: 0.9em;
  color: var(--accent-2);
}

/* AI Panel */
.epane-ai { max-width: 380px; background: var(--surface); }
.ai-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
}
.ai-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.ai-panel-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--accent-2);
}
.ai-panel-title i { font-size: 0.85rem; }

/* Quick Chips */
.quick-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  /* Cap height and scroll if too many chips on small screens */
  max-height: 100px;
  overflow-y: auto;
  overflow-x: hidden;
}
.quick-chips::-webkit-scrollbar { width: 3px; }
.quick-chips::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 2px; }
.qchip {
  padding: 0.3rem 0.75rem;
  border-radius: 99px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.qchip:hover {
  background: var(--accent-dim);
  border-color: var(--accent-dim2);
  color: var(--accent-3);
}

/* AI Chat */
.ai-chat {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.ai-msg {
  display: flex;
  gap: 0.6rem;
  max-width: 92%;
}
.ai-msg.ai-side { align-self: flex-start; }
.ai-msg.user-side { align-self: flex-end; flex-direction: row-reverse; }
.ai-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-dim);
  border: 1px solid var(--accent-dim2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-2);
  font-size: 0.7rem;
  flex-shrink: 0;
  margin-top: 2px;
}
.ai-bubble {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  border-top-left-radius: 4px;
  padding: 0.75rem 1rem;
  font-size: 0.86rem;
  line-height: 1.65;
  color: var(--text-2);
}
.ai-bubble.user {
  background: var(--accent-dim);
  border-color: var(--accent-dim2);
  color: var(--text);
  border-top-left-radius: var(--r-md);
  border-top-right-radius: 4px;
}
.ai-bubble p { margin-bottom: 0.6em; }
.ai-bubble p:last-child { margin-bottom: 0; }
.ai-bubble h1, .ai-bubble h2, .ai-bubble h3 { color: var(--text); margin: 0.75em 0 0.4em; font-size: 0.95em; }
.ai-bubble strong { color: var(--text); }
.ai-bubble blockquote { border-left: 2px solid var(--accent); padding-left: 0.75rem; color: var(--text-3); font-style: italic; margin: 0.5rem 0; }
.ai-bubble hr { border: none; text-align: center; color: var(--text-3); margin: 0.75rem 0; }
.ai-bubble hr::after { content: '— ✦ —'; font-size: 0.8em; }

.ai-typing {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.6rem;
  color: var(--text-3);
  font-size: 0.8rem;
}
.typing-dots { display: flex; gap: 4px; }
.typing-dots span {
  width: 5px; height: 5px;
  background: var(--accent-2);
  border-radius: 50%;
  animation: typingPulse 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingPulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}

/* Insert Bar */
.insert-bar {
  display: flex;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.insert-bar.hidden { display: none; }

/* AI Input */
.ai-input-zone {
  border-top: 1px solid var(--border);
  padding: 0.75rem;
  flex-shrink: 0;
}
#ai-input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 0.75rem;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 0.86rem;
  line-height: 1.6;
  resize: none;
  outline: none;
  transition: border-color 0.2s;
  display: block;
  width: 100%;
}
#ai-input:focus { border-color: var(--accent); }
#ai-input::placeholder { color: var(--text-3); }
.ai-input-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.5rem;
}
.ai-model-tag {
  font-size: 0.72rem;
  color: var(--text-4);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0.2rem 0.55rem;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.55rem 1.15rem;
  border-radius: var(--r-md);
  border: none;
  font-family: var(--font-ui);
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  line-height: 1;
  text-decoration: none;
}
.btn-primary {
  background: var(--accent);
  color: #0d0c0b;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(201,146,31,0.25);
}
.btn-primary:hover {
  background: var(--accent-2);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(201,146,31,0.35);
}
.btn-ghost {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-2);
}
.btn-ghost:hover {
  background: var(--surface-3);
  border-color: var(--border-2);
  color: var(--text);
}
.btn-accent {
  background: var(--accent-dim);
  border: 1px solid var(--accent-dim2);
  color: var(--accent-2);
}
.btn-accent:hover {
  background: var(--accent-dim2);
  color: var(--accent-3);
}
.btn-danger {
  background: var(--danger-dim);
  border: 1px solid rgba(217,85,85,0.3);
  color: var(--danger);
}
.btn-danger:hover {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}
.btn-sm {
  padding: 0.4rem 0.85rem;
  font-size: 0.8rem;
}

.icon-btn {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  background: none;
  border: none;
  color: var(--text-3);
  font-size: 0.88rem;
  cursor: pointer;
  border-radius: var(--r-sm);
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.sm { width: 26px; height: 26px; font-size: 0.78rem; }
.icon-btn.danger:hover { background: var(--danger-dim); color: var(--danger); }

/* ================================================================
   MODALS
   ================================================================ */

.modal {
  margin: auto;
  padding: 0;
  border: none;
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-modal);
  width: 90%;
  max-width: 500px;
  overflow: hidden;
}
.modal::backdrop {
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(6px);
}
.modal[open] { animation: modalIn 0.25s cubic-bezier(0.34,1.5,0.64,1); }
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.94) translateY(-8px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}
.modal.wide { max-width: 600px; }
.modal.modal-sm { max-width: 400px; }

.modal-card { display: flex; flex-direction: column; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.4rem 1.5rem;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
}
.modal-body {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  overflow-y: auto;
  max-height: 65vh;
}
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  padding: 1.1rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.modal-desc { font-size: 0.88rem; color: var(--text-3); line-height: 1.6; }

/* Forms */
.form-field { display: flex; flex-direction: column; gap: 0.5rem; }
.form-field label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-2);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.form-field input, .form-field select, .form-field textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 0.65rem 0.9rem;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
  width: 100%;
}
.form-field input:focus, .form-field select:focus, .form-field textarea:focus {
  border-color: var(--accent);
}
.form-field select { cursor: pointer; }
.form-field select option { background: var(--surface); }
.field-note {
  font-size: 0.75rem;
  color: var(--text-3);
  line-height: 1.5;
}
.help-link {
  font-size: 0.75rem;
  color: var(--accent-2);
  text-decoration: none;
}
.help-link:hover { color: var(--accent-3); }
.input-row {
  display: flex;
  gap: 0.35rem;
  align-items: center;
}
.input-row input { flex: 1; }

/* Toggle Switch */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 0;
}
.trow-label span { font-size: 0.88rem; color: var(--text-2); font-weight: 500; }
.tswitch {
  position: relative;
  width: 42px;
  height: 24px;
  cursor: pointer;
  flex-shrink: 0;
}
.tswitch input { opacity: 0; width: 0; height: 0; }
.tknob {
  position: absolute;
  inset: 0;
  background: var(--surface-3);
  border-radius: 99px;
  border: 1px solid var(--border);
  transition: background 0.2s;
}
.tknob::after {
  content: '';
  position: absolute;
  left: 3px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px; height: 16px;
  background: var(--text-3);
  border-radius: 50%;
  transition: left 0.2s, background 0.2s;
}
.tswitch input:checked + .tknob { background: var(--accent); border-color: var(--accent); }
.tswitch input:checked + .tknob::after { left: calc(100% - 19px); background: #0d0c0b; }

/* Settings Sections */
.settings-sec { display: flex; flex-direction: column; gap: 1rem; }
.settings-sec-title {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-4);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

/* Color Swatches */
.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.swatch {
  width: 32px; height: 32px;
  border-radius: var(--r-sm);
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.15s, border-color 0.15s;
}
.swatch:hover { transform: scale(1.1); }
.swatch.active { border-color: white; box-shadow: 0 0 0 2px var(--accent); }
input[type="color"] {
  width: 100%;
  height: 36px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  cursor: pointer;
  padding: 2px;
}

/* Export Grid */
.export-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
.export-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1.5rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}
.export-tile:hover {
  background: var(--accent-dim);
  border-color: var(--accent-dim2);
  transform: translateY(-2px);
}
.export-tile i { font-size: 2rem; color: var(--text-3); }
.export-tile:hover i { color: var(--accent-2); }
.et-name { font-size: 0.9rem; font-weight: 600; color: var(--text); }
.et-desc { font-size: 0.75rem; color: var(--text-3); }

/* ================================================================
   CONTEXT MENU
   ================================================================ */

.ctx-menu {
  position: fixed;
  background: var(--surface-2);
  border: 1px solid var(--border-2);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  padding: 0.35rem;
  z-index: 9999;
  min-width: 160px;
  animation: ctxIn 0.12s ease;
}
.ctx-menu.hidden { display: none; }
@keyframes ctxIn {
  from { opacity: 0; transform: scale(0.95); }
  to   { opacity: 1; transform: scale(1); }
}
.ctx-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.55rem 0.85rem;
  border: none;
  background: none;
  color: var(--text-2);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: var(--r-sm);
  transition: background 0.1s, color 0.1s;
  text-align: left;
}
.ctx-item:hover { background: var(--surface-3); color: var(--text); }
.ctx-item.ctx-danger { color: var(--danger); }
.ctx-item.ctx-danger:hover { background: var(--danger-dim); }
.ctx-divider { height: 1px; background: var(--border); margin: 0.3rem 0.35rem; }

/* ================================================================
   TOAST NOTIFICATIONS
   ================================================================ */

#toasts {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 99999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.75rem 1.1rem;
  background: var(--surface-2);
  border: 1px solid var(--border-2);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  font-size: 0.85rem;
  color: var(--text);
  pointer-events: auto;
  animation: toastIn 0.3s cubic-bezier(0.34,1.5,0.64,1);
  max-width: 320px;
  border-left: 3px solid var(--border-2);
}
.toast.success { border-left-color: var(--success); }
.toast.error   { border-left-color: var(--danger); }
.toast.info    { border-left-color: var(--info); }
.toast-icon { font-size: 0.9rem; }
.toast.success .toast-icon { color: var(--success); }
.toast.error   .toast-icon { color: var(--danger); }
.toast.info    .toast-icon { color: var(--info); }
@keyframes toastIn {
  from { opacity: 0; transform: translateX(20px) scale(0.95); }
  to   { opacity: 1; transform: translateX(0) scale(1); }
}
@keyframes toastOut {
  to { opacity: 0; transform: translateX(20px) scale(0.95); }
}

/* ================================================================
   FOCUS MODE
   ================================================================ */

body.focus-mode .sidebar,
body.focus-mode .editor-bar,
body.focus-mode .md-toolbar,
body.focus-mode .epane-preview,
body.focus-mode .epane-ai {
  display: none !important;
}
body.focus-mode .app-root {
  background: #0d0c0b;
}
body.focus-mode #markdown-editor {
  max-width: 70ch;
  margin: 0 auto;
  padding: 5rem 2rem;
  font-size: 1.15rem;
}
.focus-hint {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0.5rem 1.25rem;
  font-size: 0.8rem;
  color: var(--text-3);
  z-index: 9999;
  animation: toastIn 0.3s ease;
}
.focus-hint.hidden { display: none; }
.focus-hint kbd {
  background: var(--surface-3);
  border: 1px solid var(--border-2);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  font-size: 0.75rem;
  font-family: monospace;
  color: var(--text-2);
}

/* ================================================================
   UTILITIES
   ================================================================ */

.hidden { display: none !important; }

/* ================================================================
   RESPONSIVE — TABLET WIDE (≤ 1200px)
   ================================================================ */

@media (max-width: 1200px) {
  .epane-ai { max-width: 340px; }
}

/* ================================================================
   RESPONSIVE — TABLET (≤ 1024px)
   ================================================================ */

@media (max-width: 1024px) {
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .book-workspace { grid-template-columns: 1fr; }
  .notes-panel {
    height: 360px;
    border-top: 1px solid var(--border);
    border-right: none;
  }
  /* On tablet: hide preview pane, show write + AI side by side */
  .epane-preview { display: none; }
  .epane-ai { max-width: 320px; }
  /* Shrink pane tab labels on mid-sized screens */
  .ptab span { font-size: 0.78rem; }
  /* Toolbar scrollable hint — fade right edge */
  .md-toolbar::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 30px;
    background: linear-gradient(to left, var(--surface), transparent);
    pointer-events: none;
  }
  .md-toolbar { position: relative; }
}

/* ================================================================
   RESPONSIVE — TABLET SMALL (≤ 900px)
   Hide pane tab text labels, just icons
   ================================================================ */

@media (max-width: 900px) {
  .ptab span { display: none; }
  .ptab { padding: 0.4rem 0.75rem; gap: 0; }
  .ptab i { font-size: 0.9rem; }
  /* At this size AI pane might be too narrow — expand */
  .epane-ai { max-width: 280px; }
  /* Breadcrumb: shorter book name */
  .bc-book { max-width: 80px; }
}

/* ================================================================
   RESPONSIVE — MOBILE (≤ 768px)
   ================================================================ */

@media (max-width: 768px) {
  :root { --sidebar-w: 260px; }

  /* Sidebar becomes a drawer */
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    z-index: 300;
    box-shadow: none;
    transition: transform 0.28s cubic-bezier(0.4,0,0.2,1), box-shadow 0.28s;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  .topbar { display: flex; }
  .main-wrapper { padding-top: 0; }
  .views { top: 0; }

  /* ── Dashboard ── */
  .view-inner { padding: 1.25rem 1rem 3rem; }
  .display-title { font-size: 1.5rem; }
  .display-sub { display: none; }
  .stats-row { grid-template-columns: 1fr 1fr; gap: 0.6rem; margin-bottom: 1.5rem; }
  .stat-card { padding: 1rem 1.1rem; }
  .stat-val { font-size: 1.6rem; }
  .dashboard-header { flex-direction: column; align-items: stretch; gap: 0.9rem; margin-bottom: 1.5rem; }
  .dashboard-header .btn-primary { width: 100%; justify-content: center; }
  .library-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.75rem; }
  .book-cover { height: 110px; }
  .section-header { flex-direction: column; align-items: stretch; gap: 0.6rem; }
  .search-wrap { min-width: unset; width: 100%; }

  /* ── Book View ── */
  .book-header {
    flex-wrap: wrap;
    padding: 0.9rem 1rem;
    gap: 0.6rem;
    min-height: unset;
  }
  /* Title takes full width row */
  .book-title-group { width: 100%; order: 2; }
  .editable-title { font-size: 1.3rem; }
  .btn-back { order: 1; }
  .book-hdr-actions {
    order: 3;
    width: 100%;
    display: flex;
    gap: 0.5rem;
  }
  .book-hdr-actions .btn { flex: 1; justify-content: center; }
  .book-hdr-actions .btn span { display: inline; font-size: 0.82rem; }
  .book-workspace { grid-template-columns: 1fr; overflow: visible; }
  .chapters-panel {
    height: auto;
    min-height: 260px;
    max-height: 45vh;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .notes-panel {
    height: 280px;
    border-top: none;
  }
  .panel-top { padding: 0.75rem 1rem; }

  /* ── Editor Bar: 2-row layout ── */
  .editor-bar {
    height: auto;
    flex-wrap: wrap;
    padding: 0.6rem 0.75rem 0;
    gap: 0;
    row-gap: 0;
  }
  /* Row 1: back button + breadcrumb (left) + wordcount + focus btn (right) */
  .ebar-left {
    width: calc(100% - 120px);
    flex: none;
    min-width: 0;
  }
  .ebar-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
    width: 120px;
    justify-content: flex-end;
  }
  /* Wordcount smaller on mobile */
  .wc-wrap { font-size: 0.7rem; }
  .save-badge { font-size: 0.68rem; }
  /* Row 2: pane tabs full width with top divider */
  .pane-tabs-wrap {
    width: 100%;
    order: 10; /* goes last in flex */
    justify-content: center;
    padding: 0.5rem 0.75rem 0.65rem;
    margin-top: 0.4rem;
    border-top: 1px solid var(--border);
  }
  .pane-tabs { width: 100%; }
  .ptab {
    flex: 1;
    justify-content: center;
    padding: 0.45rem 0;
    gap: 0.4rem;
  }
  .ptab span { display: block; font-size: 0.78rem; }
  .ptab i { font-size: 0.82rem; }
  /* Breadcrumb: shrink book name more on mobile */
  .bc-book { max-width: 70px; font-size: 0.76rem; }
  .bc-sep { display: none; } /* hide separator on mobile - space is tight */
  .chapter-title-input.sm { font-size: 0.9rem; max-width: 160px; }

  /* ── MD Toolbar: hidden on mobile ── */
  .md-toolbar { display: none; }

  /* ── Editor panes as tabs (position absolute in container) ── */
  .editor-workspace {
    flex: 1;
    overflow: hidden;
    position: relative;
    /* Keep flex: 1 from base rule for height — only override display */
    display: block;
  }
  .epane {
    position: absolute;
    inset: 0;
    display: none !important;
    border-right: none;
    overflow: hidden;
  }
  .epane.active { display: flex !important; flex-direction: column; }
  .epane-ai { max-width: none; }
  /* AI panel inner */
  .quick-chips {
    max-height: 75px;
    overflow-y: auto;
    overflow-x: hidden;
    gap: 0.3rem;
    padding: 0.5rem 0.75rem;
  }
  .qchip { font-size: 0.72rem; padding: 0.28rem 0.6rem; }
  .ai-panel-header { padding: 0.75rem 1rem; }
  .ai-input-zone { padding: 0.6rem; }
  #ai-input { font-size: 0.84rem; }
  .ai-input-foot { margin-top: 0.4rem; }

  /* ── Modals ── */
  .modal, .modal.wide { width: 96%; max-width: none; margin: auto; border-radius: var(--r-lg); }
  .modal-body { max-height: 60vh; }
  .export-grid { grid-template-columns: 1fr 1fr; gap: 0.5rem; }
  .export-tile { padding: 1.1rem 0.75rem; }
  .export-tile i { font-size: 1.5rem; }
}

/* ================================================================
   RESPONSIVE — SMALL MOBILE (≤ 480px)
   ================================================================ */

@media (max-width: 480px) {
  .stats-row { grid-template-columns: 1fr 1fr; }
  .stat-val { font-size: 1.4rem; }
  .stat-card { padding: 0.85rem; }
  .stat-icon { font-size: 0.9rem; margin-bottom: 0.25rem; }
  .stat-lbl { font-size: 0.7rem; }
  #markdown-editor { padding: 1.25rem 1rem; font-size: 1rem; }
  .library-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.6rem; }
  .book-cover { height: 90px; }
  .book-card-title { font-size: 0.9rem; }
  .display-title { font-size: 1.35rem; }
  .editable-title { font-size: 1.1rem; }
  /* Export: 1 column on very small */
  .export-grid { grid-template-columns: 1fr 1fr; }
  .modal-body { max-height: 55vh; }
  /* Editor bar: even more compact */
  .ebar-left { width: calc(100% - 100px); }
  .ebar-right { width: 100px; }
  .wc-wrap { display: none; } /* hide wc on very small, just show save + focus */
}

/* ================================================================
   RESPONSIVE — TINY (≤ 360px)
   ================================================================ */

@media (max-width: 360px) {
  .view-inner { padding: 1rem 0.75rem 3rem; }
  .stats-row { gap: 0.5rem; }
  .stat-card { padding: 0.75rem; }
  .stat-val { font-size: 1.25rem; }
  .library-grid { grid-template-columns: 1fr 1fr; gap: 0.5rem; }
  .brand-sub { display: none; }
  .topbar-brand span { font-size: 0.9rem; }
  .book-hdr-actions .btn { font-size: 0.78rem; }
  .chapters-panel { max-height: 40vh; }
}

/* ================================================================
   LOADING / SKELETON STATES
   ================================================================ */

@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}
.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--r-sm);
}

/* ================================================================
   PRINT PREVIEW OVERLAY
   Lives in <body> directly, outside .app-root
   ================================================================ */

.ipo {
  position: fixed;
  inset: 0;
  z-index: 99000;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  animation: ipoIn 0.2s ease;
}
.ipo.hidden { display: none !important; }

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

/* ── IPO Header ──────────────────────────────────────── */
.ipo-header {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 1rem;
}
.ipo-header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
  overflow: hidden;
}
.ipo-brand {
  width: 30px; height: 30px;
  background: var(--accent-dim);
  border: 1px solid var(--accent-dim2);
  border-radius: var(--r-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--accent-2);
  font-size: 0.8rem;
  flex-shrink: 0;
}
.ipo-book-title {
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 300px;
}
.ipo-badge {
  font-size: 0.68rem;
  color: var(--accent-2);
  background: var(--accent-dim);
  border: 1px solid var(--accent-dim2);
  border-radius: 99px;
  padding: 0.18rem 0.55rem;
  white-space: nowrap;
  flex-shrink: 0;
}
.ipo-header-right {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-shrink: 0;
}
.ipo-hint {
  font-size: 0.75rem;
  color: var(--text-3);
  white-space: nowrap;
}
.ipo-hint strong { color: var(--text-2); }

/* ── IPO Body (scrollable area) ─────────────────────── */
.ipo-body {
  flex: 1;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 2.5rem 1.5rem 4rem;
  background: var(--surface-2);
  /* Subtle grid bg */
  background-image: radial-gradient(circle, var(--border) 1px, transparent 1px);
  background-size: 28px 28px;
}

/* ── IPO Paper (simulated light-novel page) ──────────── */
.ipo-paper {
  background: #ffffff;
  color: #1a1610;
  width: 148mm;   /* A5 width */
  min-height: 210mm; /* A5 height */
  padding: 22mm 18mm 20mm 20mm;
  box-shadow: 0 8px 40px rgba(0,0,0,0.25), 0 2px 8px rgba(0,0,0,0.1);
  border-radius: 2px;
  font-family: 'EB Garamond', Georgia, 'Times New Roman', serif;
  font-size: 11pt;
  line-height: 1.8;
  position: relative;
  overflow: visible;
}

/* ── Manuscript Title Page (in overlay) ─────────────── */
.ms-title-page {
  text-align: center;
  padding: 3cm 0 2cm;
  margin-bottom: 2rem;
  border-bottom: 0.5pt solid #d0c8c0;
}
.ms-genre {
  font-family: 'DM Sans', Arial, sans-serif;
  font-size: 7pt;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: #aaa;
  margin-bottom: 1.5rem;
}
.ms-main-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 1.8rem;
  font-weight: 700;
  color: #1a1610;
  line-height: 1.2;
  margin-bottom: 0.75rem;
}
.ms-ornament {
  font-size: 0.9rem;
  color: #bbb;
  letter-spacing: 0.4em;
  margin: 1.25rem 0;
}
.ms-meta {
  font-family: 'DM Sans', Arial, sans-serif;
  font-size: 8pt;
  color: #bbb;
  display: flex;
  gap: 0;
  justify-content: center;
  flex-wrap: wrap;
}
.ms-meta-sep { margin: 0 0.5rem; }

/* ── Synopsis Page ───────────────────────────────────── */
.ms-synopsis-page {
  margin: 1.5rem 0 2.5rem;
  padding: 1.25rem 1.5rem;
  border-left: 2pt solid #c9921f;
  background: #faf8f5;
}
.ms-section-label {
  font-family: 'DM Sans', Arial, sans-serif;
  font-size: 7pt;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: #aaa;
  margin-bottom: 0.6rem;
}
.ms-synopsis-text {
  font-size: 9.5pt;
  line-height: 1.7;
  color: #444;
  font-style: italic;
}

/* ── Manuscript Chapters (in overlay) ───────────────── */
.ms-chapter {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 0.5pt solid #e0d8d0;
}
.ms-chapter-first { border-top: none; margin-top: 0; }

.ms-chapter-head {
  text-align: center;
  padding: 1.5rem 0 1.25rem;
  margin-bottom: 1.25rem;
}
.ms-chapter-num {
  font-family: 'DM Sans', Arial, sans-serif;
  font-size: 7pt;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: #bbb;
  margin-bottom: 0.5rem;
}
.ms-chapter-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 1.1rem;
  font-weight: 700;
  color: #1a1610;
  line-height: 1.3;
  margin: 0;
}
.ms-chapter-rule {
  width: 30px;
  height: 1.5pt;
  background: #c9921f;
  margin: 0.85rem auto 0;
  border-radius: 99px;
}

/* ── Manuscript Body Typography ─────────────────────── */
.ms-chapter-body {
  font-family: 'EB Garamond', Georgia, serif;
  font-size: 11pt;
  line-height: 1.85;
  color: #1a1610;
}
.ms-chapter-body p {
  text-indent: 1.5em;
  margin: 0;
}
.ms-chapter-body p:first-of-type { text-indent: 0; }
.ms-chapter-body h1,.ms-chapter-body h2,
.ms-chapter-body h3,.ms-chapter-body h4 {
  font-family: 'Playfair Display', Georgia, serif;
  color: #1a1610;
  margin: 1.5em 0 0.5em;
  text-indent: 0;
  line-height: 1.25;
  font-weight: 600;
}
.ms-chapter-body h1 { font-size: 1.3rem; }
.ms-chapter-body h2 { font-size: 1.1rem; }
.ms-chapter-body h3 { font-size: 1rem; }
.ms-chapter-body h4 { font-size: 0.95rem; font-style: italic; }
.ms-chapter-body blockquote {
  border-left: 2pt solid #c9921f;
  padding: 0.1rem 0 0.1rem 1rem;
  margin: 1em 0;
  font-style: italic;
  color: #666;
}
.ms-chapter-body blockquote p { text-indent: 0; }
.ms-chapter-body hr {
  border: none;
  text-align: center;
  margin: 1.5em 0;
  color: #bbb;
  font-size: 0.85rem;
  letter-spacing: 0.3em;
}
.ms-chapter-body hr::after { content: '✦  ✦  ✦'; }
.ms-chapter-body strong { font-weight: 700; color: #111; }
.ms-chapter-body em { font-style: italic; }
.ms-chapter-body del { text-decoration: line-through; color: #888; }
.ms-chapter-body ul,.ms-chapter-body ol { padding-left: 1.5em; margin: 0.75em 0; }
.ms-chapter-body li { margin-bottom: 0.2em; }
.ms-chapter-body code {
  font-family: 'DM Sans', monospace;
  font-size: 8.5pt;
  background: #f5f0ea;
  padding: 0.1em 0.35em;
  border-radius: 3px;
  color: #7a4010;
}
.ms-chapter-body pre {
  font-family: 'Cascadia Code', 'Courier New', monospace;
  font-size: 8pt;
  background: #f5f0ea;
  border-left: 2pt solid #d0c8c0;
  padding: 0.75em 1em;
  margin: 1em 0;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.ms-chapter-body pre code { background: none; padding: 0; color: #2a1a0a; }
.ms-table-wrap { overflow-x: auto; margin: 1em 0; }
.ms-chapter-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 8.5pt;
}
.ms-chapter-body th {
  background: #f0ece6;
  padding: 4pt 6pt;
  font-weight: 700;
  border-bottom: 1pt solid #c8c0b8;
  text-align: left;
}
.ms-chapter-body td {
  padding: 3pt 6pt;
  border-bottom: 0.5pt solid #e0d8d0;
}

/* ── IPO Mobile responsive ───────────────────────────── */
@media (max-width: 768px) {
  .ipo-header { height: auto; flex-wrap: wrap; padding: 0.7rem 0.9rem; gap: 0.5rem; }
  .ipo-header-left { width: 100%; }
  .ipo-header-right { width: 100%; gap: 0.5rem; }
  .ipo-hint { display: none; }
  .ipo-header-right .btn { flex: 1; justify-content: center; }
  .ipo-book-title { max-width: 200px; }
  .ipo-body { padding: 1.25rem 0.5rem 3rem; background: var(--surface-3); background-image: none; }
  .ipo-paper { width: 100%; min-height: unset; padding: 1.5rem 1.1rem; }
  .ms-main-title { font-size: 1.3rem; }
  .ms-title-page { padding: 1.5rem 0 1rem; }
}

/* ================================================================
   @MEDIA PRINT
   Hides all app chrome; shows ONLY the print overlay content
   ================================================================ */

@media print {

  /* — Hide everything — */
  .app-root,
  #toasts,
  #ctx-menu,
  .focus-hint,
  .modal,
  .sidebar-overlay {
    display: none !important;
  }

  /* — Reset body for print — */
  body {
    background: #fff !important;
    overflow: visible !important;
    height: auto !important;
  }

  /* — Show the print overlay — */
  .ipo {
    display: block !important;
    position: static !important;
    animation: none !important;
    background: #fff !important;
    z-index: auto !important;
  }

  /* — Hide overlay chrome (header, buttons) — */
  .ipo-header,
  .iw-no-print {
    display: none !important;
  }

  /* — Body: no background, just let content flow — */
  .ipo-body {
    display: block !important;
    overflow: visible !important;
    padding: 0 !important;
    background: #fff !important;
    background-image: none !important;
  }

  /* — Paper: full width, no shadow, natural flow — */
  .ipo-paper {
    width: 100% !important;
    min-height: 0 !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    padding: 0 !important;
    background: #fff !important;
    color: #1a1610 !important;
    font-size: 11pt !important;
    line-height: 1.8 !important;
  }

  /* — @page: A5 light novel, with page numbers — */
  @page {
    size: A5 portrait;
    margin: 2.2cm 1.8cm 2cm 2cm;
    @bottom-right {
      content: counter(page);
      font-family: 'DM Sans', Arial, sans-serif;
      font-size: 8pt;
      color: #aaa;
    }
  }

  /* — Title page: full page height — */
  .ms-title-page {
    min-height: 100vh;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    page-break-after: always;
    break-after: page;
    border-bottom: none !important;
    padding: 0 !important;
  }

  /* — Synopsis page — */
  .ms-synopsis-page {
    page-break-after: always;
    break-after: page;
    min-height: 60vh;
    display: flex !important;
    flex-direction: column !important;
    justify-content: center !important;
  }

  /* — Each chapter starts on a new page — */
  .ms-chapter {
    page-break-before: always;
    break-before: page;
    border-top: none !important;
    margin-top: 0 !important;
    padding-top: 0 !important;
  }

  /* — Avoid breaks inside certain elements — */
  .ms-chapter-head { break-after: avoid; page-break-after: avoid; }
  .ms-chapter-body blockquote { break-inside: avoid; }
  .ms-chapter-body pre { break-inside: avoid; white-space: pre-wrap !important; }
  .ms-chapter-body table { break-inside: avoid; }
  .ms-chapter-body h1,
  .ms-chapter-body h2,
  .ms-chapter-body h3 { break-after: avoid; page-break-after: avoid; }
  .ms-chapter-body p { orphans: 3; widows: 3; }

  /* — Body typography for print — */
  .ms-chapter-body {
    font-family: 'EB Garamond', Georgia, 'Times New Roman', serif !important;
    font-size: 11pt !important;
    line-height: 1.85 !important;
    hyphens: auto !important;
    -webkit-hyphens: auto !important;
  }
  .ms-chapter-body p { text-indent: 1.5em !important; margin: 0 !important; }
  .ms-chapter-body p:first-of-type { text-indent: 0 !important; }
  .ms-chapter-body pre,
  .ms-chapter-body code {
    font-family: 'Cascadia Code', 'Courier New', monospace !important;
    font-size: 8.5pt !important;
    background: #f5f5f5 !important;
  }
  .ms-chapter-body th { background: #efefef !important; }
  .ms-table-wrap { overflow: visible !important; }

  /* — Color accuracy — */
  * {
    print-color-adjust: exact !important;
    -webkit-print-color-adjust: exact !important;
  }
}


