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

/* ── Dark theme (default) ── */
:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --accent: #0f3460;
  --green: #00c853;
  --yellow: #ffd600;
  --red: #ff1744;
  --text: #e0e0e0;
  --anchor: #f0f0f0;
  --text-dim: #78909c;
  --note-size: 3.5rem;
  --gauge-tick: #fff;
  --gauge-tick-dim: rgba(255,255,255,0.3);
  --gauge-label: rgba(255,255,255,0.4);
  --needle-idle: rgba(255,255,255,0.15);
  --hub-idle: rgba(255,255,255,0.2);
  --btn-bg: linear-gradient(135deg, #0f3460, #1a5276);
  --btn-shadow: rgba(15, 52, 96, 0.4);
}

/* ── Light theme ── */
[data-theme="light"] {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --accent: #c2d1e0;
  --text: #1a1a2e;
  --anchor: #102040;
  --text-dim: #5f6b7a;
  --gauge-tick: #1a1a2e;
  --gauge-tick-dim: rgba(26,26,46,0.3);
  --gauge-label: rgba(26,26,46,0.5);
  --needle-idle: rgba(26,26,46,0.15);
  --hub-idle: rgba(26,26,46,0.2);
  --btn-bg: linear-gradient(135deg, #3a7bd5, #5b9bf5);
  --btn-shadow: rgba(58, 123, 213, 0.35);
}

@media (prefers-color-scheme: light) {
  [data-theme="system"] {
    --bg: #f0f2f5;
    --surface: #ffffff;
    --accent: #c2d1e0;
    --text: #1a1a2e;
    --anchor: #102040;
    --text-dim: #5f6b7a;
    --gauge-tick: #1a1a2e;
    --gauge-tick-dim: rgba(26,26,46,0.3);
    --gauge-label: rgba(26,26,46,0.5);
    --needle-idle: rgba(26,26,46,0.15);
    --hub-idle: rgba(26,26,46,0.2);
    --btn-bg: linear-gradient(135deg, #3a7bd5, #5b9bf5);
    --btn-shadow: rgba(58, 123, 213, 0.35);
  }
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

a {
  color: var(--anchor);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 1rem;
}

/* Footer */
footer {
  text-align: center;
  font-size: 0.7rem;
  color: var(--text-dim);
  padding: 0.25rem 0 0.5rem;
}

/* Header */
header {
  text-align: center;
  padding-bottom: 0.75rem;
}

header h1 {
  font-size: 1.8rem;
  font-weight: 600;
  color: var(--text);
}

/* Theme toggle */
#theme-toggle {
  position: fixed;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 100;
  display: flex;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 6px;
  overflow: hidden;
}

.theme-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 0.25rem 0.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.theme-btn:hover {
  color: var(--text);
}

.theme-btn.active {
  background: var(--accent);
  color: var(--text);
}

#instrument-select {
  margin-top: 0.5rem;
}

#tuning {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  outline: none;
  cursor: pointer;
}

/* Main content */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

/* Gauge / meter */
#meter-container {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#gauge {
  width: 100%;
  max-width: 480px;
  aspect-ratio: 2 / 1;
}

#cents-display {
  display: flex;
  align-items: baseline;
  gap: 0.25rem;
  margin-top: -0.5rem;
}

#cents-value {
  font-size: 1.75rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: color 0.15s;
}

#cents-label {
  font-size: 0.85rem;
  color: var(--text-dim);
}

/* Note display */
#note-display {
  text-align: center;
  line-height: 1.1;
}

#note-name {
  font-size: var(--note-size);
  font-weight: 800;
  transition: color 0.15s;
}

#note-octave {
  font-size: 1.5rem;
  vertical-align: sub;
  color: var(--text-dim);
}

#frequency-display {
  display: block;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-top: 0.25rem;
  font-variant-numeric: tabular-nums;
}

/* String indicators */
#strings-container {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.5rem 0;
}

.string-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-direction: column;
  line-height: 1.1;
}

.string-btn .string-num {
  font-size: 0.6rem;
  color: var(--text-dim);
  font-weight: 400;
}

.string-btn.active {
  border-color: var(--green);
  background: rgba(0, 200, 83, 0.15);
  box-shadow: 0 0 12px rgba(0, 200, 83, 0.3);
}

.string-btn.close {
  border-color: var(--yellow);
  background: rgba(255, 214, 0, 0.1);
}

.string-btn.in-tune {
  border-color: var(--green);
  background: rgba(0, 200, 83, 0.25);
  box-shadow: 0 0 20px rgba(0, 200, 83, 0.5);
}

/* Controls */
#controls {
  text-align: center;
  padding: 0.5rem 0 1rem;
}

#start-btn {
  background: var(--btn-bg);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0.9rem 2.5rem;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 4px 15px var(--btn-shadow);
}

#start-btn:active {
  transform: scale(0.97);
}

#start-btn.listening {
  background: linear-gradient(135deg, #b71c1c, #c62828);
  box-shadow: 0 4px 15px rgba(183, 28, 28, 0.4);
}

#status {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: 0.5rem;
  min-height: 1.2em;
}

/* Color states */
.color-green { color: var(--green) !important; }
.color-yellow { color: var(--yellow) !important; }
.color-red { color: var(--red) !important; }

/* Landscape tweaks for small screens */
@media (max-height: 500px) and (orientation: landscape) {
  #app { padding: 0.5rem; }
  header h1 { font-size: 1rem; }
  #meter-container { max-width: 360px; }
  :root { --note-size: 2.5rem; }
  .string-btn { width: 40px; height: 40px; font-size: 0.85rem; }
  #start-btn { padding: 0.6rem 1.5rem; font-size: 0.95rem; }
}

/* Larger phones */
@media (min-width: 400px) {
  :root { --note-size: 4rem; }
  .string-btn { width: 58px; height: 58px; }
}
