/*
  Layout, smallest first:
    phone    one column that fills the screen; tunings open in a bottom sheet
    tablet   the same column, centred, with more breathing room (>= 640px)
    laptop   a web page: header, tunings in a side panel, tuner in a wide card (>= 960px)
*/

:root {
  color-scheme: light dark;
  --bg: #f5f5f2;
  --surface: #ffffff;
  --surface-2: #ebebe7;
  --text: #121214;
  --muted: #6d6d73;
  --line: #d9d9d4;
  --good: #0a9d68;
  --off: #d97706;
  --good-glow: rgba(10, 157, 104, 0.12);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0c0e;
    --surface: #17171a;
    --surface-2: #222226;
    --text: #f4f4f5;
    --muted: #8c8c95;
    --line: #2d2d33;
    --good: #34d399;
    --off: #fbbf24;
    --good-glow: rgba(52, 211, 153, 0.1);
    --shadow: none;
    --card-shadow: none;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
p {
  margin: 0;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

button:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

/* ---------- Page frame ---------- */

.page {
  min-height: 100dvh;
  max-width: 600px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 10px) calc(env(safe-area-inset-right) + 20px)
    calc(env(safe-area-inset-bottom) + 16px) calc(env(safe-area-inset-left) + 20px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 32px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.brand-mark {
  width: 24px;
  height: 24px;
  flex: none;
}

.tagline {
  display: none;
  font-size: 14px;
  color: var(--muted);
}

.site-footer {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  font-size: 13px;
  color: var(--muted);
}

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 2px 6px;
  margin-left: 14px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
  font: 600 11px/1.3 ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color: var(--text);
  text-align: center;
}

.shortcuts kbd:first-child,
.shortcuts kbd + kbd {
  margin-left: 0;
}

/* ---------- App ---------- */

.app {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.settings {
  display: grid;
  gap: 6px;
}

.tuning-panel {
  display: none;
}

.tuner {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Soft green glow behind the note when a string is in tune. */
.tuner::before {
  content: '';
  position: absolute;
  inset: -40px -20px 30%;
  background: radial-gradient(closest-side, var(--good-glow), transparent);
  opacity: 0;
  transition: opacity 0.35s ease;
  pointer-events: none;
}

.app[data-state='in'] .tuner::before {
  opacity: 1;
}

.tuner > * {
  position: relative;
}

/* ---------- Instrument and tuning ---------- */

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 12px;
}

.segmented button {
  height: 36px;
  border-radius: 9px;
  font-size: 15px;
  font-weight: 500;
  color: var(--muted);
  transition: background 0.18s, color 0.18s;
}

.segmented button[aria-selected='true'] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow), 0 1px 3px rgba(0, 0, 0, 0.12);
}

.tuning-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 0 12px;
  border-radius: 10px;
  font-size: 15px;
}

.tuning-button:active {
  background: var(--surface-2);
}

.tuning-name {
  font-weight: 600;
}

.tuning-notes {
  color: var(--muted);
  letter-spacing: 0.04em;
}

.chevron {
  width: 10px;
  height: 7px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tuning-option {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 48px;
  padding: 8px 10px;
  border-radius: 10px;
  text-align: left;
}

.tuning-option:active {
  background: var(--surface-2);
}

.tuning-option .name {
  flex: 1;
  font-size: 16px;
  font-weight: 500;
}

.tuning-option .notes {
  font-size: 14px;
  color: var(--muted);
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.tuning-option .check {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: var(--good);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  visibility: hidden;
}

.tuning-option[aria-checked='true'] .check {
  visibility: visible;
}

/* ---------- Note, status and meter ---------- */

.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 280px;
  text-align: center;
}

.note {
  display: flex;
  align-items: stretch;
  line-height: 1;
  font-size: clamp(88px, min(30vw, 22dvh), 176px);
  font-weight: 600;
  letter-spacing: -0.04em;
  transition: color 0.2s, opacity 0.2s;
}

.note-marks {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 0.1em 0 0.12em 0.04em;
  min-width: 0.3em;
  font-size: 0.3em;
  font-weight: 500;
  letter-spacing: 0;
  color: var(--muted);
}

.app[data-state='idle'] .note {
  opacity: 0.28;
}

.note.empty {
  font-weight: 200;
}

.note.empty .note-marks {
  display: none;
}

.app[data-state='in'] .note,
.app[data-state='in'] .note-marks {
  color: var(--good);
}

.status {
  margin-top: 6px;
  min-height: 24px;
  font-size: 17px;
  font-weight: 500;
  color: var(--muted);
  transition: color 0.2s;
}

.app[data-state='in'] .status {
  color: var(--good);
}

.meter {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 620px;
  margin-top: clamp(16px, 4dvh, 36px);
}

.meter-label {
  width: 12px;
  text-align: center;
  font-size: 17px;
  color: var(--muted);
}

.meter-track {
  position: relative;
  flex: 1;
  height: 56px;
}

.meter-ticks {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.meter-ticks span {
  width: 2px;
  height: 14px;
  border-radius: 1px;
  background: var(--line);
}

.meter-ticks span.mid {
  height: 24px;
}

.meter-ticks span.center {
  height: 56px;
  background: var(--muted);
}

.app[data-state='in'] .meter-ticks span.center {
  background: var(--good);
}

.meter-zone {
  position: absolute;
  top: 8px;
  bottom: 8px;
  left: 45%;
  width: 10%;
  border-radius: 8px;
  background: var(--good-glow);
}

.meter-needle {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 50%;
  width: 6px;
  margin-left: -3px;
  border-radius: 3px;
  background: var(--off);
  transition: left 0.11s linear, background 0.15s, opacity 0.25s;
}

.app[data-state='idle'] .meter-needle {
  opacity: 0;
}

.app[data-state='in'] .meter-needle {
  background: var(--good);
}

.readout {
  margin-top: 10px;
  height: 20px;
  font-size: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  transition: opacity 0.25s;
}

.readout-hz::before {
  content: '·';
  margin: 0 6px;
}

.readout-hz:empty::before {
  content: none;
}

.app[data-state='idle'] .readout {
  opacity: 0;
}

/* ---------- Strings ---------- */

.strings-panel {
  display: grid;
  gap: 12px;
}

.strings-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.strings-hint {
  flex: 1;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip {
  height: 30px;
  padding: 0 12px;
  border-radius: 15px;
  border: 1px solid var(--line);
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.chip[aria-pressed='true'] {
  background: var(--text);
  border-color: var(--text);
  color: var(--bg);
}

.icon-chip {
  width: 30px;
  padding: 0;
  display: grid;
  place-items: center;
}

.icon-chip svg {
  width: 18px;
  height: 18px;
}

.icon-chip .fill {
  fill: currentColor;
}

.icon-chip .wave,
.icon-chip .slash {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
}

.icon-chip[aria-pressed='true'] .slash,
.icon-chip[aria-pressed='false'] .wave {
  display: none;
}

.strings {
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.string {
  flex: 1;
  max-width: 76px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.string-button {
  position: relative;
  width: 100%;
  max-width: 56px;
  aspect-ratio: 1;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 18px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
}

.string-button:active {
  transform: scale(0.94);
}

.string-button sup {
  font-size: 0.6em;
  font-weight: 500;
  margin-left: 1px;
}

.string.active .string-button {
  border-color: var(--text);
}

.string.locked .string-button {
  background: var(--text);
  border-color: var(--text);
  color: var(--bg);
}

.string.tuned .string-button {
  border-color: var(--good);
}

.string.tuned:not(.locked) .string-button {
  color: var(--good);
}

.string.tuned .string-button::after {
  content: '';
  position: absolute;
  top: -3px;
  right: -3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--good)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4.5 8.3l2.3 2.3 4.7-5' fill='none' stroke='white' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E")
    center / contain;
  box-shadow: 0 0 0 2px var(--bg);
}

.string-number {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.string-step {
  display: none;
  width: 100%;
  max-width: 56px;
  height: 28px;
  border-radius: 8px;
  background: var(--surface-2);
  font-size: 17px;
  font-weight: 500;
  line-height: 1;
}

.string-step:active {
  background: var(--line);
}

.strings.editing .string-step {
  display: block;
}

.strings.editing .string-number {
  display: none;
}

/* ---------- Start button ---------- */

.bottom {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 54px;
  border-radius: 16px;
  background: var(--text);
  color: var(--bg);
  font-size: 17px;
  font-weight: 600;
  transition: background 0.2s, color 0.2s, transform 0.1s, opacity 0.15s;
}

.primary:active {
  transform: scale(0.985);
}

.primary .dot {
  display: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--good);
  animation: pulse 1.4s ease-in-out infinite;
}

.primary.listening {
  background: var(--surface-2);
  color: var(--text);
}

.primary.listening .dot {
  display: block;
}

@keyframes pulse {
  50% {
    opacity: 0.3;
  }
}

.privacy,
.error {
  text-align: center;
  font-size: 12px;
  line-height: 1.4;
  color: var(--muted);
}

.error {
  font-size: 13px;
  color: var(--off);
}

/* ---------- Tuning sheet (phones and tablets) ---------- */

.sheet {
  width: min(100%, 560px);
  max-width: 100%;
  max-height: 86dvh;
  position: fixed;
  inset: auto 0 0 0;
  margin: 0 auto;
  padding: 0;
  border: 0;
  border-radius: 22px 22px 0 0;
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}

.sheet[open] {
  display: flex;
  animation: rise 0.26s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.sheet::backdrop {
  background: rgba(0, 0, 0, 0.45);
  animation: fade 0.2s ease;
}

@keyframes rise {
  from {
    transform: translateY(40px);
    opacity: 0;
  }
}

@keyframes fade {
  from {
    opacity: 0;
  }
}

.sheet-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 8px 20px calc(env(safe-area-inset-bottom) + 16px);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0 6px;
}

.sheet-head h2 {
  font-size: 20px;
  font-weight: 700;
}

.sheet-close {
  height: 36px;
  padding: 0 4px;
  font-size: 16px;
  font-weight: 600;
}

.sheet .tuning-list {
  overflow-y: auto;
  min-height: 0;
  margin: 0 -10px;
  -webkit-overflow-scrolling: touch;
}

/* ---------- Mouse and trackpad ---------- */

@media (hover: hover) {
  .segmented button:hover:not([aria-selected='true']) {
    color: var(--text);
  }

  .tuning-button:hover,
  .tuning-option:hover,
  .sheet-close:hover {
    background: var(--surface-2);
  }

  .chip:hover {
    border-color: var(--muted);
    color: var(--text);
  }

  .chip[aria-pressed='true']:hover {
    color: var(--bg);
    opacity: 0.85;
  }

  .string-button:hover {
    border-color: var(--muted);
  }

  .string.locked .string-button:hover,
  .string.active .string-button:hover {
    border-color: var(--text);
  }

  .string.tuned .string-button:hover {
    border-color: var(--good);
  }

  .string-step:hover {
    background: var(--line);
  }

  .primary:hover {
    opacity: 0.88;
  }
}

/* ---------- Tablet ---------- */

@media (min-width: 640px) {
  .page {
    padding-top: calc(env(safe-area-inset-top) + 20px);
    padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
    gap: 20px;
  }

  .app {
    gap: 16px;
  }

  .tuner {
    gap: 24px;
  }

  .sheet {
    inset: 0;
    margin: auto;
    height: fit-content;
    border-radius: 22px;
  }
}

/* ---------- Laptop and desktop ---------- */

@media (min-width: 960px) {
  .page {
    max-width: 1180px;
    padding: 0 32px 24px;
    gap: 0;
  }

  .site-header {
    min-height: 72px;
  }

  .brand {
    font-size: 17px;
    gap: 11px;
  }

  .brand-mark {
    width: 30px;
    height: 30px;
  }

  .tagline {
    display: block;
  }

  .site-footer {
    display: flex;
    padding-top: 20px;
  }

  .app {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    align-items: stretch;
    gap: 20px;
  }

  .settings,
  .tuner {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 24px;
    box-shadow: var(--card-shadow);
  }

  /* Side panel: instrument and every tuning */
  .settings {
    align-content: start;
    gap: 18px;
    padding: 16px;
  }

  .tuning-button {
    display: none;
  }

  .tuning-panel {
    display: block;
  }

  .panel-title {
    padding: 0 10px 6px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
  }

  .tuning-option {
    min-height: 40px;
    padding: 6px 10px;
  }

  .tuning-option .name {
    font-size: 15px;
  }

  .tuning-option .notes {
    font-size: 13px;
  }

  .tuning-option[aria-checked='true'] {
    background: var(--surface-2);
  }

  /* Tuner card */
  .tuner {
    min-height: 620px;
    padding: 32px 48px 28px;
    gap: 28px;
    overflow: hidden;
  }

  .tuner::before {
    inset: 0 0 35%;
  }

  .stage {
    min-height: 320px;
  }

  .note {
    font-size: clamp(120px, 20dvh, 184px);
  }

  .status {
    font-size: 19px;
  }

  .strings-panel {
    width: 100%;
    max-width: 560px;
    margin: 0 auto;
  }

  .strings {
    justify-content: center;
    gap: 16px;
  }

  .string {
    flex: 0 0 60px;
  }

  .string-button {
    max-width: 60px;
    background: var(--bg);
  }

  .string.locked .string-button {
    background: var(--text);
  }

  .string.tuned .string-button::after {
    box-shadow: 0 0 0 2px var(--surface);
  }

  .string-step {
    max-width: 60px;
  }

  .bottom {
    align-items: center;
  }

  .primary {
    min-width: 280px;
    height: 50px;
    padding: 0 32px;
    border-radius: 14px;
    font-size: 16px;
  }
}

/* Short windows (landscape phones, squat browser windows): tighten, and let the page scroll. */
@media (max-height: 640px) {
  .stage {
    min-height: 220px;
  }

  .meter-track {
    height: 44px;
  }

  .meter-ticks span.center {
    height: 44px;
  }

  .primary {
    height: 48px;
  }
}

@media (min-width: 960px) and (max-height: 760px) {
  .site-header {
    min-height: 56px;
  }

  .tuner {
    min-height: 520px;
    padding-top: 20px;
    padding-bottom: 20px;
    gap: 18px;
  }

  .settings {
    gap: 12px;
  }

  .tuning-option {
    min-height: 34px;
    padding: 4px 10px;
  }

  .site-footer {
    padding-top: 12px;
  }
}

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