:root {
  --bg: #f3f5f8;
  --ink: #141922;
  --ink-2: #5d6675;
  --ink-3: #9aa3b0;
  --line: #dde2e9;
  --surface: rgba(255, 255, 255, 0.76);
  --wire: #1b212c;
  --wire-2: rgba(27, 33, 44, 0.36);
  --fill: rgba(255, 255, 255, 0.62);
  --accent: #2563eb;
  --good: #0e9f6a;
  --good-bg: #eaf8f2;
  --warn: #ef5a24;
  --warn-bg: #fff2ec;
  --panel-w: 100%;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(120% 70% at 50% 0%, #ffffff 0%, var(--bg) 65%);
  background-color: var(--bg);
  color: var(--ink);
  font: 15px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

button { font: inherit; color: inherit; touch-action: manipulation; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

svg { display: block; }
button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
button svg .solid { fill: currentColor; stroke: none; }

/* ---------- Stage ---------- */

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#stage path, #stage rect, #stage circle, #stage ellipse, #stage line, #stage polyline {
  vector-effect: non-scaling-stroke;
}

.w { fill: var(--fill); stroke: var(--wire); stroke-width: 1.25; stroke-linejoin: round; }
.d { fill: none; stroke: var(--wire-2); stroke-width: 1; }
.df { fill: rgba(27, 33, 44, 0.035); stroke: var(--wire-2); stroke-width: 1; }
.w.hole { fill: rgba(27, 33, 44, 0.07); }
.w.slot { fill: rgba(27, 33, 44, 0.08); }
.w.board { fill: rgba(255, 255, 255, 0.78); }
.w.nut, .w.key { fill: #fff; }
.mosaic { stroke-width: 3; stroke-dasharray: 1 2; }
.roller { fill: rgba(255, 255, 255, 0.9); stroke: var(--wire); }

.str { fill: none; stroke: #3a4250; stroke-linecap: round; stroke-linejoin: round; }
.str.ring { animation: ring 1.8s ease-out; }
.str.target { stroke: var(--warn); }
.str.target.good { stroke: var(--good); }
@keyframes ring { 0%, 15% { stroke: var(--accent); } 100% { stroke: #3a4250; } }

.guitar { transition: none; }
body[data-mode="full"] .guitar { cursor: pointer; }
body[data-mode="full"] .guitar.active:hover .head-shape { fill: rgba(37, 99, 235, 0.07); stroke: var(--accent); }

.peg .hit { fill: transparent; }
.lab { opacity: var(--z, 0); }
#stage.z0 .lab { visibility: hidden; }
body[data-mode="head"] .peg { cursor: pointer; }
.lbl { fill: #fff; stroke: var(--wire); stroke-width: 1.25; transition: fill .2s, stroke .2s; }
.lbl-t { fill: var(--ink); font-weight: 600; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.lbl-t .oct { font-size: 0.58em; fill: var(--ink-2); }
.cue { opacity: 0; fill: var(--warn); stroke: none; }

@media (hover: hover) {
  body[data-mode="head"] .peg:hover .lbl { stroke: var(--accent); }
  body[data-mode="head"] .peg:hover .key { stroke: var(--accent); }
}
.peg.play .lbl { animation: pegflash 0.7s ease-out; }
@keyframes pegflash { 0% { fill: #e7eefe; stroke: var(--accent); } 100% { fill: #fff; } }

.peg.done .lbl { fill: var(--good-bg); stroke: var(--good); }
.peg.target .lbl { stroke-width: 2; stroke: var(--ink); }
.peg.flat .lbl, .peg.sharp .lbl { stroke: var(--warn); fill: var(--warn-bg); }
.peg.flat .cue, .peg.sharp .cue { opacity: 1; }
.peg.good .lbl { stroke: var(--good); fill: var(--good-bg); }
.peg.good .cue { opacity: 1; fill: none; stroke: var(--good); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Overlays ---------- */

.overlay { position: fixed; z-index: 1; pointer-events: none; transition: opacity .3s, visibility 0s; }
.overlay > * { pointer-events: auto; }
body[data-mode="head"] #full-ui,
body[data-mode="full"] #head-ui { opacity: 0; visibility: hidden; transition: opacity .2s, visibility 0s .2s; }

#full-ui {
  left: 0;
  right: 0;
  bottom: calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.hint { margin: 0; color: var(--ink-2); font-size: 14px; }

.switcher { display: flex; align-items: center; gap: 4px; }
.arrow {
  width: 44px; height: 44px; border: 0; border-radius: 50%;
  background: transparent; display: grid; place-items: center; cursor: pointer;
}
.arrow:hover { background: rgba(20, 25, 34, 0.06); }
.arrow:disabled { opacity: 0.2; cursor: default; background: transparent; }
.who { display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 112px; }
.g-name { font-weight: 600; font-size: 16px; letter-spacing: -0.005em; }
.dots { display: flex; }
.dots button {
  width: 18px; height: 14px; padding: 0; border: 0; background: transparent;
  display: grid; place-items: center; cursor: pointer;
}
.dots button::after {
  content: ""; width: 6px; height: 6px; border-radius: 3px;
  background: var(--ink-3); transition: width .25s, background .25s;
}
.dots button[aria-current="true"]::after { width: 16px; background: var(--ink); }

#head-ui {
  top: calc(8px + env(safe-area-inset-top));
  left: 10px;
  right: 10px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
}
#head-ui .who { min-width: 96px; }
#back { position: absolute; left: 0; top: 4px; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  display: grid; place-items: center; cursor: pointer; flex: none;
}
.icon-btn:hover { border-color: var(--ink-3); }
.icon-btn.small { width: 34px; height: 34px; }
.icon-btn.small svg { width: 18px; height: 18px; }

/* ---------- Panel ---------- */

#panel {
  position: fixed;
  z-index: 2;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  max-width: 560px;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  background: var(--surface);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
  backdrop-filter: blur(18px) saturate(1.2);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 22px 22px 0 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: transform .5s cubic-bezier(.2, .8, .2, 1), opacity .3s, visibility 0s;
}
body[data-mode="full"] #panel {
  transform: translateY(calc(100% + 24px));
  opacity: 0;
  visibility: hidden;
  transition: transform .4s ease-in, opacity .3s, visibility 0s .4s;
}

/* Meter */
.meter {
  position: relative;
  flex: none;
  height: 124px;
  border-radius: 16px;
  background: #fff;
  border: 1px solid var(--line);
  overflow: hidden;
  transition: background .25s, border-color .25s;
}
.meter-off, .meter-on {
  position: absolute;
  inset: 0;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  transition: opacity .2s;
}
.meter-off { align-items: center; justify-content: center; gap: 10px; }
.meter[data-state="off"] .meter-on,
.meter[data-state="error"] .meter-on,
.meter:not([data-state="off"]):not([data-state="error"]) .meter-off { opacity: 0; visibility: hidden; }

.mic-btn {
  display: inline-flex; align-items: center; gap: 10px;
  height: 50px; padding: 0 24px 0 20px; border: 0; border-radius: 25px;
  background: var(--ink); color: #fff; font-weight: 600; font-size: 15px; cursor: pointer;
  transition: transform .15s;
}
.mic-btn:active { transform: scale(0.97); }
.sub { margin: 0; font-size: 13px; color: var(--ink-2); text-align: center; }
.meter[data-state="error"] .sub { color: var(--warn); }

.readout { display: flex; align-items: center; gap: 12px; flex: 1; min-height: 0; }
.note { font-size: 42px; font-weight: 650; letter-spacing: -0.02em; line-height: 1; min-width: 70px; font-variant-numeric: tabular-nums; }
.note sub { font-size: 0.4em; font-weight: 600; color: var(--ink-2); vertical-align: baseline; margin-left: 2px; }
.status { display: flex; flex-direction: column; gap: 3px; flex: 1; min-width: 0; }
#m-status { display: flex; align-items: center; gap: 6px; font-size: 18px; font-weight: 600; color: var(--ink-2); white-space: nowrap; }
#m-status svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; flex: none; }
#m-cents { font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; min-height: 1.3em; }
.pill {
  height: 30px; padding: 0 12px; border-radius: 15px;
  border: 1px solid var(--line); background: #fff; font-size: 13px; font-weight: 600; cursor: pointer;
}
.pill[hidden] { display: none; }

.scale { display: flex; align-items: center; gap: 10px; height: 34px; flex: none; }
.scale .end { font-size: 15px; color: var(--ink-3); width: 10px; text-align: center; }
.bar {
  position: relative; flex: 1; height: 100%;
  background:
    linear-gradient(90deg, var(--line) 1px, transparent 1px) 0 50% / 10% 12px repeat-x,
    linear-gradient(var(--line), var(--line)) 0 50% / 100% 1px no-repeat;
  border-right: 1px solid transparent;
}
.bar .zone { position: absolute; top: 4px; bottom: 4px; left: 47%; width: 6%; border-radius: 4px; background: var(--good-bg); }
.bar .mid { position: absolute; top: 2px; bottom: 2px; left: 50%; width: 2px; margin-left: -1px; border-radius: 1px; background: var(--ink-3); }
.bar .needle {
  position: absolute; top: 0; bottom: 0; left: 50%; width: 4px; margin-left: -2px;
  border-radius: 2px; background: var(--ink-3); will-change: transform; opacity: .35;
}

.meter[data-state="flat"], .meter[data-state="sharp"] { border-color: rgba(239, 90, 36, 0.35); }
.meter[data-state="flat"] #m-status, .meter[data-state="sharp"] #m-status { color: var(--warn); }
.meter[data-state="flat"] .needle, .meter[data-state="sharp"] .needle { background: var(--warn); opacity: 1; }
.meter[data-state="good"] { background: var(--good-bg); border-color: rgba(14, 159, 106, 0.35); }
.meter[data-state="good"] #m-status { color: var(--good); }
.meter[data-state="good"] .needle { background: var(--good); opacity: 1; }
.meter[data-state="good"] .zone { background: rgba(14, 159, 106, 0.16); }

/* Tunings */
.tunings { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.chip {
  min-height: 52px; padding: 7px 6px; border: 1px solid var(--line); border-radius: 12px;
  background: #fff; cursor: pointer; text-align: center;
  display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 2px;
  transition: background .15s, border-color .15s, color .15s;
}
.chip:hover { border-color: var(--ink-3); }
.chip b { font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.chip small { font-size: 11px; color: var(--ink-2); white-space: nowrap; letter-spacing: 0.01em; }
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: #fff; }
.chip[aria-pressed="true"] small { color: rgba(255, 255, 255, 0.7); }
.chip.more:not([aria-pressed="true"]) { border-style: dashed; }

/* More sheet */
.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  max-height: calc(100vh - 64px);
  max-height: calc(100dvh - 64px);
  overflow: auto;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  background: #fff;
  border-radius: inherit;
  box-shadow: 0 -10px 40px rgba(20, 25, 34, 0.1);
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: sheet-in .28s cubic-bezier(.2, .8, .2, 1);
}
.sheet[hidden] { display: none; }
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; font-weight: 600; padding-left: 2px; }
.sheet .chip { min-height: 48px; }
.ref { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 14px; color: var(--ink-2); padding: 2px 2px 0; }
.stepper { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.stepper button { width: 42px; height: 38px; border: 0; background: #fff; font-size: 18px; cursor: pointer; }
.stepper button:hover { background: var(--bg); }
.stepper output { min-width: 70px; text-align: center; font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- Side layout (landscape, roomy) ---------- */

@media (min-width: 760px) and (min-aspect-ratio: 1/1) {
  :root { --panel-w: clamp(330px, 30vw, 400px); }
  #panel {
    left: auto;
    top: 12px;
    right: 12px;
    bottom: 12px;
    width: var(--panel-w);
    height: fit-content;
    max-height: calc(100% - 24px);
    max-width: none;
    margin: auto 0;
    padding: 18px;
    border-radius: 22px;
    border-bottom: 1px solid var(--line);
    gap: 14px;
  }
  body[data-mode="full"] #panel { transform: translateX(calc(100% + 24px)); }
  #head-ui { right: calc(var(--panel-w) + 24px); }
  /* Centred on the panel, free to grow beyond it. */
  .sheet {
    top: -100vh;
    bottom: -100vh;
    height: fit-content;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
    margin: auto 0;
    padding-bottom: 18px;
    border: 1px solid var(--line);
    box-shadow: 0 12px 40px rgba(20, 25, 34, 0.12);
  }
}

/* ---------- Compact heights ---------- */

@media (max-height: 700px) {
  .meter { height: 112px; }
  .note { font-size: 36px; }
  .chip { min-height: 46px; padding: 5px 4px; }
  #panel { gap: 10px; padding-top: 12px; }
}

@media (max-height: 480px) {
  .meter { height: 100px; }
  .meter-off, .meter-on { padding: 8px 12px; }
  .mic-btn { height: 42px; }
  .note { font-size: 30px; min-width: 56px; }
  .chip { min-height: 40px; }
  .chip small { display: none; }
  #panel { padding: 10px; gap: 8px; }
  .hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
