:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #1c1c1e;
  --muted: #6e6e73;
  --line: rgba(60, 60, 67, 0.16);
  --fill: rgba(118, 118, 128, 0.12);
  --seg-on: #ffffff;
  --seg-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.04);

  --given: #2563eb;
  --given-soft: rgba(37, 99, 235, 0.12);
  --given-key: #93b4f6;
  --given-key-on: #5b8cf0;
  --target: #ea6c0a;
  --target-soft: rgba(234, 108, 10, 0.13);
  --target-key: #f8b678;
  --target-key-on: #f29440;

  --key-white: #ffffff;
  --key-white-on: #d9d9df;
  --key-edge: #c7c7cc;
  --key-black: #1d1d1f;
  --key-black-on: #48484a;
  --c-label: #a1a1a6;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #f2f2f7;
    --muted: #98989f;
    --line: rgba(84, 84, 88, 0.6);
    --fill: rgba(118, 118, 128, 0.24);
    --seg-on: #636366;
    --seg-shadow: none;

    --given: #5a8dff;
    --given-soft: rgba(90, 141, 255, 0.18);
    --given-key: #7ea3f7;
    --given-key-on: #4f7ff0;
    --target: #ff8a2a;
    --target-soft: rgba(255, 138, 42, 0.18);
    --target-key: #f5a865;
    --target-key-on: #ef8a32;

    --key-white: #ececf0;
    --key-white-on: #c4c4cb;
    --key-edge: #3a3a3c;
    --key-black: #0b0b0c;
    --key-black-on: #3a3a3c;
    --c-label: #8e8e93;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }

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

/* Header */
.top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
}
.top h1 {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
  flex: 1;
}
.status { font-size: 13px; color: var(--muted); }
.icon-btn {
  width: 40px; height: 40px;
  border: 0; border-radius: 20px;
  background: var(--fill);
  color: var(--text);
  display: grid; place-items: center;
}

/* Segmented controls */
.seg {
  display: flex;
  background: var(--fill);
  border-radius: 10px;
  padding: 2px;
  gap: 2px;
}
.seg button {
  flex: 1;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 8px 6px;
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.15s;
}
.seg button[aria-pressed="true"] {
  background: var(--seg-on);
  box-shadow: var(--seg-shadow);
  font-weight: 600;
}
.seg button:disabled { opacity: 0.3; }
.seg-sm button { padding: 6px 6px; font-size: 14px; }

/* Controls */
.controls { display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.row-label { font-size: 14px; color: var(--muted); min-width: 52px; }
.grow { flex: 1; min-width: 0; }

.select { position: relative; display: inline-flex; }
.select select {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 9px;
  background: var(--card);
  font-size: 16px; /* 16px stops iOS zooming on focus */
  font-weight: 500;
  padding: 6px 26px 6px 10px;
  min-height: 34px;
  width: 100%;
}
.select::after {
  content: "";
  position: absolute;
  right: 11px;
  top: 50%;
  width: 6px; height: 6px;
  border: solid var(--muted);
  border-width: 0 1.8px 1.8px 0;
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
}

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 -16px;
  padding: 2px 16px;
  scroll-padding: 0 16px;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  border: 0;
  border-radius: 999px;
  background: var(--card);
  padding: 7px 12px;
  font-size: 14px;
  font-weight: 600;
  min-width: 44px;
}
.chip[aria-current="true"] { background: var(--target); color: #fff; }

.range-pill {
  border: 0;
  border-radius: 9px;
  background: var(--card);
  padding: 7px 12px;
  font-size: 15px;
  font-weight: 500;
}

/* Prompt */
.card {
  background: var(--card);
  border-radius: 16px;
}
.prompt { padding: 12px 16px 14px; }
.kicker {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.title-row {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin-top: 2px;
}
.prompt h2 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.015em;
  margin: 0;
}
.badge {
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
}
.badge:empty { display: none; }
.notes {
  margin-top: 8px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 15px;
  color: var(--muted);
}
.pill {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  border-radius: 8px;
  padding: 3px 9px;
  font-weight: 600;
  font-size: 16px;
}
.pill small { font-size: 12px; font-weight: 500; opacity: 0.8; }
.pill.given { background: var(--given-soft); color: var(--given); }
.pill.target { background: var(--target-soft); color: var(--target); }
.arrow { color: var(--muted); font-size: 15px; }
.hint { font-size: 12px; font-weight: 500; opacity: 0.75; }

/* Piano */
.piano-card { padding: 10px; }
#piano { display: block; width: 100%; height: auto; touch-action: manipulation; }
.key { cursor: pointer; }
.key rect { transition: fill 0.08s; }
.key-white rect { fill: var(--key-white); stroke: var(--key-edge); stroke-width: 1; }
.key-black rect { fill: var(--key-black); }
.key-white.is-given rect { fill: var(--given-key); }
.key-black.is-given rect { fill: var(--given); }
.key-white.is-target rect { fill: var(--target-key); }
.key-black.is-target rect { fill: var(--target); }
.key-white.is-sounding rect { fill: var(--key-white-on); }
.key-black.is-sounding rect { fill: var(--key-black-on); }
.key-white.is-given.is-sounding rect { fill: var(--given-key-on); }
.key-black.is-given.is-sounding rect { fill: var(--given-key-on); }
.key-white.is-target.is-sounding rect { fill: var(--target-key-on); }
.key-black.is-target.is-sounding rect { fill: var(--target-key-on); }
.c-label { font-size: 10px; text-anchor: middle; fill: var(--c-label); font-weight: 500; }
.is-given .c-label, .is-target .c-label { fill: rgba(0, 0, 0, 0.5); }
.order-dot { fill: #fff; stroke-width: 1.5; }
.is-given .order-dot { stroke: var(--given); }
.is-target .order-dot { stroke: var(--target); }
.order-num { text-anchor: middle; font-weight: 700; }
.is-given .order-num { fill: var(--given); }
.is-target .order-num { fill: var(--target); }
.note-label { font-size: 13px; font-weight: 700; text-anchor: middle; }
.note-label.role-given { fill: var(--given); }
.note-label.role-target { fill: var(--target); }
.label-tick { stroke-width: 1.5; opacity: 0.5; }
.label-tick.role-given { stroke: var(--given); }
.label-tick.role-target { stroke: var(--target); }

/* Buttons */
.bottom { margin-top: auto; display: flex; flex-direction: column; gap: 10px; padding-top: 4px; }
.actions { display: grid; grid-template-columns: 1fr 1.15fr 1fr; gap: 10px; }
.nav { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; }
.nav.single { grid-template-columns: 1fr; }
.btn {
  border: 0;
  border-radius: 14px;
  min-height: 58px;
  padding: 8px 6px;
  background: var(--fill);
  font-weight: 600;
  font-size: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  transition: transform 0.08s, opacity 0.08s;
}
.btn:active { transform: scale(0.96); opacity: 0.85; }
.btn-main { font-size: 16px; }
.btn-sub { font-size: 12px; font-weight: 500; opacity: 0.8; white-space: nowrap; }
.btn-sub:empty { display: none; }
.btn-given { background: var(--given-soft); color: var(--given); }
.btn-reveal { background: var(--target); color: #fff; }
.btn-both { background: var(--card); }
.btn-nav { min-height: 50px; font-size: 17px; background: var(--card); }
.btn-next { background: var(--text); color: var(--bg); }

/* Settings sheet */
.sheet {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 560px;
  margin: auto auto 0;
  border-radius: 16px 16px 0 0;
  background: var(--bg);
  color: var(--text);
  max-height: 85dvh;
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.35); }
.sheet[open] { animation: sheet-up 0.22s ease-out; }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
.sheet form { padding: 14px 16px calc(env(safe-area-inset-bottom) + 18px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 20px; }
.text-btn { border: 0; background: none; color: var(--given); font-size: 17px; font-weight: 600; padding: 8px 4px; }
.group { background: var(--card); border-radius: 12px; margin-bottom: 18px; position: relative; padding-top: 0; }
.group-title {
  position: absolute; top: -20px; left: 4px;
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted);
}
.group { margin-top: 28px; }
.cell {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; min-height: 48px;
  font-size: 16px;
}
.cell + .cell { border-top: 0.5px solid var(--line); }
.cell .select select { background: var(--fill); }
.fine { font-size: 12px; color: var(--muted); margin: 0 4px; line-height: 1.4; }

.switch {
  appearance: none; -webkit-appearance: none;
  width: 51px; height: 31px; border-radius: 16px;
  background: var(--fill); position: relative; margin: 0; flex: none;
  transition: background 0.2s;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s;
}
.switch:checked { background: #34c759; }
.switch:checked::after { transform: translateX(20px); }

@media (min-height: 800px) {
  .app { gap: 12px; }
  .prompt h2 { font-size: 28px; }
}

/* Short screens: iPhone SE, or Safari with its toolbars showing */
@media (max-height: 740px) {
  .app { gap: 8px; padding-top: calc(env(safe-area-inset-top) + 4px); }
  .top { min-height: 38px; }
  .top h1 { font-size: 26px; }
  .icon-btn { width: 36px; height: 36px; }
  .seg button { padding: 6px; }
  .seg-sm button { padding: 5px 6px; }
  .select select { min-height: 32px; padding-top: 4px; padding-bottom: 4px; }
  .controls { gap: 6px; }
  .prompt { padding: 10px 14px 12px; }
  .prompt h2 { font-size: 23px; }
  .notes { margin-top: 6px; }
  .piano-card { padding: 8px; }
  .bottom { gap: 8px; padding-top: 0; }
  .btn { min-height: 50px; }
  .btn-nav { min-height: 44px; }
}
