/* =============================================================================
   그림이 분해되어 다시 연주되다 — 스타일
   갤러리 느낌의 어두운 테마. 작품(캔버스)이 주인공이 되도록 차분하게.
   ============================================================================= */
:root {
  /* site.css와 통일 — 중성 차콜 + 포인트색 하나(인디고). 표면 위계: 배경 < 패널 < 중첩. */
  --bg: #0b0c11;
  --bg2: #07080c;
  --panel: #15161d;
  --panel2: #1c1e27;
  --line: #262834;
  --text: #eceef4;
  --muted: #9a9eb0;
  --accent: #6b78c6;          /* 단 하나의 포인트색 — 남색 */
  --accent-rgb: 107,120,198;
  --accent2: #8b96d8;
  --danger: #f2667a;
  --radius: 12px;
  --panel-w: 348px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ----------------------------- 상단 바 ----------------------------- */
.topbar {
  height: 62px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  background: linear-gradient(180deg, var(--panel2), var(--panel));
  border-bottom: 1px solid var(--line);
  position: relative; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo {
  font-size: 26px; color: var(--accent);
  filter: drop-shadow(0 0 10px rgba(var(--accent-rgb), .5));
}
.brand h1 { font-size: 16px; margin: 0; letter-spacing: .2px; white-space: nowrap; }
.brand p { font-size: 11.5px; margin: 2px 0 0; color: var(--muted); white-space: nowrap; }
.top-actions { display: flex; gap: 8px; align-items: center; }

/* ----------------------------- 버튼 ----------------------------- */
.btn {
  font: inherit; font-size: 13px;
  color: var(--text); background: var(--panel2);
  border: 1px solid var(--line); border-radius: 9px;
  padding: 8px 12px; cursor: pointer;
  transition: .15s; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary { background: var(--accent); color: #fff; border: none; font-weight: 700; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }
.btn.rec { border-color: var(--danger); color: var(--danger); animation: pulse 1.1s infinite; }
@keyframes pulse { 50% { background: rgba(255, 90, 106, .14); } }
#btn-mic.on { border-color: var(--accent2); color: var(--accent2); }
.mini { font: inherit; font-size: 11px; padding: 3px 8px; border-radius: 7px;
  background: var(--panel2); color: var(--muted); border: 1px solid var(--line); cursor: pointer; }
.mini:hover { color: var(--accent); border-color: var(--accent); }
.only-mobile { display: none; }

/* ----------------------------- 레이아웃 ----------------------------- */
.layout { display: flex; height: calc(100% - 62px); }
.stage {
  position: relative; flex: 1; min-width: 0;
  background:
    radial-gradient(1200px 700px at 50% 40%, #11141f 0%, var(--bg2) 70%);
  overflow: hidden;
}
.stage.drop { outline: 3px dashed var(--accent); outline-offset: -10px; }
.stage canvas { display: block; }

/* 분석 중 오버레이 */
.busy {
  position: absolute; inset: 0; display: none;
  align-items: center; justify-content: center; gap: 12px;
  background: rgba(8, 10, 16, .55); backdrop-filter: blur(2px);
  color: var(--text); z-index: 5; font-size: 14px;
}
.busy.show { display: flex; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .2); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ----------------------------- 설정 패널 ----------------------------- */
.panel {
  width: var(--panel-w); flex: none;
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto; overflow-x: hidden;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
.panel::-webkit-scrollbar { width: 9px; }
.panel::-webkit-scrollbar-thumb { background: var(--line); border-radius: 9px; }

details { border-bottom: 1px solid var(--line); }
summary {
  cursor: pointer; padding: 13px 16px;
  font-size: 13.5px; font-weight: 700; letter-spacing: .3px;
  list-style: none; user-select: none;
  display: flex; align-items: center; gap: 8px;
}
summary::-webkit-details-marker { display: none; }
summary::before {
  content: "▸"; color: var(--accent); font-size: 11px; transition: .2s;
}
details[open] > summary::before { transform: rotate(90deg); }
summary:hover { background: var(--panel2); }
.sect { padding: 4px 16px 18px; }

.row { margin: 12px 0; }
.row.two { display: flex; gap: 10px; }
.row.two > div { flex: 1; min-width: 0; }
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
output { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }

select, input[type="text"], input[type="number"] {
  width: 100%; font: inherit; font-size: 13px;
  background: var(--bg2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
select:focus, input[type="text"]:focus, input[type="number"]:focus { outline: none; border-color: var(--accent); }

/* 슬라이더 */
input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 5px;
  background: var(--line); border-radius: 5px; outline: none; margin: 4px 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 16px; height: 16px;
  border-radius: 50%; background: var(--accent); cursor: pointer;
  box-shadow: 0 0 0 4px rgba(var(--accent-rgb), .2); transition: .12s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.15); }
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border: none; border-radius: 50%;
  background: var(--accent); cursor: pointer;
}

.chk { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text); margin: 10px 0; cursor: pointer; }
.chk input { accent-color: var(--accent); width: 16px; height: 16px; }
.grp { font-size: 12px; color: var(--accent2); font-weight: 700; margin: 16px 0 6px; }

.hint { font-size: 11.5px; color: var(--muted); line-height: 1.55; margin: 8px 0 0; }
.hint.warn { color: #ffcf8f; background: rgba(255, 180, 84, .08); border: 1px solid rgba(255, 180, 84, .25);
  padding: 9px 11px; border-radius: 9px; }

/* 원본 썸네일 */
.thumb-wrap { display: none; margin-top: 12px; position: relative; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.thumb-wrap.has { display: block; }
.thumb-wrap canvas { display: block; width: 100%; }
.thumb-wrap span { position: absolute; left: 8px; top: 8px; font-size: 10.5px;
  background: rgba(0, 0, 0, .55); padding: 2px 7px; border-radius: 6px; color: #fff; }

/* 팔레트 */
.palette-head { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; }
.palette-head span { font-size: 12px; color: var(--muted); }
.palette { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.swatch {
  flex: 1 1 42px; height: 46px; min-width: 38px; border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, .12); cursor: pointer; position: relative;
  display: flex; align-items: flex-end; justify-content: center; padding-bottom: 3px;
  transition: .15s;
}
.swatch:hover { transform: translateY(-2px); }
.swatch .pct { font-size: 10px; font-weight: 700; color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .8); background: rgba(0, 0, 0, .28); padding: 0 4px; border-radius: 5px; }
.swatch.off { opacity: .28; filter: grayscale(.7); }
.swatch.off::after { content: "✕"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 16px; color: #fff; }

/* 마이크 미터 */
.meters { display: none; margin-top: 12px; gap: 7px; flex-direction: column; }
.meters.show { display: flex; }
.meter { display: flex; align-items: center; gap: 8px; }
.meter span { width: 30px; font-size: 11px; color: var(--muted); }
.meter .bar { flex: 1; height: 8px; background: var(--bg2); border-radius: 6px; overflow: hidden; border: 1px solid var(--line); }
.meter .bar i { display: block; height: 100%; width: 0;
  background: linear-gradient(90deg, var(--accent2), var(--accent)); transition: width .06s linear; }

.presets { display: flex; flex-direction: column; gap: 8px; }
.presets .btn { text-align: left; }

/* ----------------------------- 토스트 ----------------------------- */
.toast {
  position: fixed; left: 50%; bottom: 26px;
  transform: translateX(-50%) translateY(20px);
  background: #11151f; border: 1px solid var(--accent); color: var(--text);
  padding: 11px 18px; border-radius: 11px; font-size: 13px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5); opacity: 0; pointer-events: none;
  transition: .25s; z-index: 50; max-width: 80vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ----------------------------- 모달 ----------------------------- */
.modal {
  position: fixed; inset: 0; display: none; align-items: center; justify-content: center;
  background: rgba(4, 6, 12, .7); backdrop-filter: blur(3px); z-index: 60; padding: 20px;
}
.modal.show { display: flex; }
.modal-box {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  max-width: 620px; width: 100%; max-height: 86vh; overflow-y: auto; padding: 26px 28px; position: relative;
}
.modal-box h2 { margin: 0 0 14px; font-size: 19px; }
.modal-box ol { margin: 0; padding-left: 20px; line-height: 1.7; font-size: 13.5px; }
.modal-box li { margin: 7px 0; }
.modal-box b { color: var(--accent); }
.modal-close { position: absolute; right: 14px; top: 14px; background: var(--panel2);
  border: 1px solid var(--line); color: var(--text); width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.modal-close:hover { border-color: var(--accent); }

/* ----------------------------- 반응형 ----------------------------- */
@media (max-width: 860px) {
  body { overflow: auto; }
  .brand p { display: none; }
  .top-actions .btn:not(.primary):not(.ghost) { display: none; }
  .only-mobile { display: inline-block; }
  .layout { flex-direction: column; height: calc(100% - 62px); }
  .stage { height: 56vh; }
  .panel {
    width: 100%; position: fixed; right: 0; top: 62px; bottom: 0;
    transform: translateX(100%); transition: transform .25s; z-index: 25; box-shadow: -10px 0 40px rgba(0, 0, 0, .5);
  }
  body.panel-open .panel { transform: translateX(0); }
}

/* ===== Phase A: 비례 팔레트 · 팔레트 차트 · 너비 스플리터 · K-means 모달 ===== */
#splitter { flex: 0 0 8px; cursor: col-resize; position: relative;
  background: linear-gradient(90deg, transparent, var(--line), transparent); }
#splitter::after { content: "⋮"; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); color: var(--muted); font-size: 14px; }
#splitter:hover { background: var(--accent); }
@media (max-width: 880px) { #splitter { display: none; } }

#num-k { width: 100%; }
.palette.prop { display: flex; flex-wrap: nowrap; gap: 0; height: 30px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); margin-top: 8px; }
.palette.prop .pseg { flex-grow: 1; flex-basis: 0; min-width: 2px; height: 100%; border: 0; padding: 0; cursor: pointer; position: relative; transition: filter .12s; display: flex; align-items: center; justify-content: center; }
.palette.prop .pseg:hover { filter: brightness(1.15); }
.palette.prop .pseg .pct { font-size: 9.5px; font-weight: 700; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.6); pointer-events: none; }
.palette.prop .pseg.off { filter: grayscale(.85) brightness(.5); }

.pal-charts { margin-top: 10px; }
/* 차트 탭 8개 — 좁은 패널에서도 글자가 세로로 쪼개지지 않게 4칸×2줄 격자 */
.pal-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; margin-bottom: 8px; }
.ptab { font: inherit; font-size: 11.5px; color: var(--muted); background: var(--panel2); border: 1px solid var(--line); border-radius: 7px; padding: 4px 2px; cursor: pointer; white-space: nowrap; text-align: center; min-width: 0; }
.ptab.on { color: #fff; background: var(--accent); border-color: var(--accent); font-weight: 700; }
#pal-canvas { width: 100%; background: #0c0e16; border: 1px solid var(--line); border-radius: 8px; display: block; }

.modal-box { max-height: 88vh; overflow-y: auto; }
.km-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 12px; }
.km-tab { font: inherit; font-size: 13px; color: var(--muted); background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 6px 14px; cursor: pointer; }
.km-tab.on { color: var(--text); border-color: var(--accent); background: rgba(var(--accent-rgb),.12); font-weight: 700; }
.km-level { display: none; line-height: 1.7; font-size: 14px; }
.km-level.show { display: block; }
.km-level .km-ex { background: var(--panel2); border-left: 3px solid var(--accent); padding: 9px 12px; border-radius: 0 8px 8px 0; margin: 9px 0; color: #ffe3bd; }
.km-level .km-ol, .km-level .km-ul { margin: 8px 0; padding-left: 20px; }
.km-level .km-ol li, .km-level .km-ul li { margin: 5px 0; }
.km-level code { background: var(--panel2); padding: 1px 6px; border-radius: 5px; }

/* ☀ 밝은 테마 — 패널/배경을 밝게(작품 캔버스는 앱이 어둡게 칠해 '액자' 느낌 유지) */
:root[data-theme="light"] {
  --bg: #eef1f8; --bg2: #e6e9f2; --panel: #ffffff; --panel2: #f1f3f9;
  --line: #d8deec; --text: #1b2233; --muted: #5b6580;
}
:root[data-theme="light"] .topbar { background: var(--panel); }
:root[data-theme="light"] #pal-canvas { background: #0c0e16; }
