* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    font-family: Arial, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #f0f0f0;
    margin: 0;
    padding: 12px;
    overflow: hidden;
}

h1 {
    color: #4CAF50;
    margin: 0 0 8px 0;
    font-size: 24px;
}

.controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    margin-bottom: 10px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.controls label,
.controls .stat {
    font-size: 14px;
    color: #333;
}

.controls select,
.controls button {
    font-size: 14px;
    padding: 6px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fafafa;
    cursor: pointer;
}

.controls button {
    background: #4CAF50;
    color: white;
    border-color: #3d8b40;
}

.controls button:hover { background: #43a047; }

#board {
    display: grid;
    grid-template-columns: repeat(var(--cols, 2), 1fr);
    grid-template-rows:    repeat(var(--cols, 2), 1fr);
    gap: 6px;
    /* Square board: limited by viewport width AND by the vertical space
       left after header/controls/status. ~180px reserved for chrome. */
    width:  min(95vw, calc(100vh - 180px), 600px);
    height: min(95vw, calc(100vh - 180px), 600px);
    margin-bottom: 8px;
}

.card {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #4CAF50;
    color: white;
    border-radius: 8px;
    cursor: pointer;
    user-select: none;
    font-size: clamp(18px, calc(60px - var(--cols, 2) * 4px), 56px);
    transition: background 0.2s, transform 0.15s;
    min-width: 0;
    min-height: 0;
}

.card:hover { transform: scale(1.03); }

.card.face-down::before {
    content: "?";
    font-weight: bold;
}

.card.face-up {
    background: #ffffff;
    color: #333;
    cursor: default;
}

.card.matched {
    background: #c8e6c9;
    color: #2e7d32;
    cursor: default;
    transform: none;
}

#status {
    min-height: 1.4em;
    font-size: 16px;
    color: #555;
    margin-bottom: 8px;
    text-align: center;
}

#winBanner {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
}

/* The [hidden] attribute must win over the display:flex above */
#winBanner[hidden] {
    display: none;
}

.winner-card {
    background: white;
    padding: 24px 32px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}

.winner-card h2 {
    margin: 0 0 16px 0;
    color: #4CAF50;
}

.winner-card button {
    font-size: 16px;
    padding: 10px 20px;
    background: #4CAF50;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

.winner-card button:hover { background: #43a047; }

#timer {
    position: fixed;
    left: 12px;
    bottom: 12px;
    background: #fff;
    border-radius: 8px;
    padding: 8px 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.12);
    font-family: Menlo, Consolas, monospace;
    font-size: 13px;
    color: #333;
    display: flex;
    flex-direction: row;
    gap: 14px;
    z-index: 5;
}

.timer-col {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 110px;
}

#timer .timer-row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

#timeBest, #picksBest { color: #4CAF50; font-weight: bold; }

/* ===== Mobile / narrow-screen responsive ===== */
@media (max-width: 600px) {

    /* Allow content to scroll rather than clip on short viewports */
    body {
        overflow: auto;
        padding: 8px;
    }

    h1 { font-size: 18px; margin-bottom: 6px; }

    /* Controls: tighten up so they don't eat too much vertical space */
    .controls {
        gap: 8px;
        padding: 6px 10px;
        margin-bottom: 6px;
    }
    .controls label,
    .controls .stat {
        font-size: 12px;
    }
    .controls select,
    .controls button {
        font-size: 12px;
        padding: 5px 8px;
    }

    /* Board: increase the reserved-chrome estimate to account for
       controls wrapping onto two lines on a narrow screen (~240px). */
    #board {
        width:  min(95vw, calc(100vh - 240px), 600px);
        height: min(95vw, calc(100vh - 240px), 600px);
    }

    /* Winner card: constrain width and reduce padding */
    .winner-card {
        max-width: 90vw;
        padding: 16px 20px;
    }
    .winner-card h2 { font-size: 1.1rem; margin-bottom: 10px; }
    .winner-card button { font-size: 14px; padding: 8px 16px; }

    /* Timer: move to bottom-center, shrink columns so it
       doesn't overlap board content on narrow screens */
    #timer {
        left: 50%;
        transform: translateX(-50%);
        bottom: env(safe-area-inset-bottom, 8px);
        gap: 10px;
        font-size: 11px;
        padding: 6px 10px;
    }
    .timer-col { min-width: 80px; }
}
