:root {
    color-scheme: light;
    --bg: #f6f7fb;
    --surface: #ffffff;
    --surface-strong: #f0f4f8;
    --ink: #1f2937;
    --muted: #64748b;
    --line: #d8dee8;
    --line-strong: #b8c2d1;
    --primary: #2f6f73;
    --primary-dark: #245a5e;
    --secondary: #8a3f5f;
    --amber: #c9861d;
    --blue: #315d94;
    --danger: #b93838;
    --danger-bg: #fae9e9;
    --success: #23795c;
    --shadow: 0 18px 45px rgba(31, 41, 55, 0.11);
    --shadow-soft: 0 8px 22px rgba(31, 41, 55, 0.08);
    --radius: 8px;
    --font: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --serif: Georgia, "Times New Roman", serif;
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: var(--font);
    background:
        linear-gradient(120deg, rgba(47, 111, 115, 0.08), transparent 32%),
        linear-gradient(230deg, rgba(138, 63, 95, 0.08), transparent 34%),
        var(--bg);
    color: var(--ink);
}

button,
input,
textarea,
select {
    font: inherit;
}

button {
    cursor: pointer;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.global-nav {
    position: fixed;
    inset: 20px 20px auto 20px;
    z-index: 20;
    display: flex;
    justify-content: space-between;
    pointer-events: none;
}

.nav-link {
    pointer-events: auto;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 38px;
    padding: 8px 10px;
    border-radius: var(--radius);
    color: #7b8798;
    text-decoration: none;
    font-weight: 700;
    background: rgba(255, 255, 255, 0.72);
    border: 1px solid rgba(216, 222, 232, 0.78);
    backdrop-filter: blur(12px);
}

.nav-link:hover {
    color: var(--primary);
}

.nav-home {
    width: 38px;
    justify-content: center;
    font-size: 1.2rem;
}

.app-shell {
    min-height: 100vh;
}

.screen {
    min-height: 100vh;
}

.is-hidden {
    display: none !important;
}

.setup-screen {
    display: grid;
    place-items: center;
    padding: 86px 20px 42px;
}

.setup-layout {
    width: min(1120px, 100%);
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(280px, 0.65fr);
    gap: 22px;
    align-items: stretch;
}

.setup-panel,
.setup-outline,
.editor-panel,
.book-card,
.progress-panel,
.modal-card {
    background: rgba(255, 255, 255, 0.92);
    border: 1px solid rgba(216, 222, 232, 0.9);
    box-shadow: var(--shadow-soft);
    backdrop-filter: blur(14px);
}

.setup-panel {
    padding: clamp(24px, 5vw, 46px);
    border-radius: var(--radius);
}

.eyebrow {
    margin: 0 0 8px;
    color: var(--primary);
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

h1,
h2,
h3,
p {
    margin-top: 0;
}

h1 {
    margin-bottom: 26px;
    font-family: var(--serif);
    font-size: clamp(2rem, 5vw, 4.1rem);
    line-height: 1.02;
    letter-spacing: 0;
}

h2 {
    margin-bottom: 8px;
    font-size: 1.15rem;
    line-height: 1.22;
}

.field-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.field {
    display: grid;
    gap: 7px;
}

.field-wide {
    grid-column: 1 / -1;
}

.field span,
.chapter-title-field span,
.chapter-pages-field span {
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

input,
textarea,
select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
    color: var(--ink);
    outline: none;
    transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

input,
select {
    min-height: 46px;
    padding: 10px 12px;
}

textarea {
    padding: 12px;
    resize: vertical;
    line-height: 1.55;
}

input:focus,
textarea:focus,
select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(47, 111, 115, 0.14);
}

.cover-drop,
.image-drop {
    margin-top: 20px;
    min-height: 150px;
    display: grid;
    place-items: center;
    border: 1.5px dashed var(--line-strong);
    border-radius: var(--radius);
    background: var(--surface-strong);
    color: var(--muted);
    text-align: center;
    transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}

.cover-drop:hover,
.image-drop:hover,
.cover-drop.is-dragging,
.image-drop.is-dragging {
    border-color: var(--primary);
    background: #edf7f6;
}

.drop-prompt,
.image-drop {
    gap: 6px;
}

.drop-icon {
    display: inline-grid;
    place-items: center;
    width: 42px;
    height: 42px;
    margin: 0 auto 6px;
    border-radius: var(--radius);
    background: #dfe9f5;
    color: var(--blue);
    font-weight: 900;
}

.cover-preview {
    display: none;
    max-height: 220px;
    max-width: min(320px, 86%);
    border-radius: var(--radius);
    box-shadow: var(--shadow-soft);
    object-fit: cover;
}

.cover-drop.has-image .drop-prompt {
    display: none;
}

.cover-drop.has-image .cover-preview {
    display: block;
}

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

.setup-actions {
    margin-top: 22px;
}

.btn,
.icon-btn {
    min-height: 40px;
    border: 0;
    border-radius: var(--radius);
    font-weight: 800;
    transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease, color 160ms ease;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 14px;
}

.btn:hover,
.icon-btn:hover {
    transform: translateY(-1px);
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 10px 20px rgba(47, 111, 115, 0.22);
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-secondary {
    background: #e9eef5;
    color: var(--blue);
}

.btn-secondary:hover {
    background: #dfe8f3;
}

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

.btn-ghost:hover {
    color: var(--primary);
    border-color: var(--primary);
}

.btn-danger-soft {
    width: 100%;
    margin-top: 14px;
    background: var(--danger-bg);
    color: var(--danger);
}

.icon-btn {
    width: 40px;
    display: inline-grid;
    place-items: center;
    background: #e9eef5;
    color: var(--ink);
}

.icon-btn-filled {
    background: var(--primary);
    color: #fff;
}

.icon-btn-danger {
    background: var(--danger-bg);
    color: var(--danger);
}

.setup-outline {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 28px;
    padding: 30px;
    border-radius: var(--radius);
    background:
        linear-gradient(160deg, rgba(49, 93, 148, 0.1), transparent 46%),
        rgba(255, 255, 255, 0.88);
}

.setup-outline h2 {
    font-family: var(--serif);
    font-size: 2rem;
}

.structure-list {
    margin: 0;
    padding-left: 20px;
    display: grid;
    gap: 14px;
    color: var(--ink);
    font-weight: 700;
}

.mini-note {
    display: grid;
    gap: 5px;
    padding-top: 18px;
    border-top: 1px solid var(--line);
    color: var(--muted);
}

.mini-note strong {
    color: var(--amber);
}

.workspace-screen {
    padding-top: 82px;
}

.app-header {
    width: min(1420px, calc(100% - 40px));
    margin: 0 auto 16px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
}

.book-heading h1 {
    margin: 0;
    font-size: clamp(1.65rem, 4vw, 3.2rem);
}

.save-status {
    color: var(--success);
    font-size: 0.82rem;
    font-weight: 800;
    white-space: nowrap;
}

.save-status.is-saving {
    color: var(--amber);
}

.save-status.is-error {
    color: var(--danger);
}

.workspace-layout {
    width: min(1420px, calc(100% - 40px));
    margin: 0 auto;
    display: grid;
    grid-template-columns: 310px minmax(0, 1fr);
    gap: 16px;
    padding-bottom: 40px;
}

.chapter-sidebar {
    min-width: 0;
}

.editor-area {
    min-width: 0;
}

.book-card {
    display: grid;
    grid-template-columns: 62px minmax(0, 1fr);
    gap: 12px;
    align-items: center;
    padding: 12px;
    border-radius: var(--radius);
}

.book-card strong,
.book-card span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.book-card strong {
    font-family: var(--serif);
    font-size: 1.05rem;
}

.book-card span {
    color: var(--muted);
    font-size: 0.84rem;
}

.mini-cover {
    width: 62px;
    height: 82px;
    display: grid;
    place-items: center;
    overflow: hidden;
    border-radius: var(--radius);
    background: linear-gradient(145deg, #dce8ea, #eef1f6);
    color: var(--primary);
    font-size: 1.6rem;
}

.mini-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.sidebar-title-row {
    margin: 18px 0 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-title-row h2 {
    margin: 0;
}

.chapter-list {
    display: grid;
    gap: 8px;
}

.chapter-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    width: 100%;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.85);
}

.chapter-select {
    min-width: 0;
    display: grid;
    gap: 2px;
    padding: 0;
    border: 0;
    background: transparent;
    text-align: left;
    color: var(--ink);
}

.chapter-select strong,
.chapter-select span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chapter-select strong {
    font-size: 0.92rem;
}

.chapter-select span {
    color: var(--muted);
    font-size: 0.76rem;
}

.chapter-item.is-active {
    border-color: var(--primary);
    box-shadow: inset 4px 0 0 var(--primary);
}

.chapter-actions-mini {
    display: flex;
    gap: 4px;
}

.mini-icon-btn {
    width: 26px;
    height: 26px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 6px;
    background: #eef2f6;
    color: var(--muted);
    font-weight: 900;
}

.mini-icon-btn:hover {
    color: var(--primary);
}

.progress-panel {
    margin-top: 16px;
    padding: 14px;
    border-radius: var(--radius);
}

.progress-topline {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
}

.progress-track {
    height: 9px;
    overflow: hidden;
    border-radius: 999px;
    background: #dfe5ee;
}

.progress-bar {
    width: 0%;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--primary), var(--secondary));
    transition: width 240ms ease;
}

.progress-text {
    margin-top: 9px;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.4;
}

.editor-panel {
    min-height: calc(100vh - 136px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.chapter-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 170px 40px;
    gap: 12px;
    align-items: end;
    padding: 18px;
    border-bottom: 1px solid var(--line);
}

.chapter-title-field,
.chapter-pages-field {
    display: grid;
    gap: 6px;
}

.chapter-title-field input {
    min-height: 52px;
    font-family: var(--serif);
    font-size: clamp(1.35rem, 3vw, 2rem);
    font-weight: 700;
}

.tabbar {
    display: flex;
    gap: 4px;
    padding: 0 18px;
    border-bottom: 1px solid var(--line);
    background: var(--surface-strong);
    overflow-x: auto;
}

.tab-btn {
    min-height: 52px;
    padding: 0 14px;
    border: 0;
    border-bottom: 3px solid transparent;
    background: transparent;
    color: var(--muted);
    font-weight: 850;
    white-space: nowrap;
}

.tab-btn:hover,
.tab-btn.is-active {
    color: var(--primary);
}

.tab-btn.is-active {
    border-bottom-color: var(--primary);
}

.tab-panels {
    flex: 1;
    padding: 22px;
    overflow: auto;
}

.tab-panel {
    display: none;
}

.tab-panel.is-active {
    display: block;
}

.panel-title-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 16px;
}

.panel-title-row p {
    margin: 0;
    color: var(--muted);
    line-height: 1.45;
}

.counter {
    flex: 0 0 auto;
    padding: 7px 10px;
    border-radius: var(--radius);
    background: #eef4f4;
    color: var(--primary);
    font-size: 0.78rem;
    font-weight: 850;
}

.large-textarea {
    min-height: 430px;
    font-size: 1.02rem;
    background: #fff;
}

.item-stack {
    display: grid;
    gap: 14px;
}

.entry-card {
    position: relative;
    display: grid;
    gap: 13px;
    padding: 16px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
}

.entry-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 13px;
}

.entry-grid .field-wide {
    grid-column: 1 / -1;
}

.remove-entry {
    position: absolute;
    top: 10px;
    right: 10px;
}

.empty-state {
    padding: 28px;
    border: 1.5px dashed var(--line-strong);
    border-radius: var(--radius);
    background: #f7f9fb;
    color: var(--muted);
    text-align: center;
}

.image-drop {
    min-height: 122px;
    margin-bottom: 16px;
}

.image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 14px;
}

.image-card {
    display: grid;
    gap: 10px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
}

.image-card.is-dragging {
    opacity: 0.48;
}

.image-card img {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    border-radius: 6px;
    background: var(--surface-strong);
}

.image-actions {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

.image-actions div {
    display: flex;
    gap: 4px;
}

.toast {
    position: fixed;
    left: 50%;
    bottom: 22px;
    z-index: 100;
    max-width: min(520px, calc(100vw - 36px));
    padding: 12px 14px;
    border-radius: var(--radius);
    background: #1f2937;
    color: #fff;
    box-shadow: var(--shadow);
    transform: translate(-50%, 20px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease, transform 180ms ease;
}

.toast.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(15, 23, 42, 0.45);
}

.modal-card {
    width: min(680px, 100%);
    padding: 22px;
    border-radius: var(--radius);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 18px;
}

.modal-header h2 {
    margin: 0;
    font-family: var(--serif);
    font-size: 1.7rem;
}

.modal-actions {
    justify-content: flex-end;
    margin-top: 18px;
}

.print-root {
    display: none;
}

@media (max-width: 1000px) {
    .setup-layout,
    .workspace-layout {
        grid-template-columns: minmax(0, 1fr);
    }

    .chapter-sidebar {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(220px, 0.7fr);
        gap: 12px;
        align-items: start;
    }

    .sidebar-title-row,
    .chapter-list,
    .btn-danger-soft {
        grid-column: 1 / -1;
    }

    .app-header {
        align-items: flex-start;
        flex-direction: column;
    }
}

@media (max-width: 720px) {
    .global-nav {
        inset: 12px 12px auto 12px;
    }

    .nav-link:not(.nav-home) span:last-child {
        display: none;
    }

    .setup-screen,
    .workspace-screen {
        padding-top: 70px;
    }

    .workspace-layout,
    .app-header {
        width: min(100% - 22px, 1420px);
    }

    .field-grid,
    .entry-grid,
    .chapter-header,
    .chapter-sidebar {
        grid-template-columns: 1fr;
    }

    .field-wide,
    .entry-grid .field-wide {
        grid-column: auto;
    }

    .chapter-header {
        align-items: stretch;
    }

    .header-actions {
        width: 100%;
    }

    .header-actions .btn-primary,
    .header-actions .btn-secondary {
        flex: 1 1 145px;
    }

    .tab-panels {
        padding: 16px;
    }

    .panel-title-row {
        flex-direction: column;
    }

    .large-textarea {
        min-height: 320px;
    }
}

@media print {
    @page {
        size: A4;
        margin: 16mm 14mm 18mm;
    }

    body {
        background: #fff !important;
        color: #111827;
    }

    .global-nav,
    .app-shell,
    .modal,
    .toast {
        display: none !important;
    }

    .print-root {
        display: block;
    }

    .print-page {
        page-break-after: always;
        break-after: page;
    }

    .print-page:last-child {
        page-break-after: auto;
        break-after: auto;
    }

    .print-cover {
        min-height: 245mm;
        display: grid;
        align-content: center;
        justify-items: center;
        text-align: center;
        gap: 16px;
    }

    .print-cover h1 {
        max-width: 165mm;
        font-family: Georgia, "Times New Roman", serif;
        font-size: 34pt;
        line-height: 1.08;
        margin: 0;
    }

    .print-cover p {
        margin: 0;
        font-size: 12pt;
    }

    .print-cover-image {
        width: 62mm;
        height: 86mm;
        object-fit: cover;
        border: 0.5pt solid #d1d5db;
        border-radius: 3mm;
    }

    .print-toc h2,
    .print-chapter h2 {
        font-family: Georgia, "Times New Roman", serif;
        font-size: 24pt;
    }

    .print-toc ol {
        padding-left: 0;
        list-style: none;
        display: grid;
        gap: 6mm;
    }

    .print-toc a {
        color: #111827;
        text-decoration: none;
        display: flex;
        gap: 4mm;
    }

    .print-toc .toc-line {
        flex: 1;
        border-bottom: 0.5pt dotted #9ca3af;
        transform: translateY(-2mm);
    }

    .print-chapter {
        font-size: 11pt;
        line-height: 1.55;
    }

    .print-chapter h2 {
        margin-bottom: 3mm;
    }

    .print-section {
        break-inside: avoid;
        page-break-inside: avoid;
        margin-top: 7mm;
    }

    .print-section h3 {
        margin: 0 0 2mm;
        font-size: 13pt;
    }

    .print-text p {
        margin: 0 0 2.5mm;
    }

    .print-character,
    .print-task {
        break-inside: avoid;
        page-break-inside: avoid;
        margin: 0 0 4mm;
        padding-left: 4mm;
        border-left: 1.5pt solid #2f6f73;
    }

    .print-quote {
        margin: 2mm 0;
        padding: 2mm 3mm;
        background: #f3f4f6;
        font-style: italic;
    }

    .print-images {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 5mm;
    }

    .print-image {
        break-inside: avoid;
        page-break-inside: avoid;
    }

    .print-image img {
        width: 100%;
        max-height: 95mm;
        object-fit: contain;
        border: 0.5pt solid #d1d5db;
    }

    .print-caption {
        margin-top: 1mm;
        color: #4b5563;
        font-size: 9pt;
    }
}
