:root {
    --bg: #0c0d11;
    --bg2: #111218;
    --panel: #16171e;
    --panel2: #1e2029;
    --panel3: #282b37;
    --line: rgba(255, 255, 255, .06);
    --line2: rgba(255, 255, 255, .11);
    --text: #eeece7;
    --muted: #8f919b;
    --accent: #e4b55a;
    --accent2: #f3cf86;
    --ink: #1b1406;
    --root: #f2545b;
    --chord: #4f8ff7;
    --scale: #16b981;
    --detect: #f5a524;
    --danger: #ef4444;
    --h: 38px;
    --r: 11px;
    color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
    overscroll-behavior: none;
}

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

button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

#app {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Barre du haut ---------- */
.bar {
    display: grid;
    gap: 8px 10px;
    padding: 8px 12px;
    background: linear-gradient(180deg, var(--panel), var(--bg2));
    border-bottom: 1px solid var(--line);
    align-items: center;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-areas: "brand tools" "modes modes" "ctx ctx";
    z-index: 5;
}

.brand {
    grid-area: brand;
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
    padding: 0;
    border: 0;
    background: none;
    border-radius: 9px;
    justify-self: start;
}
.brand .logo, .float-logo .logo { transition: transform .15s; }
.brand:hover .logo, .float-logo:hover .logo { transform: scale(1.06); }
.brand:active .logo, .float-logo:active .logo { transform: scale(.94); }

/* Menu masqué : seul le manche reste, avec le logo pour réafficher le menu */
.float-logo {
    position: absolute;
    top: 8px;
    left: 8px;
    display: none;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 9px;
    background: none;
    z-index: 4;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .5);
}
.float-logo .logo { width: 40px; height: 40px; }
body.menu-hidden .float-logo { display: block; }
body.menu-hidden .bar, body.menu-hidden .results, body.menu-hidden .status { display: none !important; }
.logo { width: 30px; height: 30px; flex: none; stroke: none; }
.brand-name { font-size: 15px; letter-spacing: .2px; white-space: nowrap; color: var(--muted); }
.brand-name b { color: var(--text); }
.modes { grid-area: modes; }
.ctx { grid-area: ctx; min-width: 0; }
.tools { grid-area: tools; display: flex; gap: 6px; justify-self: end; align-items: center; min-width: 0; }

@media (max-width: 559px) {
    .brand-name { display: none; }
    body[data-mode="free"] .ctx { display: none; }
}

@media (min-width: 760px) {
    .bar {
        grid-template-columns: auto auto minmax(0, 1fr);
        grid-template-areas: "brand modes tools" "ctx ctx ctx";
    }
    .modes { justify-self: start; }
}

@media (min-width: 1280px) {
    .bar {
        grid-template-columns: auto auto minmax(0, 1fr) auto;
        grid-template-areas: "brand modes ctx tools";
    }
}

/* Téléphone à l'horizontale : une seule ligne */
@media (max-height: 520px) and (orientation: landscape) {
    .bar {
        padding: 6px 10px;
        grid-template-columns: auto auto minmax(0, 1fr) auto;
        grid-template-areas: "brand modes ctx tools";
    }
    .brand-name, .status { display: none !important; }
    body[data-mode="free"] .ctx { display: block; }
    .modes button { padding: 0 10px; }
}

/* ---------- Contrôles ---------- */
.segmented {
    display: inline-flex;
    padding: 3px;
    gap: 2px;
    background: var(--panel2);
    border: 1px solid var(--line2);
    border-radius: 12px;
    min-width: 0;
}
.segmented button {
    flex: 1 1 0;
    height: 32px;
    padding: 0 14px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--muted);
    font-weight: 600;
    white-space: nowrap;
    transition: background .15s, color .15s;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-selected="true"] {
    background: var(--accent);
    color: var(--ink);
    box-shadow: 0 1px 6px rgba(228, 181, 90, .35);
}
.segmented.full { display: flex; width: 100%; }
.segmented.full button { padding: 0 6px; }

@media (max-width: 759px) {
    .modes { display: flex; width: 100%; }
    .modes button { padding: 0 4px; }
}

.ctx { overflow-x: auto; scrollbar-width: none; }
.ctx::-webkit-scrollbar { display: none; }
.ctx-pane { display: none; gap: 6px; align-items: center; min-height: var(--h); }
.ctx-pane.on { display: flex; }

.sel, .input {
    height: var(--h);
    border-radius: 10px;
    border: 1px solid var(--line2);
    background-color: var(--panel2);
    color: var(--text);
    padding: 0 10px;
    min-width: 0;
    outline: none;
    transition: border-color .15s;
}
.sel {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 28px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238f919b' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 14px;
    text-overflow: ellipsis;
    cursor: pointer;
}
.sel:hover, .input:hover { border-color: rgba(255, 255, 255, .2); }
.sel:focus, .input:focus { border-color: var(--accent); }
.sel option { background: var(--panel2); color: var(--text); }
.sel-root { width: 88px; flex: none; font-weight: 700; }
.sel-genre { width: 138px; flex: none; }
.sel-grow { flex: 1 1 auto; min-width: 140px; max-width: 260px; }
.sel-sound { width: 150px; }
@media (max-width: 559px) {
    .sel-sound { width: auto; flex: 1 1 auto; max-width: 150px; }
    .sel-genre { width: 116px; }
    .sel-root { width: 80px; }
}

.icon-btn {
    width: var(--h);
    height: var(--h);
    flex: none;
    display: grid;
    place-items: center;
    border-radius: 10px;
    border: 1px solid var(--line2);
    background: var(--panel2);
    color: var(--text);
    transition: background .15s, color .15s, border-color .15s;
}
.icon-btn:hover { background: var(--panel3); }
.icon-btn.toggle[aria-pressed="true"] { background: var(--accent); color: var(--ink); border-color: var(--accent); }
.icon-btn.stop { color: #ff8a8a; }
.icon-btn.stop:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.chip {
    height: 32px;
    padding: 0 13px;
    border-radius: 999px;
    border: 1px solid var(--line2);
    background: var(--panel2);
    color: var(--muted);
    font-weight: 600;
    font-size: 13px;
    white-space: nowrap;
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.chip:hover { color: var(--text); }
.chip.toggle[aria-pressed="true"] { color: var(--accent2); border-color: rgba(228, 181, 90, .55); background: rgba(228, 181, 90, .1); }
.chip.warn { color: #fbbf77; border-color: rgba(245, 165, 36, .45); background: rgba(245, 165, 36, .08); }

.hint { color: var(--muted); font-size: 12.5px; white-space: nowrap; padding: 0 4px; }

.det-chips { display: flex; gap: 5px; align-items: center; }
.det-note {
    height: 30px;
    min-width: 30px;
    padding: 0 10px;
    border-radius: 999px;
    border: 0;
    background: var(--detect);
    color: var(--ink);
    font-weight: 700;
    white-space: nowrap;
}
.det-note:hover { filter: brightness(1.1); text-decoration: line-through; }

/* ---------- Résultats du détecteur ---------- */
.results {
    display: flex;
    gap: 6px;
    padding: 7px 12px;
    overflow-x: auto;
    background: var(--bg2);
    border-bottom: 1px solid var(--line);
    scrollbar-width: thin;
    min-height: 48px;
    align-items: center;
}
.result {
    flex: none;
    display: flex;
    align-items: baseline;
    gap: 7px;
    height: 34px;
    padding: 0 13px;
    border-radius: 10px;
    border: 1px solid var(--line2);
    background: var(--panel2);
    white-space: nowrap;
    transition: border-color .15s, background .15s;
    line-height: 32px;
}
.result:hover { border-color: var(--scale); background: rgba(22, 185, 129, .08); }
.result b { color: var(--accent2); }
.result small { color: var(--muted); }
.results .empty { color: var(--muted); font-size: 13px; white-space: nowrap; }

/* ---------- Scène ---------- */
.stage {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    background:
        radial-gradient(ellipse 80% 70% at 50% 45%, #1a1c25 0%, rgba(12, 13, 17, 0) 70%),
        var(--bg);
}
#board {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    cursor: pointer;
}

.bubble {
    position: absolute;
    left: 0;
    top: 0;
    pointer-events: none;
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 7px 12px;
    border-radius: 12px;
    background: rgba(18, 19, 25, .92);
    border: 1px solid var(--line2);
    box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
    white-space: nowrap;
    opacity: 0;
    transition: opacity .18s;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 3;
}
.bubble.show { opacity: 1; }
.bubble b { font-size: 18px; color: var(--accent2); }
.bubble b sup { font-size: 11px; margin-left: 1px; }
.bubble b small { font-size: 11px; color: var(--muted); margin-left: 1px; }
.bubble span { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12.5px; }
.bubble em { font-style: normal; font-size: 12px; color: #9ad7bf; font-variant-numeric: tabular-nums; }

.float-tools { position: absolute; top: 10px; right: 10px; display: none; gap: 6px; z-index: 4; }
.float-tools .icon-btn { background: rgba(30, 32, 41, .8); backdrop-filter: blur(6px); }
body.focus .float-tools { display: flex; }
body.focus .bar, body.focus .results, body.focus .status { display: none !important; }

/* ---------- Barre d'état ---------- */
.status {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 34px;
    padding: 4px 12px;
    background: var(--bg2);
    border-top: 1px solid var(--line);
    font-size: 12.5px;
    color: var(--muted);
}
.preset-chip {
    display: flex;
    gap: 8px;
    align-items: baseline;
    min-width: 0;
    padding: 4px 8px;
    margin-left: -8px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.preset-chip:hover { background: var(--panel2); }
.preset-chip b { color: var(--text); font-weight: 600; }
.preset-chip span { overflow: hidden; text-overflow: ellipsis; }
.legend { display: flex; gap: 12px; flex: none; }
.legend .lg { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.legend i { width: 10px; height: 10px; border-radius: 50%; background: var(--c); display: inline-block; }
.legend i.ring { background: transparent; border: 2px dashed var(--c); }
@media (max-width: 559px) {
    .legend { gap: 8px; }
    .legend .lg span { display: none; }
    .legend .lg { gap: 0; }
}

/* ---------- Panneau réglages ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .5); z-index: 40; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }

.sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 88dvh;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    background: var(--panel);
    border-top: 1px solid var(--line2);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -12px 40px rgba(0, 0, 0, .5);
    transform: translateY(105%);
    visibility: hidden;
    transition: transform .28s cubic-bezier(.2, .8, .2, 1), visibility 0s linear .28s;
    z-index: 50;
    padding-bottom: env(safe-area-inset-bottom);
    user-select: text;
}
.sheet.open { transform: none; visibility: visible; transition: transform .28s cubic-bezier(.2, .8, .2, 1); }
@media (min-width: 760px) {
    .sheet {
        top: 0;
        left: auto;
        width: 380px;
        max-height: none;
        border-radius: 0;
        border-top: 0;
        border-left: 1px solid var(--line2);
        transform: translateX(105%);
        padding-top: env(safe-area-inset-top);
    }
    .sheet .grip { display: none; }
}

.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; position: relative; }
.sheet-head h2 { margin: 0; font-size: 17px; }
.grip { position: absolute; top: 6px; left: 50%; width: 38px; height: 4px; margin-left: -19px; border-radius: 2px; background: var(--line2); }
.sheet-body { overflow-y: auto; padding: 4px 16px 20px; overscroll-behavior: contain; }
.sheet section { padding: 12px 0 6px; border-bottom: 1px solid var(--line); }
.sheet section:last-of-type { border-bottom: 0; }
.sheet h3 { margin: 0 0 10px; font-size: 11px; letter-spacing: 1.2px; text-transform: uppercase; color: var(--accent); }

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; color: var(--muted); }
.field .sel, .field .input { width: 100%; }
.help { color: var(--muted); font-size: 12px; line-height: 1.4; }
.help.error { color: #ff8a8a; }
.input.error { border-color: var(--danger); }

.stepper { display: flex; gap: 6px; }
.stepper .input { flex: 1; text-align: center; font-variant-numeric: tabular-nums; }
.stepper button { width: 44px; height: var(--h); border-radius: 10px; border: 1px solid var(--line2); background: var(--panel2); font-size: 18px; }
.stepper button:hover { background: var(--panel3); }

.switch-row {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-between;
    min-height: 44px;
    padding: 0;
    border: 0;
    background: none;
    text-align: left;
}
.switch { width: 42px; height: 24px; border-radius: 12px; background: var(--panel3); position: relative; transition: background .2s; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #cfd0d6; transition: transform .2s, background .2s; }
.switch-row[aria-pressed="true"] .switch { background: var(--accent); }
.switch-row[aria-pressed="true"] .switch::after { transform: translateX(18px); background: #fff; }

input[type="range"] { width: 100%; accent-color: var(--accent); height: 28px; }

.install-btn {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 44px;
    margin-top: 16px;
    border: 0;
    border-radius: 12px;
    background: var(--accent);
    color: var(--ink);
    font-weight: 700;
}
.install-btn:hover { filter: brightness(1.08); }

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(16px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 14px;
    max-width: calc(100vw - 24px);
    padding: 8px 8px 8px 16px;
    border-radius: 14px;
    border: 1px solid var(--line2);
    background: var(--panel2);
    box-shadow: 0 10px 30px rgba(0, 0, 0, .55);
    white-space: nowrap;
    z-index: 60;
    animation: toast-in .3s cubic-bezier(.2, .8, .2, 1);
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 12px); } }
.toast button {
    height: 34px;
    padding: 0 14px;
    border: 0;
    border-radius: 9px;
    background: var(--accent);
    color: var(--ink);
    font-weight: 700;
}

.about { color: var(--muted); font-size: 12px; margin: 16px 0 0; text-align: center; }
.about a { color: var(--accent); }

/* Évite le zoom automatique d'iOS sur les champs */
@media (pointer: coarse) {
    .sel, .input { font-size: 16px; }
    .icon-btn { --h: 40px; }
}
