:root {
  --bg: #f4f2fb;
  --surface: #ffffff;
  --surface-2: #f0edfa;
  --text: #201c33;
  --text-dim: #6a6480;
  --accent: #7c5cff;
  --accent-2: #ff5c8a;
  --accent-3: #22c1a4;
  --border: #e1dcf5;
  --shadow: 0 8px 24px rgba(80, 60, 160, 0.08);
  --key-white: #ffffff;
  --key-black: #201c33;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131020;
    --surface: #1c1830;
    --surface-2: #241f3d;
    --text: #ede9fb;
    --text-dim: #a49dc4;
    --border: #322b4d;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    --key-white: #ede9fb;
    --key-black: #0e0c18;
  }
}

:root[data-theme="dark"] {
  --bg: #131020;
  --surface: #1c1830;
  --surface-2: #241f3d;
  --text: #ede9fb;
  --text-dim: #a49dc4;
  --border: #322b4d;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --key-white: #ede9fb;
  --key-black: #0e0c18;
}

* { box-sizing: border-box; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Pretendard", "Segoe UI", system-ui, sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
}

.app-header {
  position: relative;
  padding: 28px 20px 8px;
  text-align: center;
}

.theme-toggle {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  transition: transform 0.15s ease;
}
.theme-toggle:hover { transform: scale(1.08); }
.theme-toggle:active { transform: scale(0.96); }
.app-header h1 {
  margin: 0;
  font-size: clamp(28px, 5vw, 40px);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  letter-spacing: -0.02em;
}
.tagline {
  margin: 4px 0 0;
  color: var(--text-dim);
  font-size: 14px;
}

.tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 20px 12px 0;
  flex-wrap: wrap;
}
.tab-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fade-in 0.2s ease; }

@keyframes fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 18px;
  align-items: start;
}
@media (max-width: 860px) {
  .panel-grid { grid-template-columns: 1fr; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 20px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card h2 {
  margin: 0 0 8px;
  font-size: 17px;
}
.card h2.mt { margin-top: 18px; }
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 6px;
}
.hint {
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
  margin: 0 0 12px;
}
.hint code {
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 12px;
}

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row.mt { margin-top: 14px; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.small { padding: 4px 10px; font-size: 14px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-accent { background: var(--accent-3); border-color: var(--accent-3); color: #06251f; }

.draw-canvas {
  width: 100%;
  height: 280px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  touch-action: none;
  cursor: crosshair;
  display: block;
}

.mini-canvas {
  width: 100%;
  height: 160px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  display: block;
}

.scope-canvas {
  width: 100%;
  height: 140px;
  border-radius: 12px;
  background: #0e0c18;
  border: 1px solid var(--border);
  display: block;
  margin-bottom: 14px;
}

.combine-controls { display: flex; flex-direction: column; gap: 16px; }
.combine-row {
  display: grid;
  grid-template-columns: 90px 1fr 48px;
  align-items: center;
  gap: 12px;
}
.combine-row .wave-name {
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.combine-row .swatch {
  width: 10px; height: 10px; border-radius: 50%;
}
.combine-row input[type="range"] { width: 100%; }
.combine-row .value { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; color: var(--text-dim); }

input[type="range"] {
  accent-color: var(--accent);
  width: 100%;
  min-width: 0;
}

.preset-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.preset-row input[type="text"] {
  flex: 1;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
}
.preset-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 180px; overflow-y: auto; }
.preset-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
}
.preset-list .preset-actions { display: flex; gap: 6px; }
.preset-list button { border: none; background: none; cursor: pointer; color: var(--accent); font-weight: 600; font-size: 13px; }
.preset-list button.del { color: var(--accent-2); }

.badge {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
}

.controls-row {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.ctrl {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 600;
  flex: 1 1 110px;
  min-width: 0;
}
.octave-ctrl {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
}

.piano-grid { grid-template-columns: 1fr; }

.piano-keyboard {
  position: relative;
  display: flex;
  height: 180px;
  overflow-x: auto;
  overflow-y: hidden;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 8px;
  gap: 2px;
  -webkit-overflow-scrolling: touch;
}
.pk {
  position: relative;
  flex: 0 0 auto;
  border-radius: 0 0 8px 8px;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 8px;
  font-size: 11px;
  font-weight: 700;
  transition: background 0.05s ease;
}
.pk.white {
  width: 46px;
  height: 100%;
  background: var(--key-white);
  color: var(--text-dim);
  border: 1px solid var(--border);
  z-index: 1;
}
.pk.black {
  width: 30px;
  height: 60%;
  background: var(--key-black);
  color: #cfc9e8;
  margin-left: -16px;
  margin-right: -16px;
  z-index: 2;
}
.pk.active.white { background: var(--accent); color: #fff; }
.pk.active.black { background: var(--accent-2); color: #fff; }

.app-footer {
  text-align: center;
  padding: 20px;
  color: var(--text-dim);
  font-size: 12px;
}

@media (max-width: 480px) {
  .combine-row { grid-template-columns: 70px 1fr 40px; }
  .controls-row { gap: 12px; }
}
