:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #21252e;
  --border: #2b303b;
  --text: #e8eaf0;
  --muted: #8b93a7;
  --accent: #5b8cff;
  --accent-press: #4a76e0;
  --danger: #ff5b6e;
  --ok: #38d39f;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

/* The `hidden` attribute must beat author display rules (e.g. .modal{display:flex}). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  padding-top: calc(14px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: rgba(15, 17, 21, 0.85);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-size: 1.15rem; margin: 0; letter-spacing: 0.3px; display: flex; align-items: center; gap: 8px; }
.brand-mark { color: var(--accent); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

main { flex: 1; width: 100%; max-width: 760px; margin: 0 auto; padding: 18px; }

.view-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.view-head h2 { margin: 0; font-size: 1.3rem; }

/* Buttons */
.btn {
  font: inherit;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  color: var(--text);
  background: var(--surface-2);
  transition: transform 0.05s ease, background 0.15s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:not(:disabled):hover { background: var(--accent-press); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-sm { padding: 7px 12px; font-size: 0.85rem; }

.pill {
  font-size: 0.72rem;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  text-transform: capitalize;
}
.pill.ok { color: var(--ok); border-color: rgba(56, 211, 159, 0.4); }
.pill.warn { color: #ffce5b; border-color: rgba(255, 206, 91, 0.4); }
.pill.off { color: var(--danger); border-color: rgba(255, 91, 110, 0.4); }

.muted { color: var(--muted); }

/* Library list */
.empty { text-align: center; padding: 60px 20px; }
.empty p { margin: 6px 0; }

.doc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.doc-card {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.05s ease;
}
.doc-card:hover { border-color: var(--accent); }
.doc-card:active { transform: scale(0.99); }
.doc-thumb {
  width: 64px; height: 80px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface-2);
  flex-shrink: 0;
}
.doc-info { flex: 1; min-width: 0; }
.doc-info h3 { margin: 0 0 4px; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-info .sub { font-size: 0.82rem; color: var(--muted); display: flex; gap: 8px; align-items: center; }

/* Scanner */
.scanner-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 62vh;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}
#preview { width: 100%; height: 100%; object-fit: cover; display: block; }
.frame-guide {
  position: absolute;
  inset: 8%;
  border: 2px dashed rgba(255, 255, 255, 0.55);
  border-radius: 10px;
  pointer-events: none;
}
.scanner-hint {
  position: absolute;
  bottom: 12px;
  left: 0; right: 0;
  text-align: center;
  margin: 0;
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
}

.page-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 12px 2px;
  min-height: 12px;
}
.page-strip img {
  height: 64px;
  border-radius: 6px;
  border: 1px solid var(--border);
}

.scanner-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 6px;
}
.shutter {
  width: 70px; height: 70px;
  border-radius: 50%;
  border: 4px solid #fff;
  background: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.05s ease;
}
.shutter:active { transform: scale(0.92); }

.scanner-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  flex-wrap: wrap;
  gap: 10px;
}
.toggle { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 0;
}
.modal-card {
  background: var(--surface);
  width: 100%;
  max-width: 760px;
  max-height: 92dvh;
  border-radius: 18px 18px 0 0;
  padding: 18px;
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
  overflow-y: auto;
  animation: slideUp 0.2s ease;
}
@keyframes slideUp { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-head { display: flex; gap: 10px; align-items: center; }
.doc-title-input {
  flex: 1;
  font: inherit;
  font-size: 1.1rem;
  font-weight: 600;
  background: transparent;
  border: none;
  border-bottom: 1px solid transparent;
  color: var(--text);
  padding: 6px 2px;
}
.doc-title-input:focus { outline: none; border-bottom-color: var(--accent); }
.modal-meta { display: flex; align-items: center; gap: 10px; margin: 10px 0 16px; font-size: 0.85rem; }
.detail-pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.detail-pages img { width: 100%; border-radius: 8px; border: 1px solid var(--border); }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.modal-actions .btn { flex: 1; min-width: 120px; }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 12px 18px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 100;
  font-size: 0.9rem;
  max-width: 90vw;
}
