/* ChordProgress. Colors follow harmonic function: tonic blue, subdominant green,
   dominant orange, borrowed violet. Light warm-gray default with a dark mode. */

:root {
  --bg: #f2efea;
  --surface: #fbfaf7;
  --surface-2: #ebe7e0;
  --text: #22201d;
  --muted: #5f5a53;
  --line: #d6d0c6;
  --accent: #2b4c7e;
  --warn-bg: #fff3dc;
  --warn-text: #6b4300;

  --tonic: #2f6db5;       --tonic-fill: #dce8f7;       --tonic-tint: #edf3fb;
  --subdominant: #2c7f48; --subdominant-fill: #dbeee0; --subdominant-tint: #edf6ef;
  --dominant: #b85a17;    --dominant-fill: #fae2cd;    --dominant-tint: #fcf0e5;
  --borrowed: #6f559c;    --borrowed-fill: #e7e0f1;

  --font-display: "Avenir Next", "Segoe UI Variable Display", "Segoe UI", system-ui, sans-serif;
  --font-text: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius-pill: 999px;
  --tile-w: 76px;
  --tile-h: 56px;
  --tile-max: 124px;
  --gap: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181716;
    --surface: #22201e;
    --surface-2: #2c2a27;
    --text: #f1eee9;
    --muted: #b2aca3;
    --line: #3d3a36;
    --accent: #9cc0f0;
    --warn-bg: #3a2c12;
    --warn-text: #ffd590;

    --tonic: #7fb0ec;       --tonic-fill: #1d2f47;       --tonic-tint: #1b2533;
    --subdominant: #78c991; --subdominant-fill: #1b3524; --subdominant-tint: #1a2a20;
    --dominant: #f2a76b;    --dominant-fill: #3f2817;    --dominant-tint: #30231a;
    --borrowed: #b9a2e2;    --borrowed-fill: #2e2540;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font-text);
  overflow-x: hidden;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { font-family: var(--font-display); margin: 0; }
h2 { font-size: 16px; font-weight: 650; }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 13px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font: 700 18px var(--font-display); margin-right: auto; }
.logo { width: 28px; height: 28px; }
.logo path { fill: none; stroke: var(--accent); stroke-width: 2.4; stroke-linecap: round; }
.logo circle:nth-of-type(1) { fill: var(--tonic); }
.logo circle:nth-of-type(2) { fill: var(--subdominant); }
.logo circle:nth-of-type(3) { fill: var(--dominant); }
.key-button {
  min-height: 44px; padding: 0 14px; border-radius: var(--radius-pill);
  border: 1px solid var(--line); background: var(--surface);
}
.key-button strong { font: 700 17px var(--font-display); }
.kb-label { color: var(--muted); font-size: 13px; }
.play-button {
  width: 48px; height: 48px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--bg); display: grid; place-items: center;
}
.play-button svg { width: 22px; height: 22px; fill: currentColor; }
.play-button .i-stop, body.is-playing .play-button .i-play { display: none; }
body.is-playing .play-button .i-stop { display: block; }

/* ---------- Layout ---------- */
.layout { display: flex; flex-direction: column; gap: 14px; padding: 12px 16px 32px; }
.center { min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.left { min-width: 0; }
.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 14px;
}

/* Phone: dial is a popover under the key button. */
.dial-wrap {
  display: none; position: fixed; z-index: 30; top: 70px; right: 16px; left: 16px;
  margin: 0 auto; max-width: 300px; padding: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.18);
}
body.dial-open .dial-wrap { display: block; }
.left { order: 3; }

@media (min-width: 900px) {
  .key-button { display: none; }
  .layout {
    display: grid; grid-template-columns: 260px minmax(0, 1fr); align-items: start;
    gap: 18px; padding: 18px 20px 40px; max-width: 1500px; margin: 0 auto;
  }
  .left { order: 0; position: sticky; top: 86px; display: flex; flex-direction: column; gap: 14px; }
  .dial-wrap { display: block; position: static; box-shadow: none; max-width: none; padding: 8px; }
}

/* ---------- Dial ---------- */
.dial { width: 100%; height: auto; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
.dial-face { fill: var(--surface-2); }
.dial-inner { fill: var(--surface); }
.dial-hit { fill: transparent; cursor: pointer; }
.dial-key:hover .dial-hit { fill: color-mix(in srgb, var(--accent) 10%, transparent); }
.dial-key.selected .dial-hit { fill: var(--tonic-fill); stroke: var(--tonic); stroke-width: 2; }
.dial-major { font: 700 17px var(--font-display); fill: var(--text); text-anchor: middle; pointer-events: none; }
.dial-minor { font: 500 11px var(--font-text); fill: var(--muted); text-anchor: middle; pointer-events: none; }
.dial-key.selected .dial-major { fill: var(--tonic); }
.dial-pointer { fill: var(--accent); }

/* ---------- Board ---------- */
.board {
  overflow-x: auto; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch;
  margin: 0 -16px; padding: 6px 16px 10px;
  scrollbar-width: thin;
}
@media (min-width: 900px) { .board { margin: 0; padding: 6px 0 10px; } }
.board-inner {
  position: relative; min-width: calc(var(--tile-w) * 7 + var(--gap) * 6);
  max-width: calc(var(--tile-max) * 7 + var(--gap) * 6); margin: 0 auto;
  padding: 0 4px;
}
.routes { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
/* Labels sit at the right end of each gap, where no route crosses
   (vii° has no secondary dominant, and the borrowed row is centered). */
.row-label {
  position: sticky; right: 0; z-index: 2; display: block; width: max-content; margin-left: auto !important;
  background: var(--bg); padding: 0 2px 0 8px; border-radius: 6px;
  font: 600 12px var(--font-text); letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--muted); margin: 6px 0 6px;
}
.row { position: relative; z-index: 1; }
.grid7 { display: grid; grid-template-columns: repeat(7, minmax(var(--tile-w), 1fr)); gap: var(--gap); }
.borrowed-row { display: flex; justify-content: center; gap: var(--gap); }
.borrowed-row .tile { flex: 0 1 calc((100% - var(--gap) * 6) / 7); min-width: var(--tile-w); }
.interchange-gap { height: 22px; }
.tile-gap { min-height: var(--tile-h); }

.tile {
  --c: var(--tonic); --fill: var(--tonic-fill);
  min-height: var(--tile-h); min-width: 44px; padding: 6px 8px;
  border-radius: var(--radius-pill); border: 2px solid transparent;
  background: var(--fill); color: var(--text);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0;
  transition: transform 0.15s ease, opacity 0.15s ease, box-shadow 0.15s ease;
  touch-action: manipulation;
}
.fn-tonic { --c: var(--tonic); --fill: var(--tonic-fill); --tint: var(--tonic-tint); }
.fn-subdominant { --c: var(--subdominant); --fill: var(--subdominant-fill); --tint: var(--subdominant-tint); }
.fn-dominant { --c: var(--dominant); --fill: var(--dominant-fill); --tint: var(--dominant-tint); }
.fn-borrowed { --c: var(--borrowed); --fill: var(--borrowed-fill); --tint: var(--borrowed-fill); }

/* Chord-type outline code (FR26): main plain, secondary dashed, borrowed solid. */
.row-secondary { background: var(--tint); border: 2px dashed var(--c); }
.row-borrowed { border: 2px solid var(--c); }

.c-name { font: 700 17px/1.1 var(--font-display); white-space: nowrap; }
.c-name small { font-size: 12px; font-weight: 600; margin-left: 1px; }
.c-num { font-size: 12px; color: var(--muted); font-weight: 600; }
.c-num-only { font-size: 18px; }

.tile.is-legal { transform: scale(1.04); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 45%, transparent), 0 0 16px color-mix(in srgb, var(--c) 40%, transparent); }
.tile.is-dim { opacity: 0.4; }
.tile.is-dim:hover { opacity: 0.7; }
.tile.is-current { outline: 3px solid var(--c); outline-offset: 3px; }
.tile.is-playing { opacity: 1; transform: scale(1.08); box-shadow: 0 0 0 4px var(--c); }
.tile:active { transform: scale(0.97); }

.route { fill: none; stroke: var(--c); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: 0.28; }
.route.active { opacity: 0.85; }
.route-head { fill: none; stroke: var(--c); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: 0.28; }
.route-head.active { opacity: 0.9; }
.route-flow { fill: none; stroke: var(--surface); stroke-width: 1.6; stroke-dasharray: 3 9; stroke-linecap: round; animation: flow 0.9s linear infinite; }
.route.pulse { animation: pulse 1.1s ease-in-out infinite; }
.station { fill: var(--surface); stroke: var(--c); stroke-width: 2; opacity: 0.5; }
.station.active { opacity: 1; }
@keyframes flow { to { stroke-dashoffset: -24; } }
@keyframes pulse { 50% { stroke-width: 6; opacity: 1; } }

/* ---------- Notice (free mode) ---------- */
.notice { min-height: 0; font-size: 14px; border-radius: 12px; padding: 0 12px; max-height: 0; overflow: hidden; transition: max-height 0.2s ease, padding 0.2s ease; }
.notice.show { max-height: 120px; padding: 10px 12px; }
.notice.warn { background: var(--warn-bg); color: var(--warn-text); }
.notice.info { background: var(--surface-2); }

/* ---------- Progression strip ---------- */
.strip-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.count { color: var(--muted); font-weight: 500; font-size: 13px; margin-left: 4px; }
.ghost { min-height: 40px; padding: 0 14px; border-radius: var(--radius-pill); border: 1px solid var(--line); background: transparent; }
.ghost:disabled { opacity: 0.4; cursor: default; }
.strip {
  list-style: none; margin: 0 -16px; padding: 4px 16px 10px; display: flex; gap: 8px;
  overflow-x: auto; overscroll-behavior-x: contain; min-height: 70px;
}
@media (min-width: 900px) { .strip { margin: 0; padding: 4px 2px 10px; flex-wrap: wrap; overflow: visible; } }
.strip-empty { color: var(--muted); align-self: center; font-size: 14px; }
.chip {
  --c: var(--tonic); --fill: var(--tonic-fill);
  flex: 0 0 auto; position: relative; display: flex; align-items: stretch;
  border-radius: 14px; background: var(--fill); border: 2px solid transparent;
  user-select: none; -webkit-user-select: none; touch-action: pan-x;
}
.chip.row-secondary { background: var(--tint); border: 2px dashed var(--c); }
.chip.row-borrowed { border: 2px solid var(--c); }
.chip-play { border: 0; background: transparent; min-width: 64px; min-height: 52px; padding: 4px 6px 4px 12px; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; }
.chip-del { border: 0; background: transparent; width: 32px; font-size: 18px; color: var(--muted); border-radius: 0 12px 12px 0; }
.chip-del:hover { color: var(--text); background: color-mix(in srgb, var(--c) 15%, transparent); }
.chip.is-playing { box-shadow: 0 0 0 3px var(--c); }
.chip.is-focus { box-shadow: 0 0 0 2px color-mix(in srgb, var(--c) 60%, transparent); }
.chip.dragging { z-index: 5; opacity: 0.85; box-shadow: 0 8px 20px rgb(0 0 0 / 0.2); transition: none; }

.legend { list-style: none; margin: 4px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.sw { width: 26px; height: 14px; border-radius: 7px; background: var(--surface-2); border: 2px solid transparent; }
.sw-secondary { border: 2px dashed var(--muted); }
.sw-borrowed { border: 2px solid var(--muted); }

/* ---------- Controls ---------- */
.transport { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.field input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 32px; }
.field select { width: 100%; min-width: 0; min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); padding: 0 8px; color: var(--text); }
.field output { color: var(--text); font-weight: 650; }
.field-row { display: flex; gap: 12px; align-items: flex-end; }
.field-row > * { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.seg { display: flex; border: 1px solid var(--line); border-radius: var(--radius-pill); padding: 3px; background: var(--surface-2); }
.seg button { flex: 1; min-height: 38px; border: 0; background: transparent; border-radius: var(--radius-pill); font-size: 14px; padding: 0 10px; }
.seg button[aria-checked="true"] { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); font-weight: 650; }
.seg.small button { min-height: 34px; font-size: 13px; }

.drum-controls { display: flex; flex-direction: column; gap: 10px; }
.drum-controls .field-row { flex-direction: column; align-items: stretch; gap: 10px; }
.drum-controls[hidden] { display: none; }
.mixer { border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px 10px; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.mixer legend { font-size: 13px; color: var(--muted); padding: 0 4px; }

/* ---------- Diagram panel ---------- */
.diagram-panel { display: flex; flex-direction: column; gap: 10px; }
.diagram-panel p { margin: 0; }
.dg-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.dg-head h2 { font-size: 20px; }
.dg-board { overflow-x: auto; margin: 0 -6px; }
.dg-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.dg-key { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.dg-key .k { width: 12px; height: 12px; border-radius: 50%; display: inline-block; margin-left: 6px; }
.dg-key .k.root { background: var(--dominant); }
.dg-key .k.tone { background: var(--accent); }
.dg-key .k.scale { border: 2px solid var(--accent); }

.fretboard { display: block; width: 100%; min-width: 520px; height: auto; }
@media (min-width: 900px) { .fretboard { min-width: 0; } }
.fb-string { stroke: var(--muted); stroke-width: 1.2; }
.fb-fret { stroke: var(--line); stroke-width: 2; }
.fb-nut { stroke: var(--text); stroke-width: 4; }
.fb-marker { fill: var(--surface-2); }
.fb-num, .fb-label { font-size: 10px; fill: var(--muted); text-anchor: middle; }
.fb-label { text-anchor: start; font-weight: 600; }
.fb-mute { font-size: 14px; fill: var(--muted); text-anchor: middle; }
.fb-dot circle { stroke-width: 2; }
.fb-dot text { font: 700 9px var(--font-text); text-anchor: middle; pointer-events: none; }
.fb-dot.tone circle { fill: var(--accent); stroke: var(--accent); }
.fb-dot.tone text { fill: var(--bg); }
.fb-dot.root circle { fill: var(--dominant); stroke: var(--dominant); }
.fb-dot.root text { fill: var(--bg); }
.fb-dot.scale circle { fill: var(--surface); stroke: var(--accent); }
.fb-dot.scale text { fill: var(--text); }

@media (prefers-reduced-motion: reduce) {
  .route-flow, .route.pulse { animation: none; }
  .tile { transition: none; }
}
