:root {
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --accent: #007AFF;
  --text: #1C1C1E;
  --muted: #8E8E93;
  --separator: rgba(60, 60, 67, 0.18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-user-select: none;
  user-select: none;
}

button {
  font: inherit;
  color: var(--accent);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

[hidden] { display: none !important; }

.icon-btn {
  width: 44px; height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  border-radius: 12px;
  padding: 12px 22px;
}

/* 토스트 */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 72px);
  transform: translate(-50%, 12px);
  max-width: calc(100vw - 48px);
  background: rgba(28, 28, 30, 0.88);
  color: #fff;
  font-size: 15px;
  padding: 10px 16px;
  border-radius: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ── 서재 ── */
#library {
  min-height: 100vh;
  padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 20px) calc(var(--safe-bottom) + 32px) calc(var(--safe-left) + 20px);
}

.lib-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 8px -10px 16px 0;
}
.lib-header h1 { font-size: 34px; font-weight: 700; letter-spacing: 0.01em; margin: 0; }

#grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 24px 18px;
}

.card { cursor: pointer; }
.thumb-wrap {
  aspect-ratio: 3 / 4;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.thumb {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 6px 16px rgba(0,0,0,.10);
  transition: transform 0.15s;
}
.card:active .thumb { transform: scale(0.97); }

.meta { display: flex; align-items: flex-start; gap: 4px; margin-top: 10px; }
.meta .text { flex: 1; min-width: 0; }
.name {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
.progress { font-size: 12px; color: var(--muted); margin-top: 3px; }
.more { color: var(--muted); fill: currentColor; width: 32px; height: 28px; margin-right: -6px; flex: none; }

.card.loading .thumb-wrap {
  align-items: center;
  background: rgba(0,0,0,.04);
  border-radius: 4px;
}
.spinner {
  width: 26px; height: 26px;
  border: 3px solid rgba(0,0,0,.12);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#empty {
  min-height: 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  color: var(--muted);
  font-size: 17px;
}
#empty p { margin: 0; }

/* ── 뷰어 ── */
#viewer {
  position: fixed;
  inset: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

#stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  background: var(--bg);
}
#track {
  display: flex;
  width: 300%;
  height: 100%;
  transform: translateX(calc(-100% / 3));
  will-change: transform;
}
.slot {
  position: relative;
  flex: 0 0 calc(100% / 3);
  height: 100%;
  overflow: hidden;
}
.page {
  position: absolute;
  left: 0;
  top: 0;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.08), 0 4px 14px rgba(0,0,0,.06);
}
.page canvas { display: block; width: 100%; height: 100%; }
.page-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 15px;
}

.toolbar {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: calc(var(--safe-top) + 44px);
  padding: var(--safe-top) calc(var(--safe-right) + 4px) 0 calc(var(--safe-left) + 4px);
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(248, 248, 248, 0.82);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 0.5px solid var(--separator);
  z-index: 10;
  transition: opacity 0.2s;
}
.text-btn {
  display: inline-flex;
  align-items: center;
  height: 44px;
  padding-right: 8px;
  font-size: 17px;
  flex: none;
}
#doc-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toolbar-right { display: flex; flex: none; }
#bookmark-btn svg { fill: none; }
#bookmark-btn[aria-pressed="true"] svg { fill: currentColor; }

#page-indicator {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 16px);
  transform: translateX(-50%);
  min-width: 72px;
  padding: 7px 14px;
  border-radius: 999px;
  background: rgba(28, 28, 30, 0.72);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  z-index: 10;
  transition: opacity 0.2s;
}

#viewer.chrome-hidden .toolbar,
#viewer.chrome-hidden #page-indicator {
  opacity: 0;
  pointer-events: none;
}

/* ── 시트 ── */
.sheet-backdrop {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0);
  transition: background 0.25s;
}
.sheet-backdrop.open { background: rgba(0, 0, 0, 0.25); }
.sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  max-height: 70vh;
  overflow-y: auto;
  padding: 8px calc(var(--safe-right) + 20px) calc(var(--safe-bottom) + 20px) calc(var(--safe-left) + 20px);
  background: #fff;
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -2px 20px rgba(0,0,0,.08);
  transform: translateY(100%);
  transition: transform 0.25s ease-out;
}
.sheet-backdrop.open .sheet { transform: translateY(0); }
.sheet::before {
  content: '';
  display: block;
  width: 36px; height: 5px;
  margin: 0 auto 10px;
  border-radius: 3px;
  background: rgba(60, 60, 67, 0.3);
}
.sheet h3 { margin: 0 0 14px; font-size: 17px; font-weight: 600; text-align: center; }

.goto-row { display: flex; gap: 10px; }
#goto-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 17px;
  padding: 11px 14px;
  border: 0;
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
#goto-error { min-height: 20px; margin: 8px 2px 0; color: #FF3B30; font-size: 14px; }

#bookmarks-list { list-style: none; margin: 0; padding: 0; }
#bookmarks-list li { display: flex; align-items: center; border-bottom: 0.5px solid var(--separator); }
#bookmarks-list li:last-child { border-bottom: 0; }
.bm-go { flex: 1; text-align: left; padding: 14px 4px; color: var(--text); font-size: 17px; }
.bm-del { width: 44px; height: 44px; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; }
#bookmarks-empty { margin: 8px 0 12px; text-align: center; color: var(--muted); font-size: 15px; }
