:root {
    --felt: #0b6e4f;
    --felt-dark: #084d38;
    --card-bg: #fefefe;
    --card-border: #d1d5db;
    --card-back: repeating-linear-gradient(45deg, #1d4ed8, #1d4ed8 6px, #1e40af 6px, #1e40af 12px);
    --suit-black: #111827;
    --suit-red: #dc2626;

    /* Card size (overridden smaller under 700px - see that media query).
       --card-overlap/--card-overlap-y drive the "poker fan" negative-
       margin overlap used on desktop, where there's room to spare;
       --card-overlap is horizontal (top/bottom hands), --card-overlap-y
       is vertical (left/right hands, applied against card *height* which
       is much larger than width, hence the different value). Below
       700px, hands stop using overlap margins entirely and shrink-to-fit
       via flexbox instead - see the media query at the bottom. */
    --card-w: 46px;
    --card-h: 66px;
    --card-overlap: 14px;
    --card-overlap-y: 50px;
    --mini-card-w: 30px;
    --mini-card-h: 42px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --felt: #064e3b;
        --felt-dark: #022c22;
        --card-bg: #f1f5f9;
        --card-border: #94a3b8;
        --suit-black: #111827;
        --suit-red: #dc2626;
    }
}

/* ---------------- Seat legend ---------------- */
.cp-seat-legend {
    max-width: 900px;
    margin: 0 auto 16px;
    padding: 10px 16px;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 13px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 14px;
}
.cp-legend-chip {
    padding: 3px 10px;
    border-radius: 999px;
    font-weight: 600;
    font-size: 12px;
    color: #fff;
}
.cp-legend-chip.cp-team-a { background: #dc2626; }
.cp-legend-chip.cp-team-b { background: #2563eb; }
.cp-legend-note {
    color: var(--text-muted);
    font-size: 12px;
}

/* ---------------- Config screen ---------------- */
.cp-config {
    display: flex;
    justify-content: center;
}

.cp-config-panel {
    width: 100%;
    max-width: 480px;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 1px 3px var(--shadow);
    padding: 28px;
}

.cp-config-panel h2 {
    margin: 18px 0 10px;
    font-size: 18px;
}
.cp-config-panel h2:first-child { margin-top: 0; }

.cp-config-panel .hint {
    font-size: 13px;
    color: var(--text-muted);
    margin: 0 0 12px;
}

.cp-toggle-row {
    display: flex;
    gap: 8px;
    margin-bottom: 10px;
}
.cp-toggle-btn {
    flex: 1;
    padding: 10px;
    border-radius: 8px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--text);
    cursor: pointer;
    font-size: 13px;
}
.cp-toggle-btn.active {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
}

.cp-radio-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 10px 0 4px;
    font-size: 14px;
    cursor: pointer;
}

#localConfig select, #onlineConfig input {
    width: 100%;
    padding: 8px;
    margin-bottom: 6px;
    background: var(--input-bg);
    color: var(--text);
    border: 1px solid var(--input-border);
    border-radius: 8px;
}

.cp-seat-preview {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
    margin: 14px 0;
    font-size: 13px;
}
.cp-seat-preview div {
    padding: 8px;
    border-radius: 8px;
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    text-align: center;
}
.cp-seat-preview div.is-human { border-color: var(--brand); font-weight: 700; }

#onlineConfig hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 18px 0;
}

.cp-config-message {
    margin-top: 14px;
    font-size: 14px;
    color: var(--error);
    text-align: center;
    min-height: 20px;
}

/* ---------------- Table screen ---------------- */
.cp-table-screen {
    max-width: 1100px;
    margin: 0 auto;
}

.cp-info-panel {
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 18px;
    margin-bottom: 14px;
    font-size: 14px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px 24px;
    align-items: center;
}
.cp-score-row { display: flex; gap: 24px; flex-wrap: wrap; }
#roomCodeDisplay {
    margin-left: auto;
    font-weight: 700;
    background: var(--input-bg);
    padding: 4px 10px;
    border-radius: 999px;
}

.cp-main-area {
    display: flex;
    gap: 16px;
    align-items: flex-start;
    justify-content: center;
    flex-wrap: wrap;
}

.cp-card-table {
    width: min(90vw, 640px);
    /* A square-ish minimum, not a fixed aspect-ratio: a 13-card hand on
       the left/right seats needs more vertical room than a fixed ratio
       would allow, and fixed-ratio content used to just overflow the box
       instead of growing it. min-height gives the same default look
       while still letting the table grow when a hand needs more space. */
    min-height: min(76vw, 544px);
    background: radial-gradient(circle at center, var(--felt) 0%, var(--felt-dark) 100%);
    border-radius: 20px;
    border: 10px solid #5b3a21;
    box-shadow: 0 10px 30px -10px rgba(0,0,0,0.6);
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    /* auto, not 1fr: this container's height is no longer fixed (see
       min-height above), and `fr` tracks in a container whose own height
       is content-driven rather than definite are resolved inconsistently
       across browsers - sometimes collapsing rows instead of sizing them
       to content. `auto` unambiguously sizes each row to fit what's
       actually inside it. */
    grid-template-rows: auto auto auto;
    padding: 10px;
    position: relative;
}

.cp-seat { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.cp-seat-top { grid-column: 2; grid-row: 1; }
.cp-seat-left { grid-column: 1; grid-row: 2; }
.cp-seat-right { grid-column: 3; grid-row: 2; }
.cp-seat-bottom { grid-column: 2; grid-row: 3; }

.cp-seat.cp-active-turn .cp-seat-label {
    animation: cp-pulse 1s ease-in-out infinite;
    box-shadow: 0 0 0 2px #fde047;
}
@keyframes cp-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }

.cp-seat-label {
    font-size: 12px;
    color: #fff;
    background: rgba(0,0,0,0.35);
    padding: 3px 10px;
    border-radius: 999px;
}
.cp-seat-label .cp-role { opacity: 0.8; font-size: 10px; }

.cp-hand {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 100%;
}
.cp-seat-left .cp-hand, .cp-seat-right .cp-hand {
    flex-direction: column;
}

/* Top/bottom hands overlap horizontally; left/right hands overlap
   vertically (they're stacked in a column) - each direction only applies
   its own margin so cards actually overlap instead of just shifting. */
.cp-seat-top .cp-hand > .cp-card,
.cp-seat-bottom .cp-hand > .cp-card {
    margin-left: calc(-1 * var(--card-overlap));
}
.cp-seat-top .cp-hand > .cp-card:first-child,
.cp-seat-bottom .cp-hand > .cp-card:first-child {
    margin-left: 0;
}
.cp-seat-left .cp-hand > .cp-card,
.cp-seat-right .cp-hand > .cp-card {
    margin-left: 0;
    margin-top: calc(-1 * var(--card-overlap-y));
}
.cp-seat-left .cp-hand > .cp-card:first-child,
.cp-seat-right .cp-hand > .cp-card:first-child {
    margin-top: 0;
}

.cp-center-pile {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    flex-wrap: wrap;
    max-width: 140px;
    margin: 0 auto;
}

/* ---------------- Cards ---------------- */
.cp-card {
    width: var(--card-w);
    height: var(--card-h);
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-weight: 700;
    font-size: clamp(10px, 3.4vw, 14px);
    box-shadow: 0 2px 3px rgba(0,0,0,0.3);
    cursor: default;
    user-select: none;
    transition: transform 0.15s ease;
}
.cp-card .cp-rank { line-height: 1; }
.cp-card .cp-suit { line-height: 1; font-size: 1.15em; }
.cp-card.cp-black { color: var(--suit-black); }
.cp-card.cp-red { color: var(--suit-red); }

.cp-card.cp-back {
    background: var(--card-back);
    border: 1px solid #1e3a8a;
}

.cp-card.cp-legal {
    cursor: pointer;
    transform: translateY(-8px);
    box-shadow: 0 0 0 3px #fde047, 0 4px 6px rgba(0,0,0,0.4);
    /* Overlapping siblings paint on top in DOM order regardless of the
       translateY lift above - without this, a legal card sandwiched
       between others would look raised but its tap target would still
       be covered by the next card in the hand. */
    z-index: 10;
}
.cp-card.cp-legal:hover { transform: translateY(-14px) scale(1.05); }

.cp-card.cp-mini {
    width: var(--mini-card-w);
    height: var(--mini-card-h);
    font-size: 10px;
}

/* ---------------- Spectator sidebar ---------------- */
.cp-spectator-sidebar {
    width: 220px;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    font-size: 13px;
}
.cp-spectator-sidebar h3 { margin: 0 0 10px; font-size: 14px; }
.cp-spectator-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
}
.cp-spectator-row button {
    font-size: 11px;
    padding: 4px 8px;
    width: auto;
}

/* ---------------- Controls / commentary ---------------- */
.cp-controls-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin: 16px 0;
    flex-wrap: wrap;
}
.cp-controls-row button { width: auto; }

.cp-commentary {
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px 14px;
    height: 160px;
    overflow-y: auto;
    font-size: 13px;
    line-height: 1.6;
    font-family: ui-monospace, monospace;
}
.cp-commentary div { border-bottom: 1px dashed var(--border); padding: 2px 0; }

/* ---------------- Modals ---------------- */
.cp-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
}
.cp-modal {
    background: var(--panel-bg);
    border-radius: 14px;
    padding: 26px;
    max-width: 420px;
    width: 90%;
    text-align: center;
    box-shadow: 0 20px 40px rgba(0,0,0,0.5);
}
.cp-modal h2 { margin-top: 0; }
.cp-modal button { margin: 8px 6px 0; width: auto; }

.cp-trump-choices {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 16px;
}
.cp-suit-choice {
    padding: 20px;
    font-size: 28px;
    border-radius: 10px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--suit-black);
    cursor: pointer;
}
.cp-suit-choice.cp-suit-red { color: var(--suit-red); }
.cp-suit-choice:hover { background: var(--brand); color: #fff; }

@media (max-width: 700px) {
    .cp-main-area { flex-direction: column; align-items: center; }
    .cp-spectator-sidebar { width: 100%; max-width: 400px; }

    :root {
        --card-w: 34px;
        --card-h: 48px;
        --mini-card-w: 24px;
        --mini-card-h: 34px;
    }

    /* On mobile, hands stop using the negative-margin overlap trick
       entirely and shrink-to-fit via flexbox instead: every card gets an
       equal share of the hand's available space (down to a hard
       min-width/min-height floor), computed by the browser's own flex
       algorithm rather than manually-tuned overlap pixel values. This
       guarantees every card stays at least partially visible no matter
       how narrow the screen is, instead of needing the overlap amount
       re-tuned (and re-verified by eye) for every device width. */
    .cp-hand {
        flex-wrap: nowrap;
    }

    .cp-seat-top .cp-hand > .cp-card,
    .cp-seat-bottom .cp-hand > .cp-card,
    .cp-seat-left .cp-hand > .cp-card,
    .cp-seat-right .cp-hand > .cp-card {
        margin: 0;
    }

    .cp-seat-top .cp-hand > .cp-card,
    .cp-seat-bottom .cp-hand > .cp-card {
        flex: 1 1 0;
        width: auto;
        min-width: 14px;
        max-width: var(--card-w);
    }

    .cp-seat-left .cp-hand > .cp-card,
    .cp-seat-right .cp-hand > .cp-card {
        flex: 1 1 0;
        width: var(--card-w);
        height: auto;
        min-height: 14px;
        max-height: var(--card-h);
    }
}
