:root {
  --ecru: #F4EDE1; --ecru-l: #FBF7F0; --sauge: #A8B49A; --sauge-p: #DCE2D3; --sauge-f: #7E9470;
  --cannelle: #9B6B57; --cannelle-p: #E3CFC4; --rouille: #B5653F; --rouille-p: #ECC9B5;
  --mousse: #4E4A3A; --mousse-p: #C9C5B8; --encre: #2E2B22; --wood: #7A5242; --wood-d: #6A4637;
  --text-2: #6E6A5E; --line: rgba(78,74,58,.15);
  --f-title: 'Fraunces', Georgia, serif; --f-text: 'Nunito', system-ui, sans-serif; --f-hand: 'Caveat', cursive;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overscroll-behavior: none; }
body { background: var(--ecru); color: var(--mousse); font: 16px/1.45 var(--f-text); touch-action: manipulation; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
#app { height: 100%; }
.boot { height: 100%; display: grid; place-items: center; font: 500 28px var(--f-title); color: var(--cannelle); }
.hidden { display: none !important; }

/* ---------- Common ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 0 18px; border-radius: 22px; background: var(--mousse); color: var(--ecru-l); font-weight: 500; }
.btn.soft { background: var(--ecru-l); color: var(--mousse); border: 1px solid var(--line); }
.btn.danger { background: var(--rouille); }
.btn:disabled { opacity: .5; }
.icon-btn { width: 44px; height: 44px; border-radius: 50%; display: inline-grid; place-items: center; background: var(--ecru-l); color: var(--mousse); flex-shrink: 0; }
.icon-btn svg, .tool svg { width: 22px; height: 22px; }
.icon-btn.on { background: var(--sauge-p); }
.topbar { display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--sat)) calc(14px + var(--sar)) 10px calc(14px + var(--sal)); }
.topbar h1 { font: 500 22px var(--f-title); margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { background: var(--ecru-l); border-radius: 18px; padding: 6px 14px; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; min-height: 36px; }
.pill.on { background: var(--sauge-p); }
.status { font-size: 13px; color: var(--text-2); white-space: nowrap; }
.status.err { color: var(--rouille); }

#toast { position: fixed; left: 50%; bottom: calc(24px + var(--sab)); transform: translateX(-50%) translateY(20px); background: var(--mousse); color: var(--ecru-l); padding: 10px 18px; border-radius: 20px; font-size: 15px; opacity: 0; pointer-events: none; transition: .25s; z-index: 50; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- Modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(46,43,34,.35); display: grid; place-items: center; z-index: 40; padding: 20px; animation: fade .15s; }
@keyframes fade { from { opacity: 0 } }
.modal { background: var(--ecru-l); border-radius: 18px; padding: 22px; width: min(440px, 100%); box-shadow: 0 20px 50px rgba(46,43,34,.3); max-height: 90vh; overflow: auto; }
.modal h2 { font: 500 21px var(--f-title); margin: 0 0 14px; }
.modal p { margin: 0 0 14px; color: var(--text-2); }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 14px; color: var(--text-2); margin-bottom: 6px; }
.field input[type=text], .field input[type=email], .field input[type=password], .field select {
  width: 100%; min-height: 46px; border-radius: 12px; border: 1px solid var(--line); background: #fff; padding: 0 14px; font-size: 17px; }
.field input:focus, .field select:focus { outline: 2px solid var(--sauge); border-color: transparent; }
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch { width: 38px; height: 38px; border-radius: 50%; border: 1px solid rgba(78,74,58,.2); }
.swatch.on { box-shadow: 0 0 0 3px var(--ecru-l), 0 0 0 5px var(--mousse); }
.choices { display: flex; flex-wrap: wrap; gap: 8px; }
.choice { border: 1px solid var(--line); background: #fff; border-radius: 12px; padding: 8px 12px; min-height: 40px; font-size: 15px; }
.choice.on { background: var(--sauge-p); border-color: var(--sauge); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 6px; }
.modal-actions .left { margin-right: auto; }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(420px, 100%); }
.login-card svg.logo { width: 100%; height: auto; display: block; margin-bottom: 22px; }
.login-card form { background: var(--ecru-l); border-radius: 18px; padding: 22px; border: 1px solid var(--line); }
.login-card .btn { width: 100%; margin-top: 4px; }
.login-card .alt { text-align: center; margin-top: 14px; font-size: 15px; }
.login-card .alt button { color: var(--cannelle); text-decoration: underline; }
.form-err { color: var(--rouille); font-size: 14px; min-height: 20px; margin: 0 0 8px; }

/* ---------- Library ---------- */
.library { height: 100%; display: flex; flex-direction: column; }
.library .scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 6px calc(16px + var(--sar)) calc(40px + var(--sab)) calc(16px + var(--sal)); }
.case { width: min(920px, 100%); margin: 8px auto 0; background: var(--wood); border-radius: 8px 8px 3px 3px; padding: 14px 14px 0; box-shadow: 0 10px 24px rgba(60,40,30,.25); }
.cell { background: #E6D9C4; min-height: 172px; padding: 14px 12px 0; display: flex; align-items: flex-end; gap: 4px; overflow-x: auto; border-left: 3px solid var(--wood-d); border-right: 3px solid var(--wood-d); scrollbar-width: none; }
.cell::-webkit-scrollbar { display: none; }
.plank { height: 30px; background: var(--cannelle); border-bottom: 4px solid var(--wood-d); position: relative; margin: 0 -14px; }
.case .plank:last-child { margin-bottom: 0; }
.tag { position: absolute; left: 50%; top: 4px; transform: translateX(-50%); background: var(--ecru); border: 1px solid var(--wood-d); box-shadow: inset 0 0 0 2px var(--ecru), inset 0 0 0 3px #C9B49F; border-radius: 2px; padding: 0 16px; font: 500 15px/20px var(--f-title); letter-spacing: .06em; font-variant: small-caps; height: 22px; white-space: nowrap; min-width: 80px; text-align: center; }
.feet { display: flex; justify-content: space-between; width: min(920px, 100%); margin: 0 auto; padding: 0 10px; }
.feet div { width: 22px; height: 10px; background: var(--wood-d); border-radius: 0 0 4px 4px; }
.spine { flex-shrink: 0; width: 40px; border-radius: 3px 3px 0 0; display: flex; align-items: center; justify-content: center; position: relative; box-shadow: inset -4px 0 0 rgba(0,0,0,.12), inset 3px 0 0 rgba(255,255,255,.12); color: var(--ecru-l); }
.spine .v { writing-mode: vertical-rl; transform: rotate(180deg); font: 500 15px var(--f-title); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-height: 92%; padding: 6px 0; }
.spine.light { color: var(--mousse); }
.spine::after { content: ''; position: absolute; left: 6px; right: 6px; top: 14px; height: 2px; background: rgba(255,255,255,.35); }
.spine.add { background: rgba(251,247,240,.6); border: 1.5px dashed var(--cannelle); color: var(--cannelle); box-shadow: none; height: 120px !important; }
.spine.add::after { display: none; }
.ranger .spine:not(.add) { animation: wiggle .5s ease-in-out infinite alternate; }
@keyframes wiggle { from { transform: rotate(-1.2deg) } to { transform: rotate(1.2deg) } }
.empty-shelf { align-self: center; color: var(--text-2); font: 22px var(--f-hand); padding: 0 8px; }
.addsh { width: min(920px, 100%); margin: 18px auto 0; display: flex; justify-content: center; }
.addsh button { border: 1.5px dashed var(--cannelle); color: var(--cannelle); border-radius: 10px; padding: 12px 20px; background: rgba(251,247,240,.6); width: 100%; font-size: 16px; }

/* ---------- Notebook ---------- */
.nbview { height: 100%; display: flex; flex-direction: column; background-color: #B48A6C;
  background-image: repeating-linear-gradient(90deg,rgba(255,255,255,.04) 0 2px,transparent 2px 9px,rgba(0,0,0,.035) 9px 11px,transparent 11px 23px); }
.nbview .topbar { color: var(--ecru-l); }
.nbview .topbar h1 { color: var(--ecru-l); }
.nbview .status { color: rgba(251,247,240,.85); }
.nbview .status.err { color: #FFD9C7; }
.desk { flex: 1; min-height: 0; display: flex; align-items: stretch; gap: 8px; padding: 0 calc(8px + var(--sar)) calc(12px + var(--sab)) calc(8px + var(--sal)); position: relative; }
.tools { display: flex; flex-direction: column; gap: 8px; align-self: flex-start; }
.tools .box { background: var(--ecru-l); border-radius: 16px; padding: 5px; display: flex; flex-direction: column; gap: 2px; box-shadow: 0 4px 10px rgba(50,30,20,.25); }
.tool { width: 44px; height: 44px; border-radius: 11px; display: grid; place-items: center; }
.tool.on { background: var(--sauge-p); }
.tool:disabled { opacity: .35; }
.dot { width: 26px; height: 26px; border-radius: 50%; border: 1px solid rgba(78,74,58,.25); margin: 9px; }
.tool.on .dot { box-shadow: 0 0 0 2px var(--ecru-l), 0 0 0 4px var(--mousse); }
.wdot { background: var(--mousse); border-radius: 50%; }
.tsize { font-family: var(--f-title); }
.book-area { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; position: relative; }
.book { display: flex; background: var(--book, #8E5F4D); border-radius: 8px; padding: 10px 12px 12px; box-shadow: 0 12px 26px rgba(50,30,20,.4), 0 2px 4px rgba(50,30,20,.25); position: relative; }
.leaf { background: var(--ecru-l); position: relative; overflow: hidden; }
.leaf.left { border-radius: 4px 1px 1px 4px; }
.leaf.right { border-radius: 1px 4px 4px 1px; }
.book.single .leaf.right { border-radius: 4px; }
.leaf.left::after, .leaf.right::before { content: ''; position: absolute; top: 0; bottom: 0; width: 26px; pointer-events: none; z-index: 6; }
.leaf.left::after { right: 0; background: linear-gradient(270deg,rgba(78,58,40,.16),rgba(78,58,40,0)); }
.book:not(.single) .leaf.right::before { left: 0; background: linear-gradient(90deg,rgba(78,58,40,.16),rgba(78,58,40,0)); }
.arrow { position: absolute; top: 50%; transform: translateY(-50%); z-index: 8; width: 44px; height: 44px; border-radius: 50%; background: var(--ecru-l); display: grid; place-items: center; box-shadow: 0 3px 8px rgba(50,30,20,.3); }
.arrow:disabled { opacity: .35; }
.arrow svg { width: 24px; height: 24px; }
.ribbon { position: absolute; bottom: -20px; right: 22%; width: 12px; height: 32px; background: var(--sauge); clip-path: polygon(0 0,100% 0,100% 100%,50% 80%,0 100%); }

/* Contents page */
.toc { position: absolute; inset: 0; padding: 26px 26px 20px; overflow-y: auto; }
.toc h2 { font: 500 26px var(--f-title); margin: 0 0 4px; }
.toc .sub { font: 22px var(--f-hand); color: var(--cannelle); margin: 0 0 14px; }
.toc-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid #ECE3D4; width: 100%; text-align: left; }
.toc-row .t { flex: 1; min-width: 0; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toc-row.on .t { font-weight: 700; }
.toc-row .n { font-size: 13px; color: var(--text-2); }
.chip { font-size: 12px; border-radius: 10px; padding: 2px 9px; white-space: nowrap; }
.chip.encours { background: var(--sauge-p); color: #3B4A33; }
.chip.termine { background: var(--cannelle-p); color: #6A4637; }
.chip.idee { background: var(--rouille-p); color: #7A3B20; }
.chip.pause { background: var(--mousse-p); color: #3C3A30; }
.toc .add { margin-top: 14px; color: var(--cannelle); font-size: 16px; display: flex; align-items: center; gap: 6px; min-height: 44px; }
.toc .more { margin-left: 4px; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; color: var(--text-2); }
.toc .more svg { width: 20px; height: 20px; }

/* Page */
.sheet { position: absolute; left: 0; top: 0; width: 1000px; height: 1414px; transform-origin: 0 0; }
.paper-grid { background-image: linear-gradient(#EFE6D8 2px,transparent 2px),linear-gradient(90deg,#EFE6D8 2px,transparent 2px); background-size: 40px 40px; }
.paper-lines { background-image: repeating-linear-gradient(transparent 0 58px,#E6DCCB 58px 60px); background-position: 0 40px; }
.paper-dots { background-image: radial-gradient(#D6C9B4 3px,transparent 3.6px); background-size: 40px 40px; }
.paper-blank { background-image: none; }
.sheet canvas { position: absolute; inset: 0; width: 1000px; height: 1414px; z-index: 3; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; pointer-events: none; }
.editing .sheet canvas.drawing { pointer-events: auto; }
.texts { position: absolute; inset: 0; z-index: 2; }
.editing.tool-text .texts, .editing.tool-hand .texts { z-index: 4; }
.editing.tool-text .texts { cursor: text; }
.tx { position: absolute; font-family: var(--f-text); color: var(--encre); line-height: 1.35; white-space: pre-wrap; word-break: break-word; outline: none; min-width: 40px; min-height: 1.35em; border-radius: 6px; }
.tx.s { font-size: 30px; } .tx.m { font-size: 40px; } .tx.l { font-size: 58px; }
.tx.hand { font-family: var(--f-hand); }
.tx.hand.s { font-size: 40px; } .tx.hand.m { font-size: 52px; } .tx.hand.l { font-size: 72px; }
.editing.tool-text .tx { box-shadow: 0 0 0 2px rgba(168,180,154,.55); background: rgba(251,247,240,.5); }
.editing.tool-text .tx:focus { box-shadow: 0 0 0 3px var(--sauge); }
.tx .grip, .tx .del { position: absolute; top: -26px; width: 44px; height: 44px; border-radius: 50%; display: none; place-items: center; z-index: 2; }
.tx .grip > span, .tx .del > span { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: var(--ecru-l); }
.tx .grip > span { background: var(--mousse); } .tx .del > span { background: var(--rouille); }
.tx .grip svg, .tx .del svg { width: 18px; height: 18px; }
.tx .grip { left: -26px; touch-action: none; } .tx .del { right: -26px; }
.editing.tool-hand .tx { box-shadow: 0 0 0 2px var(--cannelle-p); }
.editing.tool-hand .tx .grip, .editing.tool-hand .tx .del { display: grid; }
.page-foot { position: absolute; left: 0; right: 0; bottom: 0; z-index: 7; display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; font-size: 13px; color: var(--text-2); pointer-events: none; }
.page-foot button { pointer-events: auto; color: var(--cannelle); display: inline-flex; align-items: center; gap: 4px; min-height: 36px; font-size: 14px; }
.page-foot button svg { width: 16px; height: 16px; }

.popover { position: absolute; z-index: 20; background: var(--ecru-l); border-radius: 16px; padding: 8px; box-shadow: 0 8px 22px rgba(50,30,20,.3); display: flex; flex-direction: column; gap: 4px; }

.toc-panel { position: absolute; inset: 0; z-index: 15; background: rgba(46,43,34,.25); display: flex; justify-content: center; align-items: center; padding: 20px; }
.toc-panel .leaf { width: min(520px, 100%); height: min(720px, 100%); border-radius: 8px; box-shadow: 0 12px 30px rgba(50,30,20,.4); }
.pill svg { width: 18px; height: 18px; }
.toc .add svg { width: 20px; height: 20px; }
/* Keep text handles finger-sized whatever the page zoom. */
.tx .grip, .tx .del { transform: scale(var(--inv, 1)); }
