/* Theme tokens shared with the hub live in tokens.css. */
:root {
    color-scheme: light;
    --surface-soft: #f9fafb;
    --border: #e5e7eb;
    --border-strong: #d1d5db;
    --warn: #f59e0b;
    --danger: #ef4444;
    --mono: "Roboto Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    /* main's width; BACK lines up with its first column. */
    --main-max-width: 96rem;
}

:root.dark-mode {
    color-scheme: dark;
    --surface-soft: #14161b;
    --border: #2a2f3a;
    --border-strong: #3a3f4a;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--page-bg);
    color: var(--text-primary);
    font-family: "Roboto", sans-serif;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition:
        background-color 0.3s ease,
        color 0.3s ease;
}

.app {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* On desktop only main scrolls, when the columns do not fit. */
@media (min-width: 1024px) {
    html,
    body {
        overflow: hidden;
    }
}

header {
    width: 100%;
    padding: 0.85rem 1.5rem;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.75rem;
    border-bottom: 1px solid var(--border);
    background: var(--card-bg);
    position: sticky;
    top: 0;
    z-index: 10;
    flex-shrink: 0;
    transition: background-color 0.3s ease;
}

.app-title {
    margin: 0;
    font-family: "Orbitron", sans-serif;
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--accent-color);
    letter-spacing: 0.01em;
}

.header-back {
    position: absolute;
    /* Left edge aligns with the first data column (main's box). */
    left: max((100% - var(--main-max-width)) / 2 + 1.25rem, 1.25rem);
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.85rem;
    color: #fff;
    background: var(--accent-color);
    border: none;
    border-radius: 999px;
    padding: 0.35rem 0.9rem;
    cursor: pointer;
    text-decoration: none;
    transition: filter 0.15s;
}
.header-back:hover {
    filter: brightness(0.92);
}

main {
    flex: 1;
    min-height: 0;
    width: 100%;
    max-width: var(--main-max-width);
    margin: 0 auto;
    padding: 1.25rem;
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    overflow-y: auto;
}

@media (min-width: 1024px) {
    main {
        /* The middle column is wide enough for the keyboard; the side columns keep room for
           their labels on smaller screens. */
        grid-template-columns: minmax(18rem, 1fr) minmax(0, 3fr) minmax(18rem, 1fr);
        grid-template-rows: 1fr;
        align-items: stretch;
    }
    /* Polling Rate and Input History take the height left in their columns. */
    .col > .card:has(.poll-grid) {
        flex: 1 0 auto;
        display: flex;
        flex-direction: column;
    }
    /* A 0% basis would fall back to the SVG's aspect-ratio height and stretch the page. */
    .col > .card:has(.poll-grid) .poll-plot {
        flex: 1 1 0px;
        min-height: 2.5rem;
    }
    .col > .event-log-card {
        flex: 1 1 0;
        min-height: 120px;
    }
}

.event-log-card {
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.event-log-card .event-feed {
    flex: 1;
    max-height: none;
}

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 0.85rem;
    padding: 0.95rem 1rem;
    position: relative;
    overflow: hidden;
    box-shadow: var(--shadow-card);
    transition:
        background-color 0.3s ease,
        box-shadow 0.3s ease;
}

.panel-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-muted);
    font-family: "Orbitron", sans-serif;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    margin-bottom: 0.5rem;
}

.readout {
    font-family: "Orbitron", sans-serif;
    font-size: 2.25rem;
    font-weight: 700;
    line-height: 1.05;
    color: var(--text-primary);
}
.readout .unit {
    font-family: "Roboto", sans-serif;
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-left: 0.3rem;
}

.readout-caption {
    color: var(--text-muted);
    font-size: 0.75rem;
    margin-top: 0.4rem;
}

.card-centered {
    text-align: center;
}
.card-centered .panel-title {
    justify-content: center;
}

.col {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-height: 0;
}
/* Cards clip their overflow, so they would otherwise shrink to their titles. */
.col > * {
    flex-shrink: 0;
}

.dc-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0.25rem 0 0.75rem;
}
.dc-label {
    font-family: "Orbitron", sans-serif;
    font-size: 0.7rem;
    color: var(--text-muted);
    letter-spacing: 0.08em;
    text-transform: uppercase;
}
.dc-input {
    flex: 0 0 4.5rem;
    background: var(--surface-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.35rem 0.5rem;
    font-family: var(--mono);
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    transition: border-color 0.15s;
    appearance: textfield;
}
.dc-input:focus {
    outline: none;
    border-color: var(--accent-color);
}
.dc-input::-webkit-outer-spin-button,
.dc-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.dc-unit {
    color: var(--text-muted);
    font-size: 0.85rem;
}
.dc-result {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}
.dc-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 0.5rem 0.75rem;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
}
.dc-result-label {
    font-family: "Orbitron", sans-serif;
    font-size: 0.65rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
}
.dc-result-val {
    font-family: "Orbitron", sans-serif;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}
.dc-result-val.flagged {
    color: var(--danger);
}

.poll-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    margin-top: 0.25rem;
}
.poll-cell {
    text-align: center;
    padding: 0.5rem 0.25rem;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
}
.poll-label {
    font-family: "Orbitron", sans-serif;
    font-size: 0.65rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}
.poll-val {
    font-family: "Orbitron", sans-serif;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-top: 0.2rem;
    line-height: 1.1;
}
.poll-val .unit {
    font-family: "Roboto", sans-serif;
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-left: 0.2rem;
}
.poll-plot {
    width: 100%;
    height: 2.5rem;
    display: block;
    margin-top: 0.5rem;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
}
.poll-plot polyline {
    fill: none;
    stroke: var(--accent-color);
    stroke-width: 1.5;
    stroke-linejoin: round;
    stroke-linecap: round;
    vector-effect: non-scaling-stroke;
}

.interval-plot {
    width: 100%;
    height: 3rem;
    margin-top: 0.75rem;
    display: flex;
    align-items: flex-end;
    gap: 1px;
    padding: 4px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    box-sizing: border-box;
    overflow: hidden;
}
.interval-bar {
    flex: 1 1 0;
    min-width: 0;
    background: var(--accent-color);
    border-radius: 1px 1px 0 0;
}

.feed-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.6rem;
}
.feed-title {
    color: var(--text-muted);
    font-family: "Orbitron", sans-serif;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
}
.clear-log-btn {
    background: var(--accent-color);
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 0.3rem 0.9rem;
    font-family: "Roboto", sans-serif;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: filter 0.15s;
}
.clear-log-btn:hover {
    filter: brightness(0.92);
}
.event-feed {
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.5rem;
    max-height: 240px;
    min-height: 240px;
    overflow-y: auto;
    font-family: var(--mono);
    font-size: 0.78rem;
}
.event-feed::-webkit-scrollbar {
    width: 6px;
}
.event-feed::-webkit-scrollbar-track {
    background: transparent;
}
.event-feed::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 3px;
}
.feed-empty {
    color: var(--text-muted);
    font-style: italic;
    text-align: center;
    padding: 1rem 0;
}
.log-entry {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 0.5rem;
    padding: 0.3rem 0.4rem;
    border-bottom: 1px solid var(--border);
}
.log-entry:last-child {
    border-bottom: none;
}
.log-entry:first-child {
    background: rgba(var(--accent-rgb), 0.08);
    border-radius: 0.3rem;
    border-bottom-color: transparent;
}
.log-entry .ts {
    color: var(--text-muted);
}
.log-entry .ev {
    color: var(--text-primary);
    font-weight: 500;
}
.log-entry .iv {
    color: var(--accent-color);
    font-variant-numeric: tabular-nums;
    font-weight: 700;
}
.log-entry .iv.warn {
    color: var(--warn);
}
.log-entry .iv.danger {
    color: var(--danger);
}

.col-mouse {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    position: relative;
}

.mouse-stage {
    position: relative;
    width: min(22rem, 100%);
    max-height: 100%;
    aspect-ratio: 1 / 1;
}
.mouse-art {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: contain;
    filter: drop-shadow(0 6px 18px rgba(0, 0, 0, 0.12));
    pointer-events: none;
}
.mouse-overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
}
.mouse-overlay .click-region {
    fill: transparent;
    stroke: transparent;
    stroke-width: 2;
    transition:
        fill 0.12s ease,
        stroke 0.12s ease;
}
.mouse-overlay .click-region.active {
    fill: rgba(var(--accent-rgb), 0.45);
    stroke: var(--accent-color);
}
.mouse-overlay .arrow {
    fill: var(--accent-color);
    opacity: 0;
    transition: opacity 0.08s ease;
}
.mouse-overlay .arrow.show {
    opacity: 1;
    animation: bounce 0.5s ease-in-out;
}
@keyframes bounce {
    0%,
    100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-6px);
    }
}
.tile {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 0.65rem;
    padding: 1.1rem 0.85rem;
    text-align: center;
    box-shadow: var(--shadow-card);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition:
        border-color 0.15s,
        background 0.15s;
}
.tile.active {
    border-color: var(--accent-color);
    background: rgba(var(--accent-rgb), 0.05);
}
.tile-label {
    display: block;
    color: var(--text-muted);
    font-family: "Orbitron", sans-serif;
    font-size: 0.7rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}
.tile-count {
    display: block;
    font-family: "Orbitron", sans-serif;
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1;
    margin-top: 0.3rem;
}

.tile-pair {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.7rem;
    align-items: stretch;
}
.wheel-tile {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 0.8rem;
    text-align: center;
    box-shadow: var(--shadow-card);
    transition:
        border-color 0.15s,
        background 0.15s;
}
.wheel-tile.active {
    border-color: var(--accent-color);
    background: rgba(var(--accent-rgb), 0.05);
}
.wheel-label {
    color: var(--text-muted);
    font-family: "Orbitron", sans-serif;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}
.wheel-count {
    font-family: "Orbitron", sans-serif;
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-top: 0.3rem;
}

/* Keyboard under the mouse: one row is one key unit tall. */
.keyboard {
    width: 100%;
    display: flex;
    flex-direction: column;
}
.kb-row {
    display: flex;
    aspect-ratio: var(--kb-units) / 1;
}
.kb-key,
.kb-gap {
    position: relative;
    flex: 0 0 calc(var(--w) * 100% / var(--kb-units));
}
.kb-key span {
    position: absolute;
    inset: 0.09rem;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    background: var(--card-bg);
    color: var(--text-muted);
    font-family: var(--mono);
    font-size: 0.65rem;
    white-space: nowrap;
    transition:
        border-color 0.08s,
        background 0.08s;
}
.kb-key.active span {
    border-color: var(--accent-color);
    background: rgba(var(--accent-rgb), 0.45);
    color: var(--text-primary);
}

/* The mouse and the controller, centred in the space above the keyboard at the column's bottom. */
.device-row {
    width: 100%;
    margin-block: auto;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
}
/* On desktop the mouse and controller share a row, shrinking rather than wrapping. */
@media (min-width: 1024px) {
    .device-row {
        flex-wrap: nowrap;
    }
}
.device-row .mouse-stage {
    /* Takes the room the controller leaves, up to 25rem. */
    flex: 1 1 0;
    width: auto;
    min-width: 12rem;
    max-width: 25rem;
}
.pad-stage {
    /* 21.25rem whenever the column has room; it only shrinks on smaller screens. */
    flex: 0 1 21.25rem;
    min-width: 0;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}
/* The drawing with its buttons over it, sized like the key binding dialog's in the hub. */
.pad-art {
    position: relative;
    width: 100%;
    aspect-ratio: 460 / 390;
    container-type: inline-size;
    --pad-label: #fff;
    --pad-button: #6b7280;
}
:root.dark-mode .pad-art {
    --pad-label: #e5e7eb;
    --pad-button: #374151;
}
.pad-art-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
}
.pad-body {
    fill: var(--card-bg);
    stroke: var(--border-strong);
    stroke-width: 3;
}
.pad-shadow {
    fill: var(--border);
}
.pad-seam {
    stroke: var(--border);
    stroke-width: 2;
}
.pad-well {
    fill: none;
    stroke: var(--border-strong);
    stroke-width: 3;
}
/* The dialog's label size: 0.65rem on its 26rem drawing. */
.pad-btn {
    --v: 0;
    position: absolute;
    isolation: isolate;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    border-radius: 999px;
    font-size: 2.5cqw;
    font-weight: 600;
    line-height: 1;
    color: var(--pad-label);
    background-color: var(--pad-button);
    transition: box-shadow 0.08s;
}
/* The accent fills in with the value, so an analog trigger shows how far it is pulled. */
.pad-btn::after {
    content: "";
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: inherit;
    background: var(--accent-color);
    opacity: var(--v);
}
.pad-btn.active {
    box-shadow: 0 0 0 3px rgba(var(--accent-rgb), 0.35);
}
/* A D-pad button hides its name behind an arrow pointing --turn from up. */
.pad-btn.pad-arrow {
    color: transparent;
    overflow: hidden;
    border-radius: 25%;
}
.pad-btn.pad-arrow::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    border-left: 1.15cqw solid transparent;
    border-right: 1.15cqw solid transparent;
    border-bottom: 1.55cqw solid var(--pad-label);
    transform: translate(-50%, -50%) rotate(var(--turn));
}
/* A stick's cap moves --travel (a share of its size) at full tilt. */
.pad-stick {
    --x: 0;
    --y: 0;
    position: absolute;
    transform: translate(calc(var(--x) * var(--travel)), calc(var(--y) * var(--travel)));
}
.pad-stick > .pad-btn {
    inset: 0;
}
/* A fixed line, so the name appearing doesn't move the row it sits under. */
.pad-name {
    height: 1.4em;
    line-height: 1.4em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
}
.pad-readouts {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}
.pad-readout {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.pad-axes {
    display: grid;
    grid-template-columns: auto;
    gap: 0.1rem;
}
.pad-readout-title,
.pad-axis-label {
    color: var(--text-muted);
    font-size: 0.7rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}
.pad-readout-title {
    margin-bottom: 0.2rem;
}
.pad-axis-value {
    font-size: 0.9rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-primary);
}
.pad-plot {
    flex: 1 1 0;
    min-width: 2.5rem;
    max-width: 6rem;
    aspect-ratio: 1 / 1;
}
.pad-plot-ring,
.pad-plot-axis {
    fill: none;
    stroke: var(--border-strong);
    stroke-width: 1;
}
.pad-plot-line {
    stroke: var(--accent-color);
    stroke-width: 1.5;
}
.pad-plot-dot {
    fill: var(--accent-color);
}

.footer-section {
    padding: 0.45rem 1rem 0.65rem;
    margin-top: 0;
    border-top: 1px solid var(--footer-border);
    text-align: center;
    background-color: transparent;
    flex-shrink: 0;
    transition: border-top-color 0.3s ease;
}
.footer-section p {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin: 0;
    line-height: 1.5;
}
.footer-section a {
    color: var(--accent-color);
    text-decoration: none;
    transition: opacity 0.2s ease;
}
.footer-section a:hover {
    opacity: 0.7;
    text-decoration: underline;
}

/* Shorter desktop screens: tighten the left column. */
@media (min-width: 1024px) and (min-height: 600px) and (max-height: 1150px) {
    .col:has(> .card .poll-grid) {
        gap: 0.45rem;
    }
    .col:has(> .card .poll-grid) .tile,
    .col:has(> .card .poll-grid) .wheel-tile {
        padding: 0.7rem 0.85rem;
    }
    .col:has(> .card .poll-grid) .tile-count,
    .col:has(> .card .poll-grid) .wheel-count {
        font-size: 1.25rem;
        margin-top: 0.15rem;
    }
    .col:has(> .card .poll-grid) > .card:has(.poll-grid) > .poll-plot {
        min-height: 1.8rem;
    }
}
