:root {
  --ink: #1b1b1f;
  --bg: #fff6d6;
  --card: #fff;
  --accent: #ff5a36;
  --blue: #2f6bff;
  --page-w: 4.25in;
  --page-h: 5.5in;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; font-family: "Comic Sans MS", "Chalkboard SE", system-ui, sans-serif; background: var(--bg); color: var(--ink); }
button { font: inherit; padding: .45rem .9rem; border: 2px solid var(--ink); border-radius: 8px; background: #fff; cursor: pointer; }
button:hover { background: #ffe9a8; }
button.primary { background: var(--accent); color: #fff; }
button.danger { background: #fff; color: #b00020; border-color: #b00020; }
button.tab.active { background: var(--blue); color: #fff; }

.bar { display: flex; justify-content: space-between; align-items: center; padding: .6rem 1rem; background: var(--blue); color: #fff; position: sticky; top: 0; z-index: 10; }
.bar h1 { margin: 0; font-size: 1.4rem; }
.bar-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.bar button { color: var(--ink); }
.bar button.primary { color: #fff; }
.bar button.tab.active { background: #fff; color: var(--blue); }

.editor { display: flex; gap: 1rem; padding: 1rem; align-items: flex-start; }
.palette { position: sticky; top: 4.2rem; width: 190px; flex: none; background: var(--card); border: 3px solid var(--ink); border-radius: 12px; padding: .8rem; display: flex; flex-direction: column; gap: .6rem; }
.palette h2 { margin: 0; font-size: 1.1rem; }
.chip { display: flex; align-items: center; gap: .6rem; padding: .4rem; border: 2px dashed var(--ink); border-radius: 8px; cursor: grab; background: #fff9e0; user-select: none; }
.chip:active { cursor: grabbing; }
.mini { width: 28px; height: 40px; border: 2px solid var(--ink); display: grid; grid-template: 1fr 1fr / 1fr 1fr; flex: none; }
.mini::before { content: ""; background: var(--accent); }
.m11::before { grid-area: 1 / 1; }
.m21::before { grid-area: 1 / 1 / 2 / 3; }
.m12::before { grid-area: 1 / 1 / 3 / 2; }
.m22::before { grid-area: 1 / 1 / 3 / 3; }
.hint { font-size: .8rem; margin: 0; color: #555; }

.pages { display: grid; grid-template-columns: repeat(auto-fill, 300px); gap: 1.2rem; flex: 1; }
.page-card { width: 300px; position: relative; }
.page-card.dragging { opacity: .4; }
.page-card.drop-before::before, .page-card.drop-after::after { content: ""; position: absolute; top: 0; bottom: 0; width: 6px; background: var(--blue); border-radius: 3px; z-index: 5; }
.page-card.drop-before::before { left: -.75rem; }
.page-card.drop-after::after { right: -.75rem; }
.grip { cursor: grab; user-select: none; padding: 0 .3rem; border: 2px solid var(--ink); border-radius: 6px; background: #fff9e0; }
.grip:active { cursor: grabbing; }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .3rem; font-weight: bold; }
.page-head button { padding: .1rem .5rem; font-size: .85rem; }
.page-head .btns { display: flex; gap: .25rem; }

.page { position: relative; background: #fff; width: var(--page-w); height: var(--page-h); display: grid; grid-template: 1fr 1fr / 1fr 1fr; gap: .1in; padding: .25in .25in .4in; border: 1px solid #999; }
.page-card .page { zoom: calc(300 / 408); box-shadow: 0 3px 10px rgba(0,0,0,.25); }
.page-num { position: absolute; bottom: .2in; left: 0; right: 0; text-align: center; font-size: 9pt; color: #666; }

.page.art { display: flex; flex-direction: column; gap: .15in; }
.art-label { text-align: center; font-size: 12pt; letter-spacing: .1em; text-transform: uppercase; color: #555; }
.art-title { text-align: center; font-size: 22pt; font-weight: bold; line-height: 1.1; min-height: 1.1em; outline: none; word-break: break-word; }
.page.cover .art-title { font-size: 28pt; }
.art-sub { text-align: center; font-size: 12pt; min-height: 1.2em; outline: none; }
.art-area { flex: 1; }
.page.art:not(.cover) .art-title { margin-top: .3in; }
.page.art [contenteditable]:empty::before { content: attr(data-ph); color: #bbb; }
.page.art [contenteditable]:hover { background: #fff9e0; }
.page.art .art-area { border: 2px dashed #e5e5e5; }
.half .page.art .art-area { border: none; }
@media print { .page.art .art-area { border: none; } }
.slot { border: 2px dashed #ddd; border-radius: 4px; }
.slot.hover { background: #dff6e0; border-color: #2a9d3f; }
.panel { position: relative; border: 3px solid #000; background: #fff; padding: .05in; font-size: 9pt; overflow: hidden; cursor: grab; }
.panel .text { min-height: 1em; outline: none; white-space: pre-wrap; word-break: break-word; }
.panel .del { position: absolute; top: 2px; right: 2px; padding: 0 .4rem; font-size: .8rem; display: none; border-radius: 50%; }
.panel:hover .del { display: block; }
.panel.over-bad { background: #ffe0e0; }
.drop-preview { background: rgba(47,107,255,.25); border: 3px solid var(--blue); pointer-events: none; z-index: 2; }
.drop-preview.bad { background: rgba(255,0,0,.2); border-color: #c00; }

.print-view { padding: 1rem; }
.print-help { background: #fff; border: 2px solid var(--ink); border-radius: 8px; padding: .6rem 1rem; }
.print-help p { margin: .4rem 0; }
.print-controls { display: flex; flex-direction: column; gap: .3rem; }
.print-buttons { display: flex; gap: .5rem; margin-top: .3rem; }
.sheet { position: relative; }
.cut-mark { position: absolute; background: #000; }
.cut-mark.fold-top, .cut-mark.fold-bottom { left: 4.25in; width: 1px; height: .14in; }
.cut-mark.fold-top { top: .04in; }
.cut-mark.fold-bottom { bottom: .04in; }
.cut-mark.cut-left, .cut-mark.cut-right { top: 5.5in; height: 1px; width: .14in; }
.cut-mark.cut-left { left: .04in; }
.cut-mark.cut-right { right: .04in; }
.page.bind-left { padding-left: .4in; }
.page.bind-right { padding-right: .4in; }
.page.test { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .2in; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.page.test .test-top { font-size: 16pt; font-weight: bold; }
.page.test .test-num { font-size: 100pt; font-weight: bold; line-height: 1; }
.page.test .test-label { font-size: 14pt; color: #555; }
.half .page.test.bind-left { border-left: .1in solid #333; }
.half .page.test.bind-right { border-right: .1in solid #333; }
.sheet-label { margin: 1rem 0 .2rem; font-weight: bold; }
.sheet { width: 8.5in; height: 11in; display: flex; flex-wrap: wrap; background: #fff; zoom: .6; box-shadow: 0 3px 10px rgba(0,0,0,.25); }
.half { width: 4.25in; height: 5.5in; flex: none; outline: 1px dotted #ccc; outline-offset: -1px; }
.half .page { border: none; }
.half.blank { background: #fff; }

@media print {
  @page { size: 8.5in 11in; margin: 0; }
  body { background: #fff; }
  .bar, .editor, .print-help, .sheet-label, .book-view { display: none !important; }
  .print-view, .print-view[hidden] { display: block !important; padding: 0; }
  .sheet { zoom: 1; box-shadow: none; page-break-after: always; break-after: page; }
  .sheet:last-child { page-break-after: auto; break-after: auto; }
  .half { outline: none; }
  .panel .del { display: none !important; }
}

.page.back-cover { display: flex; align-items: center; justify-content: center; background: #f1f1f1; color: #bbb; font-size: 18pt; letter-spacing: .2em; }
.back-card .page-head { color: #666; }
.page.blank-page { background: #fff; }
.blank-tag { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #ccc; font-size: 18pt; letter-spacing: .2em; pointer-events: none; }

.book-view { display: flex; flex-direction: column; align-items: center; }
.book-stage { width: 100%; height: calc(100vh - 150px); min-height: 360px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.book { position: relative; flex: none; width: 816px; height: 528px; perspective: 2400px; transition: transform .9s cubic-bezier(.4, .1, .2, 1); cursor: pointer; }
.leaf { position: absolute; left: 408px; top: 0; width: 408px; height: 528px; transform-origin: left center; transform-style: preserve-3d; transition: transform .9s cubic-bezier(.45, .05, .25, 1); }
.leaf.flipped { transform: rotateY(-180deg); }
.face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; background: #fff; overflow: hidden; box-shadow: 0 6px 18px rgba(0, 0, 0, .35); }
.face.back { transform: rotateY(180deg); }
.face .page { width: 100%; height: 100%; }
.face::before { content: ""; position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.face.front::before { background: linear-gradient(to right, rgba(0, 0, 0, .22), rgba(0, 0, 0, 0) 9%); }
.face.back::before { background: linear-gradient(to left, rgba(0, 0, 0, .22), rgba(0, 0, 0, 0) 9%); }
.face::after { content: ""; position: absolute; inset: 0; z-index: 4; pointer-events: none; opacity: 0; transition: opacity .9s ease; }
.face.front::after { background: linear-gradient(to left, rgba(0, 0, 0, .45), rgba(0, 0, 0, 0) 80%); }
.face.back::after { background: linear-gradient(to right, rgba(0, 0, 0, .45), rgba(0, 0, 0, 0) 80%); opacity: 1; }
.leaf.flipped .face.front::after { opacity: 1; }
.leaf.flipped .face.back::after { opacity: 0; }
.book .page.art .art-area { border: none; }
.book-nav { display: flex; gap: 1rem; align-items: center; padding: .5rem; font-weight: bold; }
.book-nav button:disabled { opacity: .4; cursor: default; }

.stats { display: flex; gap: 1rem; flex-wrap: wrap; font-size: .95rem; }
.stats span { background: rgba(255, 255, 255, .2); border-radius: 999px; padding: .15rem .7rem; }

.book-name { font-weight: bold; background: #fff; color: var(--ink); border-radius: 999px; padding: .15rem .8rem; max-width: 16rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.books-dialog { border: 3px solid var(--ink); border-radius: 12px; padding: 1rem; width: min(520px, 92vw); font-family: inherit; }
.books-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.books-dialog h2 { margin: 0 0 .6rem; }
.books-list { list-style: none; margin: 0; padding: 0; max-height: 55vh; overflow: auto; display: flex; flex-direction: column; gap: .5rem; }
.book-row { display: flex; gap: .5rem; align-items: stretch; }
.book-row.current .book-open { background: #dff6e0; }
.book-open { flex: 1; display: flex; flex-direction: column; align-items: flex-start; text-align: left; }
.book-open small { color: #555; }
.dialog-actions { display: flex; justify-content: space-between; margin-top: .8rem; }

.panel { display: flex; flex-direction: column; }
.panel .text:empty::before { content: attr(data-ph); color: #bbb; }
.note-wrap { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; }
.note { max-width: 100%; text-align: center; font-size: 8.5pt; font-style: italic; color: #1d4ed8; outline: none; cursor: text; white-space: pre-wrap; word-break: break-word; padding: .03in; border-radius: 4px; }
.note:empty::before { content: attr(data-ph); color: #9db4f5; }
.note:hover, .note:focus { background: #eef3ff; }
.art-area { display: flex; align-items: center; justify-content: center; }
.art-area .note { font-size: 11pt; max-width: 80%; }

.panel { cursor: default; }
.box-grip { position: absolute; top: 2px; left: 2px; z-index: 2; display: none; cursor: grab; font-size: .8rem; line-height: 1; background: #fff9e0; border: 1px solid #000; border-radius: 4px; padding: .1rem .25rem; user-select: none; }
.panel:hover .box-grip { display: block; }
.note-wrap { cursor: text; }
