* { 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; }

#colorHint {
    background: #1976d2;
    border-color: #125aa2;
}

#colorHint:hover { background: #1565c0; }

#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-down.hint-blue {
    background: #1976d2;
}

.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;
    display: flex;
    align-items: center;
    justify-content: center;
    right: 12px;
    bottom: 12px;
    z-index: 20;
    animation: winToastIn 0.28s ease-out;
}

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

.winner-card {
    background: white;
    width: min(360px, calc(100vw - 24px));
    padding: 16px 18px;
    border: 1px solid rgba(76,175,80,0.28);
    border-radius: 8px;
    text-align: left;
    box-shadow: 0 8px 24px rgba(0,0,0,0.18);
}

.winner-card h2 {
    margin: 0 0 12px 0;
    color: #4CAF50;
    font-size: 1rem;
    line-height: 1.25;
}

.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; }

#frogParade {
    position: fixed;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
    z-index: 15;
}

.frog {
    position: absolute;
    left: 100vw;
    top: var(--lane, 50vh);
    font-size: var(--size, 42px);
    line-height: 1;
    filter: drop-shadow(0 8px 5px rgba(0,0,0,0.2));
    animation: frogCross var(--duration, 6s) linear var(--delay, 0s) forwards;
    will-change: transform;
}

.frog-icon {
    display: block;
    animation: frogHop 0.48s ease-in-out var(--delay, 0s) infinite;
    will-change: transform;
}

@keyframes winToastIn {
    from {
        opacity: 0;
        transform: translate(16px, 16px) scale(0.96);
    }
    to {
        opacity: 1;
        transform: translate(0, 0) scale(1);
    }
}

@keyframes frogCross {
    from { transform: translateX(8vw); }
    to { transform: translateX(calc(-120vw - var(--drift, 0px))); }
}

@keyframes frogHop {
    0%, 100% { transform: translateY(0) rotate(-5deg); }
    45% { transform: translateY(calc(-1 * var(--jump, 24px))) rotate(7deg); }
}

#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 multiple lines on a narrow screen (~270px). */
    #board {
        width:  min(95vw, calc(100vh - 270px), 600px);
        height: min(95vw, calc(100vh - 270px), 600px);
    }

    #winBanner {
        right: 8px;
        bottom: calc(env(safe-area-inset-bottom, 0px) + 72px);
    }

    /* Winner card: constrain width and reduce padding */
    .winner-card {
        width: min(320px, calc(100vw - 16px));
        padding: 12px 14px;
    }
    .winner-card h2 { font-size: 0.95rem; 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; }
}

@media (prefers-reduced-motion: reduce) {
    #winBanner,
    .frog,
    .frog-icon {
        animation: none;
    }

    .frog {
        display: none;
    }
}
