@font-face {
  font-family: "Orbitron";
  src: url("asset/fonts/orbitron-latin.woff2") format("woff2");
  font-weight: 400 900;   /* 가변 글꼴 */
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #000;
  --fg: #e8e8e8;
  --muted: #8a8a8a;
  --line: #2a2a2a;
  --accent: #ff7ad9;
  --panel: #111;
  --btn: #1a1a1a;
  --btn-hover: #242424;
  --btn-border: #333;
  --strong: #fff;
  --label: #ccc;
  --meter-off: #3a1020;
  --meter-on: #12301a;
  --meter-center: #777;
  --overlay: rgba(0, 0, 0, .78);
  --shadow: rgba(0, 0, 0, .7);
  --error: #ff8080;
  --surface: #0e0e0e;
  --staff-line: rgba(200, 200, 200, .55);
  --seg-bg: #1c1c1c;
  --seg-border: #3a3a3a;
  --seg-active: #3a3a3a;
  --seg-active-fg: #fff;
  --accent-weak: rgba(255, 122, 217, .14);
  --font-digit: "Orbitron", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f6f1e4;
  --fg: #2a251c;
  --muted: #7a7062;
  --line: #ddd3bc;
  --accent: #c2189a;
  --panel: #fbf7ec;
  --btn: #ece5d3;
  --btn-hover: #e2d9c3;
  --btn-border: #d3c9b0;
  --strong: #000;
  --label: #4a4336;
  --meter-off: #f3c6d2;
  --meter-on: #bfe6c8;
  --meter-center: #888;
  --overlay: rgba(246, 241, 228, .85);
  --shadow: rgba(0, 0, 0, .18);
  --error: #c62828;
  --surface: #efe8d7;
  --staff-line: rgba(60, 60, 60, .40);
  --seg-bg: #efe8d7;
  --seg-border: transparent;
  --seg-active: #fbf7ec;
  --seg-active-fg: #2a251c;
  --accent-weak: rgba(194, 24, 154, .10);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  overscroll-behavior: none;
}

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

/* ---------- HUD ---------- */
.hud {
  display: grid;
  grid-template-columns: auto minmax(160px, 380px) 1fr auto auto;
  grid-template-areas: "note meter stats mode tools";
  align-items: center;
  gap: 10px 28px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
}

.hud-note { grid-area: note; display: flex; align-items: baseline; gap: 10px; width: 230px; }
.note {
  font-family: var(--font-digit);
  font-size: 48px;
  font-weight: 600;
  line-height: 1;
  width: 3.1em;
  white-space: nowrap;
  transition: color .1s;
}
.cents { font-family: var(--font-digit); font-size: 16px; color: var(--muted); width: 4.2em; white-space: nowrap; }

.hud-meter { grid-area: meter; }
.meter-track {
  position: relative;
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--meter-off), var(--meter-on) 40%, var(--meter-on) 60%, var(--meter-off));
}
.meter-center { position: absolute; left: 50%; top: -3px; bottom: -3px; width: 2px; margin-left: -1px; background: var(--meter-center); }
.meter-needle {
  position: absolute; top: -4px; bottom: -4px; width: 4px; left: 50%; margin-left: -2px;
  border-radius: 2px; background: var(--accent); box-shadow: 0 0 8px var(--accent);
  opacity: 0; transition: left .06s linear, opacity .2s;
}
.meter-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 5px; }

.hud-stats { grid-area: stats; display: flex; justify-content: flex-end; gap: 8px; }
/* 숫자 길이가 바뀌어도 칸이 움직이지 않도록 너비 고정 */
.stat {
  display: flex; flex-direction: column; gap: 3px;
  flex: 0 0 var(--stat-w, 136px);
  min-width: 0;
  overflow: hidden;
  padding: 7px 12px;
  background: var(--surface);
  border-radius: 10px;
}
.stat .k { font-size: 11px; color: var(--muted); letter-spacing: .02em; }
.stat .v { font-family: var(--font-digit); font-size: 16px; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* ---------- Mode switch (연습 / 합주) ---------- */
.modes {
  grid-area: mode;
  display: inline-flex;
  padding: 3px;
  background: var(--seg-bg);
  border: 1px solid var(--seg-border);
  border-radius: 10px;
}
.modes button {
  appearance: none;
  font: inherit; font-size: 13px; font-weight: 600;
  color: var(--muted);
  background: none;
  border: none;
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.modes button:hover { color: var(--fg); }
.modes button.active { color: var(--seg-active-fg); background: var(--seg-active); box-shadow: 0 1px 3px var(--shadow); }
body:not([data-mode="ensemble"]) .ens-only { display: none; }
.panel .tip {
  font-size: 12px; line-height: 1.6; color: var(--label);
  margin: 0 0 4px; padding: 10px 12px;
  background: var(--accent-weak); border-radius: 8px;
}

/* ---------- Toolbar (top right) ---------- */
.toolbar {
  grid-area: tools;
  display: flex; gap: 4px;
  padding-left: 16px;
  border-left: 1px solid var(--line);
}
.tool {
  appearance: none;
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  min-width: 52px; height: 52px;
  padding: 4px 6px;
  font: inherit; font-size: 11px;
  color: var(--muted);
  background: none;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.tool svg, .fab svg {
  width: 22px; height: 22px;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.tool:hover { color: var(--fg); background: var(--btn); }
.tool.on { color: var(--accent); background: var(--accent-weak); }
.tool:focus-visible, .fab:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Stage ---------- */
main {
  position: relative;
  flex: 1;
  min-height: 0;
}
#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: manipulation; }

.overlay {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px 16px;
  background: var(--overlay);
  backdrop-filter: blur(3px);
  z-index: 2;
}
.overlay h1 { font-family: var(--font-digit); font-weight: 600; letter-spacing: .05em; margin: 0 0 12px; }
.overlay p { margin: 6px 0; line-height: 1.6; }
.overlay .sub { color: var(--muted); font-size: 14px; }
.overlay .error { color: var(--error); font-size: 14px; max-width: 420px; }
.overlay[hidden] { display: none; }

button.primary {
  appearance: none;
  margin-top: 18px;
  font: inherit; font-size: 18px; padding: 14px 28px;
  background: linear-gradient(135deg, #b03aa0, #5a3ad0);
  border: none; border-radius: 12px; color: #fff;
  cursor: pointer;
}
button.ghost {
  appearance: none; background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 18px; padding: 4px 8px; border-radius: 8px;
}
button.ghost:hover { color: var(--fg); background: var(--btn); }

.tap-hint {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  z-index: 2;
  padding: 12px 20px;
  font-size: 15px;
  color: var(--fg);
  background: var(--panel);
  border: 1px solid var(--btn-border);
  border-radius: 12px;
  box-shadow: 0 6px 24px var(--shadow);
  pointer-events: none;
}
.tap-hint[hidden] { display: none; }

/* ---------- Settings button (bottom right) ---------- */
.fab {
  position: absolute;
  right: 20px; bottom: 20px;
  z-index: 3;
  display: grid; place-items: center;
  width: 52px; height: 52px;
  color: var(--fg);
  background: var(--panel);
  border: 1.5px solid var(--staff-line);   /* 음계 선과 같은 색 */
  border-radius: 50%;
  box-shadow: 0 4px 16px var(--shadow);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.fab:hover { background: var(--btn); }
.fab[aria-expanded="true"] { color: var(--accent); border-color: var(--accent); }

/* ---------- Settings panel ---------- */
.panel {
  position: fixed;
  right: calc(20px + env(safe-area-inset-right));
  bottom: calc(84px + env(safe-area-inset-bottom));
  width: min(340px, calc(100vw - 32px));
  max-height: calc(100dvh - 200px); overflow: auto;
  background: var(--panel);
  border: 1px solid var(--btn-border); border-radius: 16px;
  padding: 14px 18px 18px;
  box-shadow: 0 12px 40px var(--shadow);
  z-index: 10;
}
.panel[hidden] { display: none; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.panel h2 { font-size: 16px; margin: 0; }

.field { display: block; padding: 12px 0; border-top: 1px solid var(--line); }
.panel-head + .field { border-top: none; }
.field-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.field-name { font-size: 13px; color: var(--label); }
.field-val { display: inline-flex; align-items: baseline; gap: 4px; }
.field-val output {
  font-family: var(--font-digit); font-size: 18px; font-weight: 600;
  color: var(--strong); font-variant-numeric: tabular-nums;
}
.field-val small { font-size: 12px; color: var(--muted); }
.field input[type=range] { display: block; width: 100%; margin: 10px 0 0; accent-color: var(--accent); }
.field select {
  display: block; width: 100%; margin-top: 8px;
  background: var(--btn); color: var(--fg);
  border: 1px solid var(--btn-border); border-radius: 8px; padding: 8px; font: inherit;
}
.field.check { display: flex; align-items: center; gap: 8px; }
.field.check input { accent-color: var(--accent); width: 18px; height: 18px; margin: 0; }
.panel .hint { font-size: 12px; color: var(--muted); line-height: 1.6; margin: 4px 0 0; padding-top: 12px; border-top: 1px solid var(--line); }

/* ---------- Medium screens ---------- */
@media (max-width: 1280px) {
  .hud {
    grid-template-columns: auto 1fr auto auto;
    grid-template-areas:
      "note meter mode tools"
      "stats stats stats stats";
  }
  .hud-stats { justify-content: stretch; }
  .stat { flex: 1 1 0; }
}

/* ---------- Phones ---------- */
@media (max-width: 640px) {
  .hud {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "mode tools"
      "note meter"
      "stats stats";
    gap: 10px 12px;
    padding: 10px 16px 12px;
  }
  .hud-note { width: auto; min-width: 0; }
  .note { font-size: 34px; }
  .cents { font-size: 13px; }
  .modes { justify-self: start; }
  .modes button { padding: 6px 12px; }
  .hud-meter { width: 168px; }
  .toolbar { padding-left: 0; border-left: none; gap: 2px; }
  .tool { min-width: 40px; height: 40px; padding: 0; }
  .tool-label { display: none; }
  .stat { padding: 6px 10px; }
  .stat .v { font-size: 14px; }
  .fab { right: 16px; bottom: 16px; width: 48px; height: 48px; }
  .panel {
    left: 12px; right: 12px; bottom: calc(76px + env(safe-area-inset-bottom));
    width: auto;
    max-height: calc(100dvh - 140px);
  }
}

/* ---------- Short screens (phone landscape) ---------- */
@media (max-height: 520px) and (min-width: 641px) {
  .hud {
    grid-template-columns: auto minmax(60px, 1fr) auto auto auto;
    grid-template-areas: "note meter stats mode tools";
    gap: 8px 10px;
    padding: 6px 16px;
  }
  .hud-note { width: 120px; }
  .note { font-size: 30px; }
  .cents { font-size: 13px; }
  .hud-stats { gap: 6px; }
  .stat { flex: 0 0 98px; padding: 4px 8px; }
  .modes button { font-size: 12px; padding: 5px 9px; }
  .stat .v { font-size: 13px; }
  .toolbar { padding-left: 0; border-left: none; }
  .tool { min-width: 40px; height: 40px; padding: 0; }
  .tool-label { display: none; }
  .fab { right: 14px; bottom: 14px; width: 44px; height: 44px; }
  .panel { bottom: 68px; max-height: calc(100dvh - 84px); }
}

/* ---------- Narrow + short screens (작은 폰 가로: 667×375 등) ---------- */
@media (max-height: 520px) and (min-width: 641px) and (max-width: 900px) {
  .hud {
    grid-template-columns: auto minmax(60px, 1fr) auto auto;
    grid-template-areas:
      "note meter mode tools"
      "stats stats stats stats";
    gap: 6px 10px;
  }
  .hud-stats { justify-content: stretch; }
  .stat { flex: 1 1 0; flex-direction: row; align-items: baseline; gap: 8px; padding: 3px 10px; }
}
