:root {
  --paper: #f3f0e8; --card: #fbfaf6; --ink: #1d1b17; --muted: #6c665b; --line: #ddd5c6;
  --accent: #c8442a; --accent-ink: #fff; --strip: #262320; --strip-line: #3a3631;
  --table: #d9d4c9; --page: #ffffff; --focus: #2f6fdb;
  --radius: 10px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 24px rgba(0,0,0,.07);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Noto Serif Thai", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Hiragino Sans", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #161513; --card: #1f1d1a; --ink: #ece7dc; --muted: #a39b8d; --line: #35312b;
    --strip: #0f0e0d; --strip-line: #2a2622; --table: #2a2824; --page: #f7f5f0; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #161513; --card: #1f1d1a; --ink: #ece7dc; --muted: #a39b8d; --line: #35312b;
  --strip: #0f0e0d; --strip-line: #2a2622; --table: #2a2824; --page: #f7f5f0; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--paper); color: var(--ink); font: 15px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
button, input, select { font: inherit; color: inherit; }
code { font: 12.5px/1.4 ui-monospace, "SF Mono", Menlo, monospace; background: color-mix(in srgb, var(--ink) 7%, transparent); padding: 1px 5px; border-radius: 4px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 13px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* top bar */
.bar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 10px 20px;
  background: color-mix(in srgb, var(--paper) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.brand { font: 600 17px/1 var(--serif); letter-spacing: .01em; display: flex; align-items: center; gap: 9px; white-space: nowrap; }
.mark { width: 18px; height: 18px; position: relative; }
.mark::before, .mark::after { content: ""; position: absolute; width: 11px; height: 14px; border-radius: 2px; }
.mark::before { left: 0; top: 4px; background: var(--accent); }
.mark::after { left: 6px; top: 0; background: var(--ink); }
.folder-chip { font-size: 13px; color: var(--muted); max-width: 26ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-chip::before { content: "📁 "; }
.tabs { display: flex; gap: 2px; background: color-mix(in srgb, var(--ink) 7%, transparent); padding: 3px; border-radius: 999px; }
.tab { border: 0; background: none; padding: 5px 14px; border-radius: 999px; cursor: pointer; color: var(--muted); font-weight: 500; }
.tab.is-on { background: var(--card); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.bar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.engine { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; white-space: nowrap; }

/* buttons */
.btn { border: 1px solid var(--line); background: var(--card); padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; white-space: nowrap; transition: background .15s, border-color .15s; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover:not(:disabled) { border-color: color-mix(in srgb, var(--ink) 30%, var(--line)); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn.ghost { background: transparent; }
.btn.big { padding: 13px 26px; font-size: 16px; border-radius: 10px; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.icon { border: 1px solid transparent; background: none; width: 28px; height: 28px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.icon:hover:not(:disabled) { border-color: var(--line); color: var(--ink); }
.icon:disabled { opacity: .3; cursor: default; }

/* welcome */
.view { padding: 0 20px; }
.hero { max-width: 780px; margin: 9vh auto 80px; }
.eyebrow { text-transform: uppercase; letter-spacing: .14em; font-size: 12px; color: var(--accent); font-weight: 600; margin: 0 0 14px; }
.hero h1 { font: 500 clamp(30px, 5vw, 46px)/1.12 var(--serif); margin: 0 0 18px; letter-spacing: -.01em; text-wrap: balance; }
.lede { font-size: 17px; color: var(--muted); max-width: 62ch; margin: 0 0 28px; }
.lede em { color: var(--ink); font-style: normal; font-weight: 600; }
.cta { display: flex; gap: 12px; flex-wrap: wrap; }
.support { font-size: 13px; color: var(--muted); margin: 14px 0 44px; max-width: 62ch; }
.how { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; counter-reset: s; }
.how li { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 16px 18px; counter-increment: s; }
.how li::before { content: counter(s); display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--ink); color: var(--paper); font-size: 12px; font-weight: 700; margin-bottom: 10px; }
.how b { display: block; font: 600 17px/1.2 var(--serif); margin-bottom: 4px; }
.how span { font-size: 14px; color: var(--muted); }
@media (max-width: 720px) { .how { grid-template-columns: 1fr; } .hero { margin-top: 5vh; } }

/* scanning */
.scan-head { max-width: 720px; margin: 7vh auto 24px; text-align: center; }
.scan-head h2 { font: 500 26px/1.2 var(--serif); margin: 0 0 6px; }
.scan-head p { min-height: 1.5em; margin: 0 0 16px; }
.progress { height: 6px; background: color-mix(in srgb, var(--ink) 10%, transparent); border-radius: 99px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.scan-grid { display: flex; flex-wrap: wrap; gap: 4px; justify-content: center; max-width: 1100px; margin: 0 auto 60px; }
.scan-grid img { height: 64px; border-radius: 3px; animation: pop .25s ease-out; }
@keyframes pop { from { opacity: 0; transform: scale(.9); } }

/* groups */
.controls { display: flex; align-items: center; flex-wrap: wrap; gap: 14px 22px; padding: 16px 0 14px; position: sticky; top: 53px; z-index: 10; background: var(--paper); border-bottom: 1px solid var(--line); }
.seg { display: flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--card); }
.seg button { border: 0; background: none; padding: 7px 13px; cursor: pointer; color: var(--muted); font-weight: 500; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button.is-on { background: var(--ink); color: var(--paper); }
.slider-wrap { display: flex; align-items: center; gap: 8px; }
.slider-wrap input { width: 180px; accent-color: var(--accent); }
.pill { font-size: 13px; font-weight: 600; background: var(--card); border: 1px solid var(--line); padding: 2px 10px; border-radius: 99px; min-width: 9ch; text-align: center; }
.check { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; font-size: 14px; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.notice { margin: 14px 0 0; padding: 10px 14px; border-radius: 8px; background: color-mix(in srgb, #e0a100 14%, var(--card)); border: 1px solid color-mix(in srgb, #e0a100 35%, var(--line)); font-size: 14px; }
.groups { display: flex; flex-direction: column; gap: 14px; padding: 18px 0 8px; }
.group { background: var(--strip); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.g-head { display: flex; align-items: center; gap: 10px; padding: 8px 10px 8px 14px; background: var(--card); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.g-num { font: 700 13px/1 ui-monospace, Menlo, monospace; color: #fff; background: hsl(var(--hue) 55% 42%); padding: 5px 8px; border-radius: 6px; letter-spacing: .04em; }
.g-label { border: 1px solid transparent; background: transparent; padding: 4px 8px; border-radius: 6px; width: 16ch; }
.g-label:hover, .g-label:focus { border-color: var(--line); background: var(--paper); }
.g-count { font-size: 13px; color: var(--muted); }
.g-spacer { flex: 1; }
.g-merge { border: 1px solid var(--line); background: var(--paper); border-radius: 6px; padding: 4px 6px; font-size: 13px; max-width: 180px; }
.strip { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px; min-height: 104px; transition: background .15s; }
.strip.drop-into { background: color-mix(in srgb, var(--accent) 25%, var(--strip)); }
.thumb { position: relative; border: 0; padding: 0; background: #000; height: 118px; aspect-ratio: 2/3; border-radius: 4px; overflow: hidden; cursor: grab; outline: 2px solid transparent; outline-offset: 0; flex: none; }
.thumb.land { aspect-ratio: 3/2; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.thumb .t-idx { position: absolute; left: 4px; bottom: 4px; font: 600 11px/1 ui-monospace, Menlo, monospace; color: #fff; background: rgba(0,0,0,.55); padding: 3px 5px; border-radius: 3px; }
.thumb:hover { outline-color: rgba(255,255,255,.5); }
.thumb.is-sel { outline: 3px solid var(--accent); outline-offset: -3px; }
.thumb.is-sel::after { content: "✓"; position: absolute; top: 5px; right: 5px; width: 20px; height: 20px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 12px; display: grid; place-items: center; font-weight: 700; }
.thumb.drop-before { box-shadow: -4px 0 0 0 var(--accent); }
.thumb.drop-after { box-shadow: 4px 0 0 0 var(--accent); }
body.dragging .thumb.is-sel { opacity: .45; }
.dropnew { border: 2px dashed var(--line); border-radius: var(--radius); padding: 18px; text-align: center; color: var(--muted); font-size: 14px; margin: 4px 0 0; transition: all .15s; }
body:not(.dragging) .dropnew { opacity: .6; }
.dropnew.drop-into { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.actions { position: sticky; bottom: 0; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 12px 0 14px; margin-top: 18px; background: var(--paper); border-top: 1px solid var(--line); }
.actions-right { display: flex; gap: 10px; }
.selbar { position: fixed; left: 50%; bottom: 76px; transform: translateX(-50%); z-index: 30; display: flex; align-items: center; gap: 14px; background: var(--ink); color: var(--paper); padding: 9px 14px; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.25); font-size: 14px; }
.selbar select { margin-left: 6px; border-radius: 6px; padding: 3px 6px; background: var(--paper); color: var(--ink); border: 0; }
.selbar .btn { color: var(--paper); border-color: color-mix(in srgb, var(--paper) 30%, transparent); }

/* dialogs */
.dlg { border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(760px, 94vw); background: var(--card); color: var(--ink); box-shadow: 0 30px 80px rgba(0,0,0,.3); }
.dlg::backdrop { background: rgba(20,18,15,.45); }
.dlg form { padding: 22px 24px 20px; }
.dlg h2 { font: 500 24px/1.2 var(--serif); margin: 0 0 8px; }
.rn-table-wrap { max-height: 42vh; overflow: auto; border: 1px solid var(--line); border-radius: 8px; margin: 14px 0; }
.rn-table { width: 100%; border-collapse: collapse; font: 12.5px/1.4 ui-monospace, Menlo, monospace; }
.rn-table th { position: sticky; top: 0; background: var(--paper); text-align: left; padding: 7px 10px; font: 600 12px var(--sans); color: var(--muted); }
.rn-table td { padding: 4px 10px; border-top: 1px solid var(--line); word-break: break-all; }
.rn-table td:nth-child(2) { color: var(--muted); width: 1%; }
.rn-table td:nth-child(3) { color: var(--accent); }
.rn-table tr.same td { opacity: .45; }
.rn-table tr.side td:first-child::after { content: " (matching file)"; color: var(--muted); font-family: var(--sans); }
.error { color: #b3261e; font-weight: 500; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.dlg .progress { margin: 10px 0; }

.lightbox { border: 0; padding: 0; margin: 0; width: 100vw; height: 100vh; max-width: none; max-height: none; background: rgba(12,11,10,.94); color: #eee; }
.lightbox[open] { display: grid; grid-template-rows: auto 1fr; }
.lightbox::backdrop { background: transparent; }
.lb-top { display: flex; align-items: center; gap: 14px; padding: 10px 16px; font-size: 14px; }
.lb-top .muted { color: #aaa; }
.lb-top .btn { color: #eee; border-color: #555; }
.lb-spacer { flex: 1; }
#lb-img { max-width: calc(100vw - 140px); max-height: calc(100vh - 70px); object-fit: contain; margin: auto; grid-row: 2; grid-column: 1; align-self: center; justify-self: center; }
.lb-nav { position: fixed; top: 50%; transform: translateY(-50%); width: 50px; height: 90px; border: 0; background: rgba(255,255,255,.06); color: #fff; font-size: 40px; cursor: pointer; border-radius: 8px; }
.lb-nav:hover:not(:disabled) { background: rgba(255,255,255,.14); }
.lb-nav:disabled { opacity: .2; }
.lb-nav.prev { left: 12px; } .lb-nav.next { right: 12px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: 10px; z-index: 60; box-shadow: 0 10px 30px rgba(0,0,0,.25); max-width: 90vw; font-size: 14px; }
.busy { position: fixed; inset: 0; background: rgba(20,18,15,.35); display: grid; place-items: center; z-index: 70; }
.busy-card { background: var(--card); padding: 20px 24px; border-radius: 12px; width: min(360px, 90vw); box-shadow: var(--shadow); }
.busy-card p { margin: 0 0 12px; font-weight: 500; }

/* photobook */
#book-view .controls { gap: 12px 18px; }
.field { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.field input, .field select { border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: 7px; padding: 6px 9px; }
.field input { width: 22ch; }
#book-pdf { margin-left: auto; }
.btn.toggle[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn.toggle[aria-pressed="true"]::before { content: "✓"; }
.book-hint { margin: 12px 0 0; }
.book { display: flex; flex-direction: column; align-items: center; gap: 34px; padding: 28px 0 80px; background: var(--table); border-radius: var(--radius); margin: 14px 0 30px; }
.spread { display: flex; flex-direction: column; align-items: center; gap: 8px; width: min(860px, 94%); }
.spread .pair { display: grid; grid-template-columns: 1fr 1fr; width: 100%; box-shadow: 0 12px 34px rgba(0,0,0,.22); position: relative; }
.spread .pair::after { content: ""; position: absolute; left: 50%; top: 0; bottom: 0; width: 26px; transform: translateX(-50%); background: linear-gradient(90deg, transparent, rgba(0,0,0,.12) 46%, rgba(0,0,0,.18) 50%, rgba(0,0,0,.12) 54%, transparent); pointer-events: none; }
.cover-row { width: min(330px, 40%); }
.cover-row .page { box-shadow: 0 12px 34px rgba(0,0,0,.25); }
.spread-label { font-size: 12px; color: var(--muted); letter-spacing: .04em; }
.page { position: relative; background: var(--page); overflow: hidden; width: 100%; container-type: inline-size; }
.page-endpaper { background: #ece7dc; }
.cell { position: absolute; overflow: hidden; background: #ddd; }
.cell img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.cell.contain { background: none; }
.cell.half-L img, .cell.half-R img { position: absolute; top: 0; width: 200%; max-width: none; }
.cell.half-L img { left: 0; } .cell.half-R img { left: -100%; }
.chapter-title { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; align-items: center; color: #1d1b17; gap: 2cqw; }
.ch-num { font: 400 15cqw/1 var(--serif); letter-spacing: .02em; }
.ch-label { font: 500 3.6cqw/1.2 var(--sans); letter-spacing: .3em; text-transform: uppercase; }
.chapter-caption { position: absolute; left: 6cqw; bottom: 5cqw; font: 500 3cqw/1 var(--sans); letter-spacing: .25em; color: #fff; text-shadow: 0 1px 6px rgba(0,0,0,.5); text-transform: uppercase; white-space: pre; }
.cover-title { position: absolute; left: 0; right: 0; bottom: 8cqw; text-align: center; font: 500 8cqw/1.1 var(--serif); color: #fff; text-shadow: 0 2px 16px rgba(0,0,0,.45); padding: 0 6cqw; }
.folio { position: absolute; bottom: 2.6cqw; left: 0; right: 0; text-align: center; font: 500 2.4cqw/1 var(--sans); color: #8a857b; letter-spacing: .1em; }
@media (max-width: 720px) { .cover-row { width: 80%; } .spread { width: 100%; } }

/* printing */
#print-root { display: none; }
@media print {
  body.printing > *:not(#print-root) { display: none !important; }
  body.printing { background: #fff; }
  body.printing #print-root { display: block; }
  .print-sheet { overflow: hidden; break-after: page; page-break-after: always; position: relative; }
  .print-sheet:last-child { break-after: auto; }
  .print-sheet .page { width: 100%; height: 100%; aspect-ratio: auto !important; box-shadow: none; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* Google Drive */
.gd-mark { width: 18px; height: 16px; display: inline-block; background:
  conic-gradient(from 210deg at 50% 62%, #1fa463 0 120deg, #ffd04b 0 240deg, #4688f4 0); clip-path: polygon(50% 0, 100% 86%, 0 86%); }
.drive-dlg { width: min(620px, 94vw); }
.dd-wrap { padding: 22px 24px 20px; }
.dd-roots { display: flex; gap: 4px; margin: 6px 0 12px; flex-wrap: wrap; }
.dd-roots button { border: 1px solid var(--line); background: var(--paper); padding: 6px 12px; border-radius: 999px; cursor: pointer; font-size: 14px; }
.dd-roots button.is-on { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.dd-crumbs { display: flex; flex-wrap: wrap; gap: 2px; font-size: 14px; margin-bottom: 8px; }
.crumb { border: 0; background: none; padding: 2px 4px; cursor: pointer; color: var(--muted); }
.crumb:not(:last-child)::after { content: " ›"; color: var(--muted); }
.crumb:last-child { color: var(--ink); font-weight: 600; }
.dd-list { border: 1px solid var(--line); border-radius: 8px; height: 300px; overflow: auto; background: var(--paper); }
.dd-row { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
.dd-row input { margin: 0 0 0 12px; width: 16px; height: 16px; accent-color: var(--accent); flex: none; }
.dd-row .dd-item { border-bottom: 0; }
.dd-here { min-height: 26px; margin-bottom: 6px; font-size: 14px; }
.dd-picked { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.dd-chip { display: inline-flex; align-items: center; gap: 4px; background: var(--ink); color: var(--paper); border-radius: 999px; padding: 3px 4px 3px 11px; font-size: 13px; max-width: 100%; }
.dd-chip button { border: 0; background: none; color: inherit; cursor: pointer; font-size: 15px; line-height: 1; padding: 0 5px; opacity: .7; }
.dd-chip button:hover { opacity: 1; }
.dd-item { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 9px 12px 9px 36px; cursor: pointer; position: relative; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dd-item::before { content: ""; position: absolute; left: 12px; top: 50%; width: 16px; height: 12px; transform: translateY(-50%); background: #8a857b; border-radius: 2px 2px 3px 3px; clip-path: polygon(0 0, 40% 0, 50% 18%, 100% 18%, 100% 100%, 0 100%); }
.dd-item:hover { background: var(--card); }
.dd-linkrow { display: flex; margin-top: 12px; }
.dd-linkrow input { flex: 1; width: auto; }

/* visually hidden but still usable (iPad needs file inputs that are not display:none) */
.vh { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
label.btn { cursor: pointer; }
/* iPad / touch */
.t-open { display: none; position: absolute; right: 4px; bottom: 4px; width: 28px; height: 28px; border-radius: 6px; background: rgba(0,0,0,.6); color: #fff; font-size: 16px; line-height: 28px; text-align: center; }
body.touch .t-open { display: block; }
body.touch .thumb { height: 132px; }
body.touch .thumb.is-sel::after { width: 24px; height: 24px; font-size: 14px; }
.sel-nudge { display: inline-flex; gap: 4px; }
body.touch .selbar { bottom: 84px; padding: 10px 14px; gap: 12px; flex-wrap: wrap; justify-content: center; max-width: calc(100vw - 24px); }
body.touch .selbar .btn.small { padding: 8px 14px; font-size: 15px; }
body.touch .selbar select { padding: 6px 8px; font-size: 15px; }
.rn-parts { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; align-items: flex-start; }
body.touch #pick-photos-label { order: -1; }

/* share link */
.share-dlg { width: min(600px, 94vw); }
.share-dlg h3 { font: 600 15px/1.3 var(--sans); margin: 18px 0 8px; }
.sh-keyrow { display: flex; flex-direction: column; gap: 4px; margin: 12px 0; }
.sh-keyrow input { width: 100%; }
.sh-keyrow .field { display: flex; }
.sh-progress { margin: 14px 0; }
.sh-progress p { margin: 0 0 8px; }
.sh-result { margin: 14px 0; display: flex; flex-direction: column; gap: 8px; }
.sh-result input { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; background: var(--paper); font: 14px ui-monospace, Menlo, monospace; }
.sh-acts { display: flex; gap: 8px; flex-wrap: wrap; }
a.btn { text-decoration: none; }
.sh-list-wrap { border-top: 1px solid var(--line); margin-top: 14px; }
.sh-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.sh-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.btn.danger { color: #b3261e; }
#book-share { margin-left: auto; }
#book-share + #book-pdf { margin-left: 0; }
