* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    overflow-x: hidden;
    min-height: 100%;
}

body {
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 8px 8px 16px;
    background: #f4f4f8;
    color: #222;
    margin: auto 0;
}

h1 {
    margin-bottom: 4px;
    font-size: 1.5rem;
}

.hidden {
    display: none !important;
}

/* ---- Auth Screen ---- */
#auth-screen {
    max-width: 360px;
    width: 100%;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.auth-tabs {
    display: flex;
    gap: 0;
    border-bottom: 2px solid #eee;
}

.auth-tab {
    flex: 1;
    padding: 10px;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    background: none;
    cursor: pointer;
    color: #888;
    border-bottom: 3px solid transparent;
    margin-bottom: -2px;
}

.auth-tab.active {
    color: #4a6cf7;
    border-bottom-color: #4a6cf7;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.auth-form input {
    font-size: 1rem;
    padding: 10px 14px;
    border-radius: 6px;
    border: 1px solid #ccc;
}

.auth-form button {
    font-size: 1rem;
    padding: 10px 14px;
    border-radius: 6px;
    border: none;
    background: #4a6cf7;
    color: #fff;
    font-weight: 600;
    cursor: pointer;
}

.auth-form button:hover {
    background: #3b5de7;
}

#auth-message {
    font-weight: 600;
    min-height: 20px;
    font-size: 0.9rem;
}

#auth-message.error { color: #c62828; }
#auth-message.success { color: #2e7d32; }

/* ---- User bar ---- */
#user-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    font-size: 0.95rem;
}

#user-bar strong {
    color: #4a6cf7;
}

.btn-small {
    font-size: 0.85rem;
    padding: 4px 12px;
    border-radius: 5px;
    border: 1px solid #ccc;
    background: #f5f5f5;
    cursor: pointer;
    color: #555;
}

.btn-small:hover {
    background: #eee;
}

/* ---- Lobby ---- */
#lobby {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    max-width: 820px;
    width: 100%;
}

.lobby-columns {
    display: flex;
    gap: 20px;
    width: 100%;
}

.lobby-left, .lobby-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.lobby-section {
    background: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.lobby-section h2 {
    font-size: 1.2rem;
    margin-bottom: 4px;
}

.lobby-section select,
.lobby-section input,
.lobby-section button {
    font-size: 1rem;
    padding: 8px 14px;
    border-radius: 6px;
    border: 1px solid #ccc;
}

.lobby-section button {
    background: #4a6cf7;
    color: #fff;
    border: none;
    cursor: pointer;
    font-weight: 600;
}

.lobby-section button:hover {
    background: #3b5de7;
}

#lobby-message {
    font-weight: 600;
    color: #c62828;
    min-height: 24px;
}

/* ---- Friends & Invites Panel ---- */
.friend-add-row {
    display: flex;
    gap: 6px;
}

.invite-settings {
    display: grid;
    grid-template-columns: 1fr;
    gap: 6px;
    padding: 10px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #f8f9ff;
}

.invite-settings h3 {
    margin: 0;
    font-size: 0.85rem;
    color: #4a6cf7;
    text-transform: uppercase;
    letter-spacing: 0.4px;
}

.invite-settings label {
    font-size: 0.85rem;
    color: #555;
    font-weight: 600;
}

.friend-add-row input {
    flex: 1;
    font-size: 0.95rem;
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid #ccc;
}

.friend-add-row button {
    font-size: 0.9rem;
    padding: 6px 14px;
    border-radius: 6px;
    border: none;
    background: #4a6cf7;
    color: #fff;
    cursor: pointer;
    font-weight: 600;
}

.friend-add-row button:hover {
    background: #3b5de7;
}

#friend-message {
    font-size: 0.85rem;
    font-weight: 600;
    min-height: 18px;
    color: #c62828;
}

#friend-message.success { color: #2e7d32; }

#friends-panel h3,
#invites-panel h3 {
    font-size: 0.9rem;
    color: #666;
    margin-top: 8px;
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

#friends-panel ul,
#invites-panel ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

#friends-panel li,
#invites-panel li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 10px;
    background: #f8f8fc;
    border-radius: 6px;
    font-size: 0.9rem;
}

#friends-panel li .friend-name,
#invites-panel li .invite-from {
    font-weight: 600;
    color: #333;
}

.btn-accept {
    font-size: 0.8rem;
    padding: 3px 10px;
    border-radius: 4px;
    border: none;
    background: #2e7d32;
    color: #fff;
    cursor: pointer;
    font-weight: 600;
}

.btn-accept:hover { background: #256d29; }

.btn-decline, .btn-remove {
    font-size: 0.8rem;
    padding: 3px 10px;
    border-radius: 4px;
    border: none;
    background: #c62828;
    color: #fff;
    cursor: pointer;
    font-weight: 600;
}

.btn-decline:hover, .btn-remove:hover { background: #a01e1e; }

.btn-invite {
    font-size: 0.8rem;
    padding: 3px 10px;
    border-radius: 4px;
    border: none;
    background: #f59e0b;
    color: #fff;
    cursor: pointer;
    font-weight: 600;
}

.btn-invite:hover { background: #d97706; }

.invite-detail {
    font-size: 0.8rem;
    color: #888;
}

.muted {
    color: #aaa;
    font-size: 0.9rem;
    font-style: italic;
}

/* ---- Waiting ---- */
#waiting {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    background: #fff;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

#waiting strong {
    font-size: 1.3rem;
    color: #4a6cf7;
    user-select: all;
}

#start-btn {
    font-size: 1.1rem;
    padding: 10px 30px;
    background: #2e7d32;
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

#start-btn:hover {
    background: #256d29;
}

/* ---- Game header with timers ---- */
#game-screen {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

#game-header {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    margin-bottom: 8px;
    width: 100%;
    max-width: 560px;
}

.player-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex: 1;
    padding: 8px 12px;
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.player-info.active-player {
    outline: 3px solid #4a6cf7;
    background: #eef1ff;
}

.player-label {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #666;
}

.timer {
    font-size: 1.6rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: #222;
}

.timer.low-time {
    color: #c62828;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

#turn-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
}

#turn-text {
    font-size: 0.95rem;
    font-weight: 700;
    color: #4a6cf7;
    white-space: nowrap;
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(74, 108, 247, 0.1);
    transition: background 0.18s ease, color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}

#game-screen.turn-locked #turn-text {
    color: #666;
    background: rgba(107, 114, 128, 0.12);
    box-shadow: 0 0 0 2px rgba(107, 114, 128, 0.12);
}

/* ---- Game controls ---- */
#game-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    padding-top: 10px;
    margin-bottom: 0;
}

#game-controls button {
    font-size: 1rem;
    padding: 8px 20px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-weight: 600;
    color: #fff;
    touch-action: manipulation;
}

#end-turn-container {
    display: flex;
    justify-content: center;
    width: 100%;
    margin-top: 32px;
}

#end-turn-btn {
    background: #4a6cf7;
    color: #fff;
    border: none;
    appearance: none;
    -webkit-appearance: none;
    font-size: 1.2rem;
    padding: 14px 42px;
    min-width: 220px;
    border-radius: 10px;
    box-shadow: 0 6px 18px rgba(74, 108, 247, 0.22);
}

#end-turn-btn:hover {
    background: #3b5de7;
}

#end-turn-btn:disabled {
    background: #aaa;
    cursor: not-allowed;
}

/* ---- Message ---- */
#message {
    min-height: 28px;
    margin-top: 10px;
    font-size: 1.1rem;
    font-weight: 600;
    text-align: center;
}

#message.success {
    color: #2e7d32;
}

#message.error {
    color: #c62828;
}

#message.info {
    color: #4a6cf7;
}

/* ---- Board ---- */
#board-wrapper {
    position: relative;
    width: 100%;
    max-width: 100vw;
    overflow: visible;
    display: flex;
    justify-content: center;
    margin-top: 32px;
    transition: opacity 0.18s ease, filter 0.18s ease, transform 0.18s ease;
}

#game-screen.turn-locked #board-wrapper {
    filter: grayscale(0.12) saturate(0.88);
}

#game-screen.turn-locked #board-wrapper::after {
    content: "";
    position: absolute;
    inset: -8px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.28);
    backdrop-filter: blur(2px);
    pointer-events: none;
    z-index: 2;
}

#board {
    border-collapse: collapse;
    margin: 0 auto;
    transition: opacity 0.18s ease, transform 0.18s ease;
}

#game-screen.turn-locked #board {
    opacity: 0.72;
}

#board td {
    width: min(60px, calc((100vw - 64px) / 9));
    height: min(60px, calc((100vw - 64px) / 9));
    text-align: center;
    vertical-align: middle;
    font-size: clamp(1rem, 2.8vw, 1.3rem);
    border: 1px solid #bbb;
    padding: 0;
    position: relative;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

#board td:nth-child(3n) {
    border-right: 3px solid #333;
}

#board td:first-child {
    border-left: 3px solid #333;
}

#board tr:nth-child(3n) td {
    border-bottom: 3px solid #333;
}

#board tr:first-child td {
    border-top: 3px solid #333;
}

#board .given {
    background: #e9e9f0;
    font-weight: 700;
    color: #222;
    cursor: default;
}

#board .placed-p1 {
    font-weight: 600;
    color: #4a6cf7;
    cursor: default;
}

#board .placed-p2 {
    font-weight: 600;
    color: #2e7d32;
    cursor: default;
}

#board .error-cell {
    background: #fdd;
}

.cell {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

    .cell:hover {
        background-color: #e8edff;
    }

    .cell-value {
        font-size: clamp(1rem, 2.8vw, 1.3rem);
        font-weight: 600;
    }

.cell-value.pending-p1 {
    color: #4a6cf7;
    opacity: 0.6;
    font-style: italic;
}

.cell-value.pending-p2 {
    color: #2e7d32;
    opacity: 0.6;
    font-style: italic;
}

.pencil-marks {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.pencil-marks span {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(0.45rem, 1.7vw, 0.6rem);
    color: #888;
    line-height: 1;
    user-select: none;
}

#board td.selected {
    background: #e8edff;
    outline: 2px solid #4a6cf7;
    outline-offset: -2px;
}

#game-screen.turn-locked #board td[data-row] {
    cursor: not-allowed;
}

#game-screen.turn-locked .cell:hover {
    background-color: transparent;
}

/* ---- Invite Accepted Popup ---- */
#invite-accepted-popup {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 300;
    backdrop-filter: blur(4px);
}

#invite-accepted-popup .overlay-content {
    border: 2px solid #2e7d32;
}

#invite-accepted-popup h2 {
    color: #2e7d32;
}

#invite-join-btn {
    font-size: 1.05rem;
    padding: 12px 28px;
    background: #2e7d32;
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    transition: background 0.15s, transform 0.1s;
}

#invite-join-btn:hover {
    background: #256d29;
    transform: scale(1.03);
}

.btn-secondary {
    font-size: 0.9rem;
    padding: 8px 20px;
    background: #e0e0e0;
    color: #555;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.15s;
}

.btn-secondary:hover {
    background: #ccc;
}

/* ---- Game Over Overlay ---- */
#game-over {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.overlay-content {
    background: #fff;
    padding: 40px;
    border-radius: 14px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 300px;
}

.overlay-content h2 {
    font-size: 1.8rem;
    color: #4a6cf7;
}

.overlay-content p {
    font-size: 1.1rem;
    color: #555;
}

#back-lobby-btn,
#solo-new-game-btn {
    font-size: 1rem;
    padding: 10px 24px;
    background: #4a6cf7;
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

#back-lobby-btn:hover,
#solo-new-game-btn:hover {
    background: #3b5de7;
}

/* ---- ELO Badge ---- */
.elo-badge {
    font-size: 0.8rem;
    font-weight: 700;
    color: #f59e0b;
    background: #fef3c7;
    padding: 2px 8px;
    border-radius: 10px;
    letter-spacing: 0.3px;
}

.elo-change {
    font-size: 1rem;
    font-weight: 700;
    min-height: 24px;
}

.elo-change.positive { color: #2e7d32; }
.elo-change.negative { color: #c62828; }
.elo-change.neutral { color: #888; }

/* ---- Give Up / Offer Draw ---- */
#give-up-btn {
    background: #c62828;
}

#give-up-btn:hover {
    background: #a01e1e;
}

#offer-draw-btn {
    background: #757575;
}

#offer-draw-btn:hover {
    background: #616161;
}

#solo-new-game-inline-btn {
    background: #2e7d32;
}

#solo-new-game-inline-btn:hover {
    background: #256d29;
}

#solo-dashboard-inline-btn {
    background: #607d8b;
}

#solo-dashboard-inline-btn:hover {
    background: #546e7a;
}

#offer-draw-btn:disabled,
#give-up-btn:disabled {
    background: #aaa;
    cursor: not-allowed;
}

#pause-btn {
    background: #6366f1;
    transition: background 0.15s;
}

#pause-btn:hover {
    background: #4f46e5;
}

#pause-btn:disabled {
    background: #aaa;
    cursor: not-allowed;
    opacity: 0.7;
}

/* ---- Pause Overlay ---- */
#pause-request-popup {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 260;
    backdrop-filter: blur(4px);
}

#pause-request-popup .overlay-content {
    border: 2px solid #a5b4fc;
}

#pause-request-popup h2 {
    color: #6366f1;
}

#pause-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 270;
    backdrop-filter: blur(20px);
}

#pause-overlay .overlay-content {
    background: #1a1a2e;
    color: #e0e0e0;
}

#pause-overlay h2 {
    color: #a5b4fc;
}

#pause-overlay .muted {
    color: #888;
}

#resume-btn {
    font-size: 1.1rem;
    padding: 12px 32px;
    background: #6366f1;
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

#resume-btn:hover {
    background: #4f46e5;
}

#resume-countdown {
    font-size: 2rem;
    font-weight: 800;
    color: #a5b4fc;
    font-variant-numeric: tabular-nums;
}

/* ---- Draw Offer Popup ---- */
#draw-offer-popup {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 250;
}

.draw-offer-buttons {
    display: flex;
    gap: 12px;
    justify-content: center;
    margin-top: 4px;
}

/* Shared accept button style for popups */
.draw-offer-buttons button:first-child,
#draw-accept-btn,
#pause-accept-btn,
#invite-notify-accept-btn {
    font-size: 1rem;
    padding: 10px 28px;
    background: #2e7d32;
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    transition: background 0.15s, transform 0.1s;
}

#draw-accept-btn:hover,
#pause-accept-btn:hover,
#invite-notify-accept-btn:hover {
    background: #256d29;
    transform: scale(1.03);
}

/* Shared decline button style for popups */
.draw-offer-buttons .btn-secondary {
    font-size: 1rem;
    padding: 10px 28px;
    transition: background 0.15s;
}

/* ---- Invite Notification Popup ---- */
#invite-notify-popup {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 280;
    backdrop-filter: blur(4px);
}

#invite-notify-popup .overlay-content {
    border: 2px solid #4a6cf7;
}

#invite-notify-popup h2 {
    color: #4a6cf7;
}

#friend-accepted-popup {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 280;
    backdrop-filter: blur(4px);
}

#friend-accepted-popup .overlay-content {
    border: 2px solid #27ae60;
}

#friend-accepted-popup h2 {
    color: #27ae60;
}

@media (max-width: 900px) {
    #lobby {
        max-width: 100%;
    }

    .lobby-columns {
        flex-direction: column;
        gap: 14px;
    }

    .lobby-left,
    .lobby-right {
        width: 100%;
    }
}

@media (max-width: 640px) {
    body {
        padding: 8px;
    }

    h1 {
        font-size: 1.4rem;
        margin-bottom: 6px;
    }

    #auth-screen {
        padding: 16px;
        gap: 12px;
    }

    #user-bar {
        width: 100%;
        flex-wrap: wrap;
        justify-content: space-between;
        gap: 8px;
    }

    #user-bar > span {
        width: 100%;
        line-height: 1.35;
        word-break: break-word;
    }

    .lobby-section {
        padding: 14px;
        gap: 8px;
    }

    .lobby-section h2 {
        font-size: 1.05rem;
    }

    .lobby-section select,
    .lobby-section input,
    .lobby-section button {
        font-size: 0.95rem;
    }

    .friend-add-row {
        flex-direction: column;
    }

    #friends-panel li,
    #invites-panel li {
        align-items: flex-start;
        flex-direction: column;
        gap: 6px;
    }

    #friends-panel li > span,
    #invites-panel li > span {
        width: 100%;
        display: flex;
        justify-content: space-between;
        gap: 8px;
        flex-wrap: wrap;
    }

    #game-header {
        gap: 8px;
        margin-bottom: 4px;
    }

    .player-info {
        padding: 4px 6px;
        gap: 2px;
    }

    .timer {
        font-size: 1.3rem;
    }

    #game-controls button {
        padding: 6px 10px;
        font-size: 0.85rem;
    }

    #game-controls {
        gap: 4px;
        margin-bottom: 0;
    }

    #end-turn-container {
        margin-top: 32px;
    }

    #end-turn-btn {
        min-width: min(260px, 100%);
        padding: 12px 28px;
        font-size: 1.05rem;
    }

    h1 {
        font-size: 1.2rem;
        margin-bottom: 4px;
    }

    #board td {
        width: min(52px, calc((100vw - 36px) / 9));
        height: min(52px, calc((100vw - 36px) / 9));
    }
}
