/* === Beat Crafter v2 — Main Stylesheet === */
/* Dark theme, mobile-first, step sequencer focused */

/* --- CSS Custom Properties --- */
:root {
  --bg:            #0f0f1a;
  --surface:       #1a1a2e;
  --surface-2:     #252540;
  --border:        #3d3d5c;
  --text:          #e0e0e0;
  --text-muted:    #a8a8b8;
  --text-dim:      #9595a8;
  --accent:        #e8a838;
  --accent-glow:   rgba(232, 168, 56, 0.4);
  --danger:        #e05555;

  /* Track colors */
  --kick-color:    #ff5e5b;
  --snare-color:   #ffd644;
  --hh-color:      #4cd964;
  --ohh-color:     #5ac8fa;
  --clap-color:    #ff9500;
  --ride-color:    #af52de;

  /* Grid */
  --cell-size:     38px;
  --cell-gap:      3px;
  --label-width:   90px;
  --step-on-scale: 0.94;

  /* Typography */
  --font:          -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  --font-mono:     'SF Mono', 'Fira Code', 'Fira Mono', 'Roboto Mono', monospace;

  /* Radius */
  --radius-sm:     4px;
  --radius-md:     8px;
  --radius-lg:     14px;
}

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
  border: none;
  background: none;
  cursor: pointer;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

input, select {
  font: inherit;
  color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
}

/* --- Header --- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  /* Safe area for iPhone notch */
  padding-top: max(12px, env(safe-area-inset-top));
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.about-link {
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.15s;
}
.about-link:hover { color: var(--text); }

.header-actions {
  display: flex;
  gap: 4px;
}

/* --- Buttons --- */
.btn-primary {
  background: var(--accent);
  color: var(--bg);
  font-weight: 600;
  transition: opacity 0.15s;
}
.btn-primary:active { opacity: 0.8; }

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 13px;
  transition: background 0.15s;
}
.btn-secondary:active { background: var(--border); }

.btn-small { padding: 6px 12px; font-size: 12px; }

.btn-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}
.btn-icon:active { background: var(--surface-2); color: var(--text); }

/* --- Transport Bar --- */
.transport-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 8px;
  flex-wrap: wrap;
}

.transport-left,
.transport-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Play Button */
.btn-play {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, box-shadow 0.1s;
  box-shadow: 0 0 12px var(--accent-glow);
}
.btn-play:active {
  transform: scale(0.92);
  box-shadow: 0 0 20px var(--accent-glow);
}

/* Stop Button */
.btn-transport {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.btn-transport:active { background: var(--border); }

.icon-hidden { display: none; }

/* BPM Display */
.bpm-display {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 12px;
}

.bpm-value {
  width: 48px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 600;
  padding: 4px 6px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
}
.bpm-value:focus {
  border-color: var(--accent);
  outline: none;
  background: var(--surface-2);
}
.bpm-value::-webkit-inner-spin-button,
.bpm-value::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.bpm-value { -moz-appearance: textfield; }

.bpm-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-bpm-stepper {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}
.btn-bpm-stepper:active {
  color: var(--accent);
  background: var(--surface);
}

/* Master Volume */
.master-volume {
  display: flex;
  align-items: center;
  gap: 4px;
}
.vol-label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
}

.vol-slider {
  -webkit-appearance: none;
  width: 80px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  border: none;
  padding: 0;
}
.vol-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

/* --- Generator Bar --- */
.generator-bar {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.style-select {
  flex: 1;
  max-width: 140px;
  padding: 8px 10px;
  font-size: 13px;
  font-weight: 600;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%23808090'%3E%3Cpath d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 28px;
}

.saved-beat-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-left: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 280px;
}

/* --- Sequencer Grid --- */
.sequencer-container {
  padding: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.grid-scroll {
  min-width: fit-content;
}

.grid {
  display: block;
  width: 100%;
  min-width: fit-content;
  position: relative;
  border-collapse: collapse;
}

.track-row {
  display: flex;
  align-items: center;
  margin-bottom: var(--cell-gap);
  position: relative;
  z-index: 2;
}

.track-info {
  width: var(--label-width);
  min-width: var(--label-width);
  display: flex;
  align-items: center;
  gap: 3px;
  padding-right: 8px;
}

/* Beat ruler — column-aligned with grid, bar/beat labels */
.beat-ruler {
  display: flex;
  align-items: stretch;
  margin-bottom: 2px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none; /* allow drag seeking */
}

.ruler-spacer {
  width: var(--label-width);
  min-width: var(--label-width);
}

/* Step-width ruler columns — same flex as .track-steps for alignment */
.ruler-steps {
  display: flex;
  gap: var(--cell-gap);
  flex: 1;
}

.ruler-mark {
  flex: 1;
  min-width: 28px;
  max-width: 48px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 2px;
  border-left: 2px solid transparent;
  border-radius: var(--radius-sm);
  transition: border-color 0.06s;
  cursor: pointer;
  position: relative;
}

/* Beat boundary markers — every 4th step (quarter note) */
.ruler-mark.beat {
  border-left-color: var(--border);
}

.ruler-beat-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: -0.2px;
  line-height: 1;
}

/* Current step — accent border = playhead line */
.ruler-mark.current {
  border-left-color: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

.ruler-mark.current .ruler-beat-label {
  color: var(--accent);
}

.track-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.track-steps {
  display: flex;
  gap: var(--cell-gap);
  flex: 1;
}

/* Step Cells */
.step {
  flex: 1;
  min-width: 28px;
  max-width: 48px;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: background 0.06s, transform 0.08s, box-shadow 0.06s;
  position: relative;
  cursor: pointer;
}

.step:active {
  transform: scale(var(--step-on-scale));
}

/* Beat markers (every 4th step = quarter note) */
.step.beat {
  border-left-width: 2px;
}

/* Active state per track — colored by track index */
.step.on[data-track="0"] {
  background: var(--kick-color);
  box-shadow: 0 0 8px rgba(255, 94, 91, 0.5);
  border-color: var(--kick-color);
}

.step.on[data-track="1"] {
  background: var(--snare-color);
  box-shadow: 0 0 8px rgba(255, 214, 68, 0.5);
  border-color: var(--snare-color);
}

.step.on[data-track="2"] {
  background: var(--hh-color);
  box-shadow: 0 0 8px rgba(76, 217, 100, 0.5);
  border-color: var(--hh-color);
}

.step.on[data-track="3"] {
  background: var(--ohh-color);
  box-shadow: 0 0 8px rgba(90, 200, 250, 0.5);
  border-color: var(--ohh-color);
}

.step.on[data-track="4"] {
  background: var(--clap-color);
  box-shadow: 0 0 8px rgba(255, 149, 0, 0.5);
  border-color: var(--clap-color);
}

.step.on[data-track="5"] {
  background: var(--ride-color);
  box-shadow: 0 0 8px rgba(175, 82, 222, 0.5);
  border-color: var(--ride-color);
}

/* Current-step column highlight — entire grid column glows */
.step.current {
  filter: brightness(1.55);
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.15);
}

/* --- Saved Beats Section --- */
.saved-section {
  padding: 16px;
  border-top: 1px solid var(--border);
}

.saved-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.saved-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin: 0;
}

.saved-actions {
  display: flex;
  gap: 2px;
}

/* Small icon button variant (export/import) */
.btn-icon-sm {
  width: 32px;
  height: 32px;
}

.saved-disclaimer {
  color: var(--text-dim);
  font-size: 11px;
  line-height: 1.5;
  margin-bottom: 10px;
}

.saved-empty {
  color: var(--text-dim);
  font-size: 13px;
  font-style: italic;
}

.saved-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.saved-beat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  min-width: 180px;
  text-align: left;
  transition: background 0.15s;
}
.saved-beat:active { background: var(--surface); }

.saved-beat.active {
  border-color: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

.saved-beat-name {
  font-size: 13px;
  font-weight: 600;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 4px;
}

.saved-beat-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.saved-beat-date {
  font-size: 11px;
  color: var(--text-muted);
}

.saved-beat-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: 8px;
}

.saved-beat-del,
.saved-beat-export,
.saved-beat-rename {
  font-size: 12px;
  color: var(--text-muted);
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}
.saved-beat-del:active { background: var(--danger); color: white; }
.saved-beat-export:active { background: var(--accent); color: var(--bg); }
.saved-beat-rename:active { background: var(--surface); color: var(--accent); }

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 24px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  visibility: hidden;
  transition: all 0.25s ease;
  z-index: 2000;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  border: 1px solid var(--border);
}
.toast.visible {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}

/* --- Responsive --- */

@media (max-width: 640px) {
  :root {
    --cell-size: 32px;
    --label-width: 72px;
  }

  .header { padding: 8px 12px; }
  .logo { font-size: 16px; }

  .transport-bar { padding: 6px 8px; gap: 4px; }
  .btn-play { width: 42px; height: 42px; }
  .bpm-value { font-size: 16px; width: 42px; }

  .generator-bar { padding: 6px 8px; }
  .style-select { max-width: 110px; font-size: 12px; }

  .step { min-width: 26px; }
  .track-name { font-size: 11px; }

  .vol-slider { width: 50px; }
}

@media (max-width: 420px) {
  :root {
    --cell-size: 28px;
    --label-width: 64px;
    --cell-gap: 2px;
  }

  .transport-bar { justify-content: center; }
  .bpm-display { order: 2; }
  .transport-left { flex-wrap: wrap; justify-content: center; }

  .step { min-width: 24px; }

  .saved-list { gap: 6px; }
  .saved-beat { min-width: 140px; padding: 8px 10px; }
}

@media (min-width: 900px) {
  :root {
    --cell-size: 44px;
    --label-width: 100px;
  }

  .header { padding: 14px 24px; }
  .logo { font-size: 20px; }
  .transport-bar { padding: 10px 20px; }
  .generator-bar { padding: 10px 20px; }
}