:root {
  --bg: #1d2433;
  --fg: #eef1f6;
  --muted: #9aa4b5;
  --accent: #e0a43c;
  --card: #283147;
  font-family: -apple-system, system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

[hidden] { display: none !important; }

#library {
  padding: calc(env(safe-area-inset-top) + 16px) 24px 24px;
  min-height: 100%;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

h1 {
  margin: 0;
  font-family: Georgia, serif;
  font-style: italic;
  font-size: 2.2rem;
}

.actions { display: flex; gap: 10px; }

.button, .card-actions button {
  font: inherit;
  font-size: 1rem;
  color: var(--fg);
  background: var(--card);
  border: 1px solid #3a4560;
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
}

.button.primary { background: var(--accent); color: #1d2433; border-color: var(--accent); font-weight: 600; }

body.busy .actions { opacity: 0.4; pointer-events: none; }

#status { min-height: 1.4em; color: var(--muted); }
#status.error { color: #ff8a80; }
#empty { color: var(--muted); }

#pieces {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 20px;
}

.card {
  background: var(--card);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.card img {
  width: 100%;
  aspect-ratio: 0.72;
  object-fit: cover;
  object-position: top;
  background: #fff;
}

.card .meta { padding: 8px 10px 4px; display: flex; flex-direction: column; }
.card .name { font-weight: 600; overflow-wrap: anywhere; }
.card .pages { color: var(--muted); font-size: 0.85rem; }

.card-actions { display: flex; gap: 6px; padding: 6px 10px 10px; }
.card-actions button { padding: 4px 10px; font-size: 0.8rem; border-radius: 8px; }

#storage { margin-top: 32px; color: var(--muted); font-size: 0.8rem; }

/* ── viewer ─────────────────────────────────────────────────────────── */
#viewer {
  position: fixed;
  inset: 0;
  background: #000;
}

.stage {
  position: absolute;
  inset: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: flex;
  align-items: center;
  justify-content: center;
}

.sheet {
  position: relative;
  background: #fff;
  flex: none;
}

.part {
  position: absolute;
  left: 0;
  width: 100%;
  overflow: hidden;
}

.part img {
  position: absolute;
  left: 0;
  max-width: none;
  pointer-events: none;
}

/* Marks the seam when the top part already belongs to the next page. */
.sheet.mixed .part + .part { border-top: 3px solid var(--accent); }

.indicator {
  position: absolute;
  right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(29, 36, 51, 0.85);
  color: var(--fg);
  font-size: 0.9rem;
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}
.indicator.show { opacity: 1; }

.toolbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  display: flex;
  align-items: center;
  gap: 16px;
  background: rgba(29, 36, 51, 0.95);
}
.toolbar .title { flex: 1; text-align: center; font-weight: 600; }
