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

/* ===== Dark Palettes ===== */

/* Ocean (default dark, colorblind-friendly) */
:root,
[data-theme="dark"][data-palette="ocean"] {
  --bg: #0f1b2d;
  --bg-surface: #132438;
  --bg-elevated: #1a3a5c;
  --text: #d4e5f7;
  --text-muted: #7a9ec0;
  --accent: #00b4d8;
  --accent-hover: #48cae4;
  --border: #1e3650;
  --beat-bg: #162d45;
  --beat-active: #00b4d8;
  --vol-active: #00b4d8;
  --vol-inactive: #1e3650;
  --btn-bg: #1a3a5c;
  --btn-hover: #244a6e;
  --input-bg: #132438;
  --input-border: #1e3650;
}

/* Ruby (dark) */
[data-theme="dark"][data-palette="ruby"] {
  --bg: #1a1a2e;
  --bg-surface: #16213e;
  --bg-elevated: #0f3460;
  --text: #e0e0e0;
  --text-muted: #8888aa;
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --border: #2a2a4a;
  --beat-bg: #1e2a4a;
  --beat-active: #e94560;
  --vol-active: #e94560;
  --vol-inactive: #333355;
  --btn-bg: #0f3460;
  --btn-hover: #1a4a80;
  --input-bg: #16213e;
  --input-border: #2a2a4a;
}

/* Forest (dark) */
[data-theme="dark"][data-palette="forest"] {
  --bg: #1a2e1a;
  --bg-surface: #162e1c;
  --bg-elevated: #0f4a2a;
  --text: #d8e8d8;
  --text-muted: #7aaa7a;
  --accent: #4caf50;
  --accent-hover: #66cc6a;
  --border: #2a4a2a;
  --beat-bg: #1e3a22;
  --beat-active: #4caf50;
  --vol-active: #4caf50;
  --vol-inactive: #2a4a2a;
  --btn-bg: #0f4a2a;
  --btn-hover: #1a5a35;
  --input-bg: #162e1c;
  --input-border: #2a4a2a;
}

/* Amber (dark, colorblind-friendly) */
[data-theme="dark"][data-palette="amber"] {
  --bg: #1f1a0e;
  --bg-surface: #2a2010;
  --bg-elevated: #3d2e0f;
  --text: #f0e0c0;
  --text-muted: #b09870;
  --accent: #f0a030;
  --accent-hover: #f5b850;
  --border: #3a3018;
  --beat-bg: #302810;
  --beat-active: #f0a030;
  --vol-active: #f0a030;
  --vol-inactive: #3a3018;
  --btn-bg: #3d2e0f;
  --btn-hover: #4d3e1f;
  --input-bg: #2a2010;
  --input-border: #3a3018;
}

/* ===== Light Palettes ===== */

/* Ruby (light) */
[data-theme="light"][data-palette="ruby"] {
  --bg: #f5f0f0;
  --bg-surface: #ffffff;
  --bg-elevated: #f0e0e0;
  --text: #222222;
  --text-muted: #666666;
  --accent: #d63031;
  --accent-hover: #e84343;
  --border: #dcc;
  --beat-bg: #f0e0e0;
  --beat-active: #d63031;
  --vol-active: #d63031;
  --vol-inactive: #ccbbbb;
  --btn-bg: #e8d8d8;
  --btn-hover: #dcc8c8;
  --input-bg: #ffffff;
  --input-border: #dcc;
}

/* Forest (light) */
[data-theme="light"][data-palette="forest"] {
  --bg: #f0f5f0;
  --bg-surface: #ffffff;
  --bg-elevated: #ddf0dd;
  --text: #1a2e1a;
  --text-muted: #557755;
  --accent: #2e8b32;
  --accent-hover: #3da342;
  --border: #bbddbb;
  --beat-bg: #ddf0dd;
  --beat-active: #2e8b32;
  --vol-active: #2e8b32;
  --vol-inactive: #bbccbb;
  --btn-bg: #d0e8d0;
  --btn-hover: #c0d8c0;
  --input-bg: #ffffff;
  --input-border: #bbddbb;
}

/* Ocean (light, colorblind-friendly) */
[data-theme="light"][data-palette="ocean"] {
  --bg: #eef5f8;
  --bg-surface: #ffffff;
  --bg-elevated: #d8eef5;
  --text: #0f2b3d;
  --text-muted: #4a7a8f;
  --accent: #0088a8;
  --accent-hover: #00a0c4;
  --border: #b0d4e0;
  --beat-bg: #d8eef5;
  --beat-active: #0088a8;
  --vol-active: #0088a8;
  --vol-inactive: #b0c8d0;
  --btn-bg: #c8e0ea;
  --btn-hover: #b8d0da;
  --input-bg: #ffffff;
  --input-border: #b0d4e0;
}

/* Amber (light, colorblind-friendly) */
[data-theme="light"][data-palette="amber"] {
  --bg: #f8f4ee;
  --bg-surface: #ffffff;
  --bg-elevated: #f0e8d8;
  --text: #2a2010;
  --text-muted: #7a6840;
  --accent: #c87d18;
  --accent-hover: #da9030;
  --border: #d8c8a8;
  --beat-bg: #f0e8d8;
  --beat-active: #c87d18;
  --vol-active: #c87d18;
  --vol-inactive: #c8b898;
  --btn-bg: #e8dcc8;
  --btn-hover: #d8ccb8;
  --input-bg: #ffffff;
  --input-border: #d8c8a8;
}

html {
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 1rem;
}

.container {
  max-width: 600px;
  width: 100%;
}

/* Header */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.5rem;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.25rem;
  display: flex;
  gap: 0.35rem;
  align-items: center;
  color: var(--text);
}

.btn-icon:hover {
  color: var(--accent);
}

/* Drawer */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
  z-index: 100;
}

.drawer-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  max-width: 85vw;
  height: 100%;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 101;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.drawer-header h2 {
  font-size: 1.2rem;
  color: var(--text);
}

.drawer-section {
  margin-bottom: 1.5rem;
}

.drawer-section h3 {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.theme-toggle-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  width: 100%;
  border-radius: 6px;
  background: var(--btn-bg);
}

.theme-toggle-row:hover {
  background: var(--btn-hover);
}

#theme-label {
  font-size: 0.9rem;
}

.theme-icon {
  opacity: 0.4;
  transition: opacity 0.15s;
}

.theme-icon.active {
  opacity: 1;
  fill: currentColor;
}

.palette-select {
  width: 100%;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 0.5rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.drawer-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
}

.sound-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.sound-btn {
  background: var(--btn-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s;
}

.sound-btn:hover {
  background: var(--btn-hover);
}

.sound-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.drawer-text {
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.5;
  margin-bottom: 0.5rem;
}

.header h1 {
  font-size: 1.5rem;
  font-weight: 700;
}

/* Buttons */
.btn {
  background: var(--btn-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 1rem;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.15s;
}

.btn:hover {
  background: var(--btn-hover);
}

.btn-small {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

/* Tempo Section */
.tempo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 1.5rem;
}

.tempo-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.tempo-input {
  width: 100px;
  font-size: 2.5rem;
  font-weight: 700;
  text-align: center;
  background: transparent;
  color: var(--text);
  border: none;
  border-bottom: 2px solid var(--border);
  outline: none;
  -moz-appearance: textfield;
}

.tempo-input::-webkit-inner-spin-button,
.tempo-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
}

.tempo-input:focus {
  border-bottom-color: var(--accent);
}

.tempo-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.tempo-unit {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  line-height: 1;
}

.tempo-name {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
  line-height: 1.3;
}

.tempo-slider {
  width: 100%;
  max-width: 400px;
  margin-bottom: 1rem;
  accent-color: var(--accent);
}

.btn-tap {
  padding: 0.75rem 2rem;
  font-size: 1.1rem;
  font-weight: 600;
  min-height: 48px;
  min-width: 140px;
}

.btn-tap:active {
  background: var(--accent);
  color: white;
}

/* Time Signature Section */
.time-sig-section {
  margin-bottom: 1.5rem;
}

.time-sig-controls {
  display: flex;
  gap: 2rem;
  justify-content: center;
  flex-wrap: wrap;
}

.time-sig-group label,
.subdiv-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
  text-align: center;
}

.time-sig-selectors {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.time-sig-selectors select {
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  font-size: 1.2rem;
  cursor: pointer;
  text-align: center;
}

.time-sig-divider {
  font-size: 1.5rem;
  font-weight: 700;
}

.subdiv-selector {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.subdiv-btn {
  background: var(--btn-bg);
  color: var(--text);
  border: none;
  border-right: 1px solid var(--border);
  padding: 0.4rem 0.75rem;
  cursor: pointer;
  font-size: 1.1rem;
  transition: background 0.15s;
}

.subdiv-btn:last-child {
  border-right: none;
}

.subdiv-btn.active {
  background: var(--accent);
  color: white;
}

.subdiv-btn:hover:not(.active) {
  background: var(--btn-hover);
}

/* Beat Section */
.beat-section {
  margin-bottom: 1.5rem;
}

.beat-options {
  display: flex;
  justify-content: center;
  margin-bottom: 0.75rem;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.toggle-label input[type="checkbox"] {
  accent-color: var(--accent);
}

.beat-grid-wrapper {
  position: relative;
}

.beat-grid {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.beat-group {
  display: flex;
  gap: 3px;
  padding: 4px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--beat-bg) 50%, var(--bg));
}

.beat-group.first-beat {
  border-left: 2px solid var(--accent);
}

.beat-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 6px;
  min-width: 40px;
  background: var(--beat-bg);
  border: 2px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.1s, background 0.1s;
  user-select: none;
}

.beat-cell:hover {
  border-color: var(--accent);
}

.beat-cell.active {
  border-color: var(--beat-active);
  background: color-mix(in srgb, var(--beat-active) 20%, var(--beat-bg));
}

.beat-cell.subdivision-beat {
  min-width: 32px;
  opacity: 0.75;
}

.beat-cell.subdivision-beat .beat-label {
  font-size: 0.7rem;
}

.beat-label {
  font-size: 0.85rem;
  font-weight: 700;
}

.volume-dots {
  display: flex;
  gap: 3px;
}

.vol-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--vol-inactive);
}

.vol-dot.active {
  background: var(--vol-active);
}

.beat-volume {
  width: 100%;
  height: 3px;
  border-radius: 2px;
  background: var(--vol-inactive);
  transition: background 0.15s;
}

.beat-volume.vol-3 { background: var(--vol-active); }
.beat-volume.vol-2 { background: color-mix(in srgb, var(--vol-active) 60%, var(--vol-inactive)); }
.beat-volume.vol-1 { background: color-mix(in srgb, var(--vol-active) 30%, var(--vol-inactive)); }
.beat-volume.vol-0 { background: var(--vol-inactive); }

/* Transport */
.transport-section {
  text-align: center;
  margin-bottom: 1.5rem;
}

.btn-play {
  padding: 0.8rem 3rem;
  font-size: 1.3rem;
  font-weight: 700;
  border-radius: 50px;
  background: var(--accent);
  color: white;
  border: none;
  letter-spacing: 0.05em;
}

.btn-play:hover {
  background: var(--accent-hover);
}

.btn-play.playing {
  background: var(--btn-bg);
  color: var(--text);
  border: 2px solid var(--accent);
}

.btn-play.playing:hover {
  background: var(--btn-hover);
}

.session-timer {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  margin-top: 0.5rem;
  text-align: center;
}

.session-timer-buttons {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.practice-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin-top: 0.5rem;
  text-align: center;
}

.practice-col {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.practice-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.practice-value {
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.practice-avg-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.practice-avg-value {
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

/* Metrics Section */
.metrics-section {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  margin-bottom: 1.5rem;
}

.metrics-section h2 {
  font-size: 1rem;
  margin-bottom: 0.5rem;
  color: var(--text-muted);
}

.metrics-details summary {
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
}

.metrics-group h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
  color: var(--text);
}

.metrics-row {
  display: flex;
  justify-content: space-between;
  padding: 0.2rem 0;
  font-size: 0.8rem;
  border-bottom: 1px solid var(--border);
}

.metrics-label {
  color: var(--text-muted);
}

.metrics-value {
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* Presets Section */
.preset-section {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  margin-bottom: 1.5rem;
}

.preset-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.preset-section h2 {
  font-size: 1rem;
  margin-bottom: 0;
  color: var(--text-muted);
}

.preset-controls {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.preset-list {
  width: 100%;
  max-height: calc(5 * 2.2rem);
  overflow-y: auto;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 6px;
}

.preset-item {
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  transition: background 0.1s;
}

.preset-item:last-child {
  border-bottom: none;
}

.preset-item:hover {
  background: var(--btn-hover);
}

.preset-item.active {
  background: var(--accent);
  color: white;
}

.preset-item .preset-item-badge {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-left: 0.4rem;
}

.preset-item.active .preset-item-badge {
  color: rgba(255, 255, 255, 0.7);
}

.preset-new-row {
  display: flex;
  gap: 0.5rem;
}

.preset-name-input {
  flex: 1;
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 0.35rem 0.6rem;
  font-size: 0.85rem;
}

.preset-name-input::placeholder {
  color: var(--text-muted);
}

.preset-buttons {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* Responsive */
@media (max-width: 480px) {
  .tempo-input {
    font-size: 2rem;
    width: 80px;
  }

  .time-sig-controls {
    gap: 1rem;
  }

  .beat-cell {
    min-width: 34px;
    padding: 6px 4px;
  }

  .btn-play {
    padding: 0.7rem 2.5rem;
    font-size: 1.1rem;
  }

  .preset-buttons {
    justify-content: center;
  }
}
