/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0a0f;
  --surface: #161625;
  --card: #1c1c30;
  --card-hover: #242440;
  --primary: #e94560;
  --secondary: #4ecdc4;
  --accent: #ffd93d;
  --text: #f0f0f0;
  --text-dim: #999;
  --text-muted: #555;
  --border: #2a2a40;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

#app { height: 100%; }

/* ===== View System ===== */
.view {
  display: none;
  position: fixed;
  inset: 0;
  flex-direction: column;
  background: var(--bg);
}
.view.active { display: flex; }

.view-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  padding-top: calc(12px + var(--safe-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: 56px;
  gap: 12px;
}
.view-header h1 { font-size: 22px; font-weight: 700; }
.view-header h2 {
  font-size: 18px; font-weight: 600; flex: 1;
  text-align: center; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.view-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px;
  padding-bottom: calc(80px + var(--safe-bottom));
}

/* ===== Common Components ===== */
.hdr-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.hdr-btn.accent { background: var(--primary); border-color: var(--primary); font-weight: 600; }

.btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 28px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
  font-weight: 500;
}
.btn.primary { background: var(--primary); border-color: var(--primary); }

.btn-sm {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 36px; height: 36px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.btn-sm:disabled { opacity: 0.3; pointer-events: none; }

input[type="text"], input[type="number"], select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 15px;
  font-family: inherit;
  width: 100%;
  outline: none;
}
input:focus, select:focus { border-color: var(--secondary); }
input[type="number"] { -moz-appearance: textfield; }

label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}

/* ===== FAB ===== */
.fab {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  right: 24px;
  width: 60px; height: 60px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  font-size: 32px;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(233, 69, 96, 0.4);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===== Song List ===== */
.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 60vh; color: var(--text-muted); text-align: center;
}
.empty-icon { font-size: 64px; margin-bottom: 16px; }
.empty-state p { font-size: 18px; margin: 4px 0; }

.song-card {
  display: flex; align-items: center;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 20px; margin-bottom: 12px;
  cursor: pointer; transition: background 0.15s;
}
.song-card:active { background: var(--card-hover); }
.song-card-main { flex: 1; min-width: 0; }
.song-card-main h3 { font-size: 17px; font-weight: 600; margin-bottom: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.song-meta { font-size: 13px; color: var(--text-dim); }
.song-card-actions { display: flex; gap: 8px; margin-left: 12px; flex-shrink: 0; }
.btn-icon { background: none; border: none; font-size: 20px; cursor: pointer; padding: 8px; border-radius: 8px; color: var(--text-dim); }
.btn-icon:hover { background: var(--surface); }
.btn-icon.play { color: var(--secondary); }

/* ===== Editor ===== */
.editor-top {
  display: grid; grid-template-columns: 1fr auto auto;
  gap: 16px; align-items: end; margin-bottom: 20px;
}
.editor-top .field-group.wide { grid-column: 1; }
.field-group { margin-bottom: 0; }
.field-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 12px; margin-bottom: 12px;
}
.duration-display { font-size: 14px; color: var(--text-dim); padding: 10px 0; display: block; }

.time-sig-input { display: flex; align-items: center; gap: 6px; }
.time-sig-input input { width: 60px; text-align: center; }
.time-sig-input select { width: 60px; }
.ts-slash { font-size: 20px; color: var(--text-dim); }

.tempo-input-row { display: flex; gap: 6px; align-items: center; }
.tempo-input-row input { flex: 1; min-width: 0; }

/* Section Cards */
.section-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); margin-bottom: 12px; overflow: hidden;
}
.section-card-header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.section-badge {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary); color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; flex-shrink: 0;
}
.section-name-input {
  flex: 1; background: transparent; border: 1px solid transparent;
  font-size: 15px; font-weight: 600; padding: 6px 8px;
}
.section-name-input:focus { border-color: var(--border); background: var(--surface); }
.section-actions { display: flex; gap: 4px; flex-shrink: 0; }
.section-card-body { padding: 16px; }

/* Subdivision selector */
.subdiv-selector { display: flex; gap: 6px; }
.subdiv-opt {
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text-dim); padding: 6px 12px; border-radius: 6px;
  font-size: 13px; cursor: pointer; font-family: inherit;
}
.subdiv-opt.active { border-color: var(--secondary); color: var(--secondary); background: rgba(78,205,196,0.1); }

/* Event Timeline */
.events-section { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border); }
.events-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.events-header label { margin-bottom: 0; font-size: 12px; }

.event-row {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 8px; padding: 10px 12px;
  background: var(--surface); border-radius: 8px;
  border-left: 4px solid var(--text-muted);
  flex-wrap: wrap;
}
.event-row.evt-rit { border-left-color: var(--accent); }
.event-row.evt-accel { border-left-color: var(--secondary); }
.event-row.evt-warning { border-left-color: #ff8c00; }
.event-row.evt-tempo_change { border-left-color: var(--primary); }

.event-row .evt-type {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.5px; min-width: 50px; flex-shrink: 0;
}
.evt-rit .evt-type { color: var(--accent); }
.evt-accel .evt-type { color: var(--secondary); }
.evt-warning .evt-type { color: #ff8c00; }
.evt-tempo_change .evt-type { color: var(--primary); }

.event-row .field-inline { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--text-dim); }
.event-row input[type="number"] { width: 56px; padding: 6px 8px; font-size: 13px; }
.event-row input[type="text"] { width: 120px; padding: 6px 8px; font-size: 13px; }
.event-row select { width: auto; padding: 6px 8px; font-size: 13px; }
.warning-color-preview { width: 16px; height: 16px; border-radius: 4px; flex-shrink: 0; }

.add-event-bar { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-add-event {
  background: none; border: 1px dashed var(--border);
  color: var(--text-dim); padding: 6px 12px;
  border-radius: 6px; font-size: 12px; cursor: pointer;
}

.btn-add-section {
  display: block; width: 100%;
  background: none; border: 2px dashed var(--border);
  color: var(--text-dim); padding: 16px;
  border-radius: var(--radius); font-size: 15px;
  cursor: pointer; margin-top: 4px;
}

/* ===== Performance View ===== */
.perf-view { background: var(--bg); overflow: hidden; }

/* Tiny control bar at top */
.perf-controls {
  flex-shrink: 0; display: flex; align-items: center;
  justify-content: space-between; padding: 4px 12px;
  padding-top: calc(4px + var(--safe-top));
}
.perf-ctrl-btn {
  background: rgba(255,255,255,0.08); border: none; color: var(--text);
  width: 40px; height: 40px; border-radius: 50%;
  font-size: 16px; cursor: pointer; flex-shrink: 0;
}
.perf-ctrl-tags { flex: 1; display: flex; align-items: center; justify-content: center; gap: 12px; }
.perf-tag { font-size: clamp(13px, 1.8vw, 16px); color: var(--text-dim); white-space: nowrap; font-weight: 500; }
.perf-tag.song-name { color: var(--text); font-weight: 600; max-width: 200px; overflow: hidden; text-overflow: ellipsis; }
.perf-tag.tempo { color: var(--accent); font-weight: 700; }
.perf-tag.key { color: var(--secondary); font-weight: 600; }
.perf-tag-sep { color: var(--text-muted); font-size: 12px; }
.perf-audio-btn {
  background: none; border: none; font-size: 16px;
  color: var(--text-dim); cursor: pointer; padding: 4px 8px;
}

/* Main content — fills remaining space, pinned to top */
.perf-main {
  flex: 1; display: flex; flex-direction: column;
  justify-content: flex-start;
  padding: 8px 16px; min-height: 0;
}

/* Row 1: Section + Current Bar side by side — fixed height to prevent shift */
.perf-row-top {
  flex-shrink: 0;
  height: clamp(80px, 18vw, 180px);
  display: flex; gap: clamp(12px, 2vw, 24px);
  align-items: baseline; padding: 0 4px;
  overflow: hidden;
}
.section-box {
  flex: 0 0 auto; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.bar-box {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}

.section-letter {
  font-size: clamp(56px, 12vw, 100px);
  font-weight: 900; line-height: 1;
  color: var(--primary); letter-spacing: 1px;
}
.section-label {
  font-size: clamp(14px, 2.5vw, 22px);
  color: var(--text-dim); margin-top: 2px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 100%;
}
.bar-number {
  font-size: clamp(80px, 18vw, 180px);
  font-weight: 800; line-height: 1;
  font-variant-numeric: tabular-nums; letter-spacing: -3px;
  color: var(--text); transition: color 0.2s;
}
.bar-number.count-in-mode { color: var(--accent); }

/* Row 2: Beat indicators — fill remaining space */
.perf-row-beats {
  flex: 1; display: flex; gap: clamp(8px, 1.5vw, 16px);
  padding: 8px 4px; align-items: stretch;
  min-height: 0;
}
.beat-col {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; gap: 8px;
  min-height: 0;
}
.beat-dot {
  width: 100%; flex: 1; min-height: 0;
  border-radius: 20px; border: 4px solid var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(48px, 12vw, 120px);
  font-weight: 800; color: var(--text-muted);
  transition: border-color 0.08s, color 0.08s, background 0.08s, box-shadow 0.08s;
}

/* Subdivision — small circles below main beat */
.sub-dots {
  display: flex; gap: 6px; justify-content: center;
  flex-shrink: 0; height: 24px; align-items: center;
}
.sub-dot {
  width: clamp(12px, 2vw, 20px);
  height: clamp(12px, 2vw, 20px);
  border-radius: 50%; border: 2px solid var(--text-muted);
  background: transparent;
  transition: background 0.05s, border-color 0.05s;
}
.sub-dot.filled {
  background: var(--secondary); border-color: var(--secondary);
  box-shadow: 0 0 8px rgba(78, 205, 196, 0.5);
}
.sub-dot.filled.db-sub {
  background: var(--primary); border-color: var(--primary);
  box-shadow: 0 0 8px rgba(233, 69, 96, 0.5);
}
.sub-dot.filled.ci-sub {
  background: var(--accent); border-color: var(--accent);
  box-shadow: 0 0 8px rgba(255, 217, 61, 0.5);
}

.beat-dot.active {
  border-color: var(--secondary); background: var(--secondary);
  color: var(--bg); box-shadow: 0 0 50px rgba(78, 205, 196, 0.6);
}
.beat-dot.active.downbeat {
  border-color: var(--primary); background: var(--primary);
  box-shadow: 0 0 70px rgba(233, 69, 96, 0.8);
}
.beat-dot.active.count-in-beat {
  border-color: var(--accent); background: var(--accent);
  color: var(--bg); box-shadow: 0 0 50px rgba(255, 217, 61, 0.6);
}

/* Row 3: Warning banner — fixed height, always in flow */
.perf-row-warning {
  text-align: center; padding: 8px 24px;
  border-radius: 20px;
  transition: background-color 0.3s, color 0.3s;
  height: clamp(60px, 10vw, 100px);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.perf-row-warning.hidden { background: transparent !important; color: transparent; }
.perf-row-warning .warning-label {
  font-size: clamp(36px, 8vw, 72px);
  font-weight: 900; letter-spacing: 2px;
  color: white; text-shadow: 0 2px 12px rgba(0,0,0,0.5);
}

/* Downbeat screen pulse */
.perf-view.downbeat-flash {
  animation: downbeat-pulse 0.25s ease-out;
}
@keyframes downbeat-pulse {
  0% { box-shadow: inset 0 0 120px rgba(233, 69, 96, 0.4); }
  100% { box-shadow: inset 0 0 0 transparent; }
}

/* Footer with transport + scrubber */
.perf-footer { flex-shrink: 0; padding: 8px 20px; padding-bottom: calc(8px + var(--safe-bottom)); }

.perf-scrubber { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.perf-scrub-input {
  flex: 1; -webkit-appearance: none; appearance: none;
  height: 8px; background: rgba(255,255,255,0.12);
  border-radius: 4px; outline: none; cursor: pointer;
}
.perf-scrub-input::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--secondary); border: none;
  box-shadow: 0 0 8px rgba(78, 205, 196, 0.5);
  cursor: pointer;
}
.perf-scrub-input::-moz-range-thumb {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--secondary); border: none; cursor: pointer;
}
.perf-scrub-input:disabled { opacity: 0.4; cursor: default; }
.perf-scrub-label {
  font-size: 14px; color: var(--text-dim); white-space: nowrap;
  min-width: 80px; text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Transport buttons */
.perf-transport {
  display: flex; justify-content: center; align-items: center;
  gap: 16px; margin-bottom: 6px;
}
.transport-btn {
  background: rgba(255,255,255,0.08); border: 2px solid var(--border);
  color: var(--text); border-radius: 50%;
  font-size: 20px; cursor: pointer;
  width: 52px; height: 52px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.12s, border-color 0.12s;
}
.transport-btn:active { background: rgba(255,255,255,0.15); }
.transport-btn.play-btn {
  width: 64px; height: 64px; font-size: 26px;
  border-color: var(--secondary); color: var(--secondary);
}
.transport-btn.play-btn.playing {
  border-color: var(--accent); color: var(--accent);
}

.perf-next { font-size: clamp(16px, 2.5vw, 26px); color: var(--text-dim); text-align: center; min-height: 20px; font-weight: 500; }
.perf-next.imminent { color: var(--accent); font-weight: 700; font-size: clamp(20px, 3.5vw, 32px); }

/* Overlays */


@keyframes pulse-border {
  0%, 100% { box-shadow: 0 0 0 0 rgba(233, 69, 96, 0.4); }
  50% { box-shadow: 0 0 0 16px rgba(233, 69, 96, 0); }
}

/* ===== Header Tools ===== */
.hdr-tools { display: flex; gap: 4px; flex-shrink: 0; }
.hdr-tools .btn-icon { font-size: 22px; width: 40px; height: 40px; padding: 0; }

/* ===== Dialogs ===== */
.dialog-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.72);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.dialog-overlay.hidden { display: none; }

.dialog-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px;
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 16px;
}

.dialog-header {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 16px; font-weight: 700;
}

.dialog-hint { font-size: 13px; color: var(--text-dim); line-height: 1.5; }

/* ===== Tap Tempo ===== */
.tap-bpm {
  text-align: center;
  font-size: clamp(52px, 14vw, 80px);
  font-weight: 900;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  letter-spacing: -2px;
  min-height: 80px;
  display: flex; align-items: center; justify-content: center;
}
.tap-bpm.tap-hint {
  font-size: 28px; font-weight: 500; color: var(--text-muted); letter-spacing: 0;
}

.tap-big-btn {
  width: 100%; padding: 28px;
  background: var(--card); border: 3px solid var(--secondary);
  color: var(--secondary); font-size: 22px; font-weight: 800;
  border-radius: var(--radius); cursor: pointer;
  letter-spacing: 4px;
  transition: background 0.08s, transform 0.08s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.tap-big-btn:active { background: rgba(78,205,196,0.18); transform: scale(0.97); }

.tap-reset { width: 100%; font-size: 14px; color: var(--text-muted); padding: 10px; }

/* ===== Import/Export ===== */
.io-btn-row { display: flex; gap: 12px; }
.io-btn-row .btn { flex: 1; text-align: center; font-size: 15px; padding: 14px; }

/* ===== Responsive ===== */
@media (max-width: 600px) {
  .editor-top { grid-template-columns: 1fr; }
  .field-row { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 1024px) {
  .song-card, .section-card, .btn-add-section { max-width: 800px; margin-left: auto; margin-right: auto; }
}
@media (orientation: landscape) and (max-height: 500px) {
  /* Percentage-based layout so everything fits predictably */
  .perf-controls { padding: 2px 12px; padding-top: calc(2px + var(--safe-top)); }
  .perf-ctrl-btn { width: 32px; height: 32px; font-size: 14px; }

  .perf-main { padding: 2px 12px; }
  .perf-row-top { height: 25%; }
  .section-letter { font-size: clamp(24px, 8vh, 48px); }
  .section-label { display: none; }
  .bar-number { font-size: clamp(40px, 16vh, 72px); }
  .perf-row-beats { flex: 1; min-height: 0; padding: 2px 4px; }
  .beat-col { gap: 4px; }
  .beat-dot { font-size: clamp(20px, 8vh, 44px) !important; flex: 0 1 80%; }
  .sub-dots { height: 18px; gap: 4px; }
  .sub-dot { width: 12px; height: 12px; }
  .perf-row-warning { height: 15%; }
  .perf-row-warning .warning-label { font-size: clamp(18px, 6vh, 36px); }

  .perf-footer { padding: 2px 20px; padding-bottom: calc(2px + var(--safe-bottom)); }
  .perf-scrubber { margin-bottom: 2px; }
  .perf-transport { gap: 10px; margin-bottom: 0; }
  .transport-btn { width: 36px; height: 36px; font-size: 14px; }
  .transport-btn.play-btn { width: 44px; height: 44px; font-size: 18px; }
  .perf-next { min-height: 14px; font-size: 13px; }
  .perf-next.imminent { font-size: 15px; }
}
