:root {
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #111827;
  color: #f8fafc;
  --case: #15130f;
  --case-hi: #31281d;
  --line: #4b3723;
  --ink-soft: #c8bda7;
  --accent: #d69a2d;
  --key: #fff9ea;
  --key-side: #b9a98b;
  --key-active: #eab857;
  --key-edge: #5b3b14;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent 22%),
    linear-gradient(135deg, #171a21 0%, #0e1117 100%);
  touch-action: none;
  user-select: none;
}

button {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: 100%;
  min-height: 100vh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: grid;
  grid-template-rows: 64px 1fr;
  gap: 10px;
}

.topbar,
.instrument-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, var(--case-hi), var(--case));
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.34);
}

.topbar {
  min-height: 0;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.eyebrow,
.label {
  display: block;
  margin: 0 0 2px;
  color: var(--ink-soft);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.eyebrow span {
  color: var(--accent);
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 3vw, 2.1rem);
  line-height: 1.05;
  letter-spacing: 0;
}

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

.secondary-button {
  min-width: 92px;
  min-height: 42px;
  border-radius: 8px;
  font-weight: 850;
  cursor: pointer;
}

.secondary-button {
  border: 1px solid #6f5535;
  background: #241c15;
  color: #fff7e8;
}

.instrument-panel {
  min-height: 0;
  padding: 10px 12px 14px;
  display: grid;
  grid-template-rows: 42px 38px 1fr;
  gap: 0;
  overflow: hidden;
}

.now-playing {
  min-height: 0;
  padding: 0 6px;
  border: 0;
  background: transparent;
  display: flex;
  align-items: center;
}

strong {
  display: block;
  overflow-wrap: anywhere;
  color: #fff7e8;
  font-size: clamp(1rem, 2.3vw, 1.45rem);
  line-height: 1.1;
}

.rail {
  border: 1px solid #050608;
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background:
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.08) 0 2px, transparent 2px 20px),
    linear-gradient(180deg, #3f3020, #15110d);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.08), inset 0 -12px 18px rgba(0, 0, 0, 0.34);
}

.keyboard {
  position: relative;
  min-height: 0;
  display: flex;
  gap: 0;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  border: 1px solid #07090c;
  border-top: 0;
  border-radius: 0 0 8px 8px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 4%),
    #090a0c;
  box-shadow: inset 0 8px 20px rgba(0, 0, 0, 0.55);
}

.key {
  position: relative;
  appearance: none;
  -webkit-appearance: none;
  flex: 1 1 0;
  margin: 0;
  min-width: 0;
  min-height: 100%;
  padding: 0 6px 18px;
  border: 0;
  border-right: 1px solid var(--key-side);
  border-bottom: 13px solid var(--key-edge);
  border-radius: 0 0 10px 10px;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.18), transparent 10%, transparent 90%, rgba(0, 0, 0, 0.2)),
    linear-gradient(180deg, #fffef8 0%, #fff7e4 58%, #ded0b3 100%),
    var(--key);
  color: #111827;
  outline: none;
  display: grid;
  place-items: end center;
  grid-template-rows: 1fr auto auto;
  box-shadow:
    inset 0 5px 0 rgba(255, 255, 255, 0.92),
    inset 10px 0 16px rgba(255, 255, 255, 0.32),
    inset -12px 0 18px rgba(66, 44, 18, 0.12),
    inset 0 -26px 30px rgba(76, 51, 20, 0.17);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

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

.key::before {
  content: "";
  position: absolute;
  inset: 0 12% auto;
  height: 42%;
  border-radius: 0 0 999px 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.55), transparent);
  pointer-events: none;
}

.key::after {
  content: "";
  position: absolute;
  inset: auto 10px 0;
  height: 12px;
  border-radius: 999px 999px 0 0;
  background: rgba(255, 255, 255, 0.18);
  pointer-events: none;
}

.key:last-child {
  border-right: 0;
}

.key-label {
  font-size: clamp(2.3rem, 5vw, 4.4rem);
  font-weight: 900;
  line-height: 0.92;
  z-index: 1;
}

.note-label {
  margin-top: 10px;
  color: #725d3c;
  font-size: clamp(0.9rem, 2vw, 1.25rem);
  font-weight: 850;
  z-index: 1;
}

.key.active {
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.18), transparent 12%, transparent 88%, rgba(0, 0, 0, 0.2)),
    linear-gradient(180deg, #f7d37b 0%, #eab857 58%, #b7791f 100%),
    var(--key-active);
  border-bottom-width: 6px;
  transform: translateY(7px);
  color: #1d2730;
}

/* Labels must not swallow touches — elementFromPoint should return the .key */
.key-label,
.note-label {
  pointer-events: none;
}

/* Darker grooves so adjacent white keys read as separate reeds */
.key.white {
  border-right: 2px solid rgba(12, 9, 5, 0.5);
}

/* Komal/tivra black keys overlaid on the white bed */
.key.black {
  position: absolute;
  top: 0;
  width: 8%;
  height: 62%;
  max-height: 240px;
  min-height: 0;
  z-index: 5;
  padding: 0 0 9px;
  border: 0;
  border-radius: 0 0 5px 5px;
  transform: none;
  background: linear-gradient(180deg, #4a4a52 0%, #18181c 55%, #050506 100%);
  color: #efe7d4;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.22),
    inset 6px 0 8px rgba(0, 0, 0, 0.4),
    inset -6px 0 8px rgba(0, 0, 0, 0.4),
    inset 0 -10px 14px rgba(0, 0, 0, 0.6),
    0 7px 11px rgba(0, 0, 0, 0.55);
  display: grid;
  grid-template-rows: 1fr auto;
  place-items: end center;
}

.key.black::before,
.key.black::after {
  display: none;
}

.key.black .key-label {
  font-size: clamp(0.8rem, 2.4vw, 1.15rem);
  font-weight: 800;
  color: #f2ead8;
}

.key.black.active {
  background: linear-gradient(180deg, #dcaa40 0%, #9a6f1e 60%, #5c3f12 100%);
  color: #1d2730;
  transform: translateY(4px);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.4),
    inset 0 -6px 10px rgba(0, 0, 0, 0.4),
    0 3px 6px rgba(0, 0, 0, 0.5);
}

/* ---------- Riyaz practice page ---------- */

.link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  text-align: center;
}

.practice-panel {
  grid-template-rows: 1fr auto;
  padding: 18px 16px;
  gap: 16px;
}

.practice-status {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
}

.swara-display strong {
  font-size: clamp(4rem, 18vw, 9rem);
  font-weight: 900;
  line-height: 1;
  color: var(--accent);
}

.beat-dots {
  display: flex;
  gap: 10px;
  min-height: 14px;
}

.beat-dots .dot {
  width: 14px;
  height: 14px;
  border-radius: 999px;
  background: #3a3226;
  animation: dot-fill 0.25s ease-out both;
}

@keyframes dot-fill {
  from {
    background: #3a3226;
    transform: scale(1);
  }
  to {
    background: var(--accent);
    transform: scale(1.15);
  }
}

.sequence-strip {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.seq-cell {
  min-width: 44px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #241c15;
  color: var(--ink-soft);
  font-weight: 800;
  font-size: 0.95rem;
}

.seq-cell.current {
  background: var(--key-active);
  border-color: var(--accent);
  color: #1d2730;
}

.dir-label {
  min-height: 0.9em;
  color: var(--accent);
  font-size: 0.8rem;
}

.practice-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding-bottom: 6px;
  flex-wrap: wrap;
}

.practice-pickers {
  display: flex;
  align-items: end;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.picker-group {
  display: grid;
  gap: 3px;
}

.section-row {
  display: flex;
  gap: 6px;
}

.section-button {
  min-width: 46px;
  min-height: 42px;
  border: 1px solid #6f5535;
  border-radius: 8px;
  background: #241c15;
  color: var(--ink-soft);
  font-weight: 800;
  cursor: pointer;
}

.section-button.selected {
  background: var(--key-active);
  border-color: var(--accent);
  color: #1d2730;
}

.primary-button {
  min-width: 140px;
  min-height: 52px;
  border: 1px solid #7a5a2a;
  border-radius: 10px;
  background: linear-gradient(180deg, #f0c060, #c98f28);
  color: #241a08;
  font-weight: 900;
  font-size: 1.1rem;
  cursor: pointer;
}

.primary-button.playing {
  background: linear-gradient(180deg, #4a3a22, #2c2114);
  color: #f7d99a;
}

.config-link {
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid #6f5535;
  border-radius: 8px;
  background: #241c15;
  color: var(--ink-soft);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.config-link:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.section-config-dialog {
  width: min(680px, calc(100vw - 28px));
  max-height: min(760px, calc(100vh - 28px));
  padding: 0;
  border: 1px solid #6f5535;
  border-radius: 12px;
  background: linear-gradient(180deg, var(--case-hi), var(--case));
  color: #fff7e8;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6);
}

.section-config-dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
}

.section-config-dialog form {
  padding: 18px;
}

.config-heading,
.config-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.config-heading h2 {
  margin: 0;
  font-size: 1.3rem;
}

.dialog-close {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-soft);
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
}

.config-help {
  margin: 12px 0;
  color: var(--ink-soft);
  font-size: 0.88rem;
}

.section-config-rows {
  display: grid;
  gap: 8px;
  max-height: min(50vh, 440px);
  overflow: auto;
  padding-right: 2px;
}

.config-section-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.config-section-tabs .section-button {
  min-width: 40px;
  min-height: 36px;
}

.round-config-card {
  min-width: 0;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.14);
}

.round-config-card legend {
  padding: 0 5px;
  color: #fff7e8;
  font-size: 0.84rem;
  font-weight: 800;
}

.tempo-step-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.tempo-step-pair {
  display: grid;
  gap: 5px;
}

.tempo-step-pair input {
  width: 100%;
  padding: 8px 6px;
  border: 1px solid #6f5535;
  border-radius: 7px;
  background: #241c15;
  color: #fff7e8;
  font: inherit;
  font-weight: 800;
  text-align: center;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.config-actions {
  margin-top: 14px;
}

.config-save {
  min-width: 128px;
  min-height: 42px;
  font-size: 0.95rem;
}

/* ---------- Raag library and practice ---------- */

.raag-shell {
  grid-template-rows: 64px 1fr;
}

.raag-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  gap: 10px;
}

.raag-library {
  min-height: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, var(--case-hi), var(--case));
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.34);
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: auto;
}

.raag-library h2 {
  margin: 0;
  color: #fff7e8;
  font-size: 1.7rem;
}

.raag-note,
.future-practices {
  margin: 5px 0 0;
  color: var(--ink-soft);
  font-size: 0.86rem;
  line-height: 1.4;
}

.raag-note strong {
  display: inline;
  color: var(--accent);
  font-size: inherit;
}

.practice-card {
  width: 100%;
  padding: 14px;
  border: 1px solid #6f5535;
  border-radius: 10px;
  background: #241c15;
  color: #fff7e8;
  font: inherit;
  text-align: left;
  cursor: pointer;
  display: grid;
  gap: 6px;
}

.practice-card.selected {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.practice-card strong {
  font-size: 1.05rem;
}

.practice-card > span:last-child {
  color: var(--ink-soft);
  font-size: 0.78rem;
  line-height: 1.45;
}

.future-practices {
  margin-top: auto;
}

.raag-practice-panel {
  height: 100%;
}

.yaman-sequence .seq-cell {
  min-width: 48px;
  font-size: 1.05rem;
}

@media (max-width: 760px) and (orientation: portrait) {
  body {
    touch-action: pan-y;
  }

  .raag-shell {
    min-height: 100vh;
    grid-template-rows: auto auto;
  }

  .raag-layout {
    grid-template-columns: 1fr;
  }

  .raag-library {
    overflow: visible;
  }

  .raag-practice-panel {
    min-height: 560px;
  }
}

@media (max-width: 560px) {
  .section-config-dialog form {
    padding: 14px;
  }

  .tempo-step-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .instrument-panel.practice-panel {
    grid-template-rows: 1fr auto;
    padding: 10px;
    gap: 8px;
  }

  .raag-layout {
    grid-template-columns: minmax(180px, 24vw) minmax(0, 1fr);
    gap: 6px;
  }

  .raag-library {
    padding: 10px;
    gap: 8px;
  }

  .raag-library h2 {
    font-size: 1.15rem;
  }

  .practice-card {
    padding: 8px;
    gap: 3px;
  }

  .future-practices {
    display: none;
  }

  .practice-status {
    gap: 8px;
  }

  .swara-display strong {
    font-size: clamp(2.6rem, 14vh, 5rem);
  }

  .practice-controls {
    padding-bottom: 2px;
  }

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

@media (orientation: landscape) {
  .app-shell {
    height: 100vh;
  }
}

@media (orientation: portrait) {
  .app-shell {
    min-height: 100vh;
  }
}

@media (max-width: 620px) {
  .app-shell {
    grid-template-rows: 60px 1fr;
  }
}

/* Landscape phone harmonium layout */
@media (orientation: landscape) and (max-height: 500px) {
  .app-shell {
    grid-template-rows: auto 1fr;
    gap: 6px;
    padding: 6px;
  }

  .topbar {
    min-height: 0;
    padding: 5px 10px;
    gap: 8px;
  }

  .topbar h1 {
    font-size: clamp(0.95rem, 3.5vw, 1.2rem);
    line-height: 1.1;
  }

  .eyebrow {
    margin-bottom: 1px;
    font-size: 0.58rem;
  }

  .secondary-button {
    min-width: 64px;
    min-height: 30px;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 0.75rem;
  }

  .instrument-panel {
    position: relative;
    grid-template-rows: 1fr;
    padding: 5px;
    overflow: hidden;
  }

  .now-playing {
    position: absolute;
    top: 8px;
    left: 50%;
    z-index: 10;
    transform: translateX(-50%);
    padding: 3px 12px;
    border-radius: 999px;
    background: rgba(9, 10, 12, 0.72);
    backdrop-filter: blur(4px);
    pointer-events: none;
  }

  .now-playing strong {
    font-size: 0.85rem;
    color: var(--accent);
  }

  .rail {
    display: none;
  }

  .keyboard {
    position: relative;
    height: 100%;
    border: 1px solid #07090c;
    border-radius: 8px;
    background:
      linear-gradient(180deg, rgba(255, 255, 255, 0.06), transparent 6%),
      repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.03) 0 2px, transparent 2px 18px),
      #0a0b0d;
    box-shadow:
      inset 0 2px 4px rgba(255, 255, 255, 0.06),
      inset 0 -10px 24px rgba(0, 0, 0, 0.55),
      0 14px 30px rgba(0, 0, 0, 0.35);
  }

  .keyboard::before {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    z-index: 2;
    height: 7px;
    border-radius: 8px 8px 0 0;
    background: linear-gradient(180deg, #2a2018, #0f0c09);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
    pointer-events: none;
  }

  .key {
    padding: 0 3px 10px;
    border-right: 1px solid #9e8d70;
    border-bottom: 10px solid #3d2810;
    border-radius: 0 0 5px 5px;
    background:
      linear-gradient(90deg, rgba(0, 0, 0, 0.22), transparent 8%, transparent 92%, rgba(0, 0, 0, 0.24)),
      linear-gradient(180deg, #fffef8 0%, #fff7e4 55%, #e8dac0 100%);
    box-shadow:
      inset 0 3px 0 rgba(255, 255, 255, 0.9),
      inset 6px 0 10px rgba(255, 255, 255, 0.25),
      inset -8px 0 12px rgba(66, 44, 18, 0.1),
      inset 0 -18px 22px rgba(76, 51, 20, 0.14),
      1px 0 0 rgba(255, 255, 255, 0.1);
  }

  .key::before {
    inset: 0 10% auto;
    height: 36%;
  }

  .key::after {
    right: 6px;
    bottom: 0;
    left: 6px;
    height: 8px;
  }

  .key-label {
    font-size: clamp(1.4rem, 7vh, 2.6rem);
  }

  .note-label {
    margin-top: 3px;
    font-size: 0.65rem;
  }

  .key.active {
    background:
      linear-gradient(90deg, rgba(0, 0, 0, 0.22), transparent 10%, transparent 90%, rgba(0, 0, 0, 0.24)),
      linear-gradient(180deg, #f7d37b 0%, #eab857 55%, #c48a24 100%);
    border-bottom-width: 4px;
    transform: translateY(6px);
    box-shadow:
      inset 0 3px 0 rgba(255, 255, 255, 0.7),
      inset 6px 0 10px rgba(255, 255, 255, 0.2),
      inset -8px 0 12px rgba(66, 44, 18, 0.1),
      inset 0 -10px 14px rgba(120, 80, 20, 0.18);
  }
}
