/* Modern Heritage design tokens */
:root {
  --surface-page: #FAF6EE;
  --surface-card: #FFFEFB;
  --surface-alt:  #F2EBDC;
  --surface-dark: #1A1614;
  --surface-overlay: rgba(26, 22, 20, 0.55);

  --text-primary:   #1A1614;
  --text-secondary: #5C5651;
  --text-muted:     #8C857E;
  --text-inverse:   #FAF6EE;

  --border:        #E8DEC9;
  --border-strong: #C9B898;

  --accent:        #862A2D;
  --accent-hover:  #6B1E22;
  --accent-subtle: #F5E5E5;
  --accent-faint:  #FBF1F0;

  --gold:        #B68A2E;
  --gold-deep:   #8E6A1F;
  --gold-subtle: #F0E2BD;
  --gold-faint:  #FAF1D8;

  --success: #2D7A3E;
  --error:   #B83242;
  --warning: #C28A1F;

  --font-serif: "Source Han Serif SC", "Songti SC", "Noto Serif CJK SC", "Source Han Serif", "Times New Roman", serif;
  --font-sans:  "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans CJK SC", system-ui, -apple-system, sans-serif;
  --font-mono:  ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --t-caption: 12px;
  --t-small:   13px;
  --t-body:    15px;
  --t-lead:    17px;
  --t-h3:      20px;
  --t-h2:      26px;
  --t-h1:      36px;

  --s1:4px;  --s2:8px;  --s3:12px;  --s4:16px;  --s5:20px;
  --s6:24px; --s8:32px; --s10:40px; --s12:48px; --s16:64px;

  --r-sm:4px;  --r-md:8px;  --r-lg:12px;  --r-xl:20px;

  --shadow-sm: 0 1px 2px rgba(26,22,20,0.04), 0 1px 1px rgba(26,22,20,0.02);
  --shadow-md: 0 2px 6px rgba(26,22,20,0.06), 0 1px 2px rgba(26,22,20,0.03);
  --shadow-lg: 0 8px 24px rgba(26,22,20,0.08), 0 2px 6px rgba(26,22,20,0.04);

  --header-h: 64px;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);

  --banner-bg:       rgba(189, 123, 88, 0.55);
  --banner-bg-hover: rgba(165, 99, 71, 0.65);
  --banner-fg:       var(--text-primary);
  --banner-fg-muted: rgba(250, 244, 228, 0.72);
  --banner-border:   rgba(0, 0, 0, 0.18);
}

/* Styles copied from 9_29 HTML UI build (simplified for Stage 1) */

:root {
  --primary-color: #8b2635;
  --secondary-color: #f8f4f4;
  --accent-color: #b83650;
  --text-color: #2d1b1b;
  --border-color: #d4a5a5;
  --success-color: #28a745;
  --error-color: #dc3545;
  --warning-color: #ffc107;
  --dark-burgundy: #5d1a1a;
  --medium-burgundy: #8b2635;
  --light-burgundy: #b83650;
  --light-bg: #f8f9fa;
}

/* Remap legacy aliases onto new tokens */
:root {
  --primary-color:   var(--accent);
  --dark-burgundy:   var(--accent-hover);
  --medium-burgundy: var(--accent);
  --light-burgundy:  var(--surface-card);
  --secondary-color: var(--surface-alt);
  --accent-color:    var(--gold);
  --border-color:    var(--border);
  --light-bg:        var(--surface-page);
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--light-bg);
  color: var(--text-color);
  line-height: 1.6;
}
#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.hidden {
  display: none !important;
}

.app-header {
  background: var(--banner-bg);
  color: var(--banner-fg);
  padding: 0 var(--s6);
  border-bottom: 1px solid var(--banner-border);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-left, .header-right { display: flex; align-items: center; gap: var(--s3); flex: 0 0 auto; }
.header-center { display: flex; align-items: center; gap: var(--s3); flex: 1 1 auto; min-width: 0; justify-content: center; }
.brand-glyph { height: 30px; width: auto; color: var(--banner-fg); flex: 0 0 auto; }
.app-title {
  font-family: var(--font-serif);
  font-size: var(--t-h3);
  font-weight: 500;
  letter-spacing: 0.05em;
  color: var(--banner-fg);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.language-toggles {
  display: inline-flex;
  gap: 2px;
  background: rgba(255, 255, 255, 0.14);
  padding: 3px;
  border-radius: var(--r-md);
}
.language-btn {
  background: transparent;
  border: 0;
  border-radius: var(--r-sm);
  padding: 5px 12px;
  color: var(--banner-fg-muted);
  font-size: var(--t-small);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s var(--ease);
}
.language-btn:hover { color: var(--banner-fg); }
.language-btn.active {
  background: var(--surface-card);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.project-selector {
  display: flex;
  align-items: center;
}

.project-dropdown,
.stage-dropdown {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-color: var(--surface-card);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8' fill='none' stroke='%238E6A1F' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'><path d='M1 1.5l5 5 5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 10px 7px;
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 7px 32px 7px 14px;
  font-size: var(--t-small);
  font-family: var(--font-sans);
  font-weight: 400;
  cursor: pointer;
  transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease), background-color 0.15s var(--ease);
}
.project-dropdown { min-width: 180px; max-width: 280px; }
.project-dropdown:hover,
.stage-dropdown:hover {
  border-color: var(--border-strong);
  background-color: var(--surface-card);
}
.project-dropdown:focus,
.stage-dropdown:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-faint);
}

.stage-selector {
  display: flex;
  align-items: center;
}

/* (.stage-dropdown rule body removed — consolidated above with .project-dropdown) */
.stage-dropdown {
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s var(--ease);
  min-width: 200px;
}
.stage-dropdown:hover { border-color: var(--border-strong); }
.stage-dropdown:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-faint); }

.main-container {
  flex: 1;
  padding: 0.75rem;
}

.stage-panel {
  display: none;
  background: white;
  border-radius: 8px;
  padding: 2rem;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

.stage-panel.active {
  display: block;
  flex: 1;
}

.stage-panel h2 {
  color: var(--dark-burgundy);
  margin-bottom: 1.5rem;
  font-size: 1.5rem;
}

/* Auth / user menu */
.auth-section {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.8);
  color: #fff;
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 600;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
}

.user-menu {
  position: absolute;
  right: 0;
  top: 110%;
  background: #fff;
  color: #111827;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  min-width: 180px;
  z-index: 10;
}

.user-menu.hidden { display: none; }

.user-menu-info {
  padding: 10px 12px;
  border-bottom: 1px solid #e5e7eb;
}

.user-menu button {
  background: transparent;
  border: none;
  padding: 10px 12px;
  text-align: left;
  cursor: pointer;
}

.user-menu button:hover {
  background: #f3f4f6;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 50;
  overflow-y: auto;
  padding: 2vh 0;
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--surface-card);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 20px;
  width: 360px;
  max-width: 92vw;
  position: relative;
  box-shadow: var(--shadow-lg);
}

.auth-modal label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
  color: #111827;
}

.auth-modal input {
  padding: 8px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
}

.password-wrapper {
  display: flex;
  align-items: center;
  position: relative;
}
.password-wrapper input {
  flex: 1;
  padding-right: 36px;
}
.toggle-password {
  position: absolute;
  right: 4px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
  opacity: 0.5;
  line-height: 1;
}
.toggle-password:hover { opacity: 0.8; }
.toggle-password.active { opacity: 1; }

.tab-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.tab-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  background: #f9f1f1;
  cursor: pointer;
  font-weight: 600;
}

.tab-btn.active {
  background: var(--dark-burgundy);
  color: #fff;
  border-color: var(--dark-burgundy);
}

.auth-links {
  display: flex;
  justify-content: flex-end;
  margin: 4px 0 8px 0;
}

.link-btn {
  background: transparent;
  border: none;
  color: var(--dark-burgundy);
  cursor: pointer;
  font-size: 0.9rem;
  text-decoration: underline;
}

.close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
}

.remember-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
}

.auth-error {
  color: #b91c1c;
  min-height: 18px;
  font-size: 12px;
  margin: 4px 0;
}

.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(420px, 85vw);
  height: 100vh;
  background: #fff;
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.2);
  padding: 1.25rem;
  z-index: 12000;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.side-panel.hidden {
  display: none;
}

.side-panel h2 {
  margin-bottom: 0.75rem;
}

.side-panel form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.side-panel label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-weight: 600;
}

.side-panel input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 14px;
}

.side-close {
  position: absolute;
  top: 12px;
  left: 12px;
}

.profile-actions {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
}

.perm-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 60vh;
  overflow-y: auto;
}

.perm-card {
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 12px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  background: #fff;
}

.perm-card .actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.user-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border: 2px solid rgba(255,255,255,0.9);
  border-radius: 50%;
  font-size: 12px;
  line-height: 1;
}

.form-section {
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border-color);
}

.form-section:last-child {
  border-bottom: none;
}

.form-section h3 {
  margin-bottom: 1rem;
  color: var(--dark-burgundy);
  font-size: 1.2rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: var(--text-color);
}

.form-group input,
.form-group textarea {
  width: 100%;
  max-width: 480px;
  padding: 0.75rem;
  border: 2px solid var(--border-color);
  border-radius: 4px;
  font-size: 1rem;
}

.form-group textarea {
  min-height: 100px;
  resize: vertical;
}
.form-hint {
  font-size: 0.85rem;
  font-weight: 400;
  color: #6b7280;
}
.source-suggestions {
  margin-top: 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: #fff;
  max-width: 480px;
  max-height: 200px;
  overflow-y: auto;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.08);
}
.source-suggestion-item {
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
}
.source-suggestion-item:last-child {
  border-bottom: none;
}
.source-suggestion-item:hover {
  background: #f9f1f1;
}
.genre-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}
.genre-row select {
  min-width: 200px;
  padding: 0.75rem;
  border: 2px solid var(--border-color);
  border-radius: 4px;
  font-size: 1rem;
  background: #fff;
}
.genre-row input {
  flex: 1;
  min-width: 200px;
}

.primary-btn {
  background: var(--medium-burgundy);
  color: white;
  border: none;
  padding: 0.75rem 2rem;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

.primary-btn:hover {
  background: var(--dark-burgundy);
  transform: translateY(-1px);
}

.primary-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
  transform: none;
}

.secondary-btn {
  background: white;
  color: var(--primary-color);
  border: 2px solid var(--primary-color);
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.icon-btn {
  padding: 0.4rem;
  min-width: 2.1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.icon-btn svg {
  width: 16px;
  height: 16px;
  display: block;
}

.secondary-btn:hover {
  background: var(--primary-color);
  color: white;
}

.status-section {
  margin-top: 1rem;
  padding: 0.75rem;
  border-radius: 4px;
  background: #f4f4f4;
  border: 1px solid #ccc;
  font-family: Consolas, monospace;
  white-space: pre-wrap;
}

/* Stage 2 page view: scrollable window with internal pan */
#stage2-page-container {
  overflow: auto;
  max-height: 100vh;
}

#stage2-page-image {
  max-height: none;
}

#stage2 .stage2-reading-label {
  margin-left: 12px;
  font-size: 0.85rem;
}

#stage2 .stage2-reading-select {
  margin-left: 6px;
  padding: 4px 6px;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  font-size: 0.85rem;
}

/* Stage 3 page view: mirrors Stage 2 layout */
#stage3-page-container {
  overflow: auto;
  max-height: 100vh;
}

#stage3-page-image {
  max-height: none;
}

/* Modal for box labeling */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: var(--surface-card);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 1.5rem 2rem;
  box-shadow: var(--shadow-lg);
  max-width: 360px;
  width: 100%;
}

.modal-content h3 {
  margin-bottom: 1rem;
  color: var(--dark-burgundy);
}

.modal-actions {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}

/* Project deletion — checkbox column */
.delete-check-col {
  width: 36px;
}

.delete-check-cell {
  width: 36px;
  text-align: center;
  vertical-align: middle;
  padding: 0 var(--s2);
}

/* .finder-table is table-layout:fixed, so this column cannot grow past 36px.
   The generic `.finder-table thead th` rule is more specific than
   .delete-check-cell and would restore left alignment and 0.85rem side padding,
   pushing the trash icon off the checkboxes below it. */
.finder-table thead th.delete-check-cell,
.finder-table tbody td.delete-check-cell {
  padding-left: 0;
  padding-right: 0;
  text-align: center;
}

.delete-check {
  cursor: pointer;
  accent-color: var(--error);
  width: 15px;
  height: 15px;
}

/* Trash-can icon button in the checkbox column header */
.header-delete-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  padding: var(--s1);
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--r-sm);
  transition: color 0.15s var(--ease), opacity 0.15s var(--ease);
}

.header-delete-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.header-delete-btn:not(:disabled):hover,
.header-delete-btn:not(:disabled):focus-visible {
  color: var(--error);
}

.header-delete-btn:focus-visible {
  outline: 2px solid var(--error);
  outline-offset: 2px;
}

/* Danger button — uses --error token, never a hardcoded colour */
.danger-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  padding: 0 var(--s4);
  height: 36px;
  background: var(--error);
  color: var(--text-inverse);
  border: none;
  border-radius: var(--r-md);
  font-size: var(--t-small);
  font-family: var(--font-sans);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s var(--ease), opacity 0.15s var(--ease);
}

.danger-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.danger-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.danger-btn:focus-visible {
  outline: 2px solid var(--error);
  outline-offset: 2px;
}

/* Delete confirmation modal helpers */
.delete-modal-names {
  font-weight: 600;
  color: var(--text-primary);
  word-break: break-word;
  margin-bottom: var(--s2);
}

.delete-modal-counts {
  color: var(--text-secondary);
  font-size: var(--t-small);
  margin-bottom: var(--s3);
}

.delete-modal-warning {
  color: var(--error);
  font-size: var(--t-small);
  font-weight: 500;
}

.delete-modal-error {
  color: var(--error);
  font-size: var(--t-small);
  min-height: 1.2em;
}

/* Stage 4 skeleton workspace */
#stage4.stage-panel {
  display: none;
  flex-direction: column;
  gap: 1.25rem;
  padding: 0.75rem;
}

#stage4.stage-panel.active {
  display: flex;
  flex: 1;
}

.stage4-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: linear-gradient(90deg, #fff8f8, #fff);
}

.stage4-zoom-group {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.stage4-view-window {
  width: 100%;
  height: 8in;
  border: 1px dashed var(--border-color);
  border-radius: 10px;
  background: #fff;
  overflow: auto;
  box-shadow: 0 1px 3px rgba(45, 42, 36, 0.05);
  padding: 1rem;
}

.stage4-pan-surface {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 100%;
  border-radius: 8px;
  background: #fff;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.05);
  cursor: grab;
}

.stage4-pan-placeholder {
  position: absolute;
  inset: 0;
  display: none;
}

.stage4-row {
  overflow: visible;  /* allow absolute children to extend if needed */
}

/* F8: per-column cell wrapper in the horizontal CSS-grid layout.
   Each .stage4-col-cell is a grid item; image and textbox sit inside it
   via position:absolute, contained by position:relative on the cell. */
.stage4-col-cell {
  position: relative;
  overflow: visible;
}

.stage4-column-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: none;
  z-index: 2;
}

.stage4-column-divider {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: var(--border-strong, #C9B898);
  opacity: 0.55;
  pointer-events: none;
}

.stage4-column {
  position: absolute;
  top: 0;
  bottom: 0;
  border: none;
  background: transparent;
}

.stage4-prep-container {
  position: relative;
  display: inline-block;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}

.stage4-prep-container canvas {
  display: block;
}

.stage4-char-layer {
  position: absolute;
  inset: 0;
  pointer-events: auto;
  z-index: 3;
}

.stage4-char-box {
  position: absolute;
  border: 1px solid #c82829;
  background: rgba(200, 40, 41, 0.08);
  box-sizing: border-box;
  pointer-events: auto;
  cursor: pointer;
  user-select: none;
  touch-action: none;
}

.stage4-char-box.annotated {
  border-color: var(--annotation-color, #1f6f5c);
}

.stage4-char-box:hover {
  border-color: #8b2635;
  box-shadow: 0 0 0 2px rgba(139, 38, 53, 0.2);
}

.stage4-char-box.selected {
  border-color: var(--medium-burgundy);
  box-shadow: 0 0 0 2px rgba(139, 38, 53, 0.25);
}

.stage4-char-box.bbox-dirty {
  border-color: #f5a623;
  box-shadow: 0 0 0 2px rgba(245, 166, 35, 0.4);
}

.stage4-char-box.bbox-dirty.selected {
  border-color: #f5a623;
  box-shadow: 0 0 0 2px rgba(139, 38, 53, 0.5), 0 0 0 4px rgba(245, 166, 35, 0.4);
}

.stage4-char-box.bbox-deleted {
  border-color: #c82829;
  background: rgba(200, 40, 41, 0.25);
  opacity: 0.5;
  pointer-events: none;
}

.stage4-char-box.bbox-new {
  border: 2px dashed #2ecc71;
  background: rgba(46, 204, 113, 0.08);
}

.stage4-bbox-preview {
  position: absolute;
  border: 2px dashed #2ecc71;
  background: rgba(46, 204, 113, 0.08);
  pointer-events: none;
  box-sizing: border-box;
}

.stage4-no-text-select,
.stage4-no-text-select * {
  user-select: none !important;
}

.stage4-char-handle {
  position: absolute;
  width: 10px;
  height: 10px;
  background: rgba(139, 38, 53, 0.6);
  border-radius: 2px;
  border: 1px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
  pointer-events: auto;
}

.stage4-char-handle-nw {
  top: -5px;
  left: -5px;
  cursor: nwse-resize;
}

.stage4-char-handle-ne {
  top: -5px;
  right: -5px;
  cursor: nesw-resize;
}

.stage4-char-handle-sw {
  bottom: -5px;
  left: -5px;
  cursor: nesw-resize;
}

.stage4-char-handle-se {
  bottom: -5px;
  right: -5px;
  cursor: nwse-resize;
}

.stage4-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}

.stage4-placement-layer {
  position: relative;
  width: 100%;
  min-height: 100%;
  z-index: 3;
}

.stage4-line-num {
  font-size: 10px;
  color: #aaa;
  text-align: left;
  line-height: 1;
  pointer-events: none;
  user-select: none;
  padding-left: 4px;
  padding-top: 2px;
}

.stage4-line-seg {
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
}

.stage4-line-flag {
  position: absolute;
  top: 14px;
  left: 4px;
  z-index: 5;
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  line-height: 1;
  border-radius: 3px;
  background: transparent;
  border: none;
  padding: 0;
  user-select: none;
}

/* Role-aware classification: stage4LineFlagColor resolves to "red" or "green"
   per viewer tier, and styles.css just maps those to the Modern Heritage
   color tokens. data-pending is kept as a back-compat alias. */
.stage4-line-flag[data-pending="true"]   { color: var(--error); }
.stage4-line-flag[data-pending="false"]  { color: var(--success); }
.stage4-line-flag[data-status="red"]     { color: var(--error); }
.stage4-line-flag[data-status="green"]   { color: var(--success); }

.stage4-line-flag[data-interactive="true"] {
  cursor: pointer;
}

.stage4-line-flag[data-interactive="false"] {
  cursor: default;
  pointer-events: none;
}

.stage4-char-strip {
  display: flex;
  gap: 4px;
  align-items: center;
  justify-content: flex-start;
  padding: 2px;
  background: rgba(0, 0, 0, 0.02);
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.6);
}

.stage4-char-strip-item {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}

.stage4-char-strip-item img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.stage4-char-strip-item.selected {
  outline: 2px solid rgba(139, 0, 0, 0.55);
  background-color: rgba(139, 0, 0, 0.08);
}

.stage4-line-box {
  border: 1px solid var(--border-strong);
  background: var(--surface-card);
  box-shadow: 0 1px 2px rgba(45, 42, 36, 0.04);
  padding: 4px;
  font-size: 20px;
  line-height: 1.3;
  color: #2d1b1b;
  overflow: auto;
  cursor: text;
  font-family: 'YiClassic', 'YiReading', 'YiDisplayWX9', sans-serif !important;
  font-weight: normal;
}

.stage4-line-box:focus {
  outline: 2px solid rgba(139, 38, 53, 0.25);
  background: var(--surface-card);
}

.stage5-issue-highlight {
  outline: 2px solid #27ae60 !important;
  outline-offset: 1px;
  background: rgba(39, 174, 96, 0.06) !important;
}

#stage5-issues-panel {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 320px;
  max-height: 60vh;
  overflow-y: auto;
  background: #fff;
  border: 1px solid #27ae60;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  z-index: 5000;
  padding: 12px;
  font-size: 13px;
}

#stage5-issues-panel h3 {
  margin-bottom: 8px;
  font-size: 14px;
  color: #1a1a1a;
}

.stage5-issue-item {
  padding: 6px 0;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}

.stage5-issue-item:hover {
  background: #f5fff8;
}

.stage5-issue-item:focus {
  outline: 2px solid #27ae60;
  outline-offset: -2px;
  background: #e8f5e9;
}

.stage5-panel-icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0 4px;
  color: #555;
}
.stage5-panel-icon-btn:hover {
  color: #000;
}

.stage5-issue-item.resolved {
  opacity: 0.45;
  text-decoration: line-through;
}

.stage4-column-separator {
  pointer-events: none;
}

.stage4-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.stage4-modal-panel {
  background: #fff;
  border-radius: 6px;
  padding: 1rem;
  min-width: 320px;
  max-width: 520px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.stage4-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.stage4-modal-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.stage4-modal-body canvas {
  width: 64px;
  height: 64px;
  object-fit: contain;
  image-rendering: pixelated;
}

.char-modal-tabs {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.char-modal-tab-buttons {
  display: flex;
  gap: 6px;
}

.char-modal-tab-buttons button {
  flex: 1;
  padding: 6px 8px;
  border: 1px solid #ccc;
  background: #f7f7f7;
  cursor: pointer;
}

.char-modal-tab-buttons button.active {
  background: #ffe9e9;
  border-color: #d66;
}

.char-modal-preview-slot {
  display: flex;
  gap: 10px;
  align-items: center;
  min-height: 72px;
  padding: 4px 0;
}

.char-preview-wrapper {
  width: 64px;
  height: 64px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fdfdfd;
  border: 2px solid #c00;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.25);
  padding: 2px;
  position: relative;
  z-index: 10;
}

.char-modal-tab-content {
  border: 1px solid #e5e5e5;
  padding: 8px;
  border-radius: 4px;
}

.char-modal-pane {
  display: none;
  flex-direction: column;
  gap: 6px;
}

.char-modal-pane.active {
  display: flex;
}

.char-modal-results {
  max-height: 220px;
  overflow: auto;
  border: 1px solid #eee;
  padding: 6px;
  background: #fafafa;
  font-size: 0.9rem;
}

.char-modal-result {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 2px;
  border-bottom: 1px solid #eee;
}

.char-modal-result.selected {
  border: 1px solid #c00;
  background: #ffecec;
}

.char-modal-result:last-child {
  border-bottom: none;
}

.char-modal-thumb {
  width: 48px;
  height: 48px;
  border: 1px solid #ddd;
  background: #fff;
  object-fit: contain;
  flex-shrink: 0;
}

.char-modal-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* Stage 5 preview */
.stage5-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  border-radius: 6px 6px 0 0;
  border: 1px solid var(--border-color);
  border-bottom: none;
  background: linear-gradient(90deg, #fff8f8, #fff);
}

.stage5-view-window {
  width: 100%;
  height: 8in;
  border: 1px solid var(--border-color);
  overflow: auto;
  background: #fefefe;
  box-sizing: border-box;
}

.stage5-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  border: 1px solid var(--border-color);
  border-top: none;
  border-radius: 0 0 6px 6px;
  background: #fff;
}

.stage5-lines {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
}
.stage5-line-card {
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 8px;
  background: #fafafa;
}
.stage5-line-header {
  font-weight: bold;
  margin-bottom: 6px;
}
.stage5-tokens-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.stage5-gloss-row {
  margin-top: 4px;
  display: flex;
  gap: 6px;
}
.stage5-gloss-input {
  flex: 1 1 0;
  min-width: 0;
  padding: 4px 6px;
  font-size: 13px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}
.stage5-gloss-input:focus {
  outline: none;
  border-color: #2196f3;
}
.stage5-token {
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 6px;
  width: 120px;
  box-sizing: border-box;
  background: #fff;
  user-select: none;
}
.stage5-token * {
  user-select: none;
}
.stage5-token.annotation-selected {
  border-color: #1f6f5c;
  box-shadow: 0 0 0 2px rgba(31, 111, 92, 0.35);
}
.stage5-token.annotated {
  box-shadow: 0 0 0 2px var(--annotation-color, #1f6f5c);
}
.stage5-token.annotated:not(.bh-group) {
  border-color: var(--annotation-color, #1f6f5c);
}
.stage5-token.strokes-segmented {
  border-left: 4px solid #0f766e;
  background: linear-gradient(90deg, rgba(15, 118, 110, 0.12) 0%, #fff 40%);
}
.stage5-token.strokes-auto {
  border-left: 4px dotted #9ca3af;
  background: #fff;
}
.stage5-token.bh-group {
  border-color: rgba(139, 0, 0, 0.6);
  background: rgba(139, 0, 0, 0.05);
}
.stage5-token.bh-group .stage5-token-han {
  font-weight: 600;
}
.stage5-token-img {
  display: block;
  width: 100%;
  height: 80px;
  object-fit: contain;
  background: #f7f7f7;
}
canvas.stage5-token-img {
  object-fit: unset;
}
.stage5-token-ipa {
  font-size: 13px;
  margin-top: 4px;
}
.stage5-token-yi {
  font-size: 13px;
  margin-top: 4px;
  font-family: 'YiClassic', 'YiReading', 'YiDisplayWX9', sans-serif !important;
}
.stage5-token-han {
  font-size: 13px;
  color: #444;
  margin-top: 4px;
}
.stage5-edit-modal {
  width: 860px;
  max-width: 95vw;
}
.stage5-edit-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.stage5-edit-header h3 {
  margin: 0;
}
.stage5-status-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.stage5-status-badge.manual {
  background: #0f766e;
  color: #fff;
}
.stage5-status-badge.auto {
  background: #e5e7eb;
  color: #6b7280;
}
.stage5-status-badge.unsegmented {
  background: #fef3c7;
  color: #92400e;
}
.stage5-edit-preview {
  display: flex;
  justify-content: center;
  margin: 8px 0 12px 0;
}
.stage5-edit-preview img {
  width: 96px;
  height: 96px;
  object-fit: contain;
  background: #f7f7f7;
  border: 1px solid #ddd;
}
.stage5-edit-fields {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.stage5-edit-fields label {
  font-weight: 600;
  font-size: 0.9rem;
}
.stage5-edit-fields input {
  padding: 8px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
}

.stage5-edit-group-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.stage5-edit-group-actions button {
  font-size: 0.8rem;
  padding: 4px 10px;
}
.stage5-edit-group-browser {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 6px;
  max-height: 200px;
  overflow-y: auto;
  margin-top: 6px;
  padding: 6px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fafafa;
}
.stage5-edit-group-browser .group-thumb {
  width: 52px;
  height: 52px;
  object-fit: contain;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  background: #fff;
}
.stage5-edit-group-browser .group-thumb:hover {
  border-color: #8b2635;
  box-shadow: 0 0 0 2px rgba(139, 38, 53, 0.2);
}

.stage5-radical-editor {
  display: flex;
  gap: 16px;
  margin-top: 14px;
  align-items: flex-start;
}

.stage5-radical-canvas-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#stage5-radical-canvas {
  width: 160px;
  height: 160px;
  border: 1px solid #d1d5db;
  background: #fff;
  image-rendering: pixelated;
}

.stage5-radical-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  justify-content: center;
}

.stage5-radical-toolbar .secondary-btn.active {
  background: var(--primary-color);
  color: #fff;
}

.stage5-radical-zoom {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  padding-left: 6px;
  border-left: 1px solid #e2e8f0;
}

.stage5-radical-zoom span {
  min-width: 48px;
  text-align: center;
}

.stage5-radical-side {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stage5-radical-list {
  max-height: 240px;
  overflow: auto;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stage5-radical-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.secondary-btn.danger-btn {
  color: var(--error);
  border-color: var(--error);
}

.secondary-btn.danger-btn:hover {
  background: var(--accent-faint);
}

.stage5-radical-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stage5-radical-row.active {
  background: rgba(15, 118, 110, 0.08);
  border-radius: 6px;
}

.stage5-radical-thumb {
  width: 32px;
  height: 32px;
  border: 1px solid #cbd5e1;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stage5-radical-thumb img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.stage5-stroke-label {
  flex: 1;
  font-size: 0.85rem;
  color: #475569;
}

.stage5-radical-picker {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stage5-radical-selected {
  border: 1px solid #cbd5e1;
  background: #fff;
  border-radius: 4px;
  padding: 2px 6px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  cursor: pointer;
}

.stage5-radical-selected img {
  width: 26px;
  height: 26px;
  object-fit: contain;
}

.stage5-radical-menu {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: #fff;
  max-height: 160px;
  overflow: auto;
}

.stage5-radical-menu.hidden {
  display: none;
}

.stage5-radical-option {
  border: 1px solid #cbd5e1;
  background: #fff;
  border-radius: 4px;
  padding: 2px 4px;
  min-width: 32px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  cursor: pointer;
}

.stage5-radical-option img {
  width: 26px;
  height: 26px;
  object-fit: contain;
}

.stage5-radical-option.active {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 1px var(--primary-color);
}

.stage5-radical-row .secondary-btn {
  padding: 4px 8px;
}

.char-modal-title {
  font-weight: 600;
  font-size: 0.95rem;
}

.char-modal-sub {
  font-size: 0.8rem;
  color: #555;
}

.char-modal-select-row {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.char-inline-img {
  display: inline-block;
  width: 20px;
  height: 20px;
  vertical-align: middle;
  background: center center / contain no-repeat #fff;
  border: 1px solid #ccc;
  margin: 0 1px;
}

.stage4-modal-field {
  font-size: 0.95rem;
}

.stage4-error {
  padding: 1rem;
  color: #b22222;
  background: #fff2f2;
  border: 1px solid #e0b3b3;
  margin-top: 1rem;
}
@font-face {
  font-family: 'YiDisplayWX9';
  src: url('./fonts/YiDisplayWX9.ttf') format('truetype');
  font-display: swap;
}

@font-face {
  font-family: 'YiClassic';
  src: url('./fonts/YiClassic.ttf') format('truetype');
  font-display: swap;
}

@font-face {
  font-family: 'YiReading';
  src: url('./fonts/YiReading.ttf') format('truetype');
  font-display: swap;
}

.yi-font-debug {
  font-family: 'YiDisplayWX9', 'YiClassic', 'YiReading', sans-serif !important;
  font-size: 16px;
  color: #444;
}

/* ---- Group Dedup Tool ---- */

.dedup-toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
  padding: 0.75rem;
  background: var(--secondary-color);
  border-radius: 8px;
}

.dedup-toolbar-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.dedup-toolbar-row label {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.85rem;
  white-space: nowrap;
}

.dedup-toolbar-row select,
.dedup-toolbar-row input[type="number"] {
  padding: 0.25rem 0.4rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 0.85rem;
}

.dedup-stats {
  font-size: 0.85rem;
  color: #666;
  margin-left: auto;
}

.dedup-pairs {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 0.25rem;
}

.dedup-card {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 1rem;
  padding: 0.75rem;
  border: 2px solid #ddd;
  border-radius: 8px;
  background: #fff;
  align-items: start;
}

.dedup-card.flag-duplicate {
  border-color: #c0392b;
  background: #fdf2f2;
}

.dedup-card.flag-distinct {
  border-color: #27ae60;
  background: #f2fdf5;
}

.dedup-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
}

.dedup-group-img {
  width: 80px;
  height: 80px;
  object-fit: contain;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: #fafafa;
}

.dedup-group-name {
  font-weight: 600;
  font-size: 0.9rem;
  text-align: center;
  word-break: break-all;
}

.dedup-group-info {
  font-size: 0.75rem;
  color: #888;
  text-align: center;
}

.dedup-group-members {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 0.25rem;
}

.dedup-member-thumb {
  width: 28px;
  height: 28px;
  object-fit: contain;
  border: 1px solid #eee;
  border-radius: 2px;
  background: #fafafa;
}

.dedup-scores {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-width: 120px;
}

.dedup-score-val {
  font-size: 0.85rem;
  text-align: center;
}

.dedup-score-val strong {
  font-size: 1rem;
}

.dedup-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.dedup-btn {
  padding: 0.25rem 0.6rem;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  background: #f5f5f5;
}

.dedup-btn:hover {
  background: #e0e0e0;
}

.dedup-btn-dup {
  color: #c0392b;
  border-color: #c0392b;
}

.dedup-btn-dup:hover {
  background: #fce4e4;
}

.dedup-btn-dist {
  color: #27ae60;
  border-color: #27ae60;
}

.dedup-btn-dist:hover {
  background: #e4fce8;
}

.dedup-loading {
  text-align: center;
  padding: 2rem;
  color: #888;
  font-size: 0.95rem;
}

/* ---- Project Dashboard Redesign ---- */

.project-search-wrap {
  margin-bottom: 1rem;
}

.project-search-input {
  width: 100%;
  padding: 0.6rem 1rem;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}
.project-search-input:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(139, 38, 53, 0.1);
}

.project-search-results {
  margin-top: 0.5rem;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  background: #fff;
  max-height: 360px;
  overflow-y: auto;
}
.project-search-results.hidden { display: none; }

.search-result-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #f3f4f6;
  flex-wrap: wrap;
}
.search-result-row:last-child { border-bottom: none; }

.search-result-info { flex: 1; min-width: 0; }
.search-result-info .result-name {
  font-weight: 600;
  color: var(--text-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-result-info .result-meta {
  font-size: 0.82rem;
  color: #6b7280;
}

.access-request-form {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.4rem;
  width: 100%;
}
.access-request-form textarea {
  width: 100%;
  padding: 0.4rem 0.6rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.875rem;
  resize: vertical;
  font-family: inherit;
}
.request-sent  { color: var(--success-color); font-size: 0.875rem; }
.request-error { color: var(--error-color);   font-size: 0.875rem; }

/* Stage-grouped sections */
.stage-group-header {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #9ca3af;
  padding: 0.5rem 0.25rem 0.25rem;
  margin-top: 0.75rem;
}
.stage-group-header:first-child {
  margin-top: 0;
}
.stage-group-header.collapsible {
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.stage-group-header.collapsible:hover {
  color: #6b7280;
}
.stage-group-chevron {
  font-size: 0.6rem;
  transition: transform 0.15s;
}
.stage-group-count {
  font-weight: 400;
  font-size: 0.72rem;
  color: #b0b5bd;
}
.collapsed {
  display: none;
}

/* Finder-style projects table — Stage 0 main list */
.finder-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  overflow: hidden;
  font-size: var(--t-small);
  box-shadow: var(--shadow-sm);
}
.finder-table thead th {
  text-align: left;
  font-weight: 600;
  padding: 0.6rem 0.85rem;
  background: var(--surface-alt);
  border-bottom: 2px solid var(--gold-subtle);
  cursor: pointer;
  user-select: none;
  color: var(--text-secondary);
  font-size: var(--t-caption);
  letter-spacing: 0.05em;
  white-space: nowrap;
}
.finder-table thead th:hover { color: var(--text-primary); }
.finder-table thead th.sort-active { color: var(--gold-deep); }
.finder-table thead th.sort-asc::after { content: " ▲"; font-size: 0.7em; opacity: 0.85; }
.finder-table thead th.sort-desc::after { content: " ▼"; font-size: 0.7em; opacity: 0.85; }
.finder-table tbody td { padding: 0.55rem 0.85rem; border-bottom: 1px solid var(--border); color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.finder-table tbody tr { cursor: pointer; transition: background 0.12s var(--ease); }
.finder-table tbody tr:hover { background: var(--accent-faint); }
.finder-table tbody tr.selected { background: var(--gold-faint); box-shadow: inset 3px 0 0 var(--gold); }
.finder-table tbody tr:last-child td { border-bottom: none; }
.finder-table .name-cell {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-family: var(--font-serif);
  font-weight: 500;
  color: var(--text-primary);
  font-size: var(--t-body);
}
.finder-table .name-cell svg { width: 16px; height: 16px; color: var(--accent); flex-shrink: 0; }
.finder-table-empty { text-align: center; color: var(--text-muted); padding: 1.5rem !important; font-style: italic; }

/* Invites popup — auto-shown on dashboard load when invites are pending */
#invites-popup .modal-content.invites-popup-content {
  max-width: 720px;
  width: 92vw;
  padding: 1.5rem 1.75rem;
  position: relative;
}
#invites-popup .invites-popup-close {
  position: absolute;
  top: 0.75rem;
  right: 0.85rem;
  background: transparent;
  border: 0;
  font-size: 1.6rem;
  line-height: 1;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: var(--r-sm);
  transition: background 0.12s var(--ease), color 0.12s var(--ease);
}
#invites-popup .invites-popup-close:hover {
  background: var(--surface-alt);
  color: var(--accent);
}
#invites-popup h3 {
  font-family: var(--font-serif);
  font-weight: 600;
  color: var(--accent);
  font-size: var(--t-h3);
  margin-bottom: 1rem;
  letter-spacing: 0.02em;
}
#invites-popup .finder-table tbody tr { cursor: default; }
#invites-popup .finder-table tbody tr:hover { background: transparent; }
#invites-popup .invite-actions { display: flex; gap: 0.4rem; white-space: nowrap; }
#invites-popup .invite-actions .primary-btn,
#invites-popup .invite-actions .secondary-btn {
  padding: 0.3rem 0.85rem;
  font-size: var(--t-small);
  border-radius: var(--r-sm);
}

/* Invites section — kept separate from the main table per design */
.project-list-section.invites-section h5 {
  font-family: var(--font-serif);
  font-size: var(--t-body);
  color: var(--accent);
  margin-bottom: 0.5rem;
  letter-spacing: 0.02em;
}

.project-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.5rem;
  padding-bottom: 0.25rem;
}

.project-card {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 0.75rem 0.875rem;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.project-card:hover {
  border-color: var(--light-burgundy);
  box-shadow: 0 2px 8px rgba(139, 38, 53, 0.08);
}
.project-card.selected {
  outline: 2px solid var(--primary-color);
  border-color: transparent;
}

.project-card-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.project-card-source {
  font-size: 0.78rem;
  color: #6b7280;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.project-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 0.4rem;
}

.badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
}
.badge-pages       { background: #f3f4f6; color: #374151; }
.badge-viewer      { background: #f3f4f6; color: #6b7280; }
.badge-editor      { background: #ccfbf1; color: #0f766e; }
.badge-co_author   { background: #fef3c7; color: #b45309; }
.badge-co_creator  { background: #fce7f3; color: var(--primary-color); }
.badge-strokes     { background: #d1fae5; color: #065f46; }

.project-card-collabs {
  display: flex;
  gap: 3px;
  margin-top: 0.4rem;
}
.collab-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--medium-burgundy);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  cursor: default;
}

.project-empty {
  padding: 0.75rem;
  text-align: center;
  color: #9ca3af;
  font-size: 0.875rem;
}

.invite-row {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #eee;
}
.invite-row-title {
  font-weight: 600;
}
.invite-row-meta {
  font-size: 0.85rem;
  color: #555;
}

/* ============================================================
   Stage 4 — "Modern Heritage" aesthetic pass (scoped to #stage4)
   Calm cream surface, hairline borders, Yi red used sparingly,
   gold accents. No global blast radius: every rule is under
   #stage4 so other stages keep their existing palette.
   ============================================================ */
#stage4 {
  --s4-paper: #faf6ee;   /* cream page */
  --s4-card: #fffefb;    /* card / surface */
  --s4-ink: #2d2a24;     /* primary text */
  --s4-muted: #8a8170;   /* secondary text */
  --s4-line: #e8dec9;    /* hairline border */
  --s4-red: #862a2d;     /* Yi red — sparing */
  --s4-red-dark: #6f2224;
  --s4-gold: #b68a2e;    /* gold accent */
}

/* Toolbar: quiet card instead of pink gradient.
   Bottom corners are squared so it reads as one block with the header rail. */
#stage4 .stage4-toolbar {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 0.75rem;
  box-shadow: 0 1px 3px rgba(45, 42, 36, 0.05);
  display: flex;
  align-items: center;
  padding: 0.5rem 0.75rem;
  gap: var(--s4);
}
#stage4 .stage4-toolbar-actions {
  display: inline-flex;
  align-items: center;
  gap: var(--s5);
}
#stage4 .toolbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
#stage4 .toolbar-btn span {
  font-size: 0.85rem;
}
#stage4 .stage4-zoom-group {
  display: inline-flex;
  gap: 4px;
}
#stage4 .stage4-zoom-group .secondary-btn {
  min-width: 28px;
  padding: 0.25rem 0.55rem;
}

/* Non-scrolling header rail — sits between toolbar and view-window, clips the
   translated frozen-header div so horizontal scroll moves labels in lockstep. */
#stage4 .stage4-header-rail {
  position: relative;
  overflow: hidden;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-bottom: 2px solid var(--gold);
  border-radius: 6px 6px 0 0;
  box-shadow: 0 1px 0 rgba(182, 138, 46, 0.08);
  padding-left: 1rem;
  /* height is set dynamically by syncHeader() to HEADER_HEIGHT * zoom */
  height: 30px;
}
/* Tool buttons: hairline, calm; red reserved for the primary action */
#stage4 .secondary-btn {
  background: var(--s4-card);
  color: var(--s4-ink);
  border: 1px solid var(--s4-line);
  font-weight: 500;
}
#stage4 .secondary-btn:hover {
  background: var(--s4-paper);
  border-color: var(--s4-gold);
  color: var(--s4-ink);
}
#stage4 .primary-btn {
  background: var(--s4-red);
  border-radius: 6px;
  padding: 0.5rem 1.1rem;
  font-weight: 600;
}
#stage4 .primary-btn:hover {
  background: var(--s4-red-dark);
}
#stage4 .toolbar-btn { padding: 0.35rem 0.7rem; }
#stage4 .toolbar-btn svg { width: 16px; height: 16px; }

/* The viewing surface */
#stage4 .stage4-view-window {
  background: var(--s4-paper);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 8px 8px;
  box-shadow: 0 2px 8px rgba(45, 42, 36, 0.06);
}
#stage4 .stage4-pan-surface {
  background: transparent;
  box-shadow: inset 0 0 0 1px rgba(232, 222, 201, 0.6);
}

/* Frozen column-label header: now mounted in the opaque rail above the
   scroll window, so it needs no independent background or shadow. */
#stage4 .stage4-frozen-header {
  background: transparent;
}
#stage4 .stage4-header-cell {
  border-right: 1px solid rgba(232, 222, 201, 0.7);
}
#stage4 .stage4-header-label {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--gold-deep);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
#stage4 .stage4-header-toggle,
#stage4 .stage4-header-restore {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 0.7rem;
  line-height: 1;
  padding: 2px 3px;
  border-radius: 3px;
  color: var(--s4-muted);
  transition: color 0.15s ease, background 0.15s ease;
}
#stage4 .stage4-header-toggle:hover {
  color: var(--s4-red);
  background: rgba(134, 42, 45, 0.07);
}
#stage4 .stage4-header-restore {
  color: var(--s4-gold);
}
#stage4 .stage4-header-restore:hover {
  color: var(--s4-red);
  background: rgba(182, 138, 46, 0.1);
}
#stage4 .stage4-header-restore-label {
  font-family: Georgia, "Songti SC", "Times New Roman", serif;
  font-size: 0.8rem;
  font-weight: 600;
  color: inherit;
}
#stage4 .stage4-restore-gutter {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 0.4rem 0 0.5rem;
  background: var(--s4-paper);
  border-left: 1px solid var(--s4-line);
  box-shadow: -6px 0 6px -4px rgba(45, 42, 36, 0.12);
  z-index: 60;
}
#stage4 .stage4-restore-gutter .stage4-header-restore {
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

/* ── Per-char text-slot editors (IPA / Han-literal rows) ─────────────────────
   Mirror the yi-slot-editor / yi-slot appearance so the three stacked rows
   align column-by-column.  Uses the same Modern-Heritage tokens as the Yi
   slots so colours stay consistent. */
.text-slot-editor {
  display: flex;
  align-items: center;
  gap: 2px;
  white-space: nowrap;
  overflow: hidden;
}

.text-slot {
  display: inline-block;
  text-align: center;
  flex: 1 1 22px;
  min-width: 22px;
  height: 22px;
  border: 1px solid rgba(0, 0, 0, 0.25);
  background: rgba(255, 255, 255, 0.85);
  border-radius: 2px;
  line-height: 1.2;
  user-select: text;
  padding: 0 4px;
  box-sizing: border-box;
  font-family: inherit;
  font-size: 11px;
}

.text-slot.text-slot-empty {
  background: rgba(0, 0, 0, 0.03);
}

.text-slot.active {
  outline: 2px solid rgba(139, 38, 53, 0.25);
  background: #fff;
}

.text-slot.bh-selected {
  outline: 2px solid rgba(139, 0, 0, 0.55);
  background-color: rgba(139, 0, 0, 0.08);
}

.text-slot.bh-group {
  border-color: rgba(139, 0, 0, 0.55);
  background-color: rgba(139, 0, 0, 0.06);
}

/* Classref slot — subtle gold accent so it's distinguishable from plain slots.
   Tokens: --s4-gold / --s4-line from the Modern-Heritage #stage4 block. */
.stage4-slot-classref {
  border-color: rgba(182, 138, 46, 0.65) !important;
  background: rgba(182, 138, 46, 0.10) !important;
  color: rgba(100, 70, 0, 0.9);
  font-style: italic;
}

/* ── Word-gloss sub-row ───────────────────────────────────────────────────────
   These rules live in the stylesheet (not injected via ensureWordGlossStyles)
   so token values come from Modern-Heritage custom-properties.  The injected
   block in ensureWordGlossStyles() retains the structural/positional rules; only
   the colour/appearance rules are overridden here. */
#stage4 .stage4-word-gloss-box {
  border-color: rgba(134, 42, 45, 0.35);
  background: rgba(134, 42, 45, 0.04);
  color: var(--s4-ink, #2d2a24);
  font-family: Georgia, "Songti SC", "Times New Roman", serif;
  font-size: 11px;
  border-radius: 3px;
}

#stage4 .stage4-word-gloss-box.active,
#stage4 .stage4-word-gloss-box:focus {
  outline: 2px solid rgba(134, 42, 45, 0.45);
  background: rgba(134, 42, 45, 0.08);
}

/* Pinyin IME candidate dropdown */
.yi-ime-dropdown {
  position: fixed;
  z-index: 9999;
  background: var(--light-bg);
  border: 1px solid var(--border-color);
  border-radius: 5px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 2px 0;
}

.yi-ime-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-width: 280px;
}

.yi-ime-col {
  display: flex;
  flex-direction: column;
  padding: 2px 0;
  background: transparent;
  transition: background 80ms ease;
}

.yi-ime-col + .yi-ime-col {
  border-left: 1px solid var(--border-color);
}

.yi-ime-col.active {
  background: rgba(0, 0, 0, 0.04);
}

.yi-ime-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  cursor: pointer;
  user-select: none;
}

.yi-ime-col.active .yi-ime-cell.highlight {
  background: var(--secondary-color);
  outline: 1px solid var(--accent-color);
  outline-offset: -1px;
}

.yi-ime-num {
  font-size: 10px;
  color: var(--primary-color);
  font-weight: 600;
  min-width: 10px;
  text-align: right;
  flex-shrink: 0;
}

.yi-ime-col:not(.active) .yi-ime-num {
  visibility: hidden;
}

.yi-ime-thumb {
  width: 36px;
  height: 36px;
  object-fit: contain;
  image-rendering: auto;
  flex-shrink: 0;
  border: 1px solid var(--border-color);
  border-radius: 2px;
  background: white;
}

.yi-ime-sub {
  font-size: 10px;
  color: var(--accent-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.yi-ime-no-match {
  padding: 6px 12px;
  font-size: 12px;
  color: var(--border-color);
  font-style: italic;
}

/* Settings dropdown — replaces the standalone Layout/Direction toggle buttons. */
#stage4 .stage4-settings-wrap {
  position: relative;
}
#stage4 #stage4-settings-btn { display: inline-flex; align-items: center; gap: 0.4rem; }
#stage4 #stage4-settings-btn svg { width: 16px; height: 16px; filter: drop-shadow(0 1px 1px rgba(26, 22, 20, 0.18)); }
#stage4 .stage4-settings-panel {
  position: absolute;
  top: calc(100% + 0.4rem);
  left: 0;
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-top: 3px solid var(--accent);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 0.85rem 1.1rem;
  z-index: 60;
  min-width: 260px;
  font-size: 0.85rem;
}
#stage4 .stage4-settings-panel[hidden] { display: none; }
#stage4 .stage4-settings-section {
  margin-bottom: 0.75rem;
}
#stage4 .stage4-settings-section:last-child { margin-bottom: 0; }
#stage4 .stage4-settings-title {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--s4-muted);
  text-transform: uppercase;
  margin-bottom: 0.4rem;
}
#stage4 .stage4-settings-panel label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.2rem 0;
  cursor: pointer;
  color: var(--s4-ink);
  font-weight: 400;
}
#stage4 .stage4-settings-panel input[type="radio"],
#stage4 .stage4-settings-panel input[type="checkbox"] {
  margin: 0;
  accent-color: var(--s4-red);
}

/* Column drag-handle — Excel-style hairline between gloss columns. */
.stage4-col-resizer {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 6px;
  margin-left: -3px;
  cursor: col-resize;
  z-index: 30;
  background: transparent;
}
.stage4-col-resizer::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 3px;
  width: 1px;
  background: var(--s4-line);
  opacity: 0.0;
  transition: opacity 0.12s ease;
}
.stage4-col-resizer:hover::before,
.stage4-col-resizer.dragging::before {
  opacity: 0.8;
}

/* Split layout — OCR on the left, annotations on the right, draggable divider between. */
#stage4 .stage4-split {
  display: flex;
  flex: 1;
  min-height: 0;
  gap: 0;
  width: 100%;
}
#stage4 .stage4-split.collapsed > .stage4-split-divider,
#stage4 .stage4-split.collapsed > .stage4-annotations-pane {
  display: none;
}
#stage4 .stage4-split > .stage4-cards-side {
  flex: 0 0 var(--stage4-left-width, 66%);
  min-width: 240px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#stage4 .stage4-split.collapsed > .stage4-cards-side {
  flex: 1 1 100%;
  min-width: 0;
}
#stage4 .stage4-cards-side > .stage4-view-window {
  flex: 1 1 auto;
  min-height: 0;
}
#stage4 .stage4-split-divider {
  flex: 0 0 6px;
  background: var(--s4-line);
  cursor: col-resize;
  user-select: none;
  position: relative;
  transition: background 0.12s ease;
}
#stage4 .stage4-split-divider:hover,
#stage4 .stage4-split-divider.dragging,
#stage4 .stage4-split-divider:focus-visible {
  background: var(--s4-gold);
}
#stage4 .stage4-annotations-pane {
  flex: 1 1 auto;
  min-width: 200px;
  height: 8in;
  overflow: auto;
  background: var(--s4-paper);
  border: 1px solid var(--s4-line);
  border-radius: 10px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.6),
    0 4px 14px rgba(45, 42, 36, 0.07);
  padding: 1rem 0.5rem;
}
#stage4 .stage4-annotations-list {
  position: relative;
}
/* One row per OCR line. Vertical position matches the OCR line's top via inline `top:Npx`. */
#stage4 .stage4-annotations-row {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 0.25rem;
}
#stage4 .stage4-annotation-box {
  display: flex;
  flex-direction: column;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
  border: 1px solid color-mix(in srgb, var(--ann-color, #862a2d) 35%, transparent);
  cursor: default;
  max-width: 100%;
}
#stage4 .stage4-annotation-tab {
  background: var(--ann-color, var(--s4-red));
  color: #fff;
  font-weight: 600;
  font-size: 11px;
  line-height: 1.3;
  padding: 3px 7px;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#stage4 .stage4-annotation-tab.no-body { border-radius: 6px; }
#stage4 .stage4-annotation-body {
  background: color-mix(in srgb, var(--ann-color, #862a2d) 14%, var(--s4-card));
  color: var(--s4-ink);
  font-weight: 400;
  font-style: italic;
  font-size: 11px;
  line-height: 1.4;
  padding: 3px 7px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#stage4 .stage4-annotation-more {
  align-self: flex-start;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 10px;
  line-height: 1.3;
  color: var(--s4-muted);
  background: transparent;
  border: 1px dashed var(--s4-line);
  cursor: pointer;
  user-select: none;
}
#stage4 .stage4-annotation-more:hover {
  color: var(--s4-ink);
  border-color: var(--s4-gold);
}
#stage4 .stage4-annotation-tab,
#stage4 .stage4-annotation-body {
  cursor: pointer;
}
#stage4 .stage4-annotation-body.editing {
  cursor: text;
  background: rgba(255, 255, 255, 0.96);
  color: var(--s4-ink);
  outline: 2px solid var(--s4-gold);
  outline-offset: 1px;
  border-radius: 3px;
  padding: 1px 5px;
  font-style: normal;
}

/* Loading overlay — shown during handleLoadProject() */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(26, 22, 20, 0.32);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.loading-overlay.hidden { display: none; }
.loading-overlay-card {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  padding: 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.85rem;
  min-width: 200px;
}
.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--surface-alt);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: loading-spinner-rotate 0.8s linear infinite;
}
@keyframes loading-spinner-rotate { to { transform: rotate(360deg); } }
.loading-overlay-text {
  font-family: var(--font-sans);
  font-size: 0.95rem;
  color: var(--text-primary);
  letter-spacing: 0.02em;
}

/* Per-column filter popovers — finder table */
.finder-table thead th { position: relative; }
.filter-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  margin-left: 0.4rem;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  vertical-align: middle;
  border-radius: 3px;
}
.filter-btn svg { width: 12px; height: 12px; display: block; }
.filter-btn:hover { color: var(--gold-deep); background: var(--accent-faint); }
.filter-btn.active { color: var(--gold); }
.filter-btn.active::after {
  content: "";
  position: absolute;
  top: 8px;
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--accent);
  transform: translate(8px, -10px);
}

.filter-popover {
  position: fixed;
  z-index: 9000;
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  padding: 0.6rem;
  min-width: 220px;
  max-width: 320px;
  max-height: 60vh;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-size: 0.9rem;
}
.filter-popover.hidden { display: none; }
.filter-search {
  width: 100%;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-page);
  font-family: var(--font-sans);
  font-size: 0.85rem;
}
.filter-popover-actions {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}
.filter-link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 0;
}
.filter-link:hover { color: var(--accent-hover); text-decoration: underline; }
.filter-popover-list {
  overflow-y: auto;
  max-height: 32vh;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 0.3rem 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.filter-popover-list label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.2rem 0.25rem;
  cursor: pointer;
  border-radius: 3px;
}
.filter-popover-list label:hover { background: var(--accent-faint); }
.filter-popover-list label.hidden-by-search { display: none; }
.filter-popover-list input { margin: 0; }
.filter-popover-footer { display: flex; justify-content: flex-end; gap: 0.5rem; }
.filter-popover-footer .secondary-btn,
.filter-popover-footer .primary-btn { padding: 0.3rem 0.7rem; font-size: 0.85rem; }

/* Keyboard Shortcuts Modal */
.shortcuts-panel {
  position: fixed;
  right: 24px;
  top: calc(var(--header-h) + 16px);
  width: 440px;
  max-width: calc(100vw - 48px);
  max-height: calc(100vh - var(--header-h) - 32px);
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  z-index: 60;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--font-sans);
}

.shortcuts-panel-header {
  display: flex;
  align-items: center;
  gap: var(--s2);
  padding: var(--s3) var(--s4);
  border-bottom: 1px solid var(--border);
  cursor: grab;
  user-select: none;
  flex-shrink: 0;
}
.shortcuts-panel-header:active { cursor: grabbing; }

.shortcuts-panel-title {
  flex: 1;
  font-family: var(--font-sans);
  font-size: var(--t-small);
  font-weight: 600;
  color: var(--text-primary);
}

.shortcuts-panel-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: var(--r-sm);
  color: var(--text-secondary);
  font-size: var(--t-body);
  line-height: 1;
  transition: background var(--ease) 0.12s, color var(--ease) 0.12s;
}
.shortcuts-panel-btn:hover {
  background: var(--accent-faint);
  color: var(--accent);
}

.shortcuts-panel-body {
  overflow-y: auto;
  padding: var(--s4) var(--s6);
  display: flex;
  flex-direction: column;
  gap: var(--s6);
}

.shortcuts-group {
  display: flex;
  flex-direction: column;
  gap: var(--s2);
}

.shortcuts-group-toggle {
  display: flex;
  align-items: center;
  gap: var(--s2);
  width: 100%;
  background: none;
  border: none;
  padding: 0 0 var(--s1) 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  text-align: left;
  border-radius: var(--r-sm);
  transition: background var(--ease) 0.12s;
}
.shortcuts-group-toggle:hover {
  background: var(--surface-alt);
}

.shortcuts-group-caret {
  font-size: var(--t-caption);
  color: var(--text-muted);
  flex-shrink: 0;
  line-height: 1;
  user-select: none;
}

.shortcuts-group-heading {
  font-family: var(--font-sans);
  font-size: var(--t-caption);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0;
}

.shortcuts-group-body {
  display: flex;
  flex-direction: column;
  gap: var(--s2);
}

/* The hidden attribute is overridden by .shortcuts-group-body's display:flex,
   so re-assert display:none for the collapsed state. */
.shortcuts-group-body[hidden] {
  display: none;
}

.shortcuts-row {
  display: flex;
  align-items: baseline;
  gap: var(--s4);
  padding: 2px 0;
}

.shortcuts-keys {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  flex-shrink: 0;
  min-width: 160px;
}

.shortcuts-kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 1px var(--s2);
  background: var(--surface-alt);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  font-family: var(--font-mono);
  font-size: var(--t-caption);
  color: var(--text-primary);
  white-space: nowrap;
  box-shadow: 0 1px 0 var(--border-strong);
}

.shortcuts-desc {
  font-family: var(--font-sans);
  font-size: var(--t-small);
  color: var(--text-secondary);
  flex: 1;
}

.shortcuts-pill {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--text-inverse);
  border: none;
  cursor: grab;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  z-index: 60;
  transition: background var(--ease) 0.12s;
  user-select: none;
}
.shortcuts-pill:hover { background: var(--accent-hover); }
.shortcuts-pill:active { cursor: grabbing; }
