/* base.css */
/* ======================================
   VMQ BASE v3.0.4 (Drop-in replacement)
   Design System + Resets + Accessibility + Shell UI
   Compatible with existing VMQ classes (module-container, btn, card, etc.)
   ====================================== */

/* ------------------------------------------------------
   1) Root tokens
   ------------------------------------------------------ */
:root {
  /* Brand */
  --primary: #3b82f6;
  --primary-dark: #1d4ed8;
  --primary-light: #93c5fd;

  /* Provide HSL + RGB forms so any hsl()/rgb() usage works (Safari-safe: space-separated RGB) */
  --primary-hsl: 217 91% 60%;
  --primary-rgb: 59 130 246;

  /* Semantic music theory */
  --perfect: #10b981;
  --major: #3b82f6;
  --minor: #f59e0b;
  --diminished: #ef4444;

  /* General semantic colors */
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #3b82f6;

  /* Neutrals */
  --bg: #f8fafc;
  --card: #ffffff;
  --surface: #f1f5f9;
  --ink: #0f172a;
  --ink-light: #475569;
  --ink-lighter: #94a3b8;
  --border: #e2e8f0;

  /* Elevation */
  --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-sm: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-md: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 25px 50px -12px rgb(0 0 0 / 0.25);

  /* Spacing */
  --space-3xs: 2px;
  --space-2xs: 4px;
  --space-xs: 8px;
  --space-sm: 12px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;
  --space-3xl: 64px;

  /* Radius */
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  /* Typography */
  --font-family-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-family-serif: "EB Garamond", "Times New Roman", serif;
  --font-family-mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", monospace;
  --font-family-music: "Bravura", "Leland", var(--font-family-mono);

  --font-size-3xs: 0.6875rem;
  --font-size-2xs: 0.75rem;
  --font-size-xs: 0.8125rem;
  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xl: 1.25rem;
  --font-size-2xl: 1.5rem;
  --font-size-3xl: 1.875rem;
  --font-size-4xl: 2.25rem;
  --font-size-5xl: 3rem;

  /* Motion */
  --motion-duration-xs: 100ms;
  --motion-duration-sm: 150ms;
  --motion-duration-base: 250ms;
  --motion-duration-lg: 400ms;
  --motion-easing: cubic-bezier(0.4, 0, 0.2, 1);

  /* Layering */
  --z-modal: 1000;
  --z-overlay: 2000;
  --z-toast: 3000;
  --z-tooltip: 4000;
}

/* Dark mode tokens (resilient even if themes.css loads late) */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --card: #101a2e;
    --surface: #0f172a;
    --ink: #e5e7eb;
    --ink-light: #cbd5e1;
    --ink-lighter: #94a3b8;
    --border: #23304a;

    --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.35);
    --shadow-sm: 0 8px 18px rgb(0 0 0 / 0.35);
    --shadow-md: 0 14px 30px rgb(0 0 0 / 0.4);
    --shadow-lg: 0 20px 40px rgb(0 0 0 / 0.45);
    --shadow-xl: 0 28px 60px rgb(0 0 0 / 0.5);
  }
}

/* ------------------------------------------------------
   2) Resets + base element defaults
   ------------------------------------------------------ */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;

  scrollbar-width: thin;
  scrollbar-color: var(--primary) var(--surface);
}

body {
  margin: 0;
  padding: 0; /* safe-area handled below */
  min-height: 100vh;
  min-height: 100svh; /* iOS browser chrome-safe */
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-family-sans);
  font-size: var(--font-size-base);
  line-height: 1.65;

  font-feature-settings: "kern" 1, "liga" 1, "calt" 1;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;

  overscroll-behavior: none;
}

@supports (padding: max(0px)) {
  body {
    padding-top: max(env(safe-area-inset-top), var(--space-sm));
    padding-bottom: max(env(safe-area-inset-bottom), var(--space-sm));
    padding-left: max(env(safe-area-inset-left), 0px);
    padding-right: max(env(safe-area-inset-right), 0px);
  }
}

/* Media defaults */
img,
svg,
video,
canvas {
  max-width: 100%;
  height: auto;
  display: block;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

button {
  touch-action: manipulation;
}

/* ------------------------------------------------------
   3) Headings + text + links
   ------------------------------------------------------ */
h1, h2, h3, h4, h5, h6 {
  margin: 0 0 var(--space-md) 0;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.025em;
}

h1 { font-size: clamp(var(--font-size-3xl), 5vw, var(--font-size-5xl)); }
h2 { font-size: clamp(var(--font-size-2xl), 4vw, var(--font-size-4xl)); }
h3 { font-size: clamp(var(--font-size-xl), 3.5vw, var(--font-size-3xl)); }
h4 { font-size: clamp(var(--font-size-lg), 3vw, var(--font-size-2xl)); }

p {
  margin: 0 0 var(--space-md) 0;
  color: var(--ink-light);
}

a {
  color: var(--primary);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition:
    border-color var(--motion-duration-sm) var(--motion-easing),
    color var(--motion-duration-sm) var(--motion-easing);
}

a:hover {
  color: var(--primary-dark);
  border-bottom-color: currentColor;
}

/* Selection */
::selection {
  background: hsl(var(--primary-hsl) / 0.28);
  color: var(--ink);
}
::-moz-selection {
  background: hsl(var(--primary-hsl) / 0.28);
  color: var(--ink);
}

/* ------------------------------------------------------
   4) Scrollbars (WebKit)
   ------------------------------------------------------ */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--surface); }
::-webkit-scrollbar-thumb {
  background: rgba(var(--primary-rgb) / 0.85); /* avoid color-mix() */
  border-radius: var(--radius-full);
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(var(--primary-rgb) / 1);
}

/* ------------------------------------------------------
   5) Focus + accessibility
   ------------------------------------------------------ */
*:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Screen-reader only */
.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* Touch targets (coarse pointers) */
@media (pointer: coarse) {
  button, [role="button"], a[href] {
    min-height: 44px;
    min-width: 44px;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* High contrast */
@media (prefers-contrast: high) {
  :root {
    --border: #000;
    --shadow-sm: 0 0 0 1px #000;
  }
}

/* ------------------------------------------------------
   6) Print
   ------------------------------------------------------ */
@media print {
  :root { --bg: #fff; --card: #fff; --ink: #000; --ink-light: #000; }
  body { background: #fff; color: #000; }
  .no-print { display: none !important; }
  .card { box-shadow: none !important; }
}

/* Slightly larger base type on mobile */
@media (max-width: 768px) {
  :root {
    --font-size-sm: 0.9375rem;
    --font-size-base: 1.0625rem;
  }
}

/* Landscape “music stand mode” helpers */
@media (max-height: 500px) and (orientation: landscape) {
  .app-wrapper { padding: var(--space-sm); }
  .module-container { gap: var(--space-sm); }
}

/* Higher density smoothing for notation blocks */
@media (min-resolution: 2dppx) {
  .music-notation { font-size: 1.1em; }
}

/* ------------------------------------------------------
   7) Common VMQ layout primitives
   ------------------------------------------------------ */
.module-container {
  width: min(960px, 100%);
  margin: 0 auto;
  padding: var(--space-xl);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: var(--space-lg);
}

.text-muted { color: var(--ink-lighter); }

/* Minimal button baseline (components.css can override) */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition:
    transform var(--motion-duration-sm) var(--motion-easing),
    box-shadow var(--motion-duration-sm) var(--motion-easing),
    background var(--motion-duration-sm) var(--motion-easing);
}

.btn:hover {
  box-shadow: var(--shadow-sm);
  transform: translateY(-1px);
}

.btn:active { transform: translateY(0); }

.btn-primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  border-color: transparent;
  color: #fff;
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* Inputs baseline (helps modules that rely on plain <input>) */
input[type="text"],
input[type="email"],
input[type="number"],
input[type="password"],
textarea,
select {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 0.9rem;
  box-shadow: 0 0 0 0 transparent;
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ------------------------------------------------------
   8) Shell loading UI used by index.html
   ------------------------------------------------------ */
@keyframes spin { to { transform: rotate(360deg); } }

.loading-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg);
  z-index: var(--z-overlay);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--motion-duration-base) var(--motion-easing);
}

.loading-screen.active {
  opacity: 1;
  pointer-events: auto;
}

.loading-content {
  width: min(560px, 92vw);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: clamp(1.25rem, 3vw, 2rem);
  text-align: center;
}

.loading-title {
  margin: 0 0 var(--space-xs) 0;
  color: var(--ink);
}

.loading-subtitle {
  margin: 0 0 var(--space-lg) 0;
  color: var(--ink-light);
}

.loading-spinner {
  width: clamp(32px, 8vw, 48px);
  height: clamp(32px, 8vw, 48px);
  border: 3px solid var(--border);
  border-top: 3px solid var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto var(--space-lg);
}

.loading-progress {
  margin: 0 0 var(--space-md) 0;
}

.progress-bar {
  height: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: var(--width, 0%);
  background: linear-gradient(90deg, var(--primary), var(--primary-dark));
  transition: width var(--motion-duration-lg) var(--motion-easing);
}

.loading-status {
  margin: 0 0 var(--space-sm) 0;
  color: var(--ink-light);
  font-size: var(--font-size-sm);
}

.engine-status {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
  justify-content: center;
  align-items: center;
  font-size: var(--font-size-xs);
  color: var(--ink-light);
}

.engine-indicator {
  padding: var(--space-2xs) var(--space-xs);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
}

/* ------------------------------------------------------
   9) Animations + utilities referenced across VMQ
   ------------------------------------------------------ */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgb(var(--primary-rgb) / 0.4); }
  50%      { box-shadow: 0 0 0 12px rgb(var(--primary-rgb) / 0); }
}

.live-dot {
  width: 8px;
  height: 8px;
  background: var(--success);
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

.shimmer {
  background: linear-gradient(
    90deg,
    var(--surface) 0%,
    hsl(var(--primary-hsl) / 0.10) 20%,
    hsl(var(--primary-hsl) / 0.18) 50%,
    hsl(var(--primary-hsl) / 0.10) 80%,
    var(--surface) 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.95); }
  to   { opacity: 1; transform: scale(1); }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* SMuFL-ready notation block */
.music-notation {
  font-family: var(--font-family-music);
  font-size: clamp(1.5rem, 6vw, 3rem);
  line-height: 1.2;
  display: block;
  margin: var(--space-xl) auto;
  text-align: center;
}

/* Keyboard tags */
kbd {
  background: var(--surface);
  color: var(--ink);
  padding: var(--space-2xs) var(--space-xs);
  border-radius: var(--radius-xs);
  font-family: var(--font-family-mono);
  font-size: var(--font-size-2xs);
  border: 1px solid var(--border);
  font-weight: 600;
}

/* ------------------------------------------------------
   10) Drag overlay used by import / data manager flows
   ------------------------------------------------------ */
.drag-overlay {
  position: fixed;
  inset: 0;
  background: rgb(var(--primary-rgb) / 0.90);
  backdrop-filter: blur(20px);
  display: grid;
  place-items: center;
  z-index: var(--z-modal);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--motion-duration-base) var(--motion-easing);
}

.drag-overlay.active {
  opacity: 1;
  pointer-events: all;
}
