/*
 * Cadence — Digital Sheet Music Management Platform
 * Copyright © DoubTech. The LICENSE file in the repo root governs use of this file.
 *
 * annotations.css — Styles for the sheet-music annotation overlay engine
 * (annotations-core.js). Lays out the per-page wrapper that hosts the pdf.js
 * canvas plus its absolutely-positioned SVG ink/highlight overlay and DOM text
 * sublayer, ensures the overlay captures stylus/mouse pointer input without
 * scrolling, and styles the floating bottom-centre annotation tool tray
 * (collapsible pencil affordance, tool buttons, and the contextual colour-swatch
 * / width-dot / text-size popover) plus the slide-out part-settings drawer.
 */

/* Page wrapper: positioning context for the canvas + overlay stack. The canvas
   keeps its own CSS width/height (set by rehearsal.js); the wrapper shrink-wraps
   it so the overlay can be inset:0 over exactly the page box. */
.rh-page-wrap {
  position: relative;
  display: inline-block;
  line-height: 0;
}

/* SVG vector sublayer for ink / highlight / markers. touch-action:none lets a
   stylus or finger draw instead of scrolling the page. Sized to cover the page
   box exactly via inset:0. */
.anno-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  overflow: visible;
}

/* In select mode the overlay must not eat taps meant for the music/nav; the
   active-tool class is toggled by JS when a drawing tool is selected. */
.anno-overlay {
  pointer-events: none;
}
.rehearsal[data-anno-tool]:not([data-anno-tool="select"]) .anno-overlay {
  pointer-events: auto;
}
/* While a drawing tool is active, the overlay's PARENT must also opt out of
   default touch gestures, otherwise iOS/iPadOS treats a stylus stroke as a
   pan/scroll before it reaches the overlay and nothing draws. Gated to drawing
   mode so pinch-zoom / swipe still work while reading. */
.rehearsal[data-anno-tool]:not([data-anno-tool="select"]) .rh-page-wrap {
  touch-action: none;
}

.anno-overlay path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* DOM sublayer for typed text boxes. Sits above the SVG; individual boxes opt
   back into pointer events so they can be selected/edited. */
.anno-text-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  line-height: normal;
}

.anno-text {
  position: absolute;
  pointer-events: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  font-weight: 600;
  text-shadow: 0 0 2px rgba(255, 255, 255, 0.8);
}

.anno-text-editing {
  outline: 1px dashed currentColor;
  min-width: 2ch;
  min-height: 1em;
  cursor: text;
}

/* ── Topbar slot (collaboration controls) ──
   The drawing tools now live in the floating tray (below); the topbar slot keeps
   only the layer-picker + share controls. Lay them out in a single non-wrapping
   row so they never break onto a second line. */
.rh-anno-slot {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 1 auto;
  min-width: 0;
  flex-wrap: nowrap;
}
.anno-layer-picker,
.anno-share-wrap {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  position: relative; /* anchor for the dropdown / share popover */
}

/* Text-label tool buttons (e.g. the picker's Layers / Share) that are NOT icon
   buttons get a themed pill so they read as native controls. Scoped with :not()
   so it never overrides the .rh-icon-btn icon tools. */
.anno-tool:not(.rh-icon-btn) {
  appearance: none;
  font: inherit;
  font-size: 0.82rem;
  line-height: 1;
  padding: 7px 12px;
  border: 1px solid var(--c-border);
  border-radius: var(--radius-sm);
  background: var(--c-surface-hover);
  color: var(--c-text-secondary);
  cursor: pointer;
  transition: color 0.12s, border-color 0.12s, background 0.12s;
}
.anno-tool:not(.rh-icon-btn):hover {
  color: var(--c-text-primary);
  border-color: var(--c-accent);
}

.anno-tool.active {
  background: var(--c-selection-bg);
  color: var(--c-accent);
}

/* ── Floating tool tray ──
   Hovers bottom-centre over the score (PencilKit / forScore convention) inside
   the rehearsal stacking context. Collapsed it shows a single pencil button;
   expanded it reveals the drawing tools, an actions cluster, and a contextual
   popover. Glass surface so the music stays visible behind it. */
.anno-tray {
  position: absolute;
  top: 0; left: 0; /* actual top/left set inline by JS (dock edge + drag) */
  z-index: 40;
  display: flex;
  align-items: center;
  padding: 6px;
  border-radius: var(--radius-xl);
  background: var(--float-bg);
  border: 1px solid var(--glass-border);
  box-shadow: var(--shadow-float);
  backdrop-filter: blur(var(--float-blur));
  -webkit-backdrop-filter: blur(var(--float-blur));
  /* Organic "toss" into place; disabled while dragging / on first paint. */
  transition: left 0.42s cubic-bezier(0.34, 1.45, 0.6, 1), top 0.42s cubic-bezier(0.34, 1.45, 0.6, 1);
}
.anno-tray.no-anim, .anno-tray.dragging { transition: none; }
.anno-tray.dragging { cursor: grabbing; }

/* Morph to a vertical column when docked on the left or right edge. */
.anno-tray.vertical { flex-direction: column; }
.anno-tray.vertical .anno-tray-tools { flex-direction: column; }
.anno-tray.vertical .anno-tool-group { flex-direction: column; }
.anno-tray.vertical .anno-tray-divider { width: 28px; height: 1px; margin: 4px 0; }
@media (prefers-reduced-motion: reduce) { .anno-tray { transition: none; } }

/* Drag handle. */
.anno-tray-grip {
  flex: 0 0 auto;
  width: 26px; height: 44px;
  display: grid; place-items: center;
  border: none; background: transparent;
  color: var(--c-text-muted);
  cursor: grab; touch-action: none;
  border-radius: var(--radius-md);
}
.anno-tray-grip:active { cursor: grabbing; }
.anno-tray-grip:hover { color: var(--c-text-primary); }
.anno-tray-grip svg { width: 18px; height: 18px; pointer-events: none; }
.anno-tray.vertical .anno-tray-grip { width: 44px; height: 26px; }
.anno-tray.vertical .anno-tray-grip svg { transform: rotate(90deg); }

/* Collapsed pencil affordance (shown only while collapsed). */
.anno-tray-toggle {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 999px;
  background: var(--c-primary);
  color: #fff;
  cursor: pointer;
  transition: background var(--t-fast), transform var(--t-fast);
}
.anno-tray-toggle:hover { background: var(--c-primary-light); }
.anno-tray.expanded .anno-tray-toggle { display: none; }

/* Expanded content: hidden until the tray is expanded. */
.anno-tray-tools {
  display: none;
  align-items: center;
  gap: 4px;
}
.anno-tray.expanded .anno-tray-tools { display: flex; }

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

.anno-tray-divider {
  width: 1px;
  height: 28px;
  margin: 0 4px;
  background: var(--c-border);
  flex: 0 0 auto;
}

/* Tray buttons — 44px touch targets with a PencilKit-style "lift" on the active
   tool. */
.anno-tray-btn {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--c-text-secondary);
  cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast), transform var(--t-fast);
}
.anno-tray-btn:hover { background: var(--c-surface-hover); color: var(--c-text-primary); }
.anno-tray-btn.active {
  background: var(--c-selection-bg);
  color: var(--c-accent);
  transform: translateY(-4px);
}
.anno-tray-btn:focus-visible,
.anno-tray-toggle:focus-visible,
.anno-swatch:focus-visible,
.anno-width-dot:focus-visible,
.anno-size-chip:focus-visible {
  outline: 2px solid var(--c-focus);
  outline-offset: 2px;
}

/* Contextual popover: colour swatches + width / size options. Opens toward the
   viewport interior based on which edge the tray is docked to. */
.anno-popover {
  position: absolute;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 12px;
  border-radius: var(--radius-lg);
  background: var(--float-bg);
  border: 1px solid var(--glass-border);
  box-shadow: var(--shadow-float);
  backdrop-filter: blur(var(--float-blur));
  -webkit-backdrop-filter: blur(var(--float-blur));
  white-space: nowrap;
}
.anno-popover[hidden] { display: none; }
.dock-bottom .anno-popover { bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); }
.dock-top .anno-popover { top: calc(100% + 8px); left: 50%; transform: translateX(-50%); }
.dock-left .anno-popover { left: calc(100% + 8px); top: 50%; transform: translateY(-50%); }
.dock-right .anno-popover { right: calc(100% + 8px); top: 50%; transform: translateY(-50%); }

.anno-pop-group { display: flex; align-items: center; gap: 6px; }

/* Colour swatch grid (two rows of four). */
.anno-swatch-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.anno-swatch {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 2px solid transparent;
  box-shadow: 0 0 0 1px var(--c-border) inset;
  cursor: pointer;
  padding: 0;
  transition: transform var(--t-fast), border-color var(--t-fast);
}
.anno-swatch:hover { transform: scale(1.08); }
.anno-swatch.active {
  border-color: var(--c-accent);
  box-shadow: 0 0 0 1px var(--c-accent) inset;
}

/* Discrete width dots — each previews its own stroke size. */
.anno-dot-row { gap: 4px; }
.anno-width-dot {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  cursor: pointer;
  transition: background var(--t-fast);
}
.anno-width-dot:hover { background: var(--c-surface-hover); }
.anno-width-dot.active { background: var(--c-selection-bg); }
.anno-width-dot-inner {
  display: block;
  border-radius: 999px;
  box-shadow: 0 0 0 1px var(--c-border);
}

/* Text-size chips (S / M / L / XL). */
.anno-size-row { gap: 4px; }
.anno-size-chip {
  min-width: 44px;
  height: 44px;
  padding: 0 10px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--c-text-secondary);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast);
}
.anno-size-chip:hover { background: var(--c-surface-hover); color: var(--c-text-primary); }
.anno-size-chip.active { background: var(--c-selection-bg); color: var(--c-accent); }

@media (prefers-reduced-motion: reduce) {
  .anno-tray-btn,
  .anno-tray-toggle,
  .anno-swatch { transition: none; }
  .anno-tray-btn.active { transform: none; }
}

/* ── Slide-out settings drawer ──
   Hosts the structured panels (timpani drum setup, percussion list). Slides in
   from the right, themed to match the app surface. */
.anno-drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 60;
  width: min(380px, 88vw);
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--c-surface);
  border-left: 1px solid var(--c-border);
  box-shadow: var(--shadow-float);
  transform: translateX(100%);
  transition: transform 0.22s ease;
}
.anno-drawer.open { transform: translateX(0); }
@media (prefers-reduced-motion: reduce) {
  .anno-drawer { transition: none; }
}

.anno-drawer-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 12px 12px 16px;
  border-bottom: 1px solid var(--c-border-subtle);
}
.anno-drawer-title {
  font-weight: 600;
  color: var(--c-text-primary);
}
.anno-drawer-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.anno-drawer-section { display: flex; flex-direction: column; }
