/* ===== CSS Variables & Reset ===== */
:root {
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --bg-card: rgba(255, 255, 255, 0.04);
  --border-glass: rgba(255, 255, 255, 0.08);
  --text-primary: #e8e8f0;
  --text-secondary: #8888aa;
  --accent-1: #6c5ce7;
  --accent-2: #a855f7;
  --accent-3: #06b6d4;
  --glow-color: rgba(108, 92, 231, 0.4);
  --pad-size: 90px;
  --pad-gap: 6px;
  --radius: 12px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== Background Gradient Blobs ===== */
body::before,
body::after {
  content: '';
  position: fixed;
  border-radius: 50%;
  filter: blur(120px);
  opacity: 0.12;
  pointer-events: none;
  z-index: 0;
}

body::before {
  width: 500px;
  height: 500px;
  background: var(--accent-1);
  top: -100px;
  left: -100px;
}

body::after {
  width: 400px;
  height: 400px;
  background: var(--accent-3);
  bottom: -80px;
  right: -80px;
}

/* ===== Main Layout: Left Info + Right Numpad ===== */
.app-layout {
  position: relative;
  z-index: 1;
  display: flex;
  gap: 50px;
  align-items: center;
  padding: 30px;
}

/* ===== Left Panel (Info & Controls) ===== */
.left-panel {
  display: flex;
  flex-direction: column;
  gap: 24px;
  max-width: 280px;
}

/* Timer */
.timer-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-card);
  border: 1px solid var(--border-glass);
  padding: 12px 16px;
  border-radius: 12px;
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  user-select: none;
  /* Prevent text selection */
  -webkit-user-select: none;
}

#timer-display {
  font-family: 'Inter', monospace;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent-3);
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(6, 182, 212, 0.4);
}

.timer-controls {
  display: flex;
  gap: 8px;
}

.timer-controls button {
  background: transparent;
  border: 1px solid var(--border-glass);
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
  font-size: 0.9rem;
}

.timer-controls button:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
  border-color: var(--accent-3);
  box-shadow: 0 0 8px var(--accent-3);
}

.header h1 {
  font-size: 2rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-1), var(--accent-2), var(--accent-3));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
}

.header p {
  color: var(--text-secondary);
  font-size: 0.78rem;
  margin-top: 8px;
  line-height: 1.6;
}

/* Controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.control-row label {
  color: var(--text-secondary);
  font-size: 0.78rem;
  min-width: 30px;
}

.control-row input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: var(--border-glass);
  outline: none;
}

.control-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent-1);
  cursor: pointer;
  box-shadow: 0 0 8px var(--glow-color);
}

#volume-label {
  font-size: 0.85rem;
  color: var(--accent-3);
  width: 36px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  margin-left: 2px;
  font-family: 'Inter', monospace;
}

.btn-row {
  display: flex;
  gap: 8px;
}

.btn-row button {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-glass);
  color: var(--text-secondary);
  font-size: 0.75rem;
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: var(--transition);
  backdrop-filter: blur(10px);
}

.btn-row button:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
  border-color: var(--accent-1);
}

.footer-note {
  color: var(--text-secondary);
  font-size: 0.65rem;
  opacity: 0.7;
  line-height: 1.5;
}

/* ===== Right Panel — Numpad ===== */
.numpad-container {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-glass);
  border-radius: 20px;
  padding: 16px;
  backdrop-filter: blur(16px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

/* Numpad indicator LED strip */
.numpad-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px 10px;
  border-bottom: 1px solid var(--border-glass);
  margin-bottom: 12px;
}

.numpad-title {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-secondary);
}

.led-indicators {
  display: flex;
  gap: 8px;
}

.led {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.led.active {
  background: #4ade80;
  box-shadow: 0 0 6px #4ade80;
}

/* ===== Numpad Grid ===== */
.numpad-grid {
  display: grid;
  grid-template-columns: repeat(4, var(--pad-size));
  grid-template-rows: repeat(5, var(--pad-size));
  gap: var(--pad-gap);
  grid-template-areas:
    "num   div   mul   back"
    "n7    n8    n9    minus"
    "n4    n5    n6    plus"
    "n1    n2    n3    enter"
    "n0    n0    dot   enter";
}

/* ===== Individual Pad ===== */
.pad {
  background: var(--bg-card);
  border: 1px solid var(--border-glass);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
  backdrop-filter: blur(12px);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.pad::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(108, 92, 231, 0.08), transparent);
  opacity: 0;
  transition: opacity var(--transition);
}

.pad:hover {
  border-color: rgba(108, 92, 231, 0.4);
  transform: translateY(-1px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3), 0 0 16px var(--glow-color);
}

.pad:hover::before {
  opacity: 1;
}

.pad:active {
  transform: scale(0.94);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4), inset 0 0 20px rgba(108, 92, 231, 0.15);
}

/* Playing animation */
.pad.playing {
  border-color: var(--accent-2);
  box-shadow: 0 0 24px rgba(168, 85, 247, 0.5), 0 0 50px rgba(168, 85, 247, 0.15);
  animation: pulse-glow 0.35s ease-out;
}

@keyframes pulse-glow {
  0% {
    transform: scale(0.93);
  }

  50% {
    transform: scale(1.02);
  }

  100% {
    transform: scale(1);
  }
}

/* Pad has audio assigned */
.pad.has-audio {
  border-color: rgba(108, 92, 231, 0.25);
}

.pad.has-audio::after {
  content: '';
  position: absolute;
  top: 6px;
  right: 6px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 6px var(--accent-2);
}

/* Pad labels */
.pad-key {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  opacity: 0.9;
  z-index: 1;
  pointer-events: none;
}

.pad-sub {
  font-size: 0.55rem;
  color: var(--text-secondary);
  opacity: 0.6;
  z-index: 1;
  pointer-events: none;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.pad-label {
  font-size: 0.55rem;
  color: var(--accent-3);
  max-width: 90%;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 1;
  pointer-events: none;
}

/* Loop badge — hidden by default, visible when pad has .looping */
.pad-loop-badge {
  display: none;
  position: absolute;
  bottom: 5px;
  left: 6px;
  font-size: 0.6rem;
  z-index: 2;
  pointer-events: none;
  opacity: 0.7;
}

.pad.looping .pad-loop-badge {
  display: block;
}

/* Stop Others Badge */
.pad-stop-others-badge {
  position: absolute;
  top: 5px;
  left: 6px;
  font-size: 0.6rem;
  z-index: 2;
  pointer-events: none;
  color: #fb2424;
  /* Amber/Yellow for lightning */
  text-shadow: 0 0 5px rgba(251, 191, 36, 0.5);
}

/* Playback Progress Bar */
.pad-progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 4px;
  background: var(--accent-1);
  width: 0%;
  transition: width 0.1s linear;
  /* Smooth linear transition */
  z-index: 10;
  box-shadow: 0 0 10px var(--accent-1);
}

/* Stutter Mode Badge */
.pad-polyphony-badge {
  position: absolute;
  top: 5px;
  left: 20px;
  /* Shifted right to sit next to the lightning bolt */
  font-size: 0.6rem;
  z-index: 2;
  pointer-events: none;
  opacity: 0.8;
  filter: drop-shadow(0 0 4px rgba(255, 255, 255, 0.4));
}

.pad.looping {
  border-color: rgba(6, 182, 212, 0.35);
  box-shadow: inset 0 0 12px rgba(6, 182, 212, 0.08);
}

/* Stop key styling */
.pad.stop-key {
  border-color: rgba(248, 113, 113, 0.35);
  box-shadow: inset 0 0 12px rgba(248, 113, 113, 0.08);
}

.pad.stop-key .pad-key {
  color: #f87171;
}

/* Stop Icon (DOM Element now) */
.pad-stop-icon {
  position: absolute;
  bottom: 6px;
  right: 6px;
  font-size: 0.7rem;
  z-index: 2;
  opacity: 0.9;
  color: #f87171;
  pointer-events: none;
}

/* Hidden key styling */
.pad.hidden-key {
  opacity: 0.15;
  transform: scale(0.92);
  pointer-events: auto;
}

.pad.hidden-key:hover {
  opacity: 0.4;
  transform: scale(0.95);
}

/* Checkbox row */
.checkbox-row {
  gap: 8px;
}

.checkbox-row input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border: 1px solid var(--border-glass);
  border-radius: 4px;
  background: var(--bg-card);
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
}

.checkbox-row input[type="checkbox"]:checked {
  background: var(--accent-1);
  border-color: var(--accent-1);
}

.checkbox-row input[type="checkbox"]:checked::after {
  content: '✓';
  position: absolute;
  top: -1px;
  left: 2px;
  font-size: 0.7rem;
  color: white;
  font-weight: 700;
}

.checkbox-row label {
  min-width: unset;
  cursor: pointer;
}

/* ===== Grid Area Assignments ===== */
.pad[data-key="num"] {
  grid-area: num;
}

.pad[data-key="div"] {
  grid-area: div;
}

.pad[data-key="mul"] {
  grid-area: mul;
}

.pad[data-key="back"] {
  grid-area: back;
}

.pad[data-key="n7"] {
  grid-area: n7;
}

.pad[data-key="n8"] {
  grid-area: n8;
}

.pad[data-key="n9"] {
  grid-area: n9;
}

.pad[data-key="minus"] {
  grid-area: minus;
}

.pad[data-key="n4"] {
  grid-area: n4;
}

.pad[data-key="n5"] {
  grid-area: n5;
}

.pad[data-key="n6"] {
  grid-area: n6;
}

.pad[data-key="plus"] {
  grid-area: plus;
}

.pad[data-key="n1"] {
  grid-area: n1;
}

.pad[data-key="n2"] {
  grid-area: n2;
}

.pad[data-key="n3"] {
  grid-area: n3;
}

.pad[data-key="enter"] {
  grid-area: enter;
}

.pad[data-key="n0"] {
  grid-area: n0;
}

.pad[data-key="dot"] {
  grid-area: dot;
}

/* ===== Context Menu ===== */
.context-menu {
  position: fixed;
  z-index: 1000;
  background: var(--bg-secondary);
  border: 1px solid var(--border-glass);
  border-radius: 12px;
  padding: 6px;
  min-width: 180px;
  max-height: calc(100vh - 20px);
  overflow-y: auto;
  backdrop-filter: blur(20px);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
  animation: menu-in 0.15s ease-out;
  display: none;
}

@keyframes menu-in {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(-4px);
  }

  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.context-menu.visible {
  display: block;
}

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  font-size: 0.82rem;
  color: var(--text-primary);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.context-menu-item:hover {
  background: rgba(108, 92, 231, 0.15);
}

.context-menu-item.danger {
  color: #f87171;
}

.context-menu-item.danger:hover {
  background: rgba(248, 113, 113, 0.1);
}

.context-menu-item .icon {
  font-size: 1rem;
  width: 20px;
  text-align: center;
}

.context-menu-divider {
  height: 1px;
  background: var(--border-glass);
  margin: 4px 8px;
}

/* Hidden file input */
#audio-file-input {
  display: none;
}

/* ===== Responsive ===== */
@media (max-width: 820px) {
  .app-layout {
    flex-direction: column;
    gap: 30px;
    align-items: center;
  }

  .left-panel {
    max-width: 100%;
    align-items: center;
    text-align: center;
  }

  :root {
    --pad-size: 72px;
  }
}

@media (max-width: 480px) {
  :root {
    --pad-size: 60px;
    --pad-gap: 4px;
  }

  .numpad-container {
    padding: 10px;
  }

  .pad-key {
    font-size: 0.9rem;
  }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
}