:root {
  --bg-primary:   #17181c;
  --bg-secondary: #20242b;
  --bg-panel:     #242a32;
  --bg-hover:     #303742;
  --bg-active:    #20384d;
  --bg-canvas:    #34383f;
  --text:         #e2e8f0;
  --text-muted:   #a0aec0;
  --accent:       #4299e1;
  --accent-h:     #3182ce;
  --danger:       #e53e3e;
  --border:       #333944;
  --border-l:     #515a67;
  --toolbar-h:    48px;
  --options-h:    40px;
  --thumb-w:      192px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body, #app { width: 100%; height: 100%; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  background: var(--bg-primary);
  color: var(--text);
  line-height: 1.4;
}

/* ---- App Layout ---- */
#app { display: flex; flex-direction: column; }

#workspace { display: flex; flex: 1; overflow: hidden; }

#status-bar {
  display: flex; align-items: center;
  height: 24px; padding: 0 10px;
  background: #15171b;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 11px;
  flex-shrink: 0;
}

/* ---- Toolbar ---- */
#toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: var(--toolbar-h);
  padding: 0 8px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
  overflow-y: hidden;
  user-select: none;
}

#nav-group { display: flex; align-items: center; gap: 4px; }

.tb-group { display: flex; align-items: center; gap: 2px; }

.tb-divider {
  width: 1px; height: 28px;
  background: var(--border-l);
  margin: 0 6px; flex-shrink: 0;
}

.tb-btn {
  display: flex; align-items: center; gap: 4px;
  height: 32px; padding: 0 10px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text);
  font-size: 12px; cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.tb-btn:hover:not(:disabled) { background: var(--bg-hover); border-color: var(--border-l); }
.tb-btn.active,
.tb-btn:active:not(:disabled) { background: var(--bg-active); border-color: var(--accent); color: var(--accent); }
.tb-btn:disabled { opacity: .35; cursor: not-allowed; }
.tb-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.tb-btn.primary:hover:not(:disabled) { background: var(--accent-h); }
.tb-btn.danger { color: var(--danger); border-color: var(--danger); }
.tb-btn.danger:hover { background: rgba(229,62,62,.15); }
.tb-btn.icon-btn { width: 32px; padding: 0; justify-content: center; font-size: 14px; }

.file-input-native {
  position: fixed;
  left: -1000px;
  top: -1000px;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.tools-group { gap: 1px; }

.tool-btn {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text);
  font-size: 15px; cursor: pointer;
  transition: background .15s;
}
.tool-btn:hover { background: var(--bg-hover); }
.tool-btn.active { background: var(--bg-active); border-color: var(--accent); color: var(--accent); }

.page-input {
  width: 48px; height: 28px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text);
  text-align: center; font-size: 12px;
}
.page-total { font-size: 12px; color: var(--text-muted); }

.zoom-select {
  height: 28px; padding: 0 4px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text);
  font-size: 12px; cursor: pointer;
}

.tb-filename {
  margin-left: auto; padding-left: 16px;
  font-size: 11px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; max-width: 220px;
}

/* ---- Tool Options ---- */
#tool-options {
  display: flex; align-items: center; gap: 8px;
  height: var(--options-h); padding: 0 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto; overflow-y: hidden;
  user-select: none;
}

.color-presets { display: flex; align-items: center; gap: 4px; }

.preset {
  width: 20px; height: 20px; border-radius: 3px;
  cursor: pointer; flex-shrink: 0;
  transition: transform .1s;
}
.preset:hover { transform: scale(1.2); }
.preset.active { outline: 2px solid var(--accent); outline-offset: 1px; }

.color-custom { width: 28px; height: 24px; padding: 1px; cursor: pointer; border-radius: 3px; border: none; }

.opt-label { font-size: 11px; color: var(--text-muted); white-space: nowrap; }

.opt-checkbox { display: flex; align-items: center; gap: 4px; font-size: 12px; cursor: pointer; white-space: nowrap; }

select#opt-stroke, select#opt-fontsize {
  height: 24px; padding: 0 4px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text); font-size: 12px;
}

input#opt-fontcolor { width: 24px; height: 24px; padding: 1px; cursor: pointer; border-radius: 3px; border: none; }

#opt-opacity { width: 80px; cursor: pointer; }
#opt-opacity-val { font-size: 11px; color: var(--text-muted); min-width: 32px; }

/* ---- Thumbnail Panel ---- */
#thumbnail-panel {
  width: var(--thumb-w);
  flex-shrink: 0;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.thumb-hint { font-size: 12px; color: var(--text-muted); text-align: center; padding: 16px; }

.thumb-item {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 4px; border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color .15s, background .15s, transform .18s ease, box-shadow .18s ease, opacity .18s ease;
}
.thumb-item:hover { background: var(--bg-hover); transform: translateY(-1px); }
.thumb-item.active { border-color: var(--accent); background: var(--bg-active); }
.thumb-item.multi-selected {
  border-color: #f6ad55;
  background: rgba(246,173,85,.12);
  box-shadow: 0 0 0 1px rgba(246,173,85,.45);
}
.thumb-item.multi-selected.active {
  border-color: var(--accent);
  background: var(--bg-active);
  box-shadow: 0 0 0 2px rgba(246,173,85,.65);
}
.thumb-item.dragging { opacity: .45; transform: scale(.96); }
.thumb-item.drop-before::before,
.thumb-item.drop-after::after {
  content: "";
  position: absolute;
  left: 6px; right: 6px;
  height: 4px;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(66,153,225,.25), 0 2px 8px rgba(66,153,225,.4);
  pointer-events: none;
  z-index: 2;
}
.thumb-item.drop-before::before { top: -6px; }
.thumb-item.drop-after::after { bottom: -6px; }
.thumb-item .thumb-source-tag {
  position: absolute;
  top: 4px; left: 4px;
  padding: 1px 5px;
  font-size: 9px;
  border-radius: 8px;
  background: rgba(15,23,42,.78);
  color: #fff;
  letter-spacing: .04em;
  pointer-events: none;
}
.thumb-item .thumb-source-tag[data-kind="blank"] { background: rgba(56,161,105,.85); }
.thumb-item .thumb-source-tag[data-kind="duplicate"] { background: rgba(128,90,213,.85); }
.thumb-item .thumb-source-tag[data-kind="imported"] { background: rgba(221,107,32,.88); }
#thumbnail-panel.dropping-end {
  box-shadow: inset 0 -4px 0 0 var(--accent);
}

.thumb-img-wrap {
  width: 160px; min-height: 100px;
  display: flex; align-items: center; justify-content: center;
  background: #fff; border-radius: 2px; overflow: hidden;
}
.thumb-img-wrap img { width: 100%; display: block; }
.thumb-placeholder { width: 160px; height: 120px; background: var(--bg-canvas); border-radius: 2px; }

.thumb-actions {
  position: absolute;
  top: 8px; right: 8px;
  display: flex; gap: 4px;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity .15s ease, transform .15s ease;
}
.thumb-item:hover .thumb-actions,
.thumb-item.active .thumb-actions {
  opacity: 1;
  transform: translateY(0);
}
.thumb-action {
  width: 24px; height: 24px;
  display: grid; place-items: center;
  border: 1px solid rgba(17,24,39,.25);
  border-radius: 4px;
  background: rgba(255,255,255,.92);
  color: #111827;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  box-shadow: 0 3px 10px rgba(0,0,0,.18);
}
.thumb-action:hover { background: #e8f3ff; border-color: var(--accent); }

.thumb-num { font-size: 11px; color: var(--text-muted); }

/* ---- Viewer ---- */
#viewer {
  flex: 1; overflow: auto;
  background: var(--bg-canvas);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 0;
}

.drop-hint {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  flex: 1; gap: 8px;
  color: var(--text-muted);
}
.drop-icon { font-size: 48px; }
.drop-sub { font-size: 12px; }

.page-wrapper {
  position: relative;
  display: block;
  box-shadow: 0 2px 12px rgba(0,0,0,.35);
  background: #fff;
  user-select: none;
  margin-bottom: 32px;
  overflow: visible;
  animation: page-in .18s ease both;
  transition: width .18s ease, height .18s ease, box-shadow .18s ease, transform .18s ease;
}
.page-wrapper:hover { box-shadow: 0 8px 24px rgba(0,0,0,.36); }

.page-surface {
  position: absolute;
  top: 0; left: 0;
  transform-origin: top left;
  transition: transform .22s cubic-bezier(.2,.8,.2,1);
}
.page-surface canvas { display: block; }

@keyframes page-in {
  from { opacity: .65; transform: translateY(8px) scale(.992); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.page-num-label {
  position: absolute;
  bottom: -20px; left: 50%;
  transform: translateX(-50%);
  font-size: 11px; color: #aaa;
  white-space: nowrap;
}

.ann-svg {
  position: absolute; top: 0; left: 0;
  touch-action: none;
}

.text-edit-target {
  fill: rgba(246, 173, 85, .12);
  stroke: rgba(246, 173, 85, .75);
  stroke-width: 1;
  stroke-dasharray: 3 2;
  cursor: text;
}
.text-edit-target:hover {
  fill: rgba(66, 153, 225, .18);
  stroke: #4299e1;
}

.loading-overlay {
  position: fixed; inset: 0; z-index: 10000;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px;
  background: rgba(15,17,20,0.62);
  color: #f8fafc;
  font-size: 14px;
}
.loading-overlay.hidden { display: none; }

.loader {
  width: 40px; height: 40px;
  border: 4px solid rgba(255,255,255,0.2);
  border-top-color: #63b3ed;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---- Drop Overlay ---- */
.drop-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(26,26,46,.85);
  display: flex; align-items: center; justify-content: center;
}
.drop-overlay.hidden { display: none; }

.drop-overlay-inner {
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  padding: 48px 64px;
  border: 2px dashed var(--accent);
  border-radius: 12px;
}
.drop-overlay-icon { font-size: 64px; }

.startup-error {
  position: fixed;
  right: 16px;
  bottom: 36px;
  z-index: 10001;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 14px 16px;
  background: #fff7ed;
  color: #1f2937;
  border: 1px solid #fb923c;
  border-radius: 6px;
  box-shadow: 0 12px 32px rgba(0,0,0,.28);
}
.startup-error.hidden { display: none; }
.startup-error code {
  display: block;
  padding: 6px 8px;
  background: #111827;
  color: #f8fafc;
  border-radius: 4px;
  font-size: 12px;
}

/* ---- Context Menu ---- */
.context-menu {
  position: fixed;
  z-index: 10002;
  min-width: 190px;
  padding: 6px;
  background: #f8fafc;
  color: #111827;
  border: 1px solid rgba(15,23,42,.18);
  border-radius: 6px;
  box-shadow: 0 18px 44px rgba(0,0,0,.32);
  animation: menu-in .12s ease both;
}
.context-menu.hidden { display: none; }
.context-menu-item {
  width: 100%;
  height: 30px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.context-menu-item:hover:not(:disabled) { background: #e8f3ff; color: #0f5fa8; }
.context-menu-item:disabled { opacity: .4; cursor: not-allowed; }
.context-menu-divider {
  height: 1px;
  margin: 5px 2px;
  background: rgba(15,23,42,.12);
}

@keyframes menu-in {
  from { opacity: 0; transform: translateY(-4px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 44px;
  transform: translate(-50%, 8px);
  z-index: 10003;
  min-width: 220px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 10px 16px;
  background: #1f2937;
  color: #f8fafc;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 6px;
  box-shadow: 0 12px 32px rgba(0,0,0,.32);
  font-size: 13px;
  opacity: 0;
  transition: opacity .18s ease, transform .18s ease;
  pointer-events: none;
  text-align: center;
}
.toast.hidden { display: none; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-variant="success"] { background: #166534; border-color: #22c55e; }
.toast[data-variant="error"] { background: #7f1d1d; border-color: #ef4444; }
.toast[data-variant="info"] { background: #1f2937; }

/* ---- Save button dirty indicator ---- */
.tb-btn.primary.has-changes {
  position: relative;
  box-shadow: 0 0 0 2px rgba(251, 191, 36, .35);
}
.tb-btn.primary.has-changes::after {
  content: "";
  position: absolute;
  top: 3px; right: 3px;
  width: 8px; height: 8px;
  background: #fbbf24;
  border-radius: 50%;
  border: 1.5px solid #1f2937;
  box-shadow: 0 0 6px rgba(251, 191, 36, .75);
}

/* ---- Phase 2: Before / After 比較 ---- */
.compare-wrap { position: relative; display: flex; align-items: center; gap: 2px; }
.tb-btn.compare-active {
  background: var(--bg-active);
  border-color: var(--accent);
  color: var(--accent);
}
.tb-btn.compare-temp {
  background: rgba(246,173,85,.15);
  border-color: #f6ad55;
  color: #f6ad55;
}
.compare-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 10004;
  min-width: 220px;
  padding: 6px;
  background: #f8fafc;
  color: #111827;
  border: 1px solid rgba(15,23,42,.18);
  border-radius: 6px;
  box-shadow: 0 18px 44px rgba(0,0,0,.32);
}
.compare-menu.hidden { display: none; }
.compare-menu-item {
  width: 100%; height: 30px;
  display: flex; align-items: center;
  padding: 0 10px; border: 0; border-radius: 4px;
  background: transparent; color: inherit;
  font: inherit; text-align: left; cursor: pointer;
}
.compare-menu-item:hover { background: #e8f3ff; color: #0f5fa8; }
.compare-menu-item.active { background: #e8f3ff; color: #0f5fa8; font-weight: 600; }
.compare-menu-divider { height: 1px; margin: 5px 2px; background: rgba(15,23,42,.12); }
.compare-menu-check {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; cursor: pointer;
}

#viewer.compare-split { padding: 24px 16px; }
#viewer.compare-split .compare-pair {
  display: flex; gap: 16px;
  margin-bottom: 32px;
}
#viewer.compare-before .page-wrapper { box-shadow: 0 2px 12px rgba(246,173,85,.4); }
.compare-badge {
  position: absolute;
  top: -22px; left: 0;
  padding: 2px 8px;
  font-size: 11px;
  color: #fff;
  border-radius: 4px 4px 0 0;
  letter-spacing: .04em;
  pointer-events: none;
  z-index: 3;
}
.compare-badge-before { background: rgba(221,107,32,.92); }
.compare-badge-after  { background: rgba(56,161,105,.92); }

.diff-overlay {
  position: absolute; top: 0; left: 0;
  pointer-events: none;
  z-index: 2;
}

/* ---- Phase 2: 保存前モーダル ---- */
.save-modal {
  position: fixed; inset: 0; z-index: 10005;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15,17,20,.7);
}
.save-modal.hidden { display: none; }
.save-modal-inner {
  width: min(880px, calc(100vw - 48px));
  max-height: calc(100vh - 48px);
  display: flex; flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--border-l);
  border-radius: 8px;
  box-shadow: 0 24px 64px rgba(0,0,0,.5);
  overflow: hidden;
}
.save-modal-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}
.save-modal-header h2 { font-size: 14px; font-weight: 600; flex: 1; }
.save-modal-tabs {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 14px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}
.save-modal-tab {
  height: 28px; padding: 0 12px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--text); font-size: 12px;
  cursor: pointer;
}
.save-modal-tab:hover { background: var(--bg-hover); }
.save-modal-tab.active { background: var(--bg-active); border-color: var(--accent); color: var(--accent); }
.save-modal-page-label { font-size: 12px; color: var(--text-muted); margin-left: 10px; }
.save-modal-preview {
  flex: 1; min-height: 360px;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-canvas);
  padding: 16px;
  overflow: auto;
}
.save-modal-preview img,
.save-modal-preview canvas {
  max-width: 100%;
  max-height: 60vh;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0,0,0,.35);
}
.save-modal-preview .save-modal-loading {
  color: var(--text-muted); font-size: 13px;
}
.save-modal-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 10px 14px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

/* ---- Phase 6: 保存モーダル拡張（セクション切替・モード選択・メタデータ） ---- */
.save-modal-sections {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 14px 0;
  background: var(--bg-secondary);
}
.save-modal-section {
  height: 28px; padding: 0 12px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px 4px 0 0;
  color: var(--text); font-size: 12px;
  cursor: pointer;
}
.save-modal-section:hover { background: var(--bg-hover); }
.save-modal-section.active {
  background: var(--bg-active);
  border-color: var(--accent);
  border-bottom-color: transparent;
  color: var(--accent);
}
.save-modal-panel {
  flex: 1;
  padding: 14px 16px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
  background: var(--bg-canvas);
}
.save-mode-option {
  display: grid;
  grid-template-columns: 18px 1fr;
  align-items: start;
  gap: 4px 10px;
  padding: 8px 10px;
  border: 1px solid var(--border-l);
  border-radius: 6px;
  cursor: pointer;
  background: var(--bg-primary);
}
.save-mode-option:hover { background: var(--bg-hover); }
.save-mode-option input[type="radio"] {
  grid-row: 1 / 3;
  margin-top: 4px;
}
.save-mode-title { font-size: 13px; font-weight: 600; }
.save-mode-desc { font-size: 12px; color: var(--text-muted); }
.save-filename-preview {
  display: inline-block;
  padding: 4px 8px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  font-size: 12px;
  color: var(--text);
  word-break: break-all;
}
.save-error-inner { width: min(640px, calc(100vw - 48px)); }
.save-error-body {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
  background: var(--bg-canvas);
}
.save-error-message {
  font-size: 13px;
  color: var(--danger, #e53e3e);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.save-error-details summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
}
.save-error-stack {
  margin-top: 8px;
  padding: 10px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  font-size: 11px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 240px;
  overflow: auto;
}

/* ---- Phase 3: ツールカーソル ---- */
#viewer.tool-mode-select { cursor: default; }
#viewer.tool-mode-rectangle .ann-svg,
#viewer.tool-mode-circle .ann-svg,
#viewer.tool-mode-line .ann-svg,
#viewer.tool-mode-arrow .ann-svg,
#viewer.tool-mode-highlight .ann-svg,
#viewer.tool-mode-ink .ann-svg { cursor: crosshair; }
#viewer.tool-mode-freetext .ann-svg,
#viewer.tool-mode-textedit .ann-svg { cursor: text; }

/* ---- Phase 3: 注釈リサイズハンドル ---- */
.ann-resize-handle { cursor: pointer; }
.ann-resize-handle.handle-nw, .ann-resize-handle.handle-se { cursor: nwse-resize; }
.ann-resize-handle.handle-ne, .ann-resize-handle.handle-sw { cursor: nesw-resize; }
.ann-resize-handle.handle-n, .ann-resize-handle.handle-s { cursor: ns-resize; }
.ann-resize-handle.handle-e, .ann-resize-handle.handle-w { cursor: ew-resize; }
.ann-resize-handle.handle-start, .ann-resize-handle.handle-end { cursor: grab; }
.ann-resize-handle:hover { fill: #e8f3ff; }

/* ---- Phase 3: マーキー ---- */
.marquee-rect { pointer-events: none; }

/* ---- Phase 3: スタイルプリセット ---- */
.preset-wrap {
  display: flex; align-items: center; gap: 6px;
  flex-shrink: 0;
}
.preset-list {
  display: flex; align-items: center; gap: 4px;
  flex-wrap: nowrap;
  max-width: 240px;
  overflow-x: auto;
}
.preset-chip {
  position: relative;
  display: inline-flex; align-items: center;
  height: 22px; padding: 2px 4px 2px 4px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  cursor: pointer;
  gap: 4px;
}
.preset-chip:hover { background: var(--bg-hover); }
.preset-chip-swatch {
  display: inline-block;
  width: 14px; height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,.15);
}
.preset-chip-del {
  display: inline-flex; align-items: center; justify-content: center;
  width: 14px; height: 14px;
  border-radius: 50%;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1;
}
.preset-chip-del:hover { color: var(--danger); background: rgba(229,62,62,.18); }

/* ---- Phase 4: 挿入メニュー ---- */
.insert-group { position: relative; }
.insert-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 10004;
  min-width: 240px;
  padding: 6px;
  background: #f8fafc;
  color: #111827;
  border: 1px solid rgba(15,23,42,.18);
  border-radius: 6px;
  box-shadow: 0 18px 44px rgba(0,0,0,.32);
}
.insert-menu.hidden { display: none; }
.insert-menu-item {
  width: 100%;
  height: 32px;
  display: flex; align-items: center;
  padding: 0 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.insert-menu-item:hover { background: #e8f3ff; color: #0f5fa8; }
.insert-menu-divider { height: 1px; margin: 5px 2px; background: rgba(15,23,42,.12); }

/* ---- Phase 4: 共通モーダル ---- */
.phase4-modal {
  position: fixed; inset: 0; z-index: 10006;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15,17,20,.7);
}
.phase4-modal.hidden { display: none; }
.phase4-modal-inner {
  width: min(720px, calc(100vw - 48px));
  max-height: calc(100vh - 48px);
  display: flex; flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--border-l);
  border-radius: 8px;
  box-shadow: 0 24px 64px rgba(0,0,0,.5);
  overflow: hidden;
}
.phase4-modal-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}
.phase4-modal-header h2 { font-size: 14px; font-weight: 600; flex: 1; }
.phase4-modal-body {
  flex: 1;
  padding: 14px 16px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
}
.phase4-row {
  display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap;
}
.phase4-label {
  font-size: 12px;
  color: var(--text-muted);
  min-width: 56px;
}
.phase4-check {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; cursor: pointer;
}
.phase4-input {
  flex: 1; min-width: 160px;
  height: 28px;
  padding: 0 8px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text);
  font-size: 13px;
}
.phase4-input-small {
  width: 64px; height: 28px;
  padding: 0 6px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text);
  font-size: 13px;
}
.phase4-modal select {
  height: 28px;
  padding: 0 6px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text);
  font-size: 13px;
}
.phase4-modal input[type="color"] {
  width: 28px; height: 28px;
  padding: 1px; cursor: pointer;
  border-radius: 3px; border: 1px solid var(--border-l);
  background: transparent;
}
.phase4-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
.phase4-section {
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: rgba(0,0,0,.12);
}
.phase4-section h3 {
  font-size: 12px; font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}
.phase4-hint {
  font-size: 12px; color: var(--text-muted);
}

/* ---- Phase 4: ライブラリグリッド（署名 / スタンプ） ---- */
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.library-card {
  position: relative;
  display: flex; flex-direction: column; align-items: stretch;
  padding: 8px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-l);
  border-radius: 6px;
  cursor: pointer;
  min-height: 88px;
}
.library-card:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
}
.library-card-preview {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  background: #fff;
  border-radius: 4px;
  min-height: 56px;
  overflow: hidden;
}
.library-card-preview img {
  max-width: 100%; max-height: 80px;
  object-fit: contain;
}
.library-card-label {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.library-card-delete {
  position: absolute;
  top: 4px; right: 4px;
  width: 20px; height: 20px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  background: rgba(0,0,0,.55);
  color: #fff;
  border: 0;
  cursor: pointer;
  font-size: 12px;
}
.library-card-delete:hover { background: var(--danger); }
.library-card-empty {
  grid-column: 1 / -1;
  text-align: center;
  padding: 24px;
  color: var(--text-muted);
  font-size: 12px;
}
.stamp-preview {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 4px 10px;
  border: 2px solid currentColor;
  border-radius: 4px;
  font-weight: 700;
  background: rgba(255,255,255,.95);
}
.qr-preview {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 160px;
  width: 100%;
  background: #fff;
  border-radius: 6px;
}
.qr-preview svg, .qr-preview canvas {
  display: block;
  max-width: 200px;
  max-height: 200px;
}
.qr-preview .qr-empty {
  color: #94a3b8; font-size: 12px;
}

/* ---- Phase 5: 検索バー ---- */
.search-bar {
  display: flex; align-items: center; gap: 6px;
  height: 36px;
  padding: 0 10px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-bar.hidden { display: none; }
.search-input {
  flex: 1;
  max-width: 360px;
  height: 26px;
  padding: 0 8px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.search-input:focus { border-color: var(--accent); }
.search-check {
  display: flex; align-items: center; gap: 4px;
  font-size: 11px; color: var(--text-muted);
  cursor: pointer;
}
.search-status {
  font-size: 11px;
  color: var(--text-muted);
  min-width: 60px;
  text-align: right;
}

/* ---- Phase 5: 検索ハイライトオーバーレイ ---- */
.search-overlay {
  position: absolute; top: 0; left: 0;
  pointer-events: none;
  z-index: 3;
}
.search-hit { fill: rgba(246, 224, 94, .45); stroke: rgba(214, 158, 46, .8); stroke-width: 1; }
.search-hit-active { fill: rgba(237, 137, 54, .55); stroke: rgba(192, 86, 33, .95); stroke-width: 1.5; }

/* ---- Phase 5: 墨消し（編集中の見た目） ---- */
#viewer.tool-mode-redaction .ann-svg { cursor: crosshair; }
.redaction-shape {
  fill: rgba(0, 0, 0, .82);
  stroke: rgba(0, 0, 0, .95);
  stroke-width: 1;
}
.redaction-stripe { fill: rgba(255, 255, 255, .25); }

/* ---- Phase 7: サムネイル幅リサイザー ---- */
.thumb-resizer {
  width: 6px;
  flex-shrink: 0;
  cursor: col-resize;
  background: transparent;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  transition: background .15s ease;
  touch-action: none;
}
.thumb-resizer:hover,
.thumb-resizer.dragging { background: var(--accent); }

/* ---- Phase 7: ページ番号オーバーレイ（ドラッグで微調整） ---- */
.page-number-overlay {
  position: absolute;
  z-index: 6;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, .82);
  border: 1px dashed rgba(66,153,225,.55);
  cursor: grab;
  user-select: none;
  touch-action: none;
  white-space: pre;
  pointer-events: auto;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans JP', sans-serif;
  line-height: 1.2;
}
.page-number-overlay:hover { border-color: var(--accent); background: rgba(255,255,255,.96); }
.page-number-overlay.dragging { cursor: grabbing; box-shadow: 0 4px 12px rgba(66,153,225,.4); }

/* ---- Phase 7: ショートカットモーダル ---- */
.shortcuts-modal-inner { width: min(640px, calc(100vw - 48px)); }
.shortcuts-list {
  display: flex; flex-direction: column;
  gap: 14px;
}
.shortcuts-section h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.shortcuts-row {
  display: grid;
  grid-template-columns: 1fr 110px 80px;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid rgba(255,255,255,.05);
}
.shortcuts-row:last-child { border-bottom: 0; }
.shortcuts-row .sc-label {
  font-size: 13px;
  color: var(--text);
}
.shortcuts-row .sc-key {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-l);
  border-radius: 4px;
  padding: 4px 8px;
  text-align: center;
  color: var(--text);
}
.shortcuts-row .sc-key.capturing {
  background: #fef3c7;
  border-color: #f59e0b;
  color: #1f2937;
}
.shortcuts-row .sc-key.conflict {
  border-color: var(--danger);
  color: var(--danger);
}
.shortcuts-row .sc-edit {
  height: 26px;
  padding: 0 8px;
  border-radius: 4px;
  border: 1px solid var(--border-l);
  background: var(--bg-primary);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
}
.shortcuts-row .sc-edit:hover { background: var(--bg-hover); border-color: var(--accent); }

/* ---- Phase 7: 階層化された右クリックメニュー ---- */
.context-menu-item.has-submenu {
  position: relative;
  padding-right: 22px;
}
.context-menu-item.has-submenu::after {
  content: "▸";
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 10px;
  color: rgba(15,23,42,.6);
}
.context-submenu {
  position: fixed;
  z-index: 10003;
  min-width: 180px;
  padding: 6px;
  background: #f8fafc;
  color: #111827;
  border: 1px solid rgba(15,23,42,.18);
  border-radius: 6px;
  box-shadow: 0 18px 44px rgba(0,0,0,.32);
}
.context-submenu.hidden { display: none; }

