:root {
  --bg: #0c0c0d;
  --panel: #141416;
  --border: #2a2a2e;
  --text: #e6e4df;
  --muted: #8a8680;
  --accent: #c4a35a;
  --accent-dim: rgba(196, 163, 90, 0.28);
  --paper: #f3efe6;
  --danger: #c45c5c;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 ui-sans-serif, system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
}
button:hover { border-color: #444; }
button.primary {
  background: var(--accent);
  color: #1a1408;
  border-color: #d4b56a;
  font-weight: 600;
}
button:disabled { opacity: .45; cursor: default; }
button.ghost { background: transparent; }
#app { height: 100%; min-height: 100dvh; }

.top {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.top h1 { font-size: 16px; font-weight: 600; margin: 0; letter-spacing: .04em; flex: 1; }
.top .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}
.muted { color: var(--muted); }
.wrap { padding: 16px; padding-bottom: calc(24px + var(--safe-bot)); max-width: 720px; margin: 0 auto; }

.lib-list { list-style: none; margin: 16px 0 0; padding: 0; }
.lib-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 14px 14px;
  margin: 0 0 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.lib-item .name { display: block; }
.lib-item .meta { display: block; font-size: 12px; color: var(--muted); margin-top: 4px; }
.empty { color: var(--muted); margin-top: 24px; }

.upload {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.upload input[type=file] { max-width: 100%; }
.link-row { margin-top: 8px; }
.link-row input[type=url] {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
}

.err { color: var(--danger); font-size: 13px; margin-top: 10px; }

.reader {
  height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.reader-body {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  touch-action: pan-y;
  overscroll-behavior-x: none;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(28px + var(--safe-bot));
}
.page-column {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  background: var(--bg);
}
.canvas-slice {
  position: relative;
  line-height: 0;
  background: var(--paper);
}
.canvas-slice canvas {
  display: block;
  width: 100%;
  height: auto;
}
.hit {
  position: absolute;
  background: transparent;
  cursor: pointer;
  touch-action: none;
  border-radius: 2px;
}
.hit.sel { background: var(--accent-dim); }
.rewrite-block {
  background: #161513;
  color: #e8e0d0;
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-size: 1.05rem;
  line-height: 1.55;
  padding: 14px 16px 10px;
  border-left: 2px solid var(--accent);
  cursor: pointer;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.rewrite-block em { font-style: italic; }
.rewrite-block .hint {
  display: block;
  margin-top: 8px;
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: .02em;
}
.busy .hit.sel { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse {
  0%, 100% { background: var(--accent-dim); }
  50% { background: rgba(196, 163, 90, 0.14); }
}
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + var(--safe-bot));
  transform: translateX(-50%);
  background: #2a1f14;
  color: #f0e6d2;
  border: 1px solid var(--accent);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 40;
  max-width: 90vw;
}
.page-num { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; min-width: 4.5em; text-align: center; }
.spinner {
  width: 14px; height: 14px; border: 2px solid #1a1408; border-top-color: transparent;
  border-radius: 50%; display: inline-block; animation: spin .7s linear infinite;
  vertical-align: -2px; margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.no-text { color: var(--muted); padding: 16px; font-size: 13px; }
