/* 覆盖 styles.css 里 converter 用的 viewport 锁高，保证动作库可整页滚动 */
html:has(body.gallery-body),
html.gallery-page {
  height: auto !important;
  min-height: 100%;
  overflow-x: hidden !important;
  overflow-y: auto !important;
}

body.gallery-body {
  height: auto !important;
  min-height: 100vh !important;
  max-height: none !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  overscroll-behavior-y: auto;
}

body.gallery-body .shell,
body.gallery-body .gallery-shell {
  height: auto !important;
  max-height: none !important;
  min-height: 100vh;
  overflow: visible !important;
}

.gallery-shell {
  max-width: 1280px;
  margin: 0 auto;
  padding-bottom: 64px;
}

.gallery-topbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-start;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.gallery-topbar h1 {
  margin: 4px 0 8px;
  font-size: clamp(1.6rem, 3vw, 2.1rem);
  letter-spacing: -0.02em;
}

.gallery-lead {
  margin: 0;
  max-width: 62ch;
  color: var(--md-on-surface-variant);
  font-size: 0.95rem;
  line-height: 1.55;
}

.gallery-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.gallery-actions a.secondary {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  color: var(--md-on-secondary-container);
  background: var(--md-secondary-container);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.88rem;
}

.gallery-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 16px 20px;
  align-items: center;
  padding: 16px 18px;
  margin-bottom: 18px;
}

/* —— M3 outlined text field —— */
.search-field.m3-text-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: min(280px, 100%);
  flex: 1 1 240px;
}

.m3-field-label {
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--md-on-surface-variant);
  padding-inline: 4px;
}

.m3-text-field input {
  border: 1px solid var(--md-outline);
  border-radius: 4px; /* M3 outlined: 4dp */
  padding: 14px 16px;
  min-height: 48px;
  background: transparent;
  color: var(--md-on-surface);
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

.m3-text-field input::placeholder {
  color: color-mix(in srgb, var(--md-on-surface-variant) 72%, transparent);
}

.m3-text-field input:hover {
  border-color: var(--md-on-surface);
}

.m3-text-field input:focus {
  border-color: var(--md-primary);
  border-width: 2px;
  padding: 13px 15px; /* keep size when border grows */
  box-shadow: none;
}

/* —— M3 Switch (settings on/off) —— */
.m3-switch-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: stretch;
}

.m3-switch {
  --switch-track-w: 52px;
  --switch-track-h: 32px;
  --switch-handle: 24px;
  --switch-handle-off: 16px;
  --switch-pad: 4px;

  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-height: 48px; /* M3 touch target */
  padding: 6px 12px 6px 8px;
  border-radius: 16px;
  border: 1px solid transparent;
  background: var(--md-surface-container-low);
  cursor: pointer;
  user-select: none;
  transition: background 120ms ease, border-color 120ms ease;
}

.m3-switch:hover {
  background: var(--md-surface-container);
}

.m3-switch:has(input:focus-visible) {
  outline: 2px solid var(--md-primary);
  outline-offset: 2px;
}

.m3-switch:has(input:checked) {
  background: color-mix(in srgb, var(--md-primary-container) 55%, var(--md-surface-container-lowest));
  border-color: color-mix(in srgb, var(--md-primary) 18%, transparent);
}

.m3-switch input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  margin: 0;
  pointer-events: none;
}

.m3-switch-track {
  position: relative;
  flex: 0 0 auto;
  width: var(--switch-track-w);
  height: var(--switch-track-h);
  border-radius: 999px;
  box-sizing: border-box;
  border: 2px solid var(--md-outline);
  background: var(--md-surface-container-highest);
  transition:
    background 160ms cubic-bezier(0.2, 0, 0, 1),
    border-color 160ms cubic-bezier(0.2, 0, 0, 1);
}

.m3-switch-handle {
  position: absolute;
  top: 50%;
  left: var(--switch-pad);
  width: var(--switch-handle-off);
  height: var(--switch-handle-off);
  border-radius: 50%;
  background: var(--md-outline);
  transform: translateY(-50%);
  transition:
    left 160ms cubic-bezier(0.2, 0, 0, 1),
    width 160ms cubic-bezier(0.2, 0, 0, 1),
    height 160ms cubic-bezier(0.2, 0, 0, 1),
    background 160ms cubic-bezier(0.2, 0, 0, 1),
    box-shadow 160ms cubic-bezier(0.2, 0, 0, 1);
  box-shadow: none;
}

/* Unselected hover: slightly larger handle (M3 state layer feel) */
.m3-switch:hover .m3-switch-handle {
  width: 20px;
  height: 20px;
}

.m3-switch input:checked + .m3-switch-track {
  background: var(--md-primary);
  border-color: var(--md-primary);
}

.m3-switch input:checked + .m3-switch-track .m3-switch-handle {
  left: calc(100% - var(--switch-pad) - var(--switch-handle));
  width: var(--switch-handle);
  height: var(--switch-handle);
  background: var(--md-on-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.m3-switch:hover input:checked + .m3-switch-track .m3-switch-handle {
  width: 28px;
  height: 28px;
  left: calc(100% - var(--switch-pad) - 28px);
}

.m3-switch-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.m3-switch-title {
  font-size: 0.875rem; /* 14sp body medium */
  font-weight: 500;
  line-height: 1.25;
  letter-spacing: 0.01em;
  color: var(--md-on-surface);
}

.m3-switch-desc {
  font-size: 0.75rem; /* 12sp body small */
  line-height: 1.3;
  color: var(--md-on-surface-variant);
}

.hint-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-left: auto;
  align-self: center;
}

.hint-pill {
  font-family: ui-monospace, monospace;
  font-size: 11px;
  padding: 5px 9px;
  border-radius: 999px;
  border: 1px solid var(--md-outline-variant);
  color: var(--md-on-surface-variant);
  background: var(--md-surface-container-low);
}

.hint-pill.ok {
  border-color: rgba(0, 106, 106, 0.28);
  color: var(--md-primary);
  background: rgba(111, 247, 244, 0.18);
}

.hint-pill.warn {
  border-color: rgba(186, 26, 26, 0.2);
  color: #8b2e2e;
  background: rgba(255, 218, 214, 0.45);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.motion-card {
  background: var(--md-surface-container-lowest);
  border: 1px solid var(--md-outline-variant);
  border-radius: 18px;
  box-shadow: var(--md-elevation-1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}

.motion-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--md-elevation-2);
  border-color: rgba(0, 106, 106, 0.35);
}

.motion-card.is-playing {
  border-color: var(--md-primary);
  box-shadow: 0 0 0 1px rgba(0, 106, 106, 0.18), var(--md-elevation-2);
}

.card-stage {
  position: relative;
  aspect-ratio: 4 / 3;
  background:
    radial-gradient(circle at 50% 20%, rgba(111, 247, 244, 0.18), transparent 45%),
    linear-gradient(180deg, #eef6f5, #dfe8e7);
  overflow: hidden;
  /* 预览区不拦截页面滚动 */
  touch-action: pan-y;
}

.card-stage canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: pan-y;
  pointer-events: none;
}

.card-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  font-family: ui-monospace, monospace;
  font-size: 10px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(22, 29, 29, 0.72);
  color: #fff;
  backdrop-filter: blur(6px);
}

.card-body {
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.card-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--md-on-surface);
  line-height: 1.3;
  word-break: break-word;
}

.card-meta {
  margin: 0;
  font-size: 0.8rem;
  color: var(--md-on-surface-variant);
  font-family: ui-monospace, monospace;
}

.card-actions {
  display: flex;
  gap: 8px;
  margin-top: auto;
}

.card-actions button {
  flex: 1;
  border-radius: 999px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 0.85rem;
}

.card-actions .play-btn {
  background: var(--md-primary);
  color: var(--md-on-primary);
}

.card-actions .mesh-btn {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}

.card-actions .retarget-btn {
  background: var(--md-tertiary-container);
  color: var(--md-on-secondary-container);
  font-weight: 600;
}

.card-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.card-loading,
.card-error {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  text-align: center;
  font-size: 0.85rem;
  color: var(--md-on-surface-variant);
  background: rgba(244, 251, 250, 0.55);
}

.card-error {
  color: var(--md-error);
}

.empty-state {
  text-align: center;
  color: var(--md-on-surface-variant);
  padding: 48px 16px;
  border: 1px dashed var(--md-outline-variant);
  border-radius: 16px;
}

.empty-state.hidden,
.hidden {
  display: none !important;
}

/* 3D modal — single shared WebGL context */
.mesh-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(16, 24, 24, 0.55);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 20px;
}

.mesh-dialog {
  width: min(1000px, 100%);
  max-height: min(92vh, 900px);
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.mesh-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--md-outline-variant);
}

.mesh-head h2 {
  margin: 4px 0 4px;
  font-size: 1.2rem;
}

.mesh-stage {
  position: relative;
  background: #101616;
  flex: 1;
  min-height: 360px;
}

.mesh-stage canvas {
  width: 100%;
  height: 100%;
  display: block;
  min-height: 360px;
}

.mesh-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #cfe7e5;
  font-family: ui-monospace, monospace;
  font-size: 13px;
  pointer-events: none;
}

.mesh-dialog .preview-controls {
  padding: 12px 16px 14px;
  border-top: 1px solid var(--md-outline-variant);
}

@media (max-width: 720px) {
  .gallery-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .hint-pills {
    margin-left: 0;
  }
}

/* 双库与本地收藏 */
.gallery-body .hidden { display: none !important; }
.library-tabs { display: flex; flex-wrap: wrap; gap: 12px; margin: 20px 0; }
.library-tabs button { min-width: 190px; }
.library-tabs button[aria-pressed="true"] { background: var(--md-primary); color: var(--md-on-primary); }
.library-tabs span { margin-left: 10px; opacity: .8; }
.library-upload { padding: 16px; margin-bottom: 18px; }
.library-drop { width: 100%; min-height: 148px; border: 2px dashed var(--md-primary); border-radius: 20px; background: transparent; color: var(--md-on-surface); display: flex; flex-direction: column; justify-content: center; gap: 10px; white-space: normal; }
.library-drop strong { font-size: 1.15rem; }
.library-drop span, .library-drop small { font-weight: 400; line-height: 1.6; }
.library-drop.is-dragging { background: var(--md-primary-container); }
#uploadStatus { margin: 8px 4px 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.format-field { display: flex; flex-direction: column; gap: 6px; min-width: 150px; }
.format-field select { min-height: 48px; }
.card-duration { position: absolute; top: 10px; right: 10px; background: rgba(15, 53, 53, .86); color: white; border-radius: 999px; padding: 5px 10px; font-size: .8rem; font-variant-numeric: tabular-nums; }
.motion-card { min-width: 0; }
.card-body { min-width: 0; }
.card-title, .card-meta, .card-notes { overflow-wrap: anywhere; }
.card-meta { line-height: 1.5; }
.card-notes { color: var(--md-on-surface-variant); font-size: .85rem; line-height: 1.5; white-space: pre-wrap; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-chip { min-height: 26px; padding: 3px 10px; font-size: .75rem; background: var(--md-secondary-container); color: var(--md-on-secondary-container); border: 0; overflow-wrap: anywhere; }
.card-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.card-actions > * { flex: 1 1 70px; }
.annotation-dialog { width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); overflow-y: auto; padding: 24px; color: var(--md-on-surface); }
.annotation-dialog::backdrop { background: rgba(0, 25, 25, .48); }
.annotation-dialog form, .annotation-dialog label { display: flex; flex-direction: column; gap: 12px; }
.annotation-dialog label { margin-bottom: 12px; }
.annotation-dialog textarea { resize: vertical; width: 100%; box-sizing: border-box; padding: 12px; font: inherit; }
.annotation-dialog p { overflow-wrap: anywhere; }
.annotation-actions { display: flex; justify-content: flex-end; gap: 12px; }
@media (max-width: 640px) {
  .library-tabs > button { flex: 1; min-width: 130px; }
  .format-field { width: 100%; }
}
.reveal-status { font-size: .78rem; color: var(--md-on-surface-variant); line-height: 1.5; overflow-wrap: anywhere; margin: 6px 0 0; }
.reveal-status:empty { display: none; }

.annotation-tag-editor { display: flex; flex-direction: column; gap: 10px; }
.annotation-tag-editor > label { margin-bottom: 0; }
.editor-tags { display: flex; flex-wrap: wrap; gap: 8px; }
.editor-tags:empty { display: none; }
.editor-tag { display: inline-flex; align-items: center; gap: 6px; max-width: 100%; padding: 4px 6px 4px 12px; border-radius: 18px; background: var(--md-secondary-container); color: var(--md-on-secondary-container); }
.editor-tag > span { overflow-wrap: anywhere; min-width: 0; }
.editor-tag .remove-tag { width: 28px; min-width: 28px; height: 28px; min-height: 28px; padding: 0; flex: 0 0 28px; border: 0; background: transparent; color: inherit; font-size: 20px; }
.tag-input-row { display: flex; gap: 10px; align-items: center; }
.tag-input-row input { width: auto; min-width: 0; flex: 1; }
.tag-input-row button { width: auto; flex: 0 0 auto; min-width: 80px; white-space: nowrap; }
#tagEditorHint { line-height: 1.5; color: var(--md-on-surface-variant); }
.annotation-actions > button { width: auto; flex: 0 0 auto; min-width: 88px; white-space: nowrap; }
.card-filename { margin: 0 0 10px; color: var(--md-on-surface-variant); font-size: .8rem; line-height: 1.5; overflow-wrap: anywhere; }
.card-actions .delete-btn { color: #a12828; background: #fce8e6; }
