:root {
    --bg: #0f1116;
    --panel: #161a22;
    --ink: #e6e7ea;
    --muted: #9aa3b2;
    --accent: #7cc4ff;
    --good: #39d353;
    --bad: #ff6b6b;
}

html,
body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
}

.wrap {
    height: calc(100vh - 200px);
    display: grid;
    grid-template-columns: 280px 1fr 380px;
    gap: 16px;
    padding-left: 16px;
    padding-right: 16px;
    padding-top: 136px;
}

.panel {
    background: var(--panel);
    border: 1px solid #222836;
    border-radius: 12px;
    box-shadow: 0 1px 0 #0a0c12 inset;
}

.hud {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    padding: 12px 12px 8px;
    align-items: center;
}

.hud .pill {
    background: #0d1119;
    padding: 8px 12px;
    border-radius: 10px;
    border: 1px solid #232a3a;
    min-width: 120px;
}

.hud .pill .k {
    display: block;
    color: var(--muted);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .06em;
}

.hud .pill .v {
    display: block;
    font-weight: 700;
    font-size: 16px;
}

.hud .controls {
    margin-left: auto;
    display: flex;
    gap: 8px;
    align-items: center;
}

button {
    background: #0d1119;
    color: var(--ink);
    border: 1px solid #2a3244;
    padding: 8px 12px;
    border-radius: 10px;
    cursor: pointer;
}

button:hover {
    border-color: #3a4660;
}

/* Gear button */
.gear {
    width: 36px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 1px solid #2a3244;
    background: #0d1119;
    cursor: pointer;
    font-size: 18px;
}

/* default: no animation */
.gear.spin {
    animation: none;
}

/* spin on hover (mouse) and focus-visible (keyboard) */
.gear.spin:hover,
.gear.spin:focus-visible {
    animation: spin 4s linear infinite;
}

/* accessibility: respect reduced motion */
@media (prefers-reduced-motion: reduce) {

    .gear.spin:hover,
    .gear.spin:focus-visible {
        animation: none;
    }
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.main {
    padding: 12px;
    display: grid;
    grid-template-rows: auto 1fr;
    gap: 12px;
}

canvas {
    width: 100%;
    height: 460px;
    background: #0b0e14;
    border: 1px solid #1e2432;
    border-radius: 12px;
    touch-action: none;
}

.side {
    display: grid;
    grid-template-rows: auto auto;
}

.side .instr {
    padding: 14px;
    border-bottom: 1px solid #222836;
}

.side h3 {
    margin: 0 0 8px;
    font-size: 16px;
}

/* Nice formatted instructions (no textarea) */
.instrBody {
    margin: 0 14px 14px;
    padding: 12px;
    background: #0b0e14;
    border: 1px solid #1e2432;
    border-radius: 8px;
}

.instrBody p {
    margin: 0 0 8px;
}

.instrBody ul {
    margin: 6px 0 0 20px;
    padding: 0;
}

.muted {
    color: var(--muted);
}

.log {
    padding: 10px 14px 14px;
}

.log h4 {
    margin: 0 0 8px;
    font-size: 14px;
    color: var(--muted);
    font-weight: 600;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 8pt;
    table-layout: fixed;
}

th,
td {
    padding: 6px 8px;
    border-bottom: 1px solid #1f2533;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

th {
    background: #121723;
    text-align: left;
    color: var(--muted);
    font-weight: 600;
}

tr {
    height: 24px;
}

.good {
    color: var(--good);
}

.bad {
    color: var(--bad);
}

/* Fixed-height viewport; we also trim to 5 rows in JS */
.logViewport {
    border: 1px solid #1e2432;
    border-radius: 8px;
    overflow: hidden;
    height: 180px;
}

/* Modal overlay */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    display: none;
    z-index: 50;
}

.overlay.open {
    display: grid;
    place-items: center;
}

.modal {
    width: min(800px, calc(100vw - 32px));
    background: #121723;
    border: 1px solid #2a3244;
    border-radius: 12px;
    box-shadow: 0 20px 80px rgba(0, 0, 0, .6);
}

.modal header,
.modal footer {
    padding: 12px 16px;
    border-bottom: 1px solid #222836;
}

.modal header {
    display: flex;
    align-items: center;
    gap: 8px;
    justify-content: space-between;
}

.modal footer {
    border-top: 1px solid #222836;
    border-bottom: none;
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.modal .content {
    padding: 12px 16px;
    display: grid;
    gap: 12px;
    grid-template-columns: 1fr 1fr;
}

.modal .row {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.modal label {
    font-size: 12px;
    color: var(--muted);
}

.modal input[type="number"],
.modal input[type="text"] {
    background: #0b0e14;
    color: var(--ink);
    border: 1px solid #1e2432;
    border-radius: 8px;
    padding: 8px 10px;
}

.keygrid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.kbdHint {
    font-size: 12px;
    color: var(--muted);
}

.checkgrid {
    display: grid;
    grid-template-columns: repeat(2, minmax(120px, 1fr));
    gap: 8px;
}

/* Metadata panel */
.panel.meta {
    display: grid;
    grid-template-rows: auto 1fr auto;
}

.metaHeader {
    padding: 12px 14px;
    border-bottom: 1px solid #222836;
}

.metaTableWrap {
    padding: 10px 14px 0;
    overflow: auto;
}

.panel.meta table.kv {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    table-layout: fixed;
}

.panel.meta th,
.panel.meta td {
    padding: 6px 6px;
    border-bottom: 1px solid #1f2533;
}

.panel.meta thead th {
    background: #121723;
    color: var(--muted);
    text-align: left;
}

.panel.meta td input {
    width: 100%;
    box-sizing: border-box;
    background: #0b0e14;
    border: 1px solid #1e2432;
    color: var(--ink);
    border-radius: 6px;
    padding: 6px 8px;
}

.metaControls {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    padding: 10px 14px 14px;
}

.metaControls button {
    min-width: 40px;
}

.panel.meta tr.selected td {
    background: rgba(124, 196, 255, 0.06);
}

/* ===== Bindings & Glyphs panel ===== */

/* Two controls (device + glyph style) already use .keygrid — keep it. */

/* Card grid for the four directions */
.bindgrid {
    display: grid;
    grid-template-columns: repeat(4, minmax(100px, 1fr));
    gap: 12px;
}

.bindRow {
    display: contents;
}

.bindCol {
    background: #0b0e14;
    border: 1px solid #1e2432;
    border-radius: 10px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.bindCol>label {
    font-weight: 600;
    color: var(--ink);
}

/* Input + Detect button row */
.bindLine {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
}

.bindLine input[type="text"] {
    flex: 1 1 auto;
    /* flex with available space */
    min-width: 60px;
    /* don’t collapse completely */
    max-width: 120px;
    /* stop it from looking huge */
    padding: 6px 8px;
    /* a bit tighter than global */
    font-size: 11px;
}

.checkLine {
    display: flex;
    align-items: center;
    gap: 6px;
    /* space between box and label */
    margin-bottom: 6px;
}

.checkLine input[type="checkbox"] {
    margin: 0;
    /* reset browser default spacing */
    width: 16px;
    height: 16px;
}

.checkLine label {
    font-size: 13px;
    color: var(--ink);
    cursor: pointer;
}

.detectBtn {
    padding: 6px 10px;
    font-size: 8px;
    line-height: 1;
    border-radius: 8px;
    background: #0d1119;
    border: 1px solid #2a3244;
    color: var(--ink);
}

/* Mini preview canvas */
.miniPrev {
    display: block;
    width: auto;
    /* Keep square but compact */
    /* aspect-ratio: 1 / 1; */
    height: auto;
    max-height: 72px;
    /* <- reduce visual size */
    background: #0a0e15;
    border: 1px solid #1e2432;
    border-radius: 8px;
}

/* Selects in modal (match inputs) */
.modal select {
    background: #0b0e14;
    color: var(--ink);
    border: 1px solid #1e2432;
    border-radius: 8px;
    padding: 8px 10px;
}

/* Subhead spacing inside modal rows */
.modal .row>h4 {
    margin: 2px 0 6px;
    font-size: 13px;
    color: var(--muted);
    font-weight: 700;
}

.spinRow {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    /* 3 label+input pairs */
    gap: 8px 12px;
    /* row gap, column gap */
    align-items: center;
}

.spinRow label {
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
}

.spinRow input[type="number"] {
    width: 100%;
    /* all same width */
    min-width: 80px;
    /* prevent collapse */
    box-sizing: border-box;
    /* include padding in width */
}

/* Anatomical panel uses same card grid; adjust spacing a touch */
.anatomicalPanel .bindgrid {
    gap: 10px;
}

/* ---------- Responsive tweaks ---------- */
@media (max-width: 1100px) {
    .bindgrid {
        grid-template-columns: repeat(2, minmax(160px, 1fr));
    }
}

/* Responsive: narrow screens -> vertical layout */
@media (max-width: 820px) {
    .wrap {
        grid-template-columns: 1fr;
    }

    .panel.meta {
        order: -1;
    }

    /* show metadata first */
    canvas {
        height: 360px;
    }

    .hud .controls {
        width: 100%;
        justify-content: flex-end;
    }

    .modal .content {
        grid-template-columns: 1fr;
    }

    .keygrid {
        grid-template-columns: 1fr 1fr;
    }

    .checkgrid {
        grid-template-columns: 1fr 1fr;
    }

    .bindgrid {
        grid-template-columns: 1fr;
    }

    .miniPrev {
        max-height: 64px;
    }
}

#studyIntroOverlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,.6);
    z-index: 10000;
  }
  
  #studyIntroOverlay[aria-hidden="false"] {
    display: flex;
  }
  
  #studyIntroOverlay .modal {
    width: min(720px, 92%);
    max-height: 80vh;
    overflow: auto;
    background: #0f1420;
    border: 1px solid #1b2232;
    border-radius: 12px;
    padding: 20px;
    color: #e6e7ea;
    line-height: 1.5;
  }
  
  /* Title area */
  #studyIntroOverlay header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
  }
  
  #studyIntroTitle {
    font-size: 1.6rem;   /* larger title */
    font-weight: 700;
    margin: 0;
  }
  
/* ===== Fix study intro overlay layout & typography ===== */

/* 1) Make the intro body full-width, readable text */
#studyIntroBody {
    /* override the global .modal .content grid */
    display: block;              /* one column, full width */
    font-size: 2rem;          /* larger, comfortable body */
    line-height: 2.5rem;
    padding: 12px 16px;
    color: #e6e7ea;
    margin-bottom: 12px;
  }
  
  /* Space for typical blocks inside the body */
  #studyIntroBody p { margin: 0 0 1rem; }
  #studyIntroBody ul,
  #studyIntroBody ol { margin: 0 0 1rem 1.25rem; }
  
  /* Optional: if you sometimes WANT two columns, add class="content twoCol" */
  #studyIntroOverlay .modal .content.twoCol {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
  /* And make normal prose span both columns even in twoCol mode */
  #studyIntroOverlay .modal .content.twoCol > h1,
  #studyIntroOverlay .modal .content.twoCol > h2,
  #studyIntroOverlay .modal .content.twoCol > h3,
  #studyIntroOverlay .modal .content.twoCol > p,
  #studyIntroOverlay .modal .content.twoCol > ul,
  #studyIntroOverlay .modal .content.twoCol > ol,
  #studyIntroOverlay .modal .content.twoCol > pre,
  #studyIntroOverlay .modal .content.twoCol > blockquote {
    grid-column: 1 / -1;
  }
  
  /* 2) Title size (you already added #studyIntroTitle, keep it) */
  #studyIntroTitle {
    font-size: 3.5rem;
    font-weight: 700;
    margin: 0;
  }
  
  /* 3) Keycap styling that aligns nicely in text & buttons */
  #studyIntroBody kbd,
  button kbd {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    font-size: 1.95rem;
    display: inline-flex;
    align-items: center;
    line-height: 2.5rem;
    padding: 2px 6px;
    border-radius: 6px;
    background: #0b1220;
    border: 1px solid #26324a;
    color: #e6e7ea;
    vertical-align: baseline;
  }
  
  /* If a button only contains a <kbd>, let the kbd define the padding */
  button:has(> kbd:only-child) {
    padding: 0;
    background: transparent; /* optional */
    border: 0;               /* optional */
  }
  
  /* 4) Keep overlay footer tidy */
  #studyIntroOverlay footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 14px;
  }
  
  /* 5) Narrow screens: keep body single-column regardless */
  @media (max-width: 700px) {
    #studyIntroOverlay .modal .content,
    #studyIntroOverlay .modal .content.twoCol {
      display: block;
    }
  }
  