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

:root {
  --bg: #f5f3ef;
  --bg2: #eceae5;
  --bg3: #e2dfd9;
  --bg4: #d8d5ce;
  --border: #ccc9c0;
  --border2: #b8b4aa;
  --text: #2a2520;
  --text2: #6a6560;
  --text3: #9a9590;
  --accent: #a0722a;
  --accent2: #c49050;
  --accentbg: rgba(160, 114, 42, 0.08);
  --danger: #8a3030;
  --success: #2a6a44;
  --r: 5px;
  --r2: 9px;
}

body.dark {
  --bg: #0c0c0d;
  --bg2: #141416;
  --bg3: #1a1a1d;
  --bg4: #212125;
  --border: #252529;
  --border2: #32323a;
  --text: #dedad3;
  --text2: #8a8780;
  --text3: #4a4845;
  --accent: #c4954a;
  --accent2: #7a5520;
  --accentbg: rgba(196, 149, 74, 0.07);
  --danger: #a84a4a;
  --success: #4a8a64;
}

body {
  font-family: 'IBM Plex Mono', monospace;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-size: 12px;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  height: 40px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
  direction: ltr;
}

.tabs {
  display: flex;
  gap: 1px;
}

.tab {
  background: none;
  border: none;
  color: var(--text3);
  padding: 5px 12px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.04em;
  transition: color 0.12s, background 0.12s;
}

.tab:hover {
  color: var(--text2);
}

.tab.active {
  color: var(--accent);
  background: var(--accentbg);
}

.topbar-right {
  display: flex;
  gap: 6px;
  align-items: center;
}

.exp-btn {
  background: none;
  border: 1px solid var(--border2);
  color: var(--text3);
  padding: 4px 10px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.06em;
  transition: border-color 0.12s, color 0.12s;
}

.exp-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#filename-input {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  padding: 3px 9px;
  border-radius: var(--r);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  outline: none;
  width: 130px;
}

#workspace {
  display: flex;
  flex: 1;
  overflow: hidden;
  direction: ltr;
}

#edit-view {
  display: flex;
  flex: 1;
  flex-direction: row;
  overflow: hidden;
  background: var(--bg3);
}

#eb-panel {
  flex: 3;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  background: var(--bg2);
  overflow: hidden;
  border-right: 1px solid var(--border);
}

#eb-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 10px;
  color: var(--text2);
  flex-shrink: 0;
}

#eb-count {
  background: var(--bg3);
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 10px;
}

.eb-type-badge {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg4);
  border-radius: var(--r);
  font-size: 10px;
  color: var(--accent);
  flex-shrink: 0;
}

.eb-style-select {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 2px 5px;
  border-radius: var(--r);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  outline: none;
  cursor: pointer;
}

.eb-actions {
  display: flex;
  gap: 2px;
}

.eb-btn {
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 12px;
  line-height: 1;
}

.eb-btn:hover {
  color: var(--accent);
  background: var(--accentbg);
}

.eb-item-body {
  padding: 0;
}

.eb-item-footer {
  display: flex;
  gap: 12px;
  padding: 5px 10px;
  border-top: 1px solid var(--border);
  font-size: 10px;
}

.eb-item.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.eb-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text3);
  cursor: pointer;
}

.eb-toggle input {
  accent-color: var(--accent);
}

.eb-break-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 4px;
}

.eb-break-toolbar {
  display: flex;
  gap: 2px;
  padding: 2px 0;
}

.eb-page-break {
  padding: 8px;
  text-align: center;
  border: 1px dashed var(--border2);
  border-radius: var(--r);
  color: var(--text3);
  font-size: 10px;
}

.eb-auto-grow {
  resize: none;
  overflow: hidden;
  min-height: 32px;
}

.eb-chapter-break {
  padding: 12px;
  text-align: center;
  background: var(--accentbg);
  border: 1px solid var(--accent);
  border-radius: var(--r2);
}

.eb-chapter-label {
  font-family: 'Amiri', serif;
  font-size: 16px;
  color: var(--accent);
  font-weight: 700;
}

.eb-view-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text3);
  padding: 4px 12px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
}

.eb-view-btn.active {
  background: var(--accentbg);
  border-color: var(--accent);
  color: var(--accent);
}

#eb-pages-toolbar {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.eb-rich-children {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: var(--bg3);
}

.eb-rich-child {
  border: 1px solid var(--border);
  border-radius: var(--r);
  overflow: hidden;
}

.eb-rich-child-header {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: var(--bg4);
  border-bottom: 1px solid var(--border);
}

.eb-rich-style-select {
  font-size: 11px;
  padding: 1px 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg);
  color: var(--text);
  max-width: 120px;
}

.eb-rich-label {
  font-size: 10px;
  color: var(--accent);
  margin: 0 4px;
}

.toggle-flow, .toggle-rich-child, .toggle-to-rich {
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 12px;
  line-height: 1;
}

.toggle-flow:hover, .toggle-rich-child:hover, .toggle-to-rich:hover {
  color: var(--accent);
  background: var(--accentbg);
}

.eb-child-textarea {
  width: 100%;
  background: var(--bg);
  border: none;
  color: var(--text);
  font-family: 'Noto Nastaliq Urdu', serif;
  font-size: 13px;
  line-height: 1.8;
  padding: 8px;
  resize: vertical;
  direction: rtl;
  text-align: right;
  outline: none;
}

.eb-empty-rich {
  padding: 12px;
  text-align: center;
  color: var(--text3);
  font-size: 10px;
}

.eb-add-btn {
  background: var(--accentbg);
  border: 1px dashed var(--accent);
  color: var(--accent);
  padding: 10px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  width: 100%;
}

.eb-add-btn.sec {
  border-color: var(--border2);
  color: var(--text2);
  background: none;
}

#eb-add-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
}

.eb-list-view table {
  margin-top: 4px;
}

.eb-list-row {
  cursor: pointer;
}

.eb-list-row:hover {
  background: var(--accentbg);
}

.eb-cover-preview {
  padding: 10px;
  background: var(--bg3);
}

#eb-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

#eb-pages {
  flex: 2;
  min-width: 120px;
  overflow: auto;
  padding: 20px;
  border-right: 1px solid var(--border);
}

#blocks-wrap {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#panel {
  width: 10vw;
  flex: 0 0 auto;
  min-width: 80px;
  background: var(--bg2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#style-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.style-tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text3);
  padding: 7px 0;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.05em;
  cursor: pointer;
  transition: color 0.12s;
  border-bottom: 2px solid transparent;
}

.style-tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.style-section {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.style-section.active {
  display: flex;
}

#styles-list, #rich-styles-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.eb-item {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--r2);
  overflow: hidden;
}

.eb-item-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  cursor: pointer;
  background: var(--bg3);
  border-bottom: 1px solid var(--border);
}

.eb-textarea {
  width: 100%;
  background: var(--bg);
  border: none;
  color: var(--text);
  font-family: 'Noto Nastaliq Urdu', serif;
  font-size: 14px;
  line-height: 2;
  padding: 10px;
  resize: vertical;
  direction: rtl;
  text-align: right;
  outline: none;
}

.btn-save {
  background: var(--accent);
  border: none;
  color: var(--bg);
  padding: 6px 18px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  font-weight: 600;
}

.btn-cancel {
  background: none;
  border: 1px solid var(--border);
  color: var(--text2);
  padding: 6px 14px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
}

#ftb {
  position: fixed;
  bottom: 20px;
  left: 20px;
  z-index: 2147483647;
  display: none;
  flex-direction: column;
  gap: 6px;
}

#ftb.show {
  display: flex;
}

#ftb button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid #e0ddd6;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  font-size: 16px;
  color: #8a8078;
  font-family: 'IBM Plex Mono', monospace;
  transition: all 0.15s;
}

#ftb button:hover {
  background: #c4954a;
  color: #fff;
}

.style-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: var(--r);
  margin-bottom: 2px;
  cursor: default;
}

.style-item:hover {
  background: var(--bg3);
}

.style-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg4);
  border-radius: 3px;
  font-size: 10px;
  color: var(--accent);
  flex-shrink: 0;
}

.style-name {
  flex: 1;
  font-size: 10px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.style-shortcut {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  border-radius: 4px;
  background: #2ecc71;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 0 4px;
  flex-shrink: 0;
  line-height: 1;
  cursor: grab;
}
.style-shortcut:active { cursor: grabbing; }
.style-item.swap-target { outline: 2px solid #c4954a; outline-offset: -2px; border-radius: 6px; }

.gear-btn {
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 11px;
}

.gear-btn:hover {
  color: var(--accent);
  background: var(--accentbg);
}

#panel-header, #panel-header-rich {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-small {
  background: none;
  border: 1px solid var(--border);
  color: var(--text2);
  padding: 3px 8px;
  border-radius: var(--r);
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 9px;
}

.btn-small:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.empty-msg {
  padding: 16px;
  text-align: center;
  color: var(--text3);
  font-size: 10px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text2);
  cursor: pointer;
}

.check-row input {
  accent-color: var(--accent);
}

#book-view {
  display: none;
  flex: 1;
  overflow: hidden;
  background: var(--bg);
}

#book-view.active {
  display: flex;
  flex-direction: column;
}

#startup-dialog {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

#startup-content {
  background: var(--bg2);
  border: 1px solid var(--border2);
  border-radius: 16px;
  padding: 32px;
  max-width: 440px;
  width: 90%;
  text-align: center;
}

#startup-content h2 {
  font-family: 'Amiri', serif;
  font-size: 24px;
  color: var(--accent);
  margin-bottom: 8px;
}

#startup-content p {
  color: var(--text2);
  font-size: 11px;
  margin-bottom: 20px;
}

#library-section {
  margin-bottom: 16px;
}

#library-pick-btn {
  width: 100%;
  padding: 14px;
  font-size: 15px;
  cursor: pointer;
}

#library-books-list {
  max-height: 280px;
  overflow-y: auto;
  margin-bottom: 16px;
  padding: 4px 0;
}

#library-books-list::-webkit-scrollbar {
  width: 3px;
}

#library-books-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.library-book-btn:hover {
  border-color: var(--accent) !important;
  background: var(--accentbg) !important;
}

#library-new-book {
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

#folder-indicator {
  display: none;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  background: var(--accentbg);
  border-radius: var(--r);
  color: var(--accent);
  font-size: 10px;
}