/*
 * 완전한 검정 배경(#000)을 기본으로 한다. OLED 화면은 검은 픽셀을 아예 끄기 때문에
 * 밝은 면적을 줄이는 것이 그대로 배터리 절약이 된다. 큰 면적은 검정으로 두고,
 * 강조는 얇은 선과 작은 글자에만 쓴다.
 */

:root {
  --bg: #000;
  --surface: #0a0a0b;
  --line: #1c1c1f;
  --line-soft: #121214;
  --text: #e9e9ec;
  --text-dim: #7a7a83;
  --text-faint: #46464e;
  --accent: #35d39a;
  --accent-dim: #1b6b50;
  --flat: #6aa6ff;
  --sharp: #ffb84d;
  --danger: #ff5f56;
  --lane-w: 62px;
  --axis-w: 40px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  /* 안드로이드에서 길게 누르면 글자가 선택되고 메뉴가 뜬다. 앱처럼 쓰는 화면이라 막는다. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* 입력란은 당연히 선택과 편집이 되어야 한다 */
input, textarea {
  -webkit-user-select: text;
  user-select: text;
}

/* CSS 의 display 가 hidden 속성을 덮지 않도록 한다 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Noto Sans KR", "Apple SD Gothic Neo", sans-serif;
  overscroll-behavior: none;
  /* 브라우저 기본 핀치 확대와 더블탭 확대를 끈다. 핀치는 차트가 직접 받는다. */
  touch-action: none;
}

body {
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

/* ---------- 시작 화면 ---------- */

#gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg);
  z-index: 20;
  padding: 24px;
}
.gate-inner { text-align: center; max-width: 320px; }
.logo {
  width: 56px; height: 96px;
  margin: 0 auto 28px;
  border: 1px solid var(--line);
  border-radius: 28px;
  background: linear-gradient(to top, transparent 18%, var(--accent-dim) 18%, var(--accent-dim) 72%, transparent 72%);
  position: relative;
}
.logo::after {
  content: "";
  position: absolute;
  left: -14px; right: -14px; top: 44%;
  height: 1px;
  background: var(--accent);
}
#gate h1 { margin: 0; font-size: 34px; font-weight: 600; letter-spacing: -0.02em; }
.tagline { margin: 8px 0 32px; color: var(--text-dim); font-size: 14px; }

.primary {
  display: block; width: 100%;
  padding: 15px;
  border-radius: 12px;
  background: var(--accent);
  color: #001b12;
  font-weight: 600;
  font-size: 16px;
}
.ghost {
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text-dim);
  font-size: 14px;
}
.ghost.small { padding: 7px 11px; font-size: 13px; }
.danger { padding: 13px 18px; border-radius: 12px; border: 1px solid #3a1c1a; color: var(--danger); }

.hint { color: var(--text-faint); font-size: 12px; line-height: 1.6; margin: 14px 0 0; }
#gateHint.error { color: var(--danger); }
#installBtn { margin: 20px auto 0; }

/* ---------- 앱 뼈대 ---------- */

#app { display: flex; flex-direction: column; flex: 1; min-height: 0; }

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line-soft);
  flex: none;
  /* 칩이 다 못 들어가는 좁은 화면에서는 눌려서 글자가 깨지는 대신 옆으로 밀린다. */
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
}
header::-webkit-scrollbar { display: none; }
header > .icon, header > .badge { flex: none; }
.spacer { flex: 1; }
.icon {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 11px;
  border-radius: 9px;
  border: 1px solid var(--line);
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
}
.icon.only { padding: 8px; }
.icon svg {
  display: block;
  width: 17px; height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.mic-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
}
#micBtn[aria-pressed="false"] .mic-dot { background: var(--text-faint); }
#micBtn[aria-pressed="false"] { color: var(--text-faint); }
.badge {
  font-size: 11px;
  color: var(--text-faint);
  border: 1px solid var(--line-soft);
  padding: 5px 8px;
  border-radius: 7px;
  white-space: nowrap;
}

#sensBtn .num { margin-left: 5px; font-variant-numeric: tabular-nums; }

/* 좁은 화면에서 칩이 다 들어가도록 여백을 줄이고 감도 숫자는 감춘다. */
@media (max-width: 360px) {
  header { gap: 6px; padding: 10px 10px; }
  .icon { padding: 7px 9px; gap: 5px; }
  .icon.only { padding: 7px; }
  .badge { padding: 5px 6px; }
  #sensBtn .num { display: none; }
}

/* ---------- 현재 음 ---------- */

#readout {
  flex: none;
  padding: 22px 16px 18px;
  text-align: center;
}
#noteBig {
  font-size: clamp(58px, 22vw, 104px);
  font-weight: 300;
  line-height: 1;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  transition: opacity 160ms linear;
}
#noteBig.idle { color: var(--text-faint); opacity: 0.6; }
#noteOct { font-size: 0.42em; color: var(--text-dim); margin-left: 0.06em; vertical-align: 0.15em; }

#centsRow { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 18px; }
#centsMeter {
  position: relative;
  width: min(58vw, 240px);
  height: 22px;
  border-bottom: 1px solid var(--line);
}
.tick { position: absolute; bottom: 0; width: 1px; background: var(--line); }
.tick.left { left: 0; height: 8px; }
.tick.right { right: 0; height: 8px; }
.tick.center { left: 50%; height: 14px; background: var(--text-faint); }
#centsBar {
  position: absolute;
  bottom: 0;
  left: 50%;
  height: 3px;
  width: 0;
  min-width: 3px; /* 0 센트에서도 지시자가 보이도록 */
  background: var(--accent);
  transform-origin: left center;
  transition: none;
}
#centsBar.flat { background: var(--flat); }
#centsBar.sharp { background: var(--sharp); }
#centsText {
  min-width: 62px;
  text-align: left;
  font-size: 14px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
#freqText { margin-top: 10px; font-size: 12px; color: var(--text-faint); font-variant-numeric: tabular-nums; }

/* ---------- 음역대 차트 ---------- */

#chart {
  flex: 1;
  min-height: 0;
  border-top: 1px solid var(--line-soft);
}
#history { display: block; width: 100%; height: 100%; touch-action: none; }

#legend {
  flex: none;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line-soft);
  touch-action: pan-x;
}
#legend::-webkit-scrollbar { display: none; }
#legend button {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  white-space: nowrap;
  font-size: 12px;
  color: var(--text-dim);
}
#legend .swatch { width: 7px; height: 7px; border-radius: 2px; flex: none; }
#legend .rg { color: var(--text-faint); font-variant-numeric: tabular-nums; font-size: 11px; }

/* ---------- 편집 시트 ---------- */

#sheet { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.72); }
.sheet-body {
  position: relative;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  touch-action: pan-y;
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.sheet-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.field { display: block; margin-bottom: 18px; }
.field > span { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 7px; }
.field input {
  width: 100%;
  padding: 12px 13px;
  font: inherit;
  color: var(--text);
  background: #000;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.field input:focus { outline: none; border-color: var(--accent-dim); }

.note-fields { display: flex; gap: 10px; margin-bottom: 16px; }
.note-field { flex: 1; text-align: center; }
.note-field .lbl { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 8px; }
.stepper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.stepper button { width: 42px; height: 44px; font-size: 19px; color: var(--text-dim); }
.stepper b { font-size: 19px; font-weight: 500; font-variant-numeric: tabular-nums; }

.sheet-actions { display: flex; gap: 10px; }
.sheet-actions .primary { flex: 1; }

.saved { margin-top: 26px; border-top: 1px solid var(--line-soft); padding-top: 18px; }
.saved-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.saved-head h3 { margin: 0; font-size: 13px; font-weight: 500; color: var(--text-dim); }
#savedList { list-style: none; margin: 0; padding: 0; }
#savedList li {
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--line-soft);
  background: var(--surface);   /* 끌 때 아래 줄을 가려야 겹쳐 보이지 않는다 */
  transition: transform 140ms ease;
}
#savedList .row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  padding: 13px 2px;
  text-align: left;
}
/* 끄는 동안에는 손가락을 그대로 따라와야 하므로 애니메이션을 끈다. */
#savedList li.dragging {
  transition: none;
  position: relative;
  z-index: 1;
  border-radius: 10px;
  border-bottom-color: transparent;
  background: #141417;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.55);
}
/* 손잡이는 세로로 끌어야 하므로 시트의 세로 스크롤에서 빼 온다. */
.handle {
  flex: none;
  padding: 12px 2px 12px 12px;
  color: var(--text-faint);
  touch-action: none;
}
.handle svg {
  display: block;
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}
#savedList li.dragging .handle { color: var(--text-dim); }
#savedList .swatch { width: 8px; height: 8px; border-radius: 2px; flex: none; }
#savedList .nm { flex: 1; min-width: 0; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#savedList .rg { font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
#savedList .empty { padding: 14px 2px; color: var(--text-faint); font-size: 13px; }
#sortHint { margin-top: 12px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0 20px;
  font-size: 13px;
  color: var(--text-dim);
}
.toggle input {
  appearance: none;
  width: 40px; height: 24px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #000;
  position: relative;
  flex: none;
  transition: border-color 120ms linear;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--text-faint);
  transition: transform 120ms ease, background 120ms linear;
}
.toggle input:checked { border-color: var(--accent-dim); }
.toggle input:checked::after { background: var(--accent); transform: translateX(16px); }

/* ---------- 감도 ---------- */

.range-field { margin-bottom: 22px; }
.range-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-dim);
}
.range-head b {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/*
 * touch-action 을 여기서 다시 none 으로 둔다. 시트가 세로 스크롤(pan-y)을 받고 있어서
 * 그대로 두면 손가락을 슬라이더 위에서 움직일 때 스크롤에 끌려간다.
 */
#fSens {
  appearance: none;
  -webkit-appearance: none;
  display: block;
  width: 100%;
  height: 30px;
  margin: 2px 0 0;
  background: none;
  touch-action: none;
}
#fSens:focus { outline: none; }
#fSens::-webkit-slider-runnable-track { height: 2px; background: var(--line); border-radius: 2px; }
#fSens::-moz-range-track { height: 2px; background: var(--line); border-radius: 2px; }
#fSens::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px; height: 22px;
  margin-top: -10px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
}
#fSens::-moz-range-thumb {
  width: 22px; height: 22px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
}

#levelMeter {
  position: relative;
  height: 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #000;
}
#levelBar {
  position: absolute;
  top: 0; bottom: 0; left: 0;
  width: 0;
  border-radius: 3px 0 0 3px;
  background: var(--text-faint);
}
#levelBar.over { background: var(--accent); }
/* 문턱 눈금. 막대가 여기를 넘어야 소리로 인정된다. */
#levelMark {
  position: absolute;
  top: -4px; bottom: -4px;
  left: 0;
  width: 2px;
  margin-left: -1px;
  background: var(--text);
}
.range-readout {
  display: flex;
  justify-content: space-between;
  margin-top: 9px;
  font-size: 11px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}
.range-field .hint { margin-top: 10px; }

.section-label {
  margin: 22px 0 12px;
  padding-top: 16px;
  border-top: 1px solid var(--line-soft);
  font-size: 12px;
  color: var(--text-faint);
}
