* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Host-site dark theme colors */
    --host-bg: #11111b;
    --host-surface: #1a1b26;
    --host-royal: #5f57f5;
    --host-royal-secondary: #38348f;
    --host-blue-primary: #2563eb;
    --host-blue-secondary: #1e40af;
    --host-white: #ffffff;
    --blue-accent: #dbeafe;
    --host-gray-muted: #64748b;
    --host-gray-light: #f8fafc;

    /* Tokyo Night colors */
    --tokyo-cyan: #7dcfff;
    --tokyo-green: #9ece6a;
    --tokyo-yellow: #e0af68;
    --tokyo-red: #f7768e;
    --tokyo-border: #3b4261;
    --tokyo-fg: #a9b1d6;

    /* Board colors */
    --square-light: #f0d9b5;
    --square-dark: #b58863;
    --square-selected: #6a994e;
    --move-from: #5090d3;
    --move-to: #81b3f0;
    --checkmated-king: #8b0000;
}

/* Base Layout */
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--host-bg);
    height: 100vh;
    width: 100vw;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.outer-container {
    width: 100%;
    height: 100vh;
    padding: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.container {
    background: var(--host-surface);
    border-radius: 12px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.4);
    padding: 1.5rem;
    width: calc(100% - 16px);
    height: calc(100% - 16px);
    color: var(--tokyo-fg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

main {
    display: grid;
    grid-template-columns: 1fr clamp(280px, 17.71vw, 340px);
    gap: clamp(1rem, 2vw, 2rem);
    flex: 1;
    min-height: 0;
    overflow: hidden;
    height: 100%;
    max-width: clamp(800px, 51vw, 980px);
    margin: 0 auto;
    align-items: center;
}

.game-area {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: clamp(360px, 22.92vw, 440px);
    overflow: visible;
    height: 100%;
}

.board-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: clamp(360px, 22.92vw, 440px);
    height: clamp(360px, 22.92vw, 440px);
    padding: clamp(16px, 1.04vw, 20px);
    position: relative;
}

.board-wrapper {
    position: relative;
    width: clamp(328px, 20.83vw, 400px);
    height: clamp(328px, 20.83vw, 400px);
    margin: 0;
}

.coordinates {
    position: absolute;
    display: flex;
    color: var(--tokyo-border);
    font-size: 0.75rem;
    font-weight: 500;
    user-select: none;
}

.coordinates.top {
    top: -18px;
    left: 0;
    right: 0;
    justify-content: space-around;
    padding: 0 2px;
}

.coordinates.left {
    left: -18px;
    top: 0;
    bottom: 0;
    flex-direction: column;
    justify-content: space-around;
    padding: 2px 0;
}

#board {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    border: 3px solid var(--tokyo-border);
    border-radius: 4px;
}

.square {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(24px, 5vw, 36px);
    cursor: pointer;
    user-select: none;
    position: relative;
    aspect-ratio: 1;
    overflow: hidden;
}

.square.light { background-color: var(--square-light); }
.square.dark { background-color: var(--square-dark); }
.square.selected { background-color: var(--square-selected) !important; }
.square.last-move-from { background-color: var(--move-from) !important; }
.square.last-move-to { background-color: var(--move-to) !important; }
.square.white-piece { color: var(--host-white); text-shadow: 1px 1px 2px rgba(0,0,0,0.5); }
.square.black-piece { color: var(--host-bg); text-shadow: 1px 1px 2px rgba(255,255,255,0.2); }

/* Move feedback animations */
@keyframes flashGreen {
    0%, 100% { background-color: inherit; }
    50% { background-color: rgba(154, 206, 106, 0.8); }
}

@keyframes flashRed {
    0%, 100% { background-color: inherit; }
    50% { background-color: rgba(247, 118, 142, 0.8); }
}

.square.flash-green {
    animation: flashGreen 0.4s ease-out;
}

.square.flash-red {
    animation: flashRed 0.4s ease-out;
    position: relative;
    z-index: 2;
}

.square.flash-red::before {
    content: '';
    position: absolute;
    inset: 0;
    background-color: rgba(247, 118, 142, 0.8);
    animation: flashFade 0.4s ease-out;
    pointer-events: none;
}

@keyframes flashFade {
    0% { opacity: 0; }
    50% { opacity: 1; }
    100% { opacity: 0; }
}

/* Checkmate indicator */
.square.mated-king::after {
    content: '';
    position: absolute;
    inset: 4px;
    border: 3px solid var(--tokyo-red);
    border-radius: 2px;
    pointer-events: none;
    z-index: 1;
}

.square.mated-king.white-piece,
.square.mated-king.black-piece {
    color: var(--checkmated-king) !important;
}

/* Copy Buttons */
.copy-btn {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    padding: 4px;
    background: var(--tokyo-border);
    border: none;
    border-radius: 4px;
    color: var(--tokyo-fg);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s, background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.move-history-container:hover .copy-btn {
    opacity: 0.7;
}

.copy-btn:hover {
    opacity: 1 !important;
    background: var(--host-royal);
}

.copy-btn.copied {
    background: var(--tokyo-green);
    opacity: 1;
}

.copy-btn.copied svg {
    display: none;
}

.copy-btn.copied::after {
    content: 'âœ“';
    font-size: 14px;
}

/* Info Panel */
.info-panel {
    background: var(--host-bg);
    border-radius: 8px;
    padding: clamp(0.75rem, 0.83vw, 1rem);
    display: flex;
    flex-direction: column;
    gap: clamp(0.75rem, 0.83vw, 1rem);
    overflow: hidden;
    width: clamp(280px, 17.71vw, 340px);
    height: clamp(360px, 22.92vw, 440px);
    align-self: center;
}

/* Status Indicators */
.status-indicators {
    display: flex;
    padding: 0.75rem;
    justify-content: space-evenly;
    align-items: center;
    background: var(--host-gray-muted);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    height: auto;
    position: relative;
}

.indicator {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.indicator .light {
    font-size: 2rem;
    font-weight: 500;
    transition: all 0.2s;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.indicator .light[data-status="white-wins"] { color: var(--tokyo-red); }
.indicator .light[data-status="black-wins"] { color: var(--tokyo-red); }

/* Status colors */
.indicator .light[data-status="healthy"] { color: var(--tokyo-green); }
.indicator .light[data-status="ok"] { color: var(--tokyo-green); }
.indicator .light[data-status="disabled"] { color: var(--tokyo-yellow); }
.indicator .light[data-status="degraded"] { color: var(--tokyo-red); }
.indicator .light[data-status="unknown"] { color: var(--tokyo-border); }
.indicator .light[data-status="white"] { color: var(--host-white); }
.indicator .light[data-status="black"] { color: var(--host-bg); }
.indicator .light[data-status="thinking"] {
    color: var(--tokyo-yellow);
    animation: pulse 1.5s infinite;
}
.indicator .light[data-status="network-error"] { color: var(--tokyo-red); }

.indicator .light[data-status="draw"],
.indicator .light[data-status="stalemate"] {
    color: var(--tokyo-yellow);
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.6; transform: scale(0.95); }
}

.indicator:hover::after {
    content: attr(data-tooltip) ": " attr(data-status);
    position: absolute;
    bottom: -30px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--tokyo-border);
    color: var(--tokyo-fg);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    z-index: 10;
    pointer-events: none;
}

/* Error Flash Overlay */
.error-flash-overlay {
    position: absolute;
    inset: 0;
    background: var(--host-bg);
    border-radius: 6px;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
    pointer-events: none;
}

.error-flash-overlay.show {
    display: flex;
    animation: none;
}

.error-flash-message {
    color: var(--tokyo-red);
    font-size: 0.85rem;
    font-weight: 500;
    text-align: center;
    padding: 0.5rem;
}

/* Move History */
.move-history-container {
    flex: 1 1 auto;
    min-height: 0;
    max-height: 100%;
    position: relative;
    background: var(--host-surface);
    border-radius: 6px;
    border: 1px solid var(--tokyo-border);
    overflow: hidden;
}

.move-history-container .copy-btn {
    right: 8px;
    top: 8px;
    transform: none;
}

.move-history {
    height: 100%;
    overflow-y: auto;
    overflow-x: auto;
    padding: 0.75rem;
}

.move-history::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.move-history::-webkit-scrollbar-track {
    background: transparent;
}

.move-history::-webkit-scrollbar-thumb {
    background: var(--tokyo-border);
    border-radius: 3px;
}

.move-grid {
    display: grid;
    grid-template-columns: 2.5rem 1fr 1fr;
    gap: 0.25rem 0.5rem;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
    align-items: center;
}

.move-number {
    color: var(--tokyo-yellow);
    text-align: right;
    font-weight: 600;
}

.move-white,
.move-black {
    padding: 0.25rem 0.5rem;
    border-radius: 3px;
}

.move-white:hover,
.move-black:hover {
    background: var(--host-royal);
}

.move-white { color: var(--host-gray-light); }
.move-black { color: var(--tokyo-cyan); }

/* Controls */
.controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    flex-shrink: 0;
}

.btn {
    padding: 0.75rem;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    color: var(--host-white);
    transition: all 0.2s;
}

.btn-primary {
    background: var(--host-blue-primary);
    color: var(--host-white);
}

.btn-primary:hover {
    background: var(--host-royal);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--host-blue-secondary);
    color: var(--host-white);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--host-royal);
    transform: translateY(-1px);
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 2000;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(4px);
}

.modal-overlay.show {
    display: flex;
}

.modal {
    background: var(--host-surface);
    border: 1px solid var(--tokyo-border);
    border-radius: 12px;
    padding: 2rem;
    width: 90%;
    max-width: 400px;
    color: var(--tokyo-fg);
}

.modal h2 {
    margin-bottom: 1.5rem;
    color: var(--host-royal);
    text-align: center;
}

.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--tokyo-cyan);
}

.form-group .group-label {
    text-align: center;
}

.radio-group {
    display: flex;
    gap: 1.5rem;
    justify-content: center;
}

.radio-group label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
    color: var(--tokyo-fg);
}

input[type="range"] {
    width: 100%;
    height: 4px;
    background: var(--tokyo-border);
    border-radius: 2px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 16px;
    height: 16px;
    background: var(--host-gray-muted);
    border-radius: 50%;
    cursor: pointer;
}

/* Modal FEN input */
.fen-input {
    width: 100%;
    padding: 0.5rem;
    background: var(--host-bg);
    border: 1px solid var(--tokyo-border);
    border-radius: 4px;
    color: var(--tokyo-fg);
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    resize: none;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.fen-input:focus {
    outline: none;
    border-color: var(--host-royal);
}

.modal-buttons {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-top: 2rem;
}

/* Auth Tabs */
.auth-modal {
    max-width: 360px;
}

.auth-tabs {
    display: flex;
    gap: 0;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid var(--tokyo-border);
}

.auth-tab {
    flex: 1;
    padding: 0.75rem;
    background: transparent;
    border: none;
    color: var(--tokyo-fg);
    cursor: pointer;
    font-size: 0.95rem;
    transition: color 0.2s, border-color 0.2s;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}

.auth-tab:hover {
    color: var(--host-royal);
}

.auth-tab.active {
    color: var(--host-royal);
    border-bottom-color: var(--host-royal);
}

.auth-form input[type="text"],
.auth-form input[type="email"],
.auth-form input[type="password"] {
    width: 100%;
    padding: 0.5rem;
    background: var(--host-bg);
    border: 1px solid var(--tokyo-border);
    border-radius: 4px;
    color: var(--tokyo-fg);
    font-size: 0.9rem;
}

.auth-form input:focus {
    outline: none;
    border-color: var(--host-royal);
}

/* Auth Indicator */
.auth-indicator .light[data-status="anonymous"] {
    color: var(--tokyo-border);
}

.auth-indicator .light[data-status="authenticated"] {
    color: var(--tokyo-green);
}

.auth-indicator {
    cursor: pointer;
}

/* Mobile/Responsiveness */
@media (max-width: 978px) {

    body {
        height: auto;
        min-height: 100vh;
        overflow-y: auto;
        overflow-x: hidden;
        scrollbar-gutter: stable;
    }

    .outer-container {
        padding: 8px;
        min-height: 100vh;
        height: auto;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        overflow: visible;
    }

    .container {
        border-radius: 12px;
        width: calc(100% - 16px);
        min-height: calc(100vh - 16px);
        height: auto;
        padding: 1rem;
        margin: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        overflow: visible;
    }

    main {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto;
        gap: 0.5rem;
        height: auto;
        max-width: none;
        margin: 0 auto;
        align-items: center;
        justify-items: center;
        overflow: visible;
        flex-shrink: 0;
    }

    .game-area {
        min-width: auto;
        height: auto;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .board-container {
        width: clamp(360px, 45vw, 440px);
        height: clamp(360px, 45vw, 440px);
        padding: clamp(16px, 2.04vw, 20px);
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        flex-shrink: 0;
    }

    .board-wrapper {
        width: clamp(328px, 41vw, 400px);
        height: clamp(328px, 41vw, 400px);
        position: relative;
        margin: 0;
    }

    .coordinates.top {
        top: -18px;
        left: 0;
        right: 0;
    }

    .coordinates.left {
        left: -18px;
        top: 0;
        bottom: 0;
    }

    #board {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .info-panel {
        width: clamp(360px, 45vw, 440px);
        height: clamp(180px, 20.4vw, 200px);
        display: grid;
        grid-template-columns: clamp(150px, 18.4vw, 180px) 1fr;
        grid-template-rows: min-content min-content 1fr;
        gap: 0.5rem 1rem;
        padding: clamp(0.75rem, 1.02vw, 1rem);
        align-self: center;
        overflow: hidden;
        flex-shrink: 0;
        margin-bottom: 1rem;
    }

    .status-indicators {
        grid-column: 1;
        grid-row: 1;
        display: flex;
        padding: 0.25rem;
        justify-content: space-evenly;
        align-items: center;
        background: var(--host-gray-muted);
        border: none;
        border-radius: 6px;
        height: auto;
        margin: 0;
    }

    .controls {
        grid-column: 1;
        grid-row: 2 / 4;
        grid-template-columns: 1fr;
        grid-template-rows: 1fr 1fr;
        gap: 0.5rem;
        align-self: stretch;
        height: auto;
        min-height: 80px;
    }

    .btn {
        padding: 0.5rem;
        font-size: 0.85rem;
        min-height: 35px;
        height: 100%;
        width: 100%;
    }

    .move-history-container {
        grid-column: 2;
        grid-row: 1 / 4;
        height: 100%;
        min-height: 0;
        max-height: 100%;
        align-self: stretch;
    }

    .move-grid {
        grid-template-columns: 2rem 1fr 1fr;
        gap: 0.25rem 0.5rem;
        font-size: 0.85rem;
    }

    .move-history {
        padding: 0.5rem;
    }

    .square {
        font-size: clamp(24px, 5vw, 36px);
    }
}

@media (max-width: 530px) {
    body {
        overflow-y: auto;
        overflow-x: auto;
        min-width: clamp(440px, 100vw, 530px);
    }

    .outer-container {
        width: clamp(440px, 100vw, 530px);
        min-width: clamp(440px, 100vw, 530px);
        padding: 8px;
        min-height: 100vh;
        height: auto;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        overflow: visible;
    }

    .container {
        width: calc(100% - 16px);
        min-width: clamp(424px, calc(100vw - 16px), 514px);
        border-radius: 12px;
        min-height: calc(100vh - 16px);
        height: auto;
        padding: 1rem;
        margin: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        overflow: visible;
    }

    .board-container,
    .info-panel {
        width: clamp(360px, 83vw, 440px);
        min-width: clamp(360px, 83vw, 440px);
    }
}