/* 揭頁 Folio — reading app styles */
:root {
  --ink: #2a241c;
  --muted: #6b5646;
  --paper: #fbf6eb;
  --chrome: #efe4d0;
  --chrome-2: #e4d5bb;
  --seal: #8b3a2a;
  --gold: #c4a574;
  --hairline: rgba(42, 36, 28, 0.14);
  --shadow: rgba(42, 36, 28, 0.2);
  --page-ink: #2a241c;
  --page-bg: #fbf6eb;
  --danger: #9b2c1e;
  --ok: #2f5d3a;
  --focus: #8b3a2a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --seg-0-width: env(viewport-segment-width 0 0, 0px);
  --seg-0-height: env(viewport-segment-height 0 0, 0px);
  --seg-0-left: env(viewport-segment-left 0 0, 0px);
  --seg-0-top: env(viewport-segment-top 0 0, 0px);
  --seg-1-width: env(viewport-segment-width 1 0, 0px);
  --seg-1-height: env(viewport-segment-height 1 0, 0px);
  --seg-1-left: env(viewport-segment-left 1 0, 0px);
  --seg-1-top: env(viewport-segment-top 1 0, 0px);
  --reader-font: 19px;
  --page-w: 300px;
  --page-h: 480px;
  --hinge: 16px;
  --tap: 48px;
  --chrome-h: 52px;
}

html.theme-light {
  --ink: #2a241c;
  --muted: #6b5646;
  --paper: #fbf6eb;
  --chrome: #efe4d0;
  --chrome-2: #e4d5bb;
  --page-ink: #2a241c;
  --page-bg: #fbf6eb;
  --hairline: rgba(42, 36, 28, 0.14);
  color-scheme: light;
}

html.theme-sepia {
  --ink: #3c2f1e;
  --muted: #7a6248;
  --paper: #f0e0c0;
  --chrome: #e2cc9f;
  --chrome-2: #d4ba86;
  --page-ink: #3c2f1e;
  --page-bg: #f3e2c0;
  --hairline: rgba(60, 47, 30, 0.16);
  color-scheme: light;
}

html.theme-dark {
  --ink: #e8dcc8;
  --muted: #b5a48c;
  --paper: #1a1612;
  --chrome: #221c16;
  --chrome-2: #2c261e;
  --page-ink: #e8dcc8;
  --page-bg: #24201a;
  --hairline: rgba(232, 220, 200, 0.14);
  --shadow: rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: "PingFang TC", "Noto Sans CJK TC", "Noto Sans CJK HK", "Source Han Sans TC", "Microsoft JhengHei", "Heiti TC", sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  min-height: 100dvh;
  overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; }
button {
  appearance: none;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
img { max-width: 100%; display: block; }
a { color: var(--seal); }

#app {
  height: 100dvh;
  height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Library */
#library-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding-top: var(--safe-t);
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
  padding-bottom: var(--safe-b);
}
#library-view[hidden], #reader-view[hidden] { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: var(--tap);
  padding: 8px 16px 4px;
}
.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
.brand h1 {
  margin: 0;
  font-family: "Songti TC", "Noto Serif CJK TC", "Iowan Old Style", serif;
  font-size: 1.55rem;
  font-weight: 600;
  letter-spacing: 0.14em;
}
.brand .sub {
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.06em;
}
.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--hairline);
  background: var(--chrome);
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 550;
  letter-spacing: 0.04em;
  touch-action: manipulation;
}
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--seal);
  color: #fbf6eb;
  border-color: transparent;
}
html.theme-dark .btn-primary { color: #fbf6eb; }
.btn-ghost {
  background: transparent;
}
.btn-danger { color: var(--danger); }
.icon-btn {
  width: var(--tap);
  padding: 0;
  border-radius: 50%;
}

.import-panel {
  padding: 8px 16px 12px;
  display: grid;
  gap: 10px;
}
.import-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.url-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.url-row input {
  flex: 1;
  min-width: 0;
  min-height: var(--tap);
  border-radius: 12px;
  border: 1px solid var(--hairline);
  background: var(--chrome);
  padding: 0 14px;
  outline: none;
}
.url-row input:focus { border-color: var(--seal); box-shadow: 0 0 0 3px rgba(139, 58, 42, 0.18); }
.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.library-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 8px 16px 24px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--muted);
}
.empty-state h2 {
  font-family: "Songti TC", "Noto Serif CJK TC", serif;
  color: var(--ink);
  letter-spacing: 0.1em;
  font-weight: 600;
}
.empty-state p { line-height: 1.7; }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 16px;
}
.book-card {
  display: flex;
  flex-direction: column;
  text-align: left;
  background: var(--chrome);
  border: 1px solid var(--hairline);
  border-radius: 16px;
  overflow: hidden;
  min-height: 0;
  color: inherit;
  box-shadow: 0 8px 20px var(--shadow);
}
.book-card .cover {
  aspect-ratio: 2 / 3;
  background: linear-gradient(160deg, var(--chrome-2), var(--chrome));
  position: relative;
  overflow: hidden;
}
.book-card .cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.book-card .cover .fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  text-align: center;
  font-family: "Songti TC", "Noto Serif CJK TC", serif;
  letter-spacing: 0.12em;
  font-size: 1.05rem;
}
.book-card .meta {
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 92px;
}
.book-card .title {
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-card .author,
.book-card .progress {
  color: var(--muted);
  font-size: 0.8rem;
}
.book-card .card-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 6px;
}
.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  background: var(--chrome-2);
  color: var(--muted);
}

.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(28, 22, 18, 0.55);
  color: #fbf6eb;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  font-size: 1.3rem;
  letter-spacing: 0.12em;
  pointer-events: none;
}
body.dropping .drop-overlay { display: flex; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + var(--safe-b));
  transform: translateX(-50%) translateY(20px);
  max-width: min(92vw, 420px);
  background: #1c1612;
  color: #fbf6eb;
  padding: 12px 16px;
  border-radius: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 80;
  font-size: 0.92rem;
  line-height: 1.5;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Reader */
#reader-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--paper);
}
.reader-chrome {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: calc(var(--chrome-h) + var(--safe-t));
  padding: calc(4px + var(--safe-t)) 8px 6px;
  padding-left: calc(8px + var(--safe-l));
  padding-right: calc(8px + var(--safe-r));
  background: linear-gradient(to bottom, color-mix(in srgb, var(--paper) 92%, transparent), transparent);
  transition: opacity 0.22s ease, transform 0.22s ease;
}
.reader-footer {
  top: auto;
  bottom: 0;
  min-height: calc(44px + var(--safe-b));
  padding: 6px 16px calc(8px + var(--safe-b));
  background: linear-gradient(to top, color-mix(in srgb, var(--paper) 92%, transparent), transparent);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 6px;
}
#reader-view.chrome-hidden .reader-chrome {
  opacity: 0;
  pointer-events: none;
  transform: translateY(-8px);
}
#reader-view.chrome-hidden .reader-footer {
  transform: translateY(8px);
}
.reader-title {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.04em;
}
.reader-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
.progress-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
}
#progress-label {
  font-size: 0.78rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
#progress-bar {
  flex: 1;
  height: 6px;
  border: 0;
  border-radius: 999px;
  overflow: hidden;
  background: var(--chrome-2);
  appearance: none;
}
#progress-bar::-webkit-progress-bar { background: var(--chrome-2); }
#progress-bar::-webkit-progress-value { background: var(--seal); }
#progress-bar::-moz-progress-bar { background: var(--seal); }

#book-stage {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--chrome-h) + var(--safe-t)) calc(8px + var(--safe-l)) calc(36px + var(--safe-b)) calc(8px + var(--safe-r));
  perspective: 2600px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#reader-view.chrome-hidden #book-stage {
  padding-top: calc(12px + var(--safe-t));
  padding-bottom: calc(16px + var(--safe-b));
}

.book {
  position: relative;
  display: flex;
  align-items: stretch;
  justify-content: center;
  height: var(--page-h);
  transform-style: preserve-3d;
  filter: drop-shadow(0 16px 28px var(--shadow));
}
.book.mode-single { width: var(--page-w); }
.book.mode-spread { width: calc(var(--page-w) * 2 + var(--hinge)); }

.slot {
  position: relative;
  width: var(--page-w);
  height: var(--page-h);
  transform-style: preserve-3d;
}
.hinge {
  width: var(--hinge);
  height: var(--page-h);
  position: relative;
  background: linear-gradient(to right,
    rgba(0,0,0,0.18),
    rgba(0,0,0,0.04) 40%,
    rgba(0,0,0,0.04) 60%,
    rgba(0,0,0,0.18));
  flex-shrink: 0;
}
html.theme-dark .hinge {
  background: linear-gradient(to right,
    rgba(0,0,0,0.55),
    rgba(255,255,255,0.04) 45%,
    rgba(0,0,0,0.55));
}
.book.mode-single .hinge,
.book.mode-single .slot-left { display: none; }

.underlay,
.leaf {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}
.underlay { z-index: 0; }
.leaf { z-index: 1; }
.slot-left .leaf { transform-origin: right center; }
.slot-right .leaf { transform-origin: left center; }
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--page-bg);
  overflow: hidden;
}
.face.back { transform: rotateY(180deg); }

.page-surface {
  background: var(--page-bg);
  color: var(--page-ink);
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
}
.slot-left .page-surface {
  border-radius: 3px 0 0 3px;
  box-shadow: inset -10px 0 16px -12px rgba(0,0,0,0.22);
}
.slot-right .page-surface,
.book.mode-single .page-surface {
  border-radius: 0 3px 3px 0;
  box-shadow: inset 10px 0 16px -12px rgba(0,0,0,0.18);
}
.book.mode-single .page-surface { border-radius: 3px; }

/* Fill the leaf; typography reserves bottom pad for the page number. */
.page-host { position: absolute; inset: 0; }
.page-num {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 6px;
  text-align: center;
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.page-blank .page-host { background: var(--page-bg); }

.leaf.flipping { z-index: 4; }
.fold-shade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: linear-gradient(to right, rgba(0,0,0,0.28), transparent 42%);
  z-index: 3;
}
.slot-left .fold-shade {
  background: linear-gradient(to left, rgba(0,0,0,0.28), transparent 42%);
}

.edge {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 22%;
  min-width: 56px;
  z-index: 6;
  background: transparent;
}
.edge-left { left: 0; }
.edge-right { right: 0; }
.edge:focus-visible { outline: 2px solid var(--seal); outline-offset: -4px; }

.busy-mask {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--paper) 72%, transparent);
  z-index: 15;
  letter-spacing: 0.12em;
  color: var(--muted);
}
.busy-mask[hidden] { display: none !important; }

/* Drawers */
.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 16, 12, 0.45);
  z-index: 30;
  border: 0;
}
.drawer {
  position: absolute;
  top: 0;
  bottom: 0;
  width: min(360px, 88vw);
  background: var(--chrome);
  z-index: 31;
  padding: calc(10px + var(--safe-t)) 16px calc(16px + var(--safe-b));
  overflow: auto;
  box-shadow: 0 0 32px var(--shadow);
}
.drawer.left { left: 0; padding-left: calc(16px + var(--safe-l)); }
.drawer.right { right: 0; padding-right: calc(16px + var(--safe-r)); }
#toc-drawer[hidden], #settings-sheet[hidden], #drawer-backdrop[hidden] { display: none !important; }

.drawer h2 {
  margin: 8px 0 16px;
  font-size: 1.1rem;
  letter-spacing: 0.1em;
}
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 12px 4px;
  min-height: 44px;
  border-bottom: 1px solid var(--hairline);
  letter-spacing: 0.04em;
}
.toc-list .depth-1 { padding-left: 18px; color: var(--muted); }

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--hairline);
  min-height: 52px;
}
.setting-row label { flex: 1; line-height: 1.4; }
.font-controls { display: flex; align-items: center; gap: 8px; }
.theme-picks { display: flex; gap: 6px; }
.theme-picks .btn { min-width: 0; padding: 0 10px; font-size: 0.85rem; }
.theme-picks .btn[aria-pressed="true"],
.font-controls .btn[aria-pressed="true"] {
  background: var(--seal);
  color: #fbf6eb;
  border-color: transparent;
}

.switch {
  position: relative;
  width: 52px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 999px;
  background: var(--chrome-2);
  border: 1px solid var(--hairline);
}
.switch[aria-checked="true"] { background: var(--seal); border-color: transparent; }
.switch i {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.18s ease;
}
.switch[aria-checked="true"] i { transform: translateX(20px); }

.setting-note {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.55;
  margin: 12px 0 0;
}

#debug-badge {
  display: none;
  position: fixed;
  top: calc(4px + var(--safe-t));
  right: calc(4px + var(--safe-r));
  z-index: 90;
  font: 11px/1.3 ui-monospace, monospace;
  background: rgba(0,0,0,0.62);
  color: #fff;
  padding: 4px 6px;
  border-radius: 6px;
  pointer-events: none;
  white-space: pre;
}
html.debug #debug-badge { display: block; }

@media (prefers-reduced-motion: reduce) {
  .leaf, .toast, .reader-chrome, .switch i { transition: none !important; }
}
