/* ============= Base ============= */
:root {
    --bg: #0a0a0a;
    --bg-raised: #141414;
    --border: #242424;
    --fg: #e8e8e8;
    --fg-dim: #9a9a9a;
    --fg-faint: #555;
    --accent: #6ab0ff;
    --accent-white: #ffffff;
    --accent-black: #d84a4a;
    --radius: 6px;
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.6);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.4;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
}

body {
    padding:
        calc(env(safe-area-inset-top) + 8px)
        env(safe-area-inset-right)
        calc(env(safe-area-inset-bottom) + 8px)
        env(safe-area-inset-left);
    display: flex;
    flex-direction: column;
    align-items: center;
}

a {
    color: var(--fg-dim);
    text-decoration: none;
    border-bottom: 1px solid var(--fg-faint);
}
a:hover { color: var(--fg); border-bottom-color: var(--fg-dim); }

h2 {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--fg-dim);
    margin: 16px 0 8px;
}
h2:first-child { margin-top: 0; }

/* ============= Header ============= */
.appHeader {
    width: 100%;
    max-width: 720px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 12px 12px;
    gap: 12px;
}

.appTitle {
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: 0.01em;
}

.appTitleSub {
    color: var(--fg-faint);
    font-weight: 400;
    font-size: 0.85rem;
    margin-left: 10px;
}

.appNav {
    display: flex;
    gap: 6px;
}

.iconBtn {
    width: 42px;
    height: 42px;
    border: 1px solid var(--border);
    background: var(--bg-raised);
    color: var(--fg-dim);
    border-radius: var(--radius);
    font-family: var(--font);
    font-size: 1.2rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color 120ms, background-color 120ms, border-color 120ms;
}
.iconBtn:hover { color: var(--fg); border-color: var(--fg-faint); }
.iconBtn[aria-expanded="true"] {
    color: var(--accent);
    border-color: var(--accent);
}

/* ============= Sheets (details-based) ============= */
.sheet {
    width: 100%;
    max-width: 720px;
    margin: 0 0 12px;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}
.sheet:not([open]) {
    display: none; /* hide entirely when closed, opened programmatically */
}
.sheetSummary {
    list-style: none;
    cursor: pointer;
    padding: 12px 16px;
    font-weight: 600;
    color: var(--fg);
    border-bottom: 1px solid var(--border);
}
.sheetSummary::-webkit-details-marker { display: none; }

.sheetContent {
    padding: 16px;
}

.field {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
    font-size: 0.9rem;
}
.field:last-child { margin-bottom: 0; }

.fieldLabel {
    flex: 0 0 auto;
    min-width: 80px;
    color: var(--fg-dim);
}

.fieldValue {
    color: var(--fg-dim);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    min-width: 64px;
    text-align: right;
}

.field select,
.field input[type="range"] {
    flex: 1 1 auto;
    min-width: 0;
}

.field select,
.sheetForm input,
.sheetForm textarea,
.sheetForm button {
    background: var(--bg);
    color: var(--fg);
    border: 1px solid var(--border);
    padding: 8px 10px;
    border-radius: var(--radius);
    font-family: var(--font);
    font-size: 0.9rem;
}

.field input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 4px;
    background: var(--border);
    border-radius: 999px;
    padding: 0;
    border: none;
}
.field input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px; height: 16px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
}
.field input[type="range"]::-moz-range-thumb {
    width: 16px; height: 16px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.field input[type="checkbox"] {
    width: 18px; height: 18px;
    accent-color: var(--accent);
    margin: 0;
}

.sheetForm {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
}
.sheetForm.sheetFormColumn {
    flex-direction: column;
}
.sheetForm input,
.sheetForm textarea {
    flex: 1 1 auto;
    min-width: 0;
    width: 100%;
}
.sheetForm textarea {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    resize: vertical;
}
.sheetForm button {
    cursor: pointer;
    background: var(--bg-raised);
}
.sheetForm button:hover { border-color: var(--fg-faint); color: var(--fg); }
.sheetForm.sheetFormColumn button { align-self: flex-end; }

.hint {
    margin: 12px 0 0;
    font-size: 0.8rem;
    color: var(--fg-dim);
}

.credits {
    color: var(--fg-dim);
    font-size: 0.8rem;
    margin-top: 16px;
}

/* ============= Main layout ============= */
.main {
    width: 100%;
    max-width: 720px;
    display: grid;
    gap: 14px;
    grid-template-areas:
        "picker"
        "voices"
        "meta"
        "board"
        "beat"
        "ticker";
    justify-items: center;
}

/* ============= Game picker ============= */
.gamePicker {
    grid-area: picker;
    width: 100%;
    overflow: hidden;
}

.gamePickerTrack {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    padding: 4px 12px 8px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}
.gamePickerTrack::-webkit-scrollbar { display: none; }

.gameCard {
    flex: 0 0 140px;
    display: flex;
    flex-direction: column;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    cursor: pointer;
    scroll-snap-align: start;
    text-align: left;
    font: inherit;
    color: var(--fg);
    transition: border-color 150ms, transform 150ms;
}
.gameCard:hover,
.gameCard:focus-visible {
    border-color: var(--fg-faint);
    outline: none;
}

.gameCard img {
    width: 100%;
    aspect-ratio: 9 / 10;
    object-fit: cover;
    border-radius: 4px;
    filter: grayscale(100%);
    transition: filter 200ms;
    display: block;
    margin-bottom: 8px;
}
.gameCard:hover img,
.gameCard.playing img {
    filter: grayscale(0%);
}

.gameCardLabel {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--fg);
    line-height: 1.2;
}
.gameCardSub {
    display: block;
    font-size: 0.75rem;
    color: var(--fg-dim);
    margin-top: 2px;
}

/* First-run attention pulse on the picker */
.gamePicker.hinting .gameCard:first-child {
    animation: cardPulse 1.8s ease-out 2;
    border-color: var(--accent);
}
@keyframes cardPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(106, 176, 255, 0); }
    50%      { box-shadow: 0 0 0 6px rgba(106, 176, 255, 0.25); }
}

/* ============= Voice selectors (above the board) ============= */
.voiceBar {
    grid-area: voices;
    width: clamp(240px, min(84vmin, 58svh), 560px);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: -4px; /* snug up against the board */
}

.voicePick {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 4px 6px 4px 10px;
    flex: 1 1 0;
    min-width: 0;
    max-width: calc(50% - 4px);
    overflow: hidden;
}
.voicePickBlack { padding: 4px 10px 4px 6px; }
.voicePick > select { min-width: 0; }
.voicePick > select:not(.octaveSelect) {
    flex: 1 1 0;
    text-overflow: ellipsis;
    overflow: hidden;
}

.voicePickDot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex: 0 0 10px;
}
.voicePickWhite .voicePickDot { background: var(--accent-white); }
.voicePickBlack .voicePickDot { background: var(--accent-black); }

.voicePick select {
    background: transparent;
    color: var(--fg);
    border: none;
    outline: none;
    font-family: var(--font);
    font-size: 0.82rem;
    padding: 2px 2px;
    max-width: 100%;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    /* caret */
    background-image: linear-gradient(45deg, transparent 50%, var(--fg-dim) 50%),
                      linear-gradient(135deg, var(--fg-dim) 50%, transparent 50%);
    background-position: calc(100% - 9px) 50%, calc(100% - 5px) 50%;
    background-size: 4px 4px, 4px 4px;
    background-repeat: no-repeat;
    padding-right: 16px;
}
.voicePickBlack select {
    padding-right: 2px;
    padding-left: 16px;
    background-position: 5px 50%, 9px 50%;
    background-image: linear-gradient(-45deg, transparent 50%, var(--fg-dim) 50%),
                      linear-gradient(-135deg, var(--fg-dim) 50%, transparent 50%);
    direction: rtl; /* align the truncation to the right edge visually */
}
.voicePickBlack select option { direction: ltr; }

.voicePick .octaveSelect {
    flex: 0 0 auto;
    padding-right: 14px;
    font-variant-numeric: tabular-nums;
    border-left: 1px solid var(--border);
    margin-left: 4px;
    padding-left: 8px;
    min-width: 44px;
}
.voicePickBlack .octaveSelect {
    border-left: none;
    border-right: 1px solid var(--border);
    margin-left: 0;
    margin-right: 4px;
    padding-left: 14px;
    padding-right: 8px;
}

.voicePick:hover,
.voicePick:focus-within {
    border-color: var(--fg-faint);
}

/* ============= Game metadata (white player · event · black player) ============= */
.gameMeta {
    grid-area: meta;
    width: clamp(240px, min(84vmin, 58svh), 560px);
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
    margin: -4px 0;
}

.gameMetaRow {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-width: 0;
}

.gameMetaPlayer {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    flex: 1 1 0;
}
.gameMetaBlack { justify-content: flex-end; text-align: right; }

.gameMetaName {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--fg);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.gameMetaElo {
    font-size: 0.78rem;
    color: var(--fg-dim);
    font-variant-numeric: tabular-nums;
}

.gameMetaVs {
    font-size: 0.72rem;
    color: var(--fg-faint);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    flex: 0 0 auto;
}

.gameMetaSub {
    font-size: 0.75rem;
    color: var(--fg-dim);
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ============= Board + sequencer strips ============= */
.boardFrame {
    grid-area: board;
    display: flex;
    align-items: stretch;
    justify-content: center;
    gap: 8px;
    width: 100%;
}

.mainBoard {
    width: clamp(240px, min(84vmin, 58svh), 560px);
    aspect-ratio: 1 / 1;
    flex: 0 0 auto;
}

.noteColumn {
    display: flex;
    gap: 2px;
    flex: 0 0 12px;
}

/* Desktop: vertical columns flanking the board */
@media (min-width: 700px) {
    .noteColumn {
        flex-direction: column-reverse; /* rank 1 at bottom */
        width: 12px;
        flex: 0 0 12px;
    }
}

/* Mobile: keep the note columns visible but slimmer so the board stays big */
@media (max-width: 699px) {
    .noteColumn {
        flex-direction: column-reverse;
        width: 8px;
        flex: 0 0 8px;
    }
    .boardFrame { gap: 4px; }

    .voicePick { padding: 3px 6px 3px 8px; gap: 4px; }
    .voicePickBlack { padding: 3px 8px 3px 6px; }
    .voicePick > select:not(.octaveSelect) { font-size: 0.72rem; }
    .voicePick .octaveSelect { font-size: 0.72rem; min-width: 36px; }
}

.noteColumn .noteCell {
    flex: 1 1 0;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: 2px;
    transition: background-color 120ms ease-out, border-color 120ms ease-out;
}

#whiteColumn .noteCell.active {
    background: var(--accent-white);
    border-color: var(--accent-white);
}
#blackColumn .noteCell.active {
    background: var(--accent-black);
    border-color: var(--accent-black);
}

/* ============= Beat bar ============= */
.beatBar {
    grid-area: beat;
    display: flex;
    gap: 3px;
    width: clamp(240px, min(84vmin, 58svh), 560px);
    height: 8px;
}
.beatBar .beatCell {
    flex: 1 1 0;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: 2px;
    transition: background-color 120ms ease-out, border-color 120ms ease-out;
}
.beatBar .beatCell.active {
    background: var(--accent);
    border-color: var(--accent);
}

/* ============= Ticker (one-line marquee, newest on the right) ============= */
.ticker {
    grid-area: ticker;
    width: clamp(240px, min(84vmin, 58svh), 560px);
    height: 2em;
    color: var(--fg-dim);
    font-family: var(--font-mono);
    font-size: 0.95rem;
    line-height: 2em;
    white-space: nowrap;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    -webkit-mask-image: linear-gradient(to right, transparent 0, #000 56px, #000 100%);
            mask-image: linear-gradient(to right, transparent 0, #000 56px, #000 100%);
}
.ticker:empty::before {
    content: "—";
    color: var(--fg-faint);
}
.ticker > span { flex: 0 0 auto; }
.ticker .moveNum {
    color: var(--fg-faint);
    margin-left: 10px;
}
.ticker .moveNum:first-child { margin-left: 0; }
.ticker .move {
    color: var(--fg-dim);
    margin-left: 5px;
    transition: color 900ms ease-out;
}
.ticker .move.moveLatest {
    color: var(--accent);
    transition: none;
}

/* ============= cm-chessboard flash markers ============= */
.cm-chessboard .marker-flash {
    fill: #ffffff;
    opacity: 0.8;
    animation: flash-animation 210ms ease-out forwards;
}
.cm-chessboard .marker-capture {
    fill: #ff4040;
    opacity: 0.85;
    animation: flash-animation 210ms ease-out forwards;
}
@keyframes flash-animation {
    0%   { opacity: 0.8; }
    100% { opacity: 0; }
}

/* ============= About page (used by about.html) ============= */
.aboutPage, .aboutPage * {
    color: var(--fg);
    font-family: var(--font);
    font-size: 0.95rem;
}
.aboutPage {
    max-width: 640px;
    width: 100%;
    padding: 20px;
    line-height: 1.6;
}
.aboutPage a { color: var(--fg-dim); }
.aboutPage a:hover { color: var(--fg); }
.aboutPage ul { padding-left: 20px; }
.aboutPage li { margin-bottom: 8px; }

/* Bigger chrome on desktop */
@media (min-width: 900px) {
    .appTitle { font-size: 1.6rem; }
    .appTitleSub { font-size: 0.95rem; }
    .iconBtn { width: 46px; height: 46px; font-size: 1.3rem; }
    .appHeader { padding: 8px 16px 16px; }
}

/* Tighter picker on short viewports so the board + ticker still fit */
@media (max-height: 780px) {
    .gameCard { flex-basis: 120px; padding: 6px; }
    .gameCard img { margin-bottom: 6px; }
    .gameCardLabel { font-size: 0.78rem; }
    .gameCardSub { font-size: 0.7rem; }
    .main { gap: 10px; }
}
@media (max-height: 640px) {
    .gameCard { flex-basis: 100px; padding: 5px; }
    .gameCardSub { display: none; }
}

/* ============= Reduced motion ============= */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    .gamePicker.hinting .gameCard:first-child { animation: none; }
}

/* ============= Touch polish ============= */
@media (hover: none) and (pointer: coarse) {
    .iconBtn { width: 40px; height: 40px; }
    .gameCard { flex-basis: 150px; }
}
