:root {
  --bg: #1a1d23;
  --surface: #242830;
  --surface2: #2e333d;
  --border: #3a4050;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --accent: #4c8bf5;
  --accent-hover: #3b7ae0;
  --danger: #c62828;
  --danger-hover: #ef5350;
  --radius: 8px;
  --shadow: 0 8px 28px rgba(0,0,0,.4);
  --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);
  --touch: 44px;
  font-family: "PingFang SC", "Helvetica Neue", "Noto Sans SC", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
html { height: 100%; height: -webkit-fill-available; }
body {
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 100%;
  min-height: -webkit-fill-available;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.top-bar {
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(6px + var(--safe-top)) 8px 6px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.top-bar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.top-bar-row::-webkit-scrollbar { display: none; }
.top-bar-row.secondary { gap: 4px; }

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  line-height: 1.2;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn.touch {
  min-height: var(--touch);
  min-width: var(--touch);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn.icon {
  padding: 0;
  width: var(--touch);
  font-size: 22px;
  font-weight: 500;
}
.btn:active:not(:disabled) { background: #3a4150; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:active:not(:disabled) { background: var(--accent-hover); }
.btn-danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.btn.tool.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.file-btn { display: inline-flex; align-items: center; cursor: pointer; }
.file-btn.big { padding: 14px 28px; font-size: 16px; min-height: 52px; }

.page-info, .zoom-info {
  min-width: 48px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  flex-shrink: 0;
}

.quality-wrap, .field-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  flex-shrink: 0;
}
.quality-wrap select, .field-inline input {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 8px;
  font-size: 14px;
  min-height: var(--touch);
}
.field-inline input { width: 56px; }

.color-wrap {
  display: inline-flex;
  width: var(--touch);
  height: var(--touch);
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.color-wrap input {
  width: 140%;
  height: 140%;
  border: none;
  transform: translate(-15%, -15%);
  cursor: pointer;
}

.workspace {
  flex: 1;
  overflow: auto;
  position: relative;
  background: radial-gradient(ellipse at top, #222733 0%, var(--bg) 55%);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.empty-state {
  max-width: 520px;
  margin: 8vh auto;
  text-align: center;
  padding: 20px 16px;
}
.empty-state h1 { margin: 0 0 12px; font-weight: 600; font-size: 24px; }
.empty-state p { color: var(--muted); line-height: 1.55; }
.empty-state .hint { font-size: 13px; }
.install-hint {
  margin-top: 18px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text) !important;
}

.viewer-wrap {
  display: flex;
  justify-content: center;
  padding: 12px 8px 24px;
  min-height: 100%;
}

.page-stack {
  position: relative;
  box-shadow: var(--shadow);
  background: #fff;
  line-height: 0;
  touch-action: pan-x pan-y;
}
.page-stack.tool-pen,
.page-stack.tool-replace {
  touch-action: none;
}

#pdfCanvas, #annoCanvas {
  display: block;
  position: absolute;
  left: 0;
  top: 0;
}
#pdfCanvas { z-index: 1; }
#annoCanvas { z-index: 3; pointer-events: none; }

.textLayer {
  position: absolute;
  text-align: initial;
  inset: 0;
  overflow: clip;
  opacity: 1;
  line-height: 1;
  z-index: 2;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  forced-color-adjust: none;
  transform-origin: 0 0;
  caret-color: CanvasText;
  /* Help iOS selection */
  -webkit-user-select: text;
  user-select: text;
}
.textLayer :is(span, br) {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
  -webkit-user-select: text;
  user-select: text;
}
.textLayer > :not(.markedContent),
.textLayer .markedContent span:not(.markedContent) { z-index: 1; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer span[role="img"] { user-select: none; cursor: default; }
.textLayer ::selection { background: rgba(56, 132, 255, 0.35); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent {
  display: block;
  position: absolute;
  inset: 100% 0 0;
  z-index: 0;
  cursor: default;
  user-select: none;
}
.textLayer.selecting .endOfContent { top: 0; }

.page-stack.tool-draw .textLayer {
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}
.page-stack.tool-select .textLayer {
  pointer-events: auto;
}

.overlay-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}
.overlay-layer .anno-ui {
  position: absolute;
  pointer-events: auto;
  transform: translate(-50%, -50%);
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
.overlay-layer .anno-ui.note-pin {
  width: 28px;
  height: 28px;
  border-radius: 6px 6px 6px 0;
  background: #ffeb3b;
  border: 1px solid #f9a825;
  box-shadow: 0 2px 6px rgba(0,0,0,.25);
}
.overlay-layer .anno-ui.note-pin.selected,
.overlay-layer .anno-ui.text-box.selected,
.overlay-layer .anno-ui.replace-box.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.overlay-layer .anno-ui.text-box,
.overlay-layer .anno-ui.replace-box {
  transform: none;
  min-width: 40px;
  min-height: 1.2em;
  padding: 4px 6px;
  border: 1px dashed transparent;
  background: transparent;
  color: #111;
  font-family: "PingFang SC", "Noto Sans SC", sans-serif;
  white-space: pre-wrap;
  word-break: break-word;
  cursor: move;
  line-height: 1.35;
}
.overlay-layer .anno-ui.text-box[contenteditable="true"],
.overlay-layer .anno-ui.replace-box[contenteditable="true"] {
  cursor: text;
  border-color: var(--accent);
  background: rgba(255,255,255,.92);
  outline: none;
  box-shadow: 0 0 0 2px rgba(76,139,245,.35);
  -webkit-user-select: text;
  user-select: text;
}

.draft-box {
  position: absolute;
  z-index: 5;
  border: 2px solid var(--accent);
  background: rgba(76,139,245,.15);
  pointer-events: none;
}

.bottom-tools {
  flex-shrink: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + var(--safe-bottom));
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.tools-scroll {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 2px;
}
.tools-scroll::-webkit-scrollbar { display: none; }
.props-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-bar {
  flex-shrink: 0;
  display: none; /* hide on very small; show above bottom on larger */
  justify-content: space-between;
  gap: 8px;
  padding: 4px 12px;
  font-size: 11px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  color: var(--muted);
}
@media (min-height: 700px) {
  .status-bar { display: flex; }
  .status-bar { padding-bottom: 2px; }
}
/* When status shown, bottom tools already have safe area */
.status-bar .muted { opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(120px + var(--safe-bottom));
  transform: translateX(-50%);
  background: #323846;
  color: #fff;
  padding: 12px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 100;
  font-size: 14px;
  max-width: 88vw;
  text-align: center;
}

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 16px;
  min-width: min(420px, 92vw);
  box-shadow: var(--shadow);
  max-width: 94vw;
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin: 0 0 10px; font-size: 16px; }
dialog textarea {
  width: 100%;
  resize: vertical;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font: inherit;
  font-size: 16px; /* avoid iOS zoom on focus */
  min-height: 100px;
}
.dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 12px;
  flex-wrap: wrap;
}

.page-stack.tool-pen, .page-stack.tool-pen * { cursor: crosshair !important; }
.page-stack.tool-replace, .page-stack.tool-replace * { cursor: crosshair !important; }
.page-stack.tool-eraser, .page-stack.tool-eraser * { cursor: pointer !important; }

/* Landscape: slightly denser */
@media (orientation: landscape) and (max-height: 500px) {
  .top-bar { padding-top: calc(4px + var(--safe-top)); }
  .btn.touch { min-height: 40px; }
  .empty-state { margin: 4vh auto; }
}
