@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  --paper: #f0eee9;
  --paper-soft: rgba(240, 238, 233, 0.72);
  --ink: #1a1612;
  --muted: rgba(26, 22, 18, 0.62);
  --line: rgba(26, 22, 18, 0.14);
  --accent: #ff4a2e;
  --accent-soft: rgba(255, 74, 46, 0.12);
  --header-h: 56px;
  --toolbar-h: 44px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: Inter, system-ui, sans-serif;
  font-size: 13px;
  color: var(--ink);
  background:
    radial-gradient(circle at 14% 0%, rgba(240, 238, 233, 0.76), transparent 34%),
    linear-gradient(180deg, var(--paper) 0%, var(--paper-soft) 100%);
  -webkit-font-smoothing: antialiased;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(26, 22, 18, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26, 22, 18, 0.03) 1px, transparent 1px);
  background-size: 56px 56px;
}

button { font: inherit; cursor: pointer; }

#file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.paste-panel {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.45);
}

.paste-panel[hidden] { display: none; }

.mode-tabs {
  display: inline-flex;
  flex-shrink: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.55);
}

.mode-tab {
  border: none;
  background: transparent;
  padding: 5px 14px;
  font-weight: 600;
  font-size: 12px;
  color: var(--muted);
}

.mode-tab.active {
  background: #fff;
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--line);
}

.mode-tab:hover:not(.active) {
  color: var(--ink);
}

.editor-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.35);
}

.editor-body {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 220px;
  min-height: 0;
}

#image-input,
#chapter-art-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.editor-pane[hidden] { display: none; }

.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.45);
}

.editor-hint {
  font-size: 11px;
  color: var(--muted);
}

.editor-hint kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: #fff;
}

#manuscript-editor {
  flex: 1;
  width: 100%;
  min-height: 0;
  padding: 16px 18px;
  border: none;
  resize: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  line-height: 1.55;
  background: #fff;
  color: var(--ink);
  outline: none;
}

.illustrations-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-left: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.5);
  min-height: 0;
  overflow: hidden;
}

.illustrations-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.illustrations-head strong {
  font-size: 12px;
}

.btn.small {
  padding: 4px 8px;
  font-size: 11px;
}

.chapter-art-label {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.chapter-art-select {
  width: 100%;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  font-size: 11px;
  background: #fff;
}

.illustrations-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.illustrations-list li button {
  display: block;
  width: 100%;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
  cursor: pointer;
}

.illustrations-list li button:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent-soft);
}

.illustrations-list img {
  display: block;
  width: 100%;
  height: 72px;
  object-fit: cover;
  background: var(--paper);
}

.illustrations-list .caption {
  display: block;
  padding: 4px 6px;
  font-size: 10px;
  color: var(--muted);
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.illustrations-hint {
  margin: 0;
  font-size: 10px;
  color: var(--muted);
  line-height: 1.35;
}

.illustrations-empty {
  font-size: 11px;
  color: var(--muted);
  margin: 0;
}

.preview-pane {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 220px;
  gap: 0;
  min-height: 0;
}

.preview-pane[hidden] { display: none; }

.paste-panel textarea {
  flex: 1;
  min-height: 120px;
  max-height: 200px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.45;
  resize: vertical;
  background: #fff;
  color: var(--ink);
}

.paste-panel .btn.primary {
  align-self: flex-end;
  white-space: nowrap;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  position: relative;
  z-index: 1;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: var(--header-h);
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: var(--paper-soft);
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--accent);
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-text strong { font-size: 14px; }
.brand-text span {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--toolbar-h);
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.35);
}

.sep {
  width: 1px;
  height: 20px;
  background: var(--line);
  margin: 0 4px;
}

.grow { flex: 1; }

.toolbar-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.toolbar-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 500;
}

.toolbar-select {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 8px;
  font: inherit;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.75);
  color: var(--ink);
  max-width: 148px;
}

.toolbar-select-narrow {
  max-width: 72px;
}

.toolbar-select:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.65);
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 12px;
  font-weight: 500;
  transition: background 0.15s;
}

.btn:hover:not(:disabled) { background: #fff; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

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

.btn.primary:hover:not(:disabled) { filter: brightness(1.05); }

.btn.icon {
  width: 32px;
  padding: 4px 0;
  font-size: 18px;
  line-height: 1;
}

.chip {
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.55);
  border: 1px solid var(--line);
  display: inline-block;
  max-width: min(220px, 42vw);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: middle;
}

.chip.muted { color: var(--muted); }

.chip.saving {
  color: #b54708;
  border-color: rgba(181, 71, 8, 0.35);
  background: rgba(181, 71, 8, 0.08);
}

.chip.saved {
  color: #067647;
  border-color: rgba(6, 118, 71, 0.35);
  background: rgba(6, 118, 71, 0.08);
}

.more-menu {
  position: relative;
  display: inline-block;
}

.more-menu summary {
  list-style: none;
}

.more-menu summary::-webkit-details-marker { display: none; }

.more-menu[open] .more-trigger {
  background: #fff;
  border-color: var(--accent);
}

.more-menu-panel {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  min-width: 140px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(26, 22, 18, 0.12);
}

.more-menu-panel .btn {
  width: 100%;
  text-align: left;
}

.spread-label {
  font-size: 12px;
  font-weight: 500;
  min-width: 120px;
  text-align: center;
}

.stage {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
  position: relative;
}

.stage.drag-over .preview-wrap {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
  background: rgba(255, 74, 46, 0.04);
}

.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  color: var(--accent);
  background: rgba(240, 238, 233, 0.88);
  pointer-events: none;
}

.drop-overlay[hidden] { display: none; }

.preview-wrap {
  position: relative;
  overflow: auto;
  padding: 24px 16px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 400px;
}

.loader {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  background: rgba(240, 238, 233, 0.85);
  z-index: 2;
}

.loader[hidden] { display: none; }

.preview-wrap:has(.pdf-page) .loader:not(.loader-error),
.preview-wrap:has(.page-frame) .loader:not(.loader-error) {
  display: none;
}

.loader .spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

#loader-msg {
  animation: none;
  border: none;
  width: auto;
  height: auto;
}

.loader .err { color: #b42318; animation: none; border: none; width: auto; height: auto; }

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

.spread-viewport {
  width: 100%;
  max-width: 980px;
}

#book-root {
  display: flex;
  justify-content: center;
}

#book-root .pdf-pages {
  display: flex;
  flex-wrap: nowrap;
  gap: 28px;
  justify-content: center;
  align-items: flex-start;
}

#book-root .pdf-page {
  box-shadow: 0 12px 40px rgba(26, 22, 18, 0.12);
  border-radius: 2px;
  flex-shrink: 0;
  background: #f0eee9;
  outline: 1px solid rgba(26, 22, 18, 0.08);
  line-height: 0;
}

#book-root .pdf-page canvas {
  display: block;
}

.sidebar {
  border-left: 1px solid var(--line);
  padding: 16px;
  background: rgba(255, 255, 255, 0.28);
  overflow: auto;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.stats div {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.5);
  text-align: center;
}

.stats b {
  display: block;
  font-size: 18px;
  margin-bottom: 2px;
}

.stats small {
  color: var(--muted);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.typography-panel {
  margin-bottom: 14px;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.5);
}

.typography-head {
  margin-bottom: 6px;
}

.typography-head strong {
  font-size: 12px;
}

.typography-current {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
}

.typography-hint {
  margin: 0;
  font-size: 11px;
}

.hint {
  font-size: 11px;
  line-height: 1.5;
  color: var(--muted);
  margin: 0;
}

.welcome-hint {
  margin: 0;
  padding: 8px 16px;
  text-align: center;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.35);
}

.welcome-hint[hidden] { display: none; }

.preflight-panel {
  margin-top: 4px;
}

.preflight-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.preflight-head strong {
  font-size: 12px;
}

.preflight-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.55);
  color: var(--muted);
}

.preflight-badge.pass {
  color: #067647;
  border-color: rgba(6, 118, 71, 0.35);
  background: rgba(6, 118, 71, 0.08);
}

.preflight-badge.fail {
  color: #b42318;
  border-color: rgba(180, 35, 24, 0.35);
  background: rgba(180, 35, 24, 0.08);
}

.preflight-badge.warn {
  color: #b54708;
  border-color: rgba(181, 71, 8, 0.35);
  background: rgba(181, 71, 8, 0.08);
}

.preflight-item.warn { border-left: 3px solid #b54708; }
.preflight-item.warn .pf-icon { color: #b54708; }

.preflight-badge.warn {
  color: #b54708;
  border-color: rgba(181, 71, 8, 0.35);
  background: rgba(181, 71, 8, 0.08);
}

.preflight-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.preflight-item {
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.5);
  font-size: 11px;
  line-height: 1.4;
}

.preflight-item.pass { border-left: 3px solid #067647; }
.preflight-item.warn { border-left: 3px solid #b54708; }
.preflight-item.fail { border-left: 3px solid #b42318; }

.preflight-item .pf-icon {
  flex-shrink: 0;
  width: 18px;
  font-size: 12px;
  line-height: 1.3;
}

.preflight-item.pass .pf-icon { color: #067647; }
.preflight-item.warn .pf-icon { color: #b54708; }
.preflight-item.fail .pf-icon { color: #b42318; }

.preflight-item strong {
  display: block;
  font-size: 11px;
  margin-bottom: 2px;
}

.preflight-item p {
  margin: 0;
  color: var(--muted);
}

.preflight-item small {
  display: block;
  margin-top: 4px;
  color: #b42318;
  font-size: 10px;
}

.preflight-item.warn small {
  color: #b54708;
}

.preflight-item cite.pf-ref {
  display: block;
  margin-top: 3px;
  font-size: 9px;
  font-style: normal;
  color: var(--muted);
  opacity: 0.85;
}

.preflight-loading,
.preflight-empty {
  font-size: 11px;
  color: var(--muted);
  padding: 8px 0;
}

/* ── Edit in book mode ── */
.edit-measure-host {
  position: absolute;
  left: -9999px;
  top: 0;
  visibility: hidden;
  pointer-events: none;
}

.edit-root .edit-pages,
#book-root .edit-pages {
  display: flex;
  flex-wrap: nowrap;
  gap: 28px;
  justify-content: center;
  align-items: flex-start;
}

.page-frame {
  position: relative;
  box-shadow: 0 12px 40px rgba(26, 22, 18, 0.12);
  border-radius: 2px;
  flex-shrink: 0;
  background: #f0eee9;
  outline: 1px solid rgba(26, 22, 18, 0.08);
  overflow: hidden;
}

.page-frame .page-inner {
  width: 432px;
  height: 648px;
  overflow: hidden;
  position: relative;
}

.edit-page-inner .edit-page-content {
  padding: 52px 45px 59px 52px;
  height: 100%;
  overflow: hidden;
  box-sizing: border-box;
}

.edit-page-content [data-editable] {
  outline: 2px solid transparent;
  outline-offset: 2px;
  border-radius: 2px;
  transition: outline-color 0.15s;
  cursor: text;
}

.edit-page-content [data-editable]:hover {
  outline-color: rgba(59, 130, 246, 0.55);
}

.edit-page-content [data-editable]:focus,
.edit-page-content [data-editable].editing {
  outline-color: #3b82f6;
  background: rgba(59, 130, 246, 0.06);
}

.edit-page-content figure img[data-replaceable] {
  cursor: grab;
  outline: 2px dashed transparent;
  outline-offset: 4px;
  transition: outline-color 0.15s;
}

.edit-page-content figure:hover img[data-replaceable],
.edit-page-content figure.image-dragging img[data-replaceable] {
  outline-color: rgba(59, 130, 246, 0.65);
}

.edit-page-content figure.image-dragging img[data-replaceable] {
  cursor: grabbing;
  opacity: 0.35;
}

.edit-page-content figure.align-left {
  float: left;
  width: 46%;
  max-width: 46%;
  margin: 0 0.9em 0.5em 0;
  text-align: left;
}

.edit-page-content figure.align-right {
  float: right;
  width: 46%;
  max-width: 46%;
  margin: 0 0 0.5em 0.9em;
  text-align: right;
}

.edit-page-content figure.align-center {
  clear: both;
  text-align: center;
}

.edit-page-content figure.align-left img,
.edit-page-content figure.align-right img {
  margin: 0;
}

.edit-page-content figure.figure-selected img[data-replaceable] {
  outline-color: #3b82f6;
}

.edit-page-content figure[data-width] img {
  width: 100%;
  max-width: 100%;
}

.figure-handles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.edit-page-content figure {
  position: relative;
}

.figure-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #fff;
  border: 2px solid #3b82f6;
  border-radius: 1px;
  pointer-events: auto;
  touch-action: none;
  box-shadow: 0 1px 3px rgba(26, 22, 18, 0.2);
}

.figure-handle.nw { top: -4px; left: -4px; cursor: nwse-resize; }
.figure-handle.ne { top: -4px; right: -4px; cursor: nesw-resize; }
.figure-handle.sw { bottom: -4px; left: -4px; cursor: nesw-resize; }
.figure-handle.se { bottom: -4px; right: -4px; cursor: nwse-resize; }

.figure-replace-btn {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font-size: 13px;
  line-height: 1;
  padding: 0;
  pointer-events: auto;
  box-shadow: 0 2px 6px rgba(26, 22, 18, 0.12);
  z-index: 3;
}

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

.image-drop-zones {
  position: fixed;
  z-index: 9998;
  pointer-events: none;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0;
}

.image-drop-zones[hidden] {
  display: none;
}

.image-drop-zones .drop-zone {
  border: 2px dashed rgba(59, 130, 246, 0.25);
  border-radius: 4px;
  background: rgba(59, 130, 246, 0.03);
  transition: background 0.12s, border-color 0.12s;
}

.image-drop-zones .drop-zone.active {
  border-color: rgba(59, 130, 246, 0.75);
  background: rgba(59, 130, 246, 0.1);
}

.image-drop-zones .zone-left.active::after,
.image-drop-zones .zone-center.active::after,
.image-drop-zones .zone-right.active::after {
  content: attr(data-zone);
  display: block;
  text-align: center;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: rgba(59, 130, 246, 0.85);
  padding-top: 6px;
}

.image-drop-zones .zone-left { grid-column: 1; }
.image-drop-zones .zone-center { grid-column: 2; }
.image-drop-zones .zone-right { grid-column: 3; }

.image-drag-ghost {
  position: fixed;
  z-index: 10000;
  pointer-events: none;
  opacity: 0.72;
  box-shadow: 0 8px 24px rgba(26, 22, 18, 0.18);
  outline: 2px dashed #3b82f6;
  outline-offset: 4px;
}

.image-drag-ghost img {
  display: block;
  max-width: 100%;
  height: auto;
}

.image-drop-indicator {
  position: fixed;
  z-index: 9999;
  height: 3px;
  margin-top: -1px;
  background: #3b82f6;
  border-radius: 2px;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.25);
  pointer-events: none;
}

.page-insert-btn {
  position: absolute;
  right: -14px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--accent);
  font-size: 18px;
  font-weight: 600;
  line-height: 1;
  padding: 0;
  opacity: 0;
  transition: opacity 0.15s, box-shadow 0.15s, border-color 0.15s;
  box-shadow: 0 2px 8px rgba(26, 22, 18, 0.1);
  z-index: 3;
}

.page-frame:hover .page-insert-btn,
.page-insert-btn:focus-visible {
  opacity: 1;
}

.page-insert-btn:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
  background: var(--accent-soft);
}

@media (max-width: 900px) {
  .toolbar {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }
  .toolbar .btn,
  .toolbar .sep,
  .toolbar .spread-label,
  .toolbar .chip {
    flex-shrink: 0;
  }
  .preview-pane { grid-template-columns: 1fr; }
  .sidebar {
    display: block;
    border-left: none;
    border-top: 1px solid var(--line);
    max-height: 220px;
  }
  #book-root .edit-pages { flex-direction: column; align-items: center; }
  #book-root .pagedjs_pages { flex-direction: column; align-items: center; }
}
