:root {
  color-scheme: light;
  --paper: #e9e5da;
  --panel: #d7d2c5;
  --panel-dark: #c7c1b4;
  --ink: #1d1d1a;
  --muted: #68665f;
  --line: #969187;
  --red: #d94a2a;
  --green: #3f7d58;
  font-family: "Helvetica Neue", Helvetica, Arial, ui-sans-serif, sans-serif;
  -webkit-font-smoothing: antialiased;
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--paper); }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  color: var(--ink);
  background: var(--paper);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; touch-action: manipulation; }

.app-shell {
  width: min(100%, 500px);
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  padding: calc(15px + env(safe-area-inset-top)) 16px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.app-header {
  min-height: 38px;
  display: grid;
  grid-template-columns: 1fr auto 42px;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--ink);
  padding-bottom: 9px;
}

.wordmark {
  color: var(--ink);
  text-decoration: none;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 0.09em;
}

.model-mark {
  margin: 0;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.16em;
}

.model-mark span { color: var(--red); margin-left: 4px; }

.icon-button, .dialog-close {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid var(--ink);
  border-radius: 50%;
  color: var(--ink);
  background: transparent;
}

.icon-button svg { width: 18px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: square; stroke-linejoin: miter; }

.tuner-panel {
  border: 1px solid var(--ink);
  background: var(--panel);
  padding: 15px;
}

.status-row, .piano-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }

.eyebrow {
  margin: 0;
  color: var(--ink);
  font-size: 10px;
  font-weight: 750;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.status {
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; border: 1px solid var(--ink); background: transparent; }
.is-listening .status-dot { border-color: var(--green); background: var(--green); animation: pulse 1.8s infinite; }

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

.meter {
  position: relative;
  height: 206px;
  overflow: hidden;
}

.scale-labels {
  position: absolute;
  top: 9px;
  left: 50%;
  width: min(86%, 320px);
  transform: translateX(-50%);
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 8px;
  font-variant-numeric: tabular-nums;
}

.ticks {
  position: absolute;
  left: 50%;
  top: 31px;
  width: 236px;
  height: 236px;
  transform: translateX(-50%);
}

.ticks span {
  position: absolute;
  left: calc(50% - 1px);
  top: 0;
  width: 1px;
  height: 14px;
  background: var(--ink);
  transform-origin: 1px 118px;
  transform: rotate(calc(var(--i) * 11deg));
}

.ticks .tick-center { width: 2px; height: 20px; background: var(--red); }

.needle {
  --needle-angle: 0deg;
  position: absolute;
  left: calc(50% - 1px);
  top: 31px;
  width: 2px;
  height: 84px;
  background: var(--red);
  transform-origin: 50% 118px;
  transform: rotate(var(--needle-angle));
  opacity: 0;
  transition: transform 100ms linear, opacity 120ms ease;
}

.has-pitch .needle { opacity: 1; }
.in-tune .needle { background: var(--green); }

.note-readout {
  position: absolute;
  inset: 69px 0 auto;
  text-align: center;
}

.note-line {
  height: 88px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  font-size: clamp(72px, 24vw, 98px);
  font-weight: 500;
  letter-spacing: -0.07em;
  line-height: 0.92;
  font-variant-numeric: tabular-nums;
}

.note-line sup { margin: 5px 0 0 8px; font-size: 18px; font-weight: 600; letter-spacing: 0; color: var(--muted); }

.frequency { margin: 8px 0 0; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.frequency span + span { margin-left: 4px; }

.readout-strip {
  display: grid;
  grid-template-columns: 1fr 80px;
  align-items: center;
  gap: 16px;
  min-height: 29px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}

.deviation { margin: 0; font-size: 12px; font-weight: 650; }

.level { height: 5px; overflow: hidden; border: 1px solid var(--ink); background: transparent; }
.level span { display: block; width: 0; height: 100%; background: var(--green); transition: width 80ms linear; }

.primary-button {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--ink);
  border-radius: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--paper);
  background: var(--ink);
  font-weight: 700;
  cursor: pointer;
}

.primary-button:active { color: var(--ink); background: var(--red); }
.primary-button:disabled { opacity: 0.5; }
.is-listening .primary-button { color: var(--ink); background: transparent; }

.button-icon { width: 20px; height: 20px; display: grid; place-items: center; }
.button-icon svg { width: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: square; stroke-linejoin: miter; }

.piano-section {
  margin-top: auto;
  border-top: 1px solid var(--ink);
  padding-top: 10px;
}

.piano-status { margin: 4px 0 0; color: var(--muted); font-size: 10px; }
.octave-label { margin: 0; color: var(--muted); font-size: 9px; font-weight: 700; letter-spacing: 0.12em; }

.piano {
  position: relative;
  height: 102px;
  margin-top: 9px;
  border: 1px solid var(--ink);
  background: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.white-keys { height: 100%; display: flex; gap: 1px; }

.piano-key {
  border: 0;
  border-radius: 0;
  padding: 0;
  cursor: pointer;
}

.white-key {
  position: relative;
  flex: 1;
  color: var(--muted);
  background: #f4f0e6;
}

.white-key span { position: absolute; left: 0; right: 0; bottom: 8px; font-size: 9px; font-weight: 650; }

.black-key {
  position: absolute;
  z-index: 2;
  top: 0;
  left: calc(var(--position) * 12.5% - 3.6%);
  width: 7.2%;
  height: 61%;
  border: 1px solid #000;
  background: var(--ink);
  box-shadow: 0 3px 0 rgba(0,0,0,0.25);
}

.white-key.is-active { background: var(--red); color: var(--ink); }
.black-key.is-active { background: var(--red); box-shadow: none; }

footer { padding: 0 2px; }
footer p { margin: 0; color: var(--muted); font-size: 9px; line-height: 1.35; text-align: center; }
footer a { color: inherit; text-underline-offset: 2px; }
.privacy-mark { display: inline-block; width: 6px; height: 6px; margin-right: 6px; border-radius: 50%; background: var(--green); }

dialog {
  width: min(calc(100% - 32px), 440px);
  padding: 0;
  border: 1px solid var(--ink);
  border-radius: 0;
  color: var(--ink);
  background: var(--paper);
  box-shadow: 18px 18px 0 rgba(29,29,26,0.22);
}

dialog::backdrop { background: rgba(29,29,26,0.62); }
.dialog-content { position: relative; padding: 28px; }
.dialog-close { position: absolute; top: 12px; right: 12px; border-radius: 0; font-size: 25px; font-weight: 300; }
.dialog-content h2 { max-width: 320px; margin: 16px 0 20px; font-size: 28px; font-weight: 500; line-height: 1.04; letter-spacing: -0.035em; }
.dialog-content ol { margin: 0; padding-left: 20px; }
.dialog-content li { margin: 12px 0; padding-left: 5px; line-height: 1.4; }
.dialog-note { margin: 20px 0 0; padding-top: 16px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; line-height: 1.45; }

:focus-visible { outline: 3px solid var(--red); outline-offset: 2px; }

@media (max-height: 740px) {
  .app-shell { gap: 8px; }
  .meter { height: 178px; }
  .ticks { top: 25px; width: 210px; height: 210px; }
  .ticks span { transform-origin: 1px 105px; }
  .needle { top: 25px; height: 76px; transform-origin: 50% 105px; }
  .note-readout { inset-block-start: 59px; }
  .note-line { height: 75px; font-size: 76px; }
  .piano { height: 86px; }
  footer { display: none; }
}

@media (max-width: 360px) {
  .app-shell { padding-left: 10px; padding-right: 10px; }
  .model-mark { display: none; }
  .app-header { grid-template-columns: 1fr 38px; }
  .tuner-panel { padding: 12px; }
}

@media (orientation: landscape) and (max-height: 600px) and (min-width: 500px) {
  html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  .app-shell {
    width: 100%;
    max-width: none;
    height: 100svh;
    min-height: 0;
    padding:
      calc(8px + env(safe-area-inset-top))
      calc(10px + env(safe-area-inset-right))
      calc(8px + env(safe-area-inset-bottom))
      calc(10px + env(safe-area-inset-left));
    display: grid;
    grid-template-columns: minmax(0, 0.77fr) minmax(0, 1.23fr);
    grid-template-rows: 34px minmax(0, 1fr);
    gap: 8px 10px;
  }

  .app-header {
    grid-column: 1 / -1;
    min-height: 34px;
    grid-template-columns: 1fr auto 34px;
    padding-bottom: 5px;
  }

  .wordmark { font-size: 16px; }
  .icon-button { width: 30px; height: 30px; }
  .icon-button svg { width: 15px; }

  .tuner-panel {
    min-width: 0;
    min-height: 0;
    padding: 10px;
    display: flex;
    flex-direction: column;
  }

  .meter {
    flex: 1;
    height: auto;
    min-height: 132px;
  }

  .scale-labels {
    top: 3px;
    width: min(78%, 290px);
  }

  .ticks {
    top: 19px;
    width: 180px;
    height: 180px;
  }

  .ticks span {
    height: 11px;
    transform-origin: 1px 90px;
  }

  .ticks .tick-center { height: 15px; }

  .needle {
    top: 19px;
    height: 64px;
    transform-origin: 50% 90px;
  }

  .note-readout { inset-block-start: 47px; }
  .note-line { height: 66px; font-size: 68px; }
  .note-line sup { margin-top: 2px; font-size: 15px; }
  .frequency { margin-top: 2px; }

  .readout-strip {
    min-height: 25px;
    padding: 5px 0;
  }

  .primary-button { min-height: 40px; }

  .piano-section {
    min-width: 0;
    min-height: 0;
    margin: 0;
    padding: 10px;
    border: 1px solid var(--ink);
    background: var(--panel);
    display: flex;
    flex-direction: column;
  }

  .piano {
    flex: 1;
    height: auto;
    min-height: 0;
    margin-top: 8px;
  }

  .white-key span { bottom: 10px; font-size: 11px; }
  footer { display: none; }
  dialog { max-height: calc(100svh - 20px); overflow-y: auto; }
}

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