:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #0f4c81;
  --accent-2: #1d7bb8;
  --dark: #0b1220;
  --success: #16794a;
  --warn: #a56600;
  --danger: #b42318;
  --shadow: 0 12px 34px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--ink); background: var(--bg); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 16px 24px; background: var(--dark); color: white; position: sticky; top: 0; z-index: 20; box-shadow: 0 8px 24px rgba(0,0,0,.18); }
.brand { display: flex; gap: 12px; align-items: center; }
.brand-mark { width: 48px; height: 48px; border-radius: 14px; display: grid; place-items: center; font-weight: 900; letter-spacing: .05em; background: linear-gradient(135deg, #2a87c8, #0f4c81); }
.brand h1 { margin: 0; font-size: 1.15rem; }
.brand p { margin: 3px 0 0; color: #cbd5e1; font-size: .78rem; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.btn { border: 0; border-radius: 10px; padding: 10px 14px; font-weight: 750; transition: .18s ease; }
.btn:hover { transform: translateY(-1px); }
.btn.primary { color: white; background: linear-gradient(135deg, var(--accent-2), var(--accent)); }
.btn.secondary { color: var(--ink); background: #fff; border: 1px solid var(--line); }
.topbar .btn.secondary { color: white; background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.18); }
.btn.small { padding: 8px 10px; font-size: .86rem; }
.btn.wide { width: 100%; }

.tabs { display: flex; gap: 6px; padding: 10px 24px; overflow-x: auto; background: white; border-bottom: 1px solid var(--line); position: sticky; top: 80px; z-index: 15; }
.tab { white-space: nowrap; border: 0; background: transparent; color: var(--muted); border-radius: 8px; padding: 10px 14px; font-weight: 800; }
.tab.active { color: var(--accent); background: #eaf4fb; }
main { padding: 24px; }
.view { display: none; }
.view.active { display: block; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); }

.hero-panel { min-height: 330px; display: grid; grid-template-columns: 1.4fr .6fr; gap: 24px; align-items: center; padding: 42px; color: white; border-radius: 24px; background: radial-gradient(circle at top right, rgba(49,130,206,.5), transparent 30%), linear-gradient(135deg, #111827, #0f4c81); box-shadow: var(--shadow); overflow: hidden; }
.hero-panel h2 { max-width: 780px; font-size: clamp(2rem, 4vw, 4rem); line-height: 1.02; margin: 10px 0 18px; }
.hero-panel p { max-width: 740px; color: #dbeafe; font-size: 1.05rem; line-height: 1.7; }
.eyebrow { color: #7dd3fc; font-weight: 900; font-size: .76rem; letter-spacing: .14em; }
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 24px; }
.book-stack { position: relative; height: 260px; }
.book { position: absolute; width: 160px; height: 230px; border-radius: 8px 18px 18px 8px; display: flex; align-items: flex-end; justify-content: center; padding: 22px; font-weight: 900; letter-spacing: .2em; box-shadow: 0 24px 50px rgba(0,0,0,.28); }
.b1 { right: 105px; top: 30px; transform: rotate(-12deg); background: #f8fafc; color: #0f4c81; }
.b2 { right: 40px; top: 10px; transform: rotate(6deg); background: #1d7bb8; }
.b3 { right: 0; top: 40px; transform: rotate(14deg); background: #0b1220; border: 1px solid rgba(255,255,255,.25); }

.dashboard-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-top: 18px; }
.stats-card { padding: 20px; }
.stats-card span, .stats-card small { display: block; color: var(--muted); }
.stats-card strong { display: block; font-size: 1.35rem; margin: 8px 0; }
.recent-panel { margin-top: 18px; padding: 22px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.panel-head h3 { margin: 4px 0 0; }
.project-card { display: flex; gap: 22px; margin-top: 18px; align-items: center; padding: 18px; background: #f8fafc; border-radius: 16px; border: 1px solid var(--line); }
.mini-cover { width: 110px; aspect-ratio: 2/3; border-radius: 6px 12px 12px 6px; color: white; display: grid; place-items: center; font-weight: 900; background: linear-gradient(145deg, #111827, #1d7bb8); box-shadow: 0 14px 30px rgba(15, 23, 42, .18); }
.project-meta h4 { font-size: 1.35rem; margin: 0; }
.project-meta p { color: var(--muted); }
.meta-row { display: flex; flex-wrap: wrap; gap: 8px; }
.meta-row span { background: white; border: 1px solid var(--line); border-radius: 999px; padding: 6px 10px; font-size: .84rem; }

.workspace { display: grid; min-height: calc(100vh - 150px); }
.editor-layout { grid-template-columns: 260px minmax(420px, 1fr) 330px; gap: 16px; }
.sidebar { background: white; border: 1px solid var(--line); border-radius: 16px; padding: 16px; box-shadow: var(--shadow); overflow: auto; max-height: calc(100vh - 160px); position: sticky; top: 145px; }
.sidebar h3 { margin-top: 0; }
.sidebar label, .cover-controls label, .reader-toolbar label { display: grid; gap: 6px; margin: 12px 0; color: #334155; font-size: .84rem; font-weight: 750; }
input, select, textarea { width: 100%; border: 1px solid #cbd5e1; border-radius: 9px; background: white; padding: 10px; color: var(--ink); }
textarea { resize: vertical; line-height: 1.45; }
.two-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.chapter-list { display: grid; gap: 8px; margin-top: 14px; }
.chapter-item { text-align: left; border: 1px solid var(--line); background: #f8fafc; border-radius: 10px; padding: 10px; font-weight: 700; }
.chapter-item.active { border-color: #60a5fa; background: #eff6ff; color: #0f4c81; }
.canvas-area { min-width: 0; }
.page-toolbar { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; background: white; border: 1px solid var(--line); border-radius: 14px; margin-bottom: 14px; }
.icon-btn { border: 1px solid var(--line); background: white; border-radius: 8px; padding: 7px 10px; font-weight: 850; }
.page-stage { min-height: 760px; display: grid; place-items: start center; overflow: auto; padding: 40px; border-radius: 16px; background: #d6d8dd; box-shadow: inset 0 0 28px rgba(15,23,42,.12); }
.print-page { width: 620px; min-height: 930px; position: relative; background: #fff; box-shadow: 0 22px 50px rgba(15,23,42,.2); transform-origin: top center; transition: transform .2s; }
.running-head { position: absolute; top: 25px; left: 55px; right: 55px; text-align: center; color: #8a8f98; font-size: 9px; letter-spacing: .14em; border-bottom: 1px solid #e5e7eb; padding-bottom: 8px; }
.page-content { padding: 95px 74px 80px; }
.page-content h1 { font-family: Georgia, serif; text-align: center; margin: 0; font-size: 34px; }
.page-content .subtitle { text-align: center; color: #64748b; font-style: italic; font-size: 17px; }
.page-content .author { text-align: center; font-weight: bold; margin: 28px 0 55px; }
.preview-body { font-family: Georgia, serif; line-height: 1.65; text-align: justify; white-space: pre-wrap; }
.preview-body h2 { text-align: center; margin-top: 44px; page-break-before: always; }
.page-number { position: absolute; bottom: 25px; left: 0; right: 0; text-align: center; font-family: Georgia, serif; }

.reader-toolbar { padding: 14px 18px; display: flex; justify-content: space-between; gap: 18px; align-items: end; }
.toolbar-group { display: flex; gap: 12px; align-items: end; flex-wrap: wrap; }
.toolbar-group.compact { align-items: center; }
.reader-toolbar label { margin: 0; min-width: 160px; }
.reader-layout { display: grid; grid-template-columns: 260px 1fr; gap: 16px; margin-top: 16px; }
.toc-panel { padding: 16px; max-height: calc(100vh - 260px); overflow: auto; }
.toc-link { display: block; width: 100%; text-align: left; border: 0; background: transparent; border-radius: 8px; padding: 9px 10px; color: var(--ink); }
.toc-link:hover, .toc-link.active { background: #eaf4fb; color: var(--accent); }
.reader-stage { min-height: 700px; border-radius: 18px; display: grid; place-items: start center; padding: 34px; background: linear-gradient(160deg, #cfd4db, #9aa5b1); overflow: auto; }
.reader-device { display: flex; flex-direction: column; width: 520px; height: 720px; border: 14px solid #1f2937; border-radius: 28px; background: #fff; box-shadow: 0 25px 60px rgba(0,0,0,.3); transition: .25s; }
.reader-device.tablet { width: 700px; height: 820px; }
.reader-device.phone { width: 350px; height: 720px; border-radius: 36px; }
.reader-device.desktop { width: 900px; height: 650px; border-radius: 14px; }
.reader-device.print { width: 620px; min-height: 900px; height: auto; border: 0; border-radius: 2px; }
.reader-topline, .reader-footer { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 12px 18px; font-size: 12px; opacity: .75; }
.reader-topline { border-bottom: 1px solid rgba(100,116,139,.25); }
.reader-footer { border-top: 1px solid rgba(100,116,139,.25); }
.reader-content { flex: 1; overflow: auto; padding: 35px 42px; font-family: Georgia, serif; line-height: 1.7; font-size: 18px; scroll-behavior: smooth; }
.reader-content h1, .reader-content h2 { text-align: center; }
.reader-content p { text-align: justify; }
.reader-nav { border: 0; background: transparent; font-weight: 700; color: inherit; }
.theme-day { background: #ffffff; color: #161616; }
.theme-sepia { background: #f4ecd8; color: #3b2f21; }
.theme-night { background: #111827; color: #eef2ff; border-color: #030712; }
.theme-contrast { background: #000; color: #fff; border-color: #fff; }

.cover-layout { display: grid; grid-template-columns: 340px 1fr; gap: 16px; }
.cover-controls { padding: 18px; }
.notice { padding: 12px; border-radius: 10px; background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412; font-size: .86rem; line-height: 1.5; }
.cover-stage { padding: 20px; overflow: auto; }
.cover-spread { min-width: 960px; height: 610px; display: grid; grid-template-columns: 1fr 28px 1fr; box-shadow: 0 24px 50px rgba(15,23,42,.22); }
.cover-panel { color: white; position: relative; overflow: hidden; }
.front-cover { background: radial-gradient(circle at 20% 20%, rgba(73,190,255,.35), transparent 24%), linear-gradient(145deg, #0b1220, #0f4c81 68%, #1d7bb8); }
.back-cover { background: linear-gradient(145deg, #0b1220, #102a43); }
.cover-inner { padding: 58px 48px; height: 100%; position: relative; z-index: 2; }
.front-content { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.cover-brand { position: absolute; top: 42px; font-size: .72rem; letter-spacing: .18em; }
.front-content h2 { font-family: Georgia, serif; font-size: 3.3rem; line-height: 1.02; margin: 0; }
.front-content p { font-size: 1.1rem; color: #dbeafe; }
.front-content strong { position: absolute; bottom: 50px; letter-spacing: .16em; }
.cover-decoration { position: absolute; inset: 22px; border: 1px solid rgba(255,255,255,.28); }
.back-cover p { line-height: 1.7; color: #dbeafe; }
.barcode-placeholder { position: absolute; bottom: 44px; right: 44px; width: 160px; height: 90px; display: grid; place-items: center; color: #111827; background: white; font-size: .7rem; }
.spine { background: #082f49; color: white; display: grid; place-items: center; overflow: hidden; }
.spine span { white-space: nowrap; writing-mode: vertical-rl; transform: rotate(180deg); font-size: .72rem; letter-spacing: .1em; }
.cover-data { display: flex; justify-content: space-between; margin-top: 14px; color: var(--muted); }

.check-layout { display: grid; grid-template-columns: 330px 1fr; gap: 16px; }
.score-panel { padding: 30px; text-align: center; }
.score-ring { width: 180px; height: 180px; margin: 0 auto 20px; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(var(--accent) 0deg, #e5e7eb 0deg); position: relative; }
.score-ring::after { content: ""; position: absolute; inset: 16px; background: white; border-radius: 50%; }
.score-ring span { position: relative; z-index: 1; font-weight: 900; font-size: 2.2rem; }
.checklist-panel { padding: 22px; }
.check-item { display: grid; grid-template-columns: 32px 1fr auto; gap: 12px; align-items: center; padding: 13px 0; border-bottom: 1px solid var(--line); }
.check-status { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; color: white; font-weight: 900; }
.check-status.ok { background: var(--success); }
.check-status.warn { background: var(--warn); }
.check-status.error { background: var(--danger); }
.check-label strong { display: block; }
.check-label small { color: var(--muted); }
.check-badge { border-radius: 999px; padding: 5px 8px; font-size: .72rem; font-weight: 800; }
.check-badge.ok { color: var(--success); background: #ecfdf3; }
.check-badge.warn { color: var(--warn); background: #fff7e6; }
.check-badge.error { color: var(--danger); background: #fff1f0; }

.export-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 16px; }
.export-card { padding: 26px; }
.export-icon { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 18px; background: #eaf4fb; color: var(--accent); font-weight: 900; }
.export-card h3 { font-size: 1.45rem; }
.export-card p { min-height: 52px; color: var(--muted); line-height: 1.6; }

.toast { position: fixed; right: 24px; bottom: 24px; background: #111827; color: white; padding: 12px 16px; border-radius: 10px; box-shadow: var(--shadow); opacity: 0; transform: translateY(12px); pointer-events: none; transition: .25s; z-index: 100; }
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1180px) {
  .editor-layout { grid-template-columns: 220px 1fr; }
  .right-sidebar { grid-column: 1 / -1; position: static; max-height: none; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .right-sidebar h3, .right-sidebar > label:last-child { grid-column: 1 / -1; }
  .dashboard-grid { grid-template-columns: repeat(2,1fr); }
  .hero-panel { grid-template-columns: 1fr; }
  .book-stack { display: none; }
}

@media (max-width: 820px) {
  .topbar { align-items: flex-start; flex-direction: column; position: static; }
  .tabs { top: 0; }
  main { padding: 12px; }
  .hero-panel { padding: 28px 22px; }
  .dashboard-grid, .export-grid, .check-layout, .cover-layout, .reader-layout { grid-template-columns: 1fr; }
  .editor-layout { grid-template-columns: 1fr; }
  .sidebar { position: static; max-height: none; }
  .left-sidebar { order: 2; }
  .right-sidebar { order: 3; display: block; }
  .page-stage { padding: 18px; min-height: 0; }
  .print-page { width: 100%; min-height: 720px; }
  .page-content { padding: 80px 34px 60px; }
  .reader-toolbar { align-items: stretch; flex-direction: column; }
  .reader-stage { padding: 14px; }
  .reader-device, .reader-device.tablet, .reader-device.desktop, .reader-device.phone { width: 100%; height: 72vh; border-width: 8px; border-radius: 18px; }
  .toc-panel { max-height: none; }
  .cover-stage { padding: 8px; }
  .dashboard-grid { grid-template-columns: 1fr; }
  .project-card { align-items: flex-start; }
  .cover-data { flex-direction: column; gap: 6px; }
}

@media print {
  body { background: white; }
  .topbar, .tabs, .sidebar, .page-toolbar, .toast, #view-library, #view-reader, #view-cover, #view-check, #view-export { display: none !important; }
  main, #view-editor, .workspace, .canvas-area, .page-stage { display: block !important; padding: 0; margin: 0; background: white; box-shadow: none; }
  .print-page { width: 100%; min-height: auto; box-shadow: none; transform: none !important; }
}
