:root{
    --ink:#17313a;
    --muted:#667a81;
    --teal:#2e7f82;
    --teal-dark:#225f63;
    --line:#9fb8ba;
    --paper:#fbfcfb;
    --panel:#ffffff;
    --cyan:#eaf4f3;
    --green:#eef5eb;
    --pink:#f8ecef;
    --yellow:#fbf5e5;
    --violet:#f1edf6;
    --blue:#edf4f8;
    --hl-bdim-bg:#b8e4d9;
    --hl-bdim-ring:#1f8a76;
    --hl-mid1-bg:#f3dfe6;
    --hl-mid1-ring:#b4778e;
    --hl-mid2-bg:#f8e4c4;
    --hl-mid2-ring:#be7f2d;
    --root:#cf554b;
    --shadow:0 12px 34px rgba(24,55,60,.10);
  }
  *{box-sizing:border-box}
  html{scroll-behavior:smooth}
  body{
    margin:0;
    color:var(--ink);
    background:linear-gradient(180deg,#f7faf9 0,#f4f7f6 100%);
    font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Arial,sans-serif;
    line-height:1.44;
  }
  .app{
    display:grid;
    grid-template-columns:minmax(430px,34vw) minmax(0,1fr);
    gap:24px;
    max-width:1680px;
    margin:0 auto;
    padding:20px;
    align-items:start;
  }
  .map-pane{
    position:sticky;
    top:20px;
    height:calc(100vh - 40px);
    min-height:620px;
    background:rgba(255,255,255,.92);
    border:1px solid #d7e3e1;
    border-radius:18px;
    box-shadow:var(--shadow);
    padding:20px;
    display:flex;
    flex-direction:column;
    justify-content:flex-start;
    overflow:hidden;
  }
  .map-top{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    margin-bottom:14px;
  }
  .map-title{
    font-size:12px;
    letter-spacing:.12em;
    font-weight:800;
    color:var(--teal-dark);
    text-transform:uppercase;
  }

  .map-controls{
    display:flex;
    align-items:center;
    gap:7px;
    margin-left:auto;
  }
  .map-details-toggle{
    width:36px;
    height:36px;
    flex:0 0 36px;
    display:grid;
    place-items:center;
    border:1px solid #aac2c1;
    border-radius:10px;
    background:#fff;
    color:#496b6e;
    cursor:pointer;
    font:800 17px/1 system-ui,sans-serif;
    transition:background .12s ease,border-color .12s ease,color .12s ease;
  }
  .map-details-toggle:hover,.map-details-toggle:focus-visible{
    background:#f2f8f7;
    border-color:#7ea5a4;
    color:var(--teal-dark);
    outline:none;
  }
  .details-chevron{
    width:10px;
    height:10px;
    border-top:2px solid currentColor;
    border-left:2px solid currentColor;
    transform:translateY(2px) rotate(45deg);
    transition:transform .16s ease;
  }
  .map-details-toggle[aria-expanded="false"] .details-chevron{
    transform:translateY(-2px) rotate(225deg);
  }
  .map-details[hidden]{display:none!important}
  .map-pane.details-hidden{height:auto;min-height:0}
  .tonic-control select{
    appearance:none;
    border:1px solid #aac2c1;
    background:#fff;
    border-radius:10px;
    padding:9px 34px 9px 11px;
    font:inherit;
    font-weight:700;
    color:var(--ink);
    cursor:pointer;
    background-image:linear-gradient(45deg,transparent 50%,#617c7e 50%),linear-gradient(135deg,#617c7e 50%,transparent 50%);
    background-position:calc(100% - 15px) 54%,calc(100% - 10px) 54%;
    background-size:5px 5px,5px 5px;
    background-repeat:no-repeat;
  }
  .square-wrap{
    width:100%;
    padding:12px;
    border:1px solid #dbe6e4;
    border-radius:15px;
    background:#fff;
  }
  .square{
    display:grid;
    grid-template-columns:repeat(6,1fr);
    grid-template-rows:repeat(6,minmax(0,1fr));
    width:100%;
    aspect-ratio:1.92/1;
    border:1px solid #86a3a3;
    overflow:hidden;
    background:white;
  }
  .cell{
    position:relative;
    display:flex;
    align-items:center;
    justify-content:center;
    font-size:clamp(17px,1.8vw,28px);
    line-height:1;
    min-height:0;
    font-weight:500;
    color:#18323b;
    border-right:1px solid #d0dddd;
    border-bottom:1px solid #d0dddd;
    transition:background .18s ease,box-shadow .18s ease,color .18s ease,transform .18s ease;
    user-select:none;
  }
  .cell.edge{
    background:#fff8ea;
    color:#718187;
    font-size:clamp(12px,1.15vw,18px);
    font-weight:400;
  }
  .cell.inner{background:#e7f3ef}
  .cell.bold{font-weight:800;color:#142e37}
  .cell.edge.bold{font-weight:400;color:#718187}
  .cell.vheavy{border-right:4px solid #224f56}
  .cell.hmid{border-bottom:2px solid #78979a}
  .cell.hheavy{border-bottom:4px solid #224f56}
  /* Le due colonne B° (sinistra/destra) condividono lo stesso colore. */
  .cell.col-1,.cell.col-4{--hl-bg:var(--hl-bdim-bg);--hl-ring:var(--hl-bdim-ring)}
  .cell.col-2{--hl-bg:var(--hl-mid1-bg);--hl-ring:var(--hl-mid1-ring)}
  .cell.col-3{--hl-bg:var(--hl-mid2-bg);--hl-ring:var(--hl-mid2-ring)}
  .cell.highlight{
    background:linear-gradient(180deg,color-mix(in srgb,var(--hl-bg,#dbece8) 74%,white),var(--hl-bg,#dbece8));
    box-shadow:
      inset 0 0 0 3px var(--hl-ring,#4d9b8c),
      inset 0 0 18px color-mix(in srgb,var(--hl-ring,#4d9b8c) 18%,transparent);
    color:#102f35;
    font-weight:850;
    z-index:2;
  }
  .cell.col-1.highlight,.cell.col-4.highlight{
    background:linear-gradient(180deg,color-mix(in srgb,var(--hl-bg) 62%,white),color-mix(in srgb,var(--hl-bg) 92%,#9fd8ca));
    box-shadow:
      inset 0 0 0 3px var(--hl-ring),
      inset 0 0 0 5px rgba(255,255,255,.38),
      inset 0 0 24px color-mix(in srgb,var(--hl-ring) 28%,transparent);
  }
  .cell.highlight.root-tone{
    box-shadow:
      inset 0 0 0 3px var(--hl-ring,#4d9b8c),
      inset 0 0 0 6px rgba(255,255,255,.72),
      inset 0 0 0 8px rgba(207,85,75,.62),
      inset 0 0 18px color-mix(in srgb,var(--hl-ring,#4d9b8c) 18%,transparent);
  }
  .cell.tonic-text{color:var(--root)}
  /* Quando la tonica del quadrato viene effettivamente suonata dalla teoria,
     il rosso di riferimento lascia spazio al colore della selezione. */
  .cell.highlight.tonic-text{color:#102f35}
  .selected-chord{
    min-height:70px;
    margin-top:15px;
    border:1px solid #d8e5e3;
    border-radius:13px;
    background:#f8fbfa;
    display:flex;
    align-items:center;
    justify-content:center;
    flex-direction:column;
    text-align:center;
    padding:10px 14px;
  }
  .selected-name{
    font-size:27px;
    font-weight:800;
    letter-spacing:-.015em;
    color:var(--teal-dark);
    min-height:34px;
  }
  .selected-hint{font-size:12px;color:var(--muted);margin-top:2px}

  .reference-block{
    margin-top:14px;
    border-top:1px solid #dbe6e4;
    padding-top:11px;
    min-height:0;
  }
  .reference-title{
    font-size:10.5px;
    letter-spacing:.09em;
    text-transform:uppercase;
    color:var(--muted);
    font-weight:800;
    margin:0 0 7px;
  }
  .reference-gallery{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:7px;
    max-height:245px;
    overflow:auto;
    padding-right:3px;
    scrollbar-width:thin;
  }
  .reference-gallery-grid{
    align-items:start;
  }
  .reference-thumb{
    border:1px solid #d7e2e0;
    border-radius:8px;
    background:#fff;
    padding:0;
    overflow:hidden;
    cursor:zoom-in;
    min-width:0;
  }
    .reference-thumb img{
    display:block;
    width:100%;
    height:104px;
    object-fit:contain;
    object-position:center;
    background:#fff;
  }
    .reference-thumb:hover,.reference-thumb:focus-visible{
    outline:none;
    border-color:#8fb8b5;
    box-shadow:0 0 0 2px rgba(46,127,130,.10);
  }
  .image-lightbox{
    position:fixed;
    inset:0;
    z-index:1000;
    display:none;
    align-items:center;
    justify-content:center;
    padding:24px;
    background:rgba(12,29,33,.82);
    backdrop-filter:blur(3px);
  }
  .image-lightbox.open{display:flex}
  .image-lightbox img{
    max-width:min(1500px,96vw);
    max-height:92vh;
    width:auto;
    height:auto;
    background:#fff;
    box-shadow:0 18px 60px rgba(0,0,0,.34);
  }
  .image-lightbox-close{
    position:fixed;
    top:14px;right:18px;
    width:40px;height:40px;
    display:grid;place-items:center;
    border:1px solid rgba(255,255,255,.4);
    border-radius:50%;
    background:rgba(0,0,0,.28);
    color:#fff;
    font:500 28px/1 system-ui,sans-serif;
    cursor:pointer;
  }

  .theory-pane{
    background:rgba(255,255,255,.96);
    border:1px solid #d9e4e2;
    border-radius:18px;
    box-shadow:var(--shadow);
    padding:34px 38px 48px;
    min-width:0;
  }
  .doc-title{
    margin:0 0 24px;
    color:var(--teal);
    font-size:30px;
    line-height:1.05;
    letter-spacing:.02em;
    font-weight:850;
    border-bottom:2px solid #4b8ead;
    padding-bottom:12px;
  }
  h2.section{
    margin:32px 0 13px;
    font-size:24px;
    line-height:1.12;
    letter-spacing:.01em;
    color:#253b42;
  }
  h2.section .n{color:var(--teal);margin-right:8px}
  h3{
    margin:23px 0 9px;
    color:var(--teal);
    font-size:17px;
  }
  p{margin:8px 0 12px}
  .callout{
    border:1.5px solid var(--teal);
    background:#f3f8f3;
    padding:9px 12px;
    margin:10px 0 14px;
    border-radius:2px;
  }
  .callout.warm{background:#fbf7e9}
  .callout p:last-child{margin-bottom:0}


  /* Limature teoriche recenti: stessa struttura, gerarchia visiva più leggibile. */
  .refined-note{
    padding:12px 14px;
    border-left-width:4px;
    line-height:1.48;
  }
  .refined-note.refined-compact{padding-top:10px;padding-bottom:10px}
  .refined-title{
    margin:0 0 7px;
    color:var(--teal-dark);
    font-weight:800;
    letter-spacing:.005em;
  }
  .refined-row + .refined-row{margin-top:6px}
  .refined-label{font-weight:760;color:#29484f}
  .refined-formulas{
    display:flex;
    flex-wrap:wrap;
    gap:6px;
    margin:7px 0 8px;
  }
  .refined-chip{
    display:inline-flex;
    align-items:center;
    min-height:27px;
    padding:4px 8px;
    border:1px solid rgba(46,127,130,.22);
    border-radius:7px;
    background:rgba(255,255,255,.62);
    color:#24464d;
    font-weight:760;
    white-space:nowrap;
  }
  .refined-example{
    margin-top:7px;
    padding:7px 9px;
    border:1px solid rgba(46,127,130,.12);
    border-radius:7px;
    background:rgba(255,255,255,.52);
  }
  .refined-rule{
    margin-top:8px;
    padding-top:8px;
    border-top:1px solid rgba(46,127,130,.22);
    font-weight:650;
  }
  .refined-foot{
    margin-top:7px;
    color:var(--muted);
    font-size:.94em;
  }
  .refined-pair{
    display:grid;
    grid-template-columns:repeat(2,minmax(0,1fr));
    gap:7px;
    margin:7px 0;
  }
  .refined-card{
    padding:7px 9px;
    border:1px solid rgba(46,127,130,.12);
    border-radius:7px;
    background:rgba(255,255,255,.48);
  }
  .refined-divider{
    margin:9px 0 5px;
    padding-top:7px;
    border-top:1px solid rgba(46,127,130,.16);
  }
  .refined-levels .refined-card{background:rgba(255,255,255,.62)}
  .extension-fields-table{table-layout:fixed}
  .extension-fields-table th:nth-child(1){width:31%}
  .extension-fields-table th:nth-child(2){width:12%}
  .extension-fields-table th:nth-child(3),
  .extension-fields-table th:nth-child(4){width:28.5%}
  .extension-fields-table td{vertical-align:top}
  .extension-fields-table td:nth-child(2){white-space:nowrap;text-align:center}
  .table-wrap{overflow-x:visible;max-width:100%;margin:9px 0 16px}

  /* Matrici funzionali del capitolo 2: coppie affiancate come nell'originale. */
  .matrix-pair{
    display:grid;
    grid-template-columns:repeat(2,minmax(0,1fr));
    gap:9px;
    margin:9px 0 16px;
    align-items:start;
    width:100%;
    max-width:100%;
  }
  .matrix-pair .matrix-wrap{
    margin:0;
    overflow:visible;
    min-width:0;
    width:100%;
    max-width:100%;
  }
  .matrix-table{
    width:100%;
    min-width:0;
    max-width:100%;
    table-layout:auto;
    font-size:12px;
    white-space:normal;
  }
  .matrix-table th,.matrix-table td{
    padding:5px 5px;
    text-align:center;
    vertical-align:middle;
  }
  .matrix-table th:first-child,.matrix-table td:first-child{
    width:auto;
    text-align:left;
    font-weight:750;
    padding-left:6px;
    padding-right:6px;
  }
  .matrix-table th{text-transform:none}
  .matrix-dominant-positive tbody td{background:#fbf5e5}
  .matrix-dominant-negative tbody td{background:#f1edf6}
  .matrix-subdominant-positive tbody td{background:#f8ecef}
  .matrix-subdominant-negative tbody td{background:#edf4f8}
  table{
    width:100%;
    min-width:0;
    max-width:100%;
    border-collapse:collapse;
    table-layout:auto;
    font-size:13.5px;
    line-height:1.28;
  }
  th,td{
    border:1px solid #b8c9c9;
    padding:7px 9px;
    vertical-align:top;
    white-space:normal;
    overflow-wrap:anywhere;
    word-break:normal;
  }
  th{background:#dfeeee;color:#34555c;font-weight:800;text-transform:uppercase;font-size:12px}
  tr.green td{background:var(--green)}
  tr.pink td{background:var(--pink)}
  tr.yellow td{background:var(--yellow)}
  tr.violet td{background:var(--violet)}
  tr.blue td{background:var(--blue)}

  .atlas-group-title{
    margin-top:27px;
    padding-top:4px;
    letter-spacing:.015em;
  }
  .atlas-wrap{margin-bottom:19px}
  .atlas-table,.atlas-compact{width:100%;min-width:0;max-width:100%}
  .atlas-category td{
    background:#f1f6f5;
    color:var(--teal-dark);
    font-size:11.5px;
    font-weight:850;
    letter-spacing:.075em;
    text-transform:uppercase;
    text-align:left;
    padding-top:7px;
    padding-bottom:7px;
  }
  .atlas-form{
    width:auto;
    color:#42595f;
    font-weight:400;
    white-space:normal;
  }
  .atlas-table td:not(:first-child),
  .atlas-compact td{
    white-space:normal;
  }


  /* Capitolo 4: le due tabelle riproducono esattamente struttura, colori e gerarchie della revisione Word. */
  .chapter4-map-table,.chapter4-equivalence-table{table-layout:fixed}
  .chapter4-map-table th,.chapter4-map-table td{text-align:center}
  .chapter4-map-table .ch4-area-col{width:7.8%}
  .chapter4-map-table th,
  .chapter4-equivalence-table th{background:#DCEBED;color:#34555c}
  .chapter4-map-table .ch4-area{
    background:#F7F9F9;
    color:#2B7F7A;
    font-weight:800;
  }
  .chapter4-map-table td,
  .chapter4-equivalence-table td{background-clip:padding-box}
  .ch4-peach-light{background:#FDE9D9!important}
  .ch4-peach{background:#FBD4B4!important}
  .ch4-green-light{background:#EAF1DD!important}
  .ch4-green{background:#C2D69B!important}
  .ch4-blue{background:#B6DDE8!important}
  .ch4-violet{background:#CCC0D9!important}
  .chapter4-equivalence-table button.chord{font-weight:400}
  .chapter4-equivalence-table .strong button.chord{font-weight:750}

  .center{text-align:center}
  .small{font-size:12.5px}
  .strong{font-weight:750}
  .italic{font-style:italic}
  .musicline{font-variant-numeric:tabular-nums}
  button.chord{
    all:unset;
    display:inline;
    white-space:normal;
    overflow-wrap:anywhere;
    word-break:normal;
    box-decoration-break:clone;
    -webkit-box-decoration-break:clone;
    color:#174f56;
    font-weight:750;
    cursor:pointer;
    border-radius:4px;
    padding:0 1px;
    border-bottom:1px dotted #4a888c;
    transition:background .12s ease,color .12s ease;
  }
  button.chord:hover,button.chord:focus-visible{background:#e1f1ee;color:#0b5558;outline:none}
  button.chord.active{background:#cde8e3;color:#0b4a4e}
  /* Nell'atlante, le colonne descrittive/di partenza restano visivamente leggere. */
  .atlas-lead-note .pitch{font-weight:400}
  .atlas-lead-chord button.chord{font-weight:750}
  .pitch{font-weight:650;color:#344d54}
  .procedure td:first-child{width:34px;text-align:center;font-weight:800}
  .procedure td:nth-child(2){width:205px;font-weight:800}

  /* Capitolo 8: colori e proporzioni ripresi esattamente dalle due tabelle della revisione. */
  .cap8-procedure,.cap8-mother{table-layout:fixed}
  .cap8-procedure td,.cap8-mother th,.cap8-mother td{border-color:#d9d9d9}
  .cap8-procedure td:first-child{width:26.62%}
  .cap8-procedure td:nth-child(2){width:73.38%}
  .cap8-mother th:nth-child(1),.cap8-mother td:nth-child(1){width:24.66%}
  .cap8-mother th:nth-child(2),.cap8-mother td:nth-child(2){width:30.82%}
  .cap8-mother th:nth-child(3),.cap8-mother td:nth-child(3){width:44.52%}
  .cap8-procedure .cap8-header-row td,.cap8-mother thead th{background:#dcebed}
  .cap8-procedure .cap8-neutral td,.cap8-mother .cap8-neutral td{background:#f7f9f9}
  .cap8-procedure .cap8-green td,.cap8-mother .cap8-green td{background:#eaf4eb}
  .cap8-procedure .cap8-pink td,.cap8-mother .cap8-pink td{background:#f8e9ed}
  .cap8-procedure .cap8-violet td,.cap8-mother .cap8-violet td{background:#f1edf8}
  .visuals{
    margin-top:24px;
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:12px;
  }
  .visuals img{width:100%;border:1px solid #d9e4e2;background:#fff}
  .footer-note{font-size:11px;color:#7a898e;margin-top:18px}
  @media (max-width:1050px){
    .app{grid-template-columns:1fr;padding:12px}
    .map-pane{position:relative;top:auto;height:auto;min-height:0;padding:16px}
    .theory-pane{padding:25px 20px 40px}
    .square{aspect-ratio:1.92/1}
  }
  @media (max-width:620px){
    .map-top{align-items:flex-start;flex-direction:column}
    .tonic-control{width:100%}.tonic-control select{width:100%}
    .square-wrap{padding:7px}
    .cell{font-size:16px}
    .selected-name{font-size:22px}
    .reference-gallery{max-height:none}
    .reference-thumb img{height:auto;object-fit:contain}
    .doc-title{font-size:25px}
    h2.section{font-size:21px}
    .visuals{grid-template-columns:1fr}
  }


.map-details{min-height:0}

/* Player file MIDI */
.midi-file-toggle{
  width:36px;
  flex-basis:36px;
  font-size:15px;
  letter-spacing:0;
}
.midi-file-panel{
  display:flex;
  flex-wrap:wrap;
  align-items:center;
  gap:6px;
  margin:-3px 0 11px;
  padding:8px;
  border:1px solid #d6e3e1;
  border-radius:11px;
  background:#f8fbfa;
}
.midi-file-panel[hidden]{display:none!important}
.midi-file-control{
  min-width:34px;
  height:30px;
  border:1px solid #b8cecc;
  border-radius:8px;
  background:#fff;
  color:#355f63;
  padding:0 9px;
  font:700 10px/1 system-ui,sans-serif;
  white-space:nowrap;
  cursor:pointer;
  transition:background .12s ease,border-color .12s ease,color .12s ease,opacity .12s ease;
}
.midi-file-control:hover,.midi-file-control:focus-visible{
  background:#eef6f4;
  border-color:#7fa7a3;
  outline:none;
}
.midi-file-control:disabled{opacity:.38;cursor:default}
.midi-file-load{min-width:34px;width:34px;padding:0}
.midi-file-wide{min-width:48px}

.midi-load-icon{
  position:relative;
  display:block;
  width:13px;
  height:13px;
  margin:auto;
}
.midi-load-icon::before{
  content:"";
  position:absolute;
  left:5.5px;
  top:0;
  width:2px;
  height:8px;
  border-radius:2px;
  background:currentColor;
}
.midi-load-icon::after{
  content:"";
  position:absolute;
  left:2px;
  top:4px;
  width:7px;
  height:7px;
  border-left:2px solid currentColor;
  border-bottom:2px solid currentColor;
  transform:rotate(-45deg);
  transform-origin:center;
}
.midi-file-load{position:relative}
.midi-file-load::after{
  content:"";
  position:absolute;
  left:7px;
  right:7px;
  bottom:5px;
  height:2px;
  border-radius:2px;
  background:currentColor;
  opacity:.75;
}
.midi-file-playpause{min-width:34px;width:34px;padding:0}
.midi-playpause-icon{
  position:relative;
  display:block;
  width:12px;
  height:12px;
  margin:auto;
}
.midi-playpause-icon::before{
  content:"";
  position:absolute;
  left:2px;
  top:1px;
  width:0;
  height:0;
  border-top:5px solid transparent;
  border-bottom:5px solid transparent;
  border-left:8px solid currentColor;
}
.midi-file-playpause.is-playing .midi-playpause-icon::before,
.midi-file-playpause.is-playing .midi-playpause-icon::after{
  content:"";
  position:absolute;
  top:1px;
  bottom:1px;
  width:3px;
  height:auto;
  border:0;
  border-radius:1px;
  background:currentColor;
}
.midi-file-playpause.is-playing .midi-playpause-icon::before{left:2px}
.midi-file-playpause.is-playing .midi-playpause-icon::after{right:2px}
.midi-file-status{
  flex:1 1 100%;
  min-width:0;
  color:#6a7f82;
  font-size:10px;
  line-height:1.25;
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

/* MIDI */
.midi-file-toggle,.midi-toggle,.neg-toggle{
  width:46px;
  height:36px;
  flex:0 0 46px;
  display:grid;
  place-items:center;
  border:1px solid #aac2c1;
  border-radius:10px;
  background:#fff;
  color:#496b6e;
  padding:0;
  font:800 10px/1 system-ui,sans-serif;
  letter-spacing:.055em;
  cursor:pointer;
  transition:background .12s ease,border-color .12s ease,color .12s ease,box-shadow .12s ease;
}
.midi-file-toggle:hover,.midi-file-toggle:focus-visible,.midi-toggle:hover,.midi-toggle:focus-visible,.neg-toggle:hover,.neg-toggle:focus-visible{
  background:#f2f8f7;
  border-color:#7ea5a4;
  color:var(--teal-dark);
  outline:none;
}
.midi-file-toggle.is-active,.midi-toggle.is-active,.neg-toggle.is-active{
  background:#e5f3ef;
  border-color:#5d9992;
  color:#1f6f68;
  box-shadow:inset 0 0 0 1px rgba(31,138,118,.12);
}
.midi-toggle:disabled{opacity:.55;cursor:default}
.midi-file-toggle{width:36px;flex-basis:36px;font-size:15px;letter-spacing:0}


.cell.negative-tone{
  position:relative;
  color:#3f6f9b;
}
.cell.highlight.negative-tone,
.cell.midi-highlight.negative-tone{
  color:#2f5f8f;
}
.cell.edge.negative-tone{
  color:#617e96;
}
.cell.negative-tone::after{
  content:"";
  position:absolute;
  inset:14%;
  border:1.5px dashed rgba(73,113,177,.62);
  border-radius:6px;
  pointer-events:none;
}
.cell.highlight.negative-tone::after,
.cell.midi-highlight.negative-tone::after{
  border-color:rgba(65,107,175,.76);
}
.cell.edge.negative-tone::after{
  inset:17%;
  border-color:rgba(96,129,175,.54);
}

/* MIDI: usa esattamente la stessa codifica cromatica per colonna della selezione normale. */
.cell.midi-highlight:not(.highlight){
  background:linear-gradient(180deg,color-mix(in srgb,var(--hl-bg,#dbece8) 74%,white),var(--hl-bg,#dbece8));
  box-shadow:
    inset 0 0 0 3px var(--hl-ring,#4d9b8c),
    inset 0 0 18px color-mix(in srgb,var(--hl-ring,#4d9b8c) 18%,transparent);
  color:#102f35;
  font-weight:850;
  z-index:2;
}
.cell.col-1.midi-highlight:not(.highlight),.cell.col-4.midi-highlight:not(.highlight){
  background:linear-gradient(180deg,color-mix(in srgb,var(--hl-bg) 62%,white),color-mix(in srgb,var(--hl-bg) 92%,#9fd8ca));
  box-shadow:
    inset 0 0 0 3px var(--hl-ring),
    inset 0 0 0 5px rgba(255,255,255,.38),
    inset 0 0 24px color-mix(in srgb,var(--hl-ring) 28%,transparent);
}

/* Dal secondo tasto MIDI in poi, la nota più bassa riceve lo stesso
   bordo aggiuntivo usato per la fondamentale degli accordi della teoria. */
.cell.midi-highlight.root-tone:not(.highlight){
  box-shadow:
    inset 0 0 0 3px var(--hl-ring,#4d9b8c),
    inset 0 0 0 6px rgba(255,255,255,.72),
    inset 0 0 0 8px rgba(207,85,75,.62),
    inset 0 0 18px color-mix(in srgb,var(--hl-ring,#4d9b8c) 18%,transparent);
}
.cell.midi-highlight::after{display:none}

/* Le due colonne esterne sono ripetizioni dello stesso bordo periodico:
   hanno quindi esattamente lo stesso colore sia a riposo sia evidenziate. */
.cell.col-0.edge,
.cell.col-5.edge{
  background:#fff8ea;
  color:#718187;
}
.cell.edge.highlight,
.cell.edge.midi-highlight:not(.highlight){
  color:#4f686c;
  font-weight:520;
  z-index:1;
}
.cell.col-0.edge.highlight,
.cell.col-5.edge.highlight,
.cell.col-0.edge.midi-highlight:not(.highlight),
.cell.col-5.edge.midi-highlight:not(.highlight){
  background:linear-gradient(180deg,#fbf3e3,#f5ead2);
  box-shadow:inset 0 0 0 1px rgba(172,144,89,.24),
             inset 0 0 10px rgba(172,144,89,.08);
}
.cell.edge.highlight.root-tone{
  box-shadow:inset 0 0 0 1px rgba(172,144,89,.26),
             inset 0 0 0 2px rgba(207,85,75,.13),
             inset 0 0 10px rgba(172,144,89,.08);
}
.cell.edge.midi-highlight.root-tone:not(.highlight){
  box-shadow:inset 0 0 0 1px rgba(172,144,89,.26),
             inset 0 0 0 2px rgba(207,85,75,.22),
             inset 0 0 0 4px rgba(255,255,255,.42),
             inset 0 0 10px rgba(172,144,89,.08);
}

/* Desktop: stessa leggibilità del mobile, ma con caratteri meno grandi e meno pesanti. */
@media (min-width:621px){
  .cell{font-size:clamp(15px,1.30vw,21px);font-weight:480}
  .cell.edge{font-size:clamp(11px,.95vw,15px);font-weight:400}
  .cell.bold{font-weight:700}
  .cell.highlight,.cell.midi-highlight:not(.highlight){font-weight:760}
}

@media (max-width:1050px){
  .map-pane{overflow:visible}
  .reference-gallery{max-height:none}
}

/* Versione mobile: quadrato leggibile, controlli compatti, teoria e tabelle scorrevoli. */
@media (max-width:620px){
  body{background:#f6f9f8}
  .app{padding:8px;gap:10px}
  .map-pane{
    position:sticky;
    top:max(4px,env(safe-area-inset-top));
    z-index:40;
    height:auto;
    min-height:0;
    max-height:calc(100svh - max(8px,env(safe-area-inset-top)));
    overflow:auto;
    overscroll-behavior:contain;
    padding:12px;
    border-radius:14px;
    box-shadow:0 7px 22px rgba(24,55,60,.08);
  }
  .map-top{
    align-items:center;
    flex-direction:row;
    gap:10px;
    margin-bottom:10px;
  }
  .map-top{
    position:sticky;
    top:0;
    z-index:4;
    background:rgba(255,255,255,.98);
  }
  .square-wrap{
    position:sticky;
    top:42px;
    z-index:3;
    background:#fff;
  }
  .map-title{font-size:11px;white-space:nowrap}
  .tonic-control{width:auto;margin-left:auto}
  .square-wrap{padding:5px;border-radius:11px}
  .square{aspect-ratio:1.82/1}
  .cell{font-size:clamp(13px,4.1vw,16px)}
  .cell.edge{font-size:clamp(10px,3.2vw,13px)}
  .cell.vheavy{border-right-width:3px}
  .cell.hmid{border-bottom-width:2px}
  .cell.hheavy{border-bottom-width:3px}
  .midi-toggle,.neg-toggle{width:43px;height:34px;flex-basis:43px;font-size:9.5px;padding:0}
  .midi-file-toggle{width:34px;height:34px;flex-basis:34px;font-size:14px;padding:0}
  .midi-file-panel{gap:5px;padding:7px;margin:-2px 0 9px}
  .midi-file-control{height:29px;padding:0 7px;font-size:9.5px}
  .midi-file-load,.midi-file-playpause{min-width:32px;width:32px;padding:0}
  .midi-file-status{font-size:9.5px}
  .selected-chord{
    min-height:56px;
    margin-top:8px;
    padding:7px 10px;
  }
  .selected-name{font-size:20px;min-height:27px}
  .selected-hint{font-size:11px}
  .reference-block{margin-top:10px;padding-top:9px}
  .reference-title{margin-bottom:6px}
  .reference-gallery{
    display:grid;
    grid-auto-flow:column;
    grid-auto-columns:minmax(68%,1fr);
    grid-template-columns:none;
    gap:7px;
    overflow-x:auto;
    overflow-y:hidden;
    overscroll-behavior-inline:contain;
    scroll-snap-type:x proximity;
    padding:0 0 5px;
  }
  .reference-thumb{grid-column:auto;scroll-snap-align:start}
  .reference-thumb img{height:92px;object-fit:contain}
  .theory-pane{
    padding:20px 14px 34px;
    border-radius:14px;
    box-shadow:0 7px 22px rgba(24,55,60,.07);
  }
  .doc-title{font-size:23px;margin-bottom:20px;padding-bottom:9px}
  h2.section{font-size:19px;margin-top:27px}
  h3{font-size:16px}
  p,.callout{font-size:14px}
  .callout{padding:8px 10px}
  .table-wrap{
    margin-left:-2px;
    margin-right:-2px;
    padding-bottom:2px;
    overflow-x:visible;
  }
  table{width:100%;min-width:0;max-width:100%;font-size:12.5px}
  .atlas-table,.atlas-compact{width:100%;min-width:0;max-width:100%}
  th,td{padding:6px 7px}
  button.chord{padding:2px 1px}
  .image-lightbox{padding:8px}
  .image-lightbox img{max-width:98vw;max-height:88vh}
}


/* Tablet/touch: stesso comportamento strutturale della versione mobile.
   Il quadrato resta visibile e i dettagli partono chiusi via JavaScript. */
@media (min-width:621px) and (max-width:900px), (pointer:coarse) and (min-width:621px) and (max-width:1100px){
  .map-pane{
    position:sticky;
    top:max(4px,env(safe-area-inset-top));
    z-index:40;
    height:auto;
    min-height:0;
    max-height:calc(100svh - max(8px,env(safe-area-inset-top)));
    overflow:auto;
    overscroll-behavior:contain;
  }
  .map-top{
    position:sticky;
    top:0;
    z-index:4;
    background:rgba(255,255,255,.98);
  }
  .square-wrap{
    position:sticky;
    top:46px;
    z-index:3;
    background:#fff;
  }
  /* Su tablet il quadrato occupa più spazio fisico: le note possono essere
     leggermente più grandi senza alterare la versione desktop o telefono. */
  .cell{font-size:clamp(17px,2.25vw,22px)}
  .cell.edge{font-size:clamp(12.5px,1.55vw,16px)}
}

@media (max-width:390px){
  .app{padding:5px}
  .map-pane{padding:9px}
  .square{aspect-ratio:1.72/1}
  .cell{font-size:12px}
  .cell.edge{font-size:10px}
}

@media (max-width:900px){
  .matrix-pair{grid-template-columns:1fr}
}

.neg-toggle{
  text-transform:none!important;
  visibility:visible!important;
  opacity:1;
}
.neg-toggle.is-active{
  background:#edf2fb;
  border-color:#8099bd;
  color:#365f92;
  box-shadow:inset 0 0 0 1px rgba(54,95,146,.10);
}


@media (max-width:700px){
  .refined-note{padding:10px 11px;line-height:1.44}
  .extension-fields-table{font-size:11.5px}
  .extension-fields-table th,.extension-fields-table td{padding:5px 4px}
  .refined-pair{grid-template-columns:1fr;gap:6px}
  .refined-formulas{gap:5px}
  .refined-chip{white-space:normal}
}
