:root {
    color-scheme: dark;
    --bg-color: #0F172A;
    --text-color: #F8FAFC;
    --board-bg: #1E293B;
    --cell-bg: #334155;
    --cell-hover: #475569;
    --pipe-empty: #94A3B8;
    --pipe-filled: #00E5FF;
    --accent-color: #FBBF24;
    --accent-text: #0F172A;
    --modal-bg: rgba(15, 23, 42, 0.95);
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    text-align: center;
}

/* Small, unobtrusive link back to the info page. Sits in normal document
   flow above the game header rather than as a fixed overlay, so it never
   competes with the "Level N" heading for space. */
.back-link-game {
    display: inline-block;
    align-self: flex-start;
    color: var(--pipe-filled);
    font-weight: bold;
    font-size: 0.95rem;
    text-decoration: none;
    margin-bottom: 10px;
}
.back-link-game:hover,
.back-link-game:focus-visible {
    text-decoration: underline;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* <main> is a flex item of body (align-items:center), which sizes it by
   shrink-to-fit with no explicit width - giving a width:100% child (like
   .game-shell below) an unambiguous number to resolve against requires
   breaking that ambiguity at the source, on <main> itself, rather than
   leaving it to nested flex children. Confirmed on Ultimate Tic Tac
   Toe's identical pattern: .game-shell's own max-width alone wasn't
   sufficient - Settings/Login overlapped the heading at wide viewports
   (reproduced at 1920px) until <main> got an explicit width too. */
main {
    width: 100%;
    max-width: 850px;
    margin: 0 auto;
}

.game-shell {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 10px;
    gap: 10px;
}
.header h1 { flex: 1 1 auto; min-width: 0; }

.header-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-shrink: 0;
}

h1 {
    margin: 0;
    color: var(--accent-color);
    font-size: 1.5rem;
    letter-spacing: 1px;
}

p {
    margin: 5px 0 15px 0;
    color: #CBD5E1;
    font-size: 1rem;
    max-width: 600px;
}

/* Plain text-style trigger for the How to Play modal - deliberately not
   the solid accent-color button used elsewhere, just a simple link. */
.how-to-play-link {
    background: none;
    border: none;
    box-shadow: none;
    color: var(--pipe-filled);
    font-weight: bold;
    font-size: 1rem;
    text-decoration: underline;
    padding: 4px;
    cursor: pointer;
    margin-bottom: 15px;
}
.how-to-play-link:hover,
.how-to-play-link:focus-visible {
    background: none;
    color: var(--accent-color);
    transform: none;
}

.splash-text {
    color: var(--pipe-filled);
    animation: bounce 1s infinite;
    display: inline-block;
    font-weight: bold;
}

#board {
    display: grid;
    background-color: var(--board-bg);
    padding: 8px;
    border-radius: 12px;
    width: 100%;
    max-width: min(100%, 850px);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    margin-bottom: 20px;
}

.cell {
    aspect-ratio: 1 / 1;
    background-color: var(--cell-bg);
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
    touch-action: manipulation;
    user-select: none;
    transition: background-color 0.2s;
}

.cell:hover { background-color: var(--cell-hover); }
.cell.fixed { cursor: default; }
.cell.fixed:hover { background-color: var(--cell-bg); }

/* Keyboard focus ring for grid cells - high contrast against every cell
   background variant (default, hover, and the black fixed-pipe tiles). */
.cell:focus-visible {
    outline: 3px solid var(--accent-color);
    outline-offset: -3px;
    z-index: 1;
}

.cell.fixed-pipe {
    background-color: #000000;
    border: 1px solid #475569;
    cursor: not-allowed;
}
.cell.fixed-pipe::after {
    content: "🔒";
    position: absolute;
    font-size: 0.8rem;
    bottom: 2px;
    right: 2px;
    opacity: 0.8;
    pointer-events: none;
}
.cell.fixed-pipe:hover { background-color: #000000; }

.pipe-svg {
    width: 100%;
    height: 100%;
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.pipe-line {
    stroke: var(--pipe-empty);
    stroke-width: 22;
    stroke-linecap: round;
    stroke-linejoin: round;
    fill: none;
    transition: stroke 0.4s ease, filter 0.4s ease;
}

.cell.filled .pipe-line, .pipe-line.fixed-filled {
    stroke: var(--pipe-filled);
    filter: drop-shadow(0 0 6px var(--pipe-filled));
}

.controls {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
}

button {
    background-color: var(--accent-color);
    color: var(--accent-text);
    border: none;
    padding: 10px 20px;
    font-size: 1rem;
    font-weight: bold;
    border-radius: 8px;
    cursor: pointer;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    transition: transform 0.1s, background-color 0.2s;
}

button:hover { background-color: #F59E0B; transform: translateY(-2px); }
button:active { transform: translateY(1px); }

#google-login-btn {
    background-color: #1e293b;
    color: #ffffff;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    font-size: 0.9rem;
    border: 1px solid #334155;
    transition: background-color 0.2s, border-color 0.2s;
    cursor: pointer;
    border-radius: 8px;
}

#google-login-btn:hover {
    background-color: #0f172a;
    border-color: var(--accent-color);
}

#google-login-btn svg { width: 18px; height: 18px; }

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: var(--modal-bg);
    z-index: 100;
    justify-content: center;
    align-items: center;
    padding: 20px;
    box-sizing: border-box;
}

#win-modal {
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
}

.modal-content {
    background: var(--board-bg);
    padding: 25px;
    border-radius: 16px;
    width: 95%;
    max-width: 650px;
    max-height: 85vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.8);
}

/* How to Play modal: left-aligned body text (body {} sets text-align:center
   globally, so this needs to explicitly opt back out), with its Close
   button last in the flex column - after the paragraphs, that already
   puts it at the bottom without any extra positioning. */
.how-to-play-content {
    text-align: left;
}
.how-to-play-content h2 {
    margin: 0;
    color: var(--accent-color);
}
.how-to-play-content button {
    align-self: center;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid var(--cell-bg);
    padding-bottom: 12px;
}

.level-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 16px;
    width: 100%;
}

.level-btn {
    padding: 0;
    aspect-ratio: 1 / 1;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.8rem;
    position: relative;
    background-color: #475569;
    color: var(--text-color);
    border-radius: 12px;
    transition: background-color 0.2s, transform 0.1s;
    border: none;
    cursor: pointer;
}

.level-btn:hover {
    background-color: #000000 !important;
    transform: scale(1.03);
}

.level-btn.completed { background-color: #065f46; color: white; }
.level-btn.current { border: 3px solid var(--accent-color); }

.star-icon {
    position: absolute;
    top: -12px;
    right: -10px;
    font-size: 1.6rem;
    filter: drop-shadow(0 2px 2px rgba(0,0,0,0.5));
}

.footer {
    margin-top: auto;
    padding-top: 30px;
    color: #94A3B8;
    font-size: 0.85rem;
    line-height: 1.5;
}

.sync-status {
    font-size: 0.75rem;
    margin-top: 8px;
    color: #64748B;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.sync-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #ef4444;
}
.sync-dot.online { background-color: #10B981; box-shadow: 0 0 6px #10B981; }

@keyframes happyDogDance {
    0%, 100% { transform: translateY(0) rotate(0deg) scale(1); }
    25% { transform: translateY(-20px) rotate(-10deg) scale(1.1); }
    50% { transform: translateY(0) rotate(0deg) scale(1.2); }
    75% { transform: translateY(-20px) rotate(-10deg) scale(1.1); }
}
.big-happy-dog {
    font-size: 5rem;
    display: inline-block;
    animation: happyDogDance 1s infinite;
    margin: 10px 0;
}
