/* ── CSS Custom Properties ──────────────────────────────────── */
:root {
  --bg:          #09090d;
  --surface:     #0f0f14;
  --surface-2:   #15151c;
  --surface-3:   #1d1d26;
  --border:      #24242f;
  --border-hi:   #32324a;
  --text:        #ede9e2;
  --text-muted:  #7b7888;
  --text-dim:    #48465a;
  --accent:      #c4975a;
  --accent-dim:  rgba(196, 151, 90, 0.14);
  --accent-glow: rgba(196, 151, 90, 0.22);
  --green:       #2dce72;
  --green-dim:   rgba(45, 206, 114, 0.10);
  --green-glow:  rgba(45, 206, 114, 0.22);
  --yellow:      #f0c040;
  --red:         #e85454;
  --radius:      14px;
  --radius-sm:   10px;
  --radius-xs:   6px;
  --font:         'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-display: 'Bebas Neue', Impact, sans-serif;
  --font-mono:    'JetBrains Mono', 'Courier New', monospace;

  /* Safe area insets for notched phones */
  --sat: env(safe-area-inset-top,    0px);
  --sar: env(safe-area-inset-right,  0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left,   0px);
}

/* ── Reset ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  height: 100%;
  /* Prevent font-size inflation on orientation change */
  -webkit-text-size-adjust: 100%;
}

body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* Prevent pull-to-refresh on Chrome Android */
  overscroll-behavior: none;
}

/* ── Utility ────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Screens (start / error) ────────────────────────────────── */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(var(--sat) + 32px)
    calc(var(--sar) + 24px)
    calc(var(--sab) + 32px)
    calc(var(--sal) + 24px);
  background: var(--bg);
  z-index: 100;
}

/* ── Start Screen ───────────────────────────────────────────── */
.start-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  text-align: center;
  max-width: 340px;
  width: 100%;
}

.start-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--surface-2) 0%, var(--surface) 100%);
  border: 1px solid var(--border-hi);
  color: var(--accent);
  box-shadow: 0 0 32px var(--accent-glow);
  animation: icon-glow 3s ease-in-out infinite;
}

@keyframes icon-glow {
  0%, 100% { box-shadow: 0 0 24px var(--accent-glow); }
  50%       { box-shadow: 0 0 48px rgba(196, 151, 90, 0.4); }
}

.start-icon svg {
  width: 22px;
  height: auto;
}

.start-inner h1 {
  font-family: var(--font-display);
  font-size: clamp(3.8rem, 18vw, 5.5rem);
  font-weight: 400;
  letter-spacing: 0.08em;
  line-height: 0.9;
  background: linear-gradient(180deg, var(--text) 20%, var(--text-muted) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.start-tagline {
  font-size: 0.95rem;
  font-weight: 400;
  color: var(--text-muted);
  line-height: 1.5;
}

.start-note {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--text-dim);
  letter-spacing: 0.04em;
}

/* ── Error Screen ───────────────────────────────────────────── */
.error-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  text-align: center;
  max-width: 320px;
  width: 100%;
}

.error-icon { font-size: 48px; }

.error-inner h2 {
  font-family: var(--font-display);
  font-size: 1.9rem;
  font-weight: 400;
  letter-spacing: 0.06em;
}

.error-inner p {
  font-size: 0.95rem;
  color: var(--text-muted);
  line-height: 1.6;
}

/* ── Buttons ────────────────────────────────────────────────── */
.btn-primary,
.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 52px;
  padding: 14px 36px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.88rem;
  font-weight: 600;
  font-family: var(--font);
  letter-spacing: 0.06em;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: opacity 0.12s, transform 0.08s, box-shadow 0.15s;
}

.btn-primary {
  background: linear-gradient(160deg, #d4a46a 0%, #b8843f 100%);
  color: #100c06;
  box-shadow: 0 4px 20px rgba(196, 151, 90, 0.35), inset 0 1px 0 rgba(255,255,255,0.14);
}

.btn-primary:hover {
  box-shadow: 0 6px 28px rgba(196, 151, 90, 0.48), inset 0 1px 0 rgba(255,255,255,0.14);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-hi);
}

.btn-primary:active,
.btn-secondary:active { opacity: 0.84; transform: scale(0.97); }

.btn-primary:focus-visible,
.btn-secondary:focus-visible,
.btn-icon:focus-visible,
.string-btn:focus-visible,
.btn-delete:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-full { width: 100%; }

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.12s, background 0.12s, border-color 0.12s;
  flex-shrink: 0;
}

.btn-icon:active { color: var(--accent); border-color: var(--accent); }

/* ── App Shell ──────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  /* Use dvh with vh fallback */
  height: 100vh;
  height: 100dvh;
  padding-top: var(--sat);
  padding-bottom: var(--sab);
  padding-left: var(--sal);
  padding-right: var(--sar);
  overflow: hidden;
}

/* ── Header ─────────────────────────────────────────────────── */
.app-header {
  flex-shrink: 0;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(15,15,20,0.9) 0%, transparent 100%);
}

.tuning-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tuning-label {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--text-dim);
  flex-shrink: 0;
}

.select-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

.select-wrap::after {
  content: '▾';
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim);
  pointer-events: none;
  font-size: 0.8rem;
}

#tuning-select {
  width: 100%;
  height: 44px;
  padding: 0 32px 0 12px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  font-family: var(--font);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.15s;
}

#tuning-select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-color: var(--accent);
}

/* ── Main / Tuner Display ───────────────────────────────────── */
.app-main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  min-height: 0;
  overflow: hidden;
}

#tuner-display {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Tuner Display ──────────────────────────────────────────── */
.td-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  gap: 0;
  padding: 12px 16px 8px;
}

.td-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.td-note {
  font-size: clamp(3.8rem, 18vw, 6rem);
  font-weight: 700;
  font-family: var(--font);
  line-height: 1;
  color: var(--text);
  letter-spacing: -0.02em;
  transition: color 0.2s;
  min-height: 1lh;
}

.td-cents {
  font-size: 1rem;
  font-weight: 500;
  font-family: var(--font-mono);
  color: var(--text-muted);
  letter-spacing: 0.06em;
  min-height: 1.4em;
  transition: color 0.15s;
}

.td-meter-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  min-height: 0;
  gap: 6px;
  padding-bottom: 4px;
}

.td-arc {
  width: 100%;
  max-width: min(420px, 90vw);
  height: auto;
  overflow: visible;
}

/* Arc track */
.td-arc-track {
  fill: none;
  stroke: var(--surface-3);
  stroke-width: 6;
  stroke-linecap: round;
}

/* In-tune zone highlight */
.td-arc-intune {
  fill: none;
  stroke: var(--green);
  stroke-width: 6;
  stroke-linecap: round;
  transition: opacity 0.2s;
  filter: drop-shadow(0 0 6px var(--green-glow));
}

/* Scale ticks */
.td-tick {
  stroke: var(--border-hi);
  stroke-width: 1.5;
  stroke-linecap: round;
}

.td-tick--center {
  stroke: var(--green);
  stroke-width: 2;
}

/* Needle */
.td-needle-group {
  transform-origin: 0px 0px;
  color: var(--text-muted);
  will-change: transform;
}

.td-needle {
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
}

/* Pivot dot */
.td-pivot {
  fill: currentColor;
  color: var(--text-muted);
}

/* In-tune state: note glows green */
.td-wrap.in-tune .td-note {
  color: var(--green);
}

/* Meter labels */
.td-labels {
  display: flex;
  justify-content: space-between;
  width: 100%;
  max-width: min(420px, 90vw);
  padding: 0 4px;
}

.td-labels span {
  font-family: var(--font-mono);
  font-size: 0.58rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-dim);
}

.td-labels span:nth-child(2) {
  color: var(--green);
  opacity: 0.7;
}

/* ── String Buttons ─────────────────────────────────────────── */
.app-footer {
  flex-shrink: 0;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: linear-gradient(0deg, rgba(15,15,20,0.9) 0%, transparent 100%);
}

.footer-row {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.string-buttons {
  display: flex;
  gap: 6px;
  justify-content: center;
  flex: 1;
  min-width: 0;
}

.btn-ref-tone {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 62px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text-dim);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.btn-ref-tone:active { color: var(--accent); border-color: var(--accent); }

.btn-ref-tone.playing {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-dim);
}

.btn-ref-tone:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.string-btn {
  flex: 1;
  max-width: 56px;
  min-height: 62px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-family: var(--font);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s, border-color 0.15s, background 0.15s, box-shadow 0.15s, transform 0.1s;
}

.string-btn .s-name {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.04em;
}

.string-btn .s-num {
  font-family: var(--font-mono);
  font-size: 0.52rem;
  font-weight: 600;
  opacity: 0.3;
  letter-spacing: 0.1em;
}

.string-btn.active,
.string-btn[aria-pressed='true'] {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-dim);
  box-shadow: 0 0 14px var(--accent-glow);
}

.string-btn.in-tune {
  color: var(--green);
  border-color: var(--green);
  background: var(--green-dim);
  box-shadow: 0 0 18px var(--green-glow);
}

.string-btn:active { transform: scale(0.91); }

/* ── Modal ──────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
  padding: 0 0 var(--sab);
}

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border-hi);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: slide-up 0.28s cubic-bezier(0.32, 0.72, 0, 1);
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0);    }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 18px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-title {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 400;
  letter-spacing: 0.07em;
}

.modal-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* ── Modal Sections ─────────────────────────────────────────── */
.modal-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.section-title {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: var(--text-dim);
}

/* ── Form Elements ──────────────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--text-dim);
}

.form-input {
  height: 48px;
  padding: 0 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-family: var(--font);
  width: 100%;
  transition: border-color 0.15s;
}

.form-input::placeholder {
  color: var(--text-dim);
}

.form-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-color: var(--accent);
}

.form-error {
  color: var(--red);
  font-size: 0.82rem;
  line-height: 1.4;
  margin-top: -4px;
}

/* ── String Editor ──────────────────────────────────────────── */
.string-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.string-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.string-row-label {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--text-dim);
  width: 28px;
  flex-shrink: 0;
  text-align: center;
}

.string-note-sel,
.string-oct-sel {
  height: 44px;
  padding: 0 8px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-family: var(--font);
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s;
}

.string-note-sel:focus,
.string-oct-sel:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-color: var(--accent);
}

.string-note-sel { flex: 3; }
.string-oct-sel  { flex: 2; }

/* ── Saved Tunings List ─────────────────────────────────────── */
.saved-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.saved-empty {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-dim);
  text-align: center;
  padding: 12px 0;
  letter-spacing: 0.04em;
}

.saved-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 13px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s;
}

.saved-item:hover {
  border-color: var(--border-hi);
}

.saved-item-name {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.saved-item-notes {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
  letter-spacing: 0.05em;
}

.btn-delete {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.12s, color 0.12s;
  flex-shrink: 0;
}

.btn-delete:hover { color: var(--red); }
.btn-delete:active { background: rgba(232, 84, 84, 0.14); color: var(--red); }

/* ── Landscape phones ───────────────────────────────────────── */
@media (orientation: landscape) and (max-height: 500px) {
  .app-footer { padding: 6px 12px; }

  .string-btn { min-height: 46px; }
  .string-btn .s-name { font-size: 1.1rem; }
  .btn-ref-tone { height: 46px; }

  .td-note { font-size: 2.8rem; }
  .td-arc { max-width: min(280px, 70vw); }

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

/* ── Larger screens ─────────────────────────────────────────── */
@media (min-width: 600px) {
  .modal-overlay { align-items: center; padding: 24px; }
  .modal-box {
    border-radius: var(--radius);
    border-bottom: 1px solid var(--border-hi);
    max-height: 82vh;
  }
  .string-buttons { max-width: 400px; margin: 0 auto; }
  .footer-row { max-width: 460px; margin: 0 auto; }
}

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