html, body {
    overflow-x: hidden;
    max-width: 100vw;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    overscroll-behavior: none;
    margin: 0;
    padding: 0;
}

.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    position: relative;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}

.history-toggle-btn {
    padding: 8px 16px;
    background: #2196F3;
    color: white;
    border: none;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 15px;
    transition: all 0.2s;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}

.history-toggle-btn:hover {
    background: #1976D2;
    transform: translateY(-2px);
}

.history-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.history-popup {
    background: white;
    border-radius: 12px;
    padding: 20px;
    width: 90%;
    max-width: 350px;
    max-height: 70vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}

.history-popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.history-popup-header h3 {
    margin: 0;
    font-size: 18px;
    color: #2196F3;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}

.history-close-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: #666;
    padding: 0 5px;
}

.history-close-btn:hover {
    color: #333;
}

.history-popup .history-list {
    overflow-y: auto;
    flex: 1;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.history-item {
    display: grid;
    grid-template-columns: 25px 1fr auto;
    grid-template-rows: auto auto;
    align-items: center;
    gap: 4px 8px;
    padding: 6px;
    background: #f5f5f5;
    border-radius: 4px;
    font-size: 14px;
}

.medal {
    font-size: 16px;
    text-align: center;
    grid-row: 1 / 3;
}

.game-num {
    font-weight: 500;
    color: #333;
}

.game-time {
    color: #666;
    font-family: monospace;
}

.game-seed {
    grid-column: 2 / 4;
    font-family: monospace;
    font-size: 11px;
    color: #2196F3;
    padding: 4px 8px;
    background: #e3f2fd;
    border-radius: 3px;
    text-align: left;
    text-decoration: none;
    display: block;
    cursor: pointer;
    border: 1px solid #bbdefb;
}

.game-seed:hover {
    background: #bbdefb;
    text-decoration: underline;
}

.help-button {
    position: relative;
    display: inline-block;
    cursor: help;
}

.help-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    background: #2196F3;
    color: white;
    border-radius: 50%;
    font-size: 16px;
    font-weight: bold;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    user-select: none;
    transition: background 0.2s;
}

.help-button:hover .help-icon,
.help-button:focus .help-icon {
    background: #1976D2;
}

.help-tooltip {
    visibility: hidden;
    opacity: 0;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    color: #333;
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    width: 280px;
    max-width: calc(100vw - 40px);
    font-size: 14px;
    line-height: 1.5;
    z-index: 1000;
    transition: opacity 0.2s, visibility 0.2s;
    border: 2px solid #2196F3;
    text-align: left;
}

.help-tooltip::before {
    display: none;
}

.help-button:hover .help-tooltip,
.help-button:focus .help-tooltip {
    visibility: visible;
    opacity: 1;
}

.help-tooltip strong {
    color: #2196F3;
}

/* Mobile: tap to toggle */
@media (max-width: 768px) {
    .help-button {
        cursor: pointer;
    }
}
    padding: 20px;
    font-family: Arial, sans-serif;
}

h1 {
    margin-bottom: 10px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 700;
}

h2 {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    font-weight: 600;
}

p {
    margin: 5px 0;
    color: #666;
}

.controls {
    margin: 20px 0;
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: center;
}

.timer-display {
    font-size: 16px;
    font-weight: bold;
    color: #666;
    font-family: 'Courier New', monospace;
    background-color: #f5f5f5;
    padding: 6px 12px;
    border-radius: 4px;
    border: 1px solid #ddd;
    margin: 8px 0;
    display: inline-block;
    text-align: center;
}

.size-buttons {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: center;
    max-width: 100%;
}

.size-btn {
    padding: 8px 12px;
    cursor: pointer;
    border: 2px solid #666;
    background-color: #f0f0f0;
    border-radius: 4px;
    font-weight: bold;
    transition: all 0.2s;
}

.size-btn:hover:not(:disabled) {
    background-color: #e0e0e0;
    transform: translateY(-2px);
}

.size-btn.active {
    background-color: #4CAF50;
    color: white;
    border-color: #4CAF50;
}

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

.controls button:not(.size-btn) {
    padding: 8px 20px;
    cursor: pointer;
    border: 2px solid #666;
    background-color: #2196F3;
    color: white;
    border-radius: 4px;
    font-weight: bold;
    transition: all 0.2s;
}

.controls button:not(.size-btn):hover:not(:disabled) {
    background-color: #1976D2;
}

.controls button:not(.size-btn):disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.toggles-container {
    display: flex;
    gap: 30px;
    margin-top: 20px;
    justify-content: center;
    align-items: center;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    user-select: none;
    font-size: 14px;
    color: #333;
}

.toggle-switch {
    position: relative;
    width: 50px;
    height: 26px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: 0.3s;
    border-radius: 26px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

.toggle-switch input:checked + .toggle-slider {
    background-color: #2196F3;
}

.toggle-switch input:checked + .toggle-slider:before {
    transform: translateX(24px);
}

.toggle-switch input:disabled + .toggle-slider {
    opacity: 0.5;
    cursor: not-allowed;
}

.controls label {
    display: flex;
    align-items: center;
    gap: 8px;
}

.spinner-container {
    margin: 20px 0;
    text-align: center;
}

.spinner {
    font-size: 64px;
    animation: spin 1.5s linear infinite;
    margin: 20px 0;
}

.spinner-container p {
    font-size: 18px;
    color: #666;
    font-weight: bold;
}

.generation-log {
    margin-top: 20px;
    background-color: #f5f5f5;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 15px;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
    height: 300px;
    overflow-y: auto;
    font-family: 'Courier New', monospace;
    font-size: 13px;
    text-align: left;
    display: flex;
    flex-direction: column-reverse;
}

.log-entry {
    padding: 3px 0;
    color: #333;
    animation: fadeIn 0.3s;
    white-space: nowrap;
}

.log-entry:first-child {
    font-weight: bold;
    color: #2196F3;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateX(-10px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.board {
    display: grid;
    gap: 2px;
    border: 3px solid #333;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    background-color: #333;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    /* Scale board to fit screen for larger sizes */
    width: min(calc(var(--board-size) * 50px + var(--board-size) * 2px + 6px), calc(100vw - 40px));
    margin: 20px auto;
    max-width: 100%;
}

.cell {
    width: 100%;
    height: 100%;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    /* Scale font-size based on board size - 64% of cell size */
    font-size: calc(min(50px, (100vw - 40px - var(--board-size) * 2px - 6px) / var(--board-size)) * 0.64);
    transition: all 0.2s;
    position: relative;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    outline: none;
    -webkit-tap-highlight-color: transparent;
    box-sizing: border-box;
}

/* Bold borders for region boundaries */
.cell.border-top {
    border-top: 1px solid #333;
}

.cell.border-bottom {
    border-bottom: 1px solid #333;
}

.cell.border-left {
    border-left: 1px solid #333;
}

.cell.border-right {
    border-right: 1px solid #333;
}

/* Responsive scaling for mobile devices */
@media (max-width: 600px) {
    .game-container {
        padding: 3px 10px;
        width: 100%;
        box-sizing: border-box;
    }
    
    .size-buttons {
        gap: 3px;
    }
    
    .size-btn {
        padding: 5px 7px;
        font-size: 12px;
        flex: 1;
        min-width: 0;
    }
    
    h1 {
        font-size: 22px;
        margin: 3px 0 5px 0;
    }
    
    h2 {
        margin: 8px 0 3px 0;
        font-size: 20px;
    }
    
    p {
        font-size: 12px;
        margin: 2px 0;
        line-height: 1.2;
    }
    
    .controls {
        gap: 6px;
        margin: 8px 0;
    }
    
    .controls button:not(.size-btn) {
        padding: 5px 14px;
        font-size: 14px;
    }
    
    .timer-display {
        margin: 3px 0;
        padding: 3px 8px;
        font-size: 13px;
    }
    
    .board {
        /* Constrain board width on mobile - make it smaller to fit */
        width: min(calc(100vw - 20px), 360px);
        margin: 3px auto;
        box-sizing: border-box;
    }
    
    .toggles-container {
        margin-top: 8px;
        gap: 15px;
        font-size: 13px;
    }
    
    .cell {
        /* Font size scales with available space on mobile */
        font-size: calc((min(calc(100vw - 20px), 370px) - var(--board-size) * 2px - 6px) / var(--board-size) * 0.64);
    }
    
    .toggles-container {
        flex-direction: column;
        gap: 15px;
    }
    
    .size-buttons {
        max-width: 100%;
    }
}

.cell.region-0 { background-color: #ff9999; }
.cell.region-1 { background-color: #99ccff; }
.cell.region-2 { background-color: #99ff99; }
.cell.region-3 { background-color: #ffcc99; }
.cell.region-4 { background-color: #ff99ff; }
.cell.region-5 { background-color: #ffff99; }
.cell.region-6 { background-color: #99ffcc; }
.cell.region-7 { background-color: #cc99ff; }
.cell.region-8 { background-color: #ffcccc; }
.cell.region-9 { background-color: #ccffcc; }

.cell.conflict {
    background-color: #ff4444 !important;
    animation: shake 0.3s;
}

.queen {
    user-select: none;
    filter: drop-shadow(2px 2px 2px rgba(0,0,0,0.3));
    /* Inherit font-size from cell */
    font-size: 1em;
}

.cross {
    user-select: none;
    /* Scale slightly smaller than queens - 87.5% of queen size */
    font-size: 0.875em;
    color: #333;
    opacity: 0.6;
}

.cross.auto {
    opacity: 0.4;
    color: #666;
}

.win-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 20px;
    z-index: 1000;
    animation: fadeIn 0.5s;
}

.win-message {
    background: white;
    padding: 30px 25px;
    border-radius: 16px;
    font-size: 24px;
    font-weight: bold;
    color: #2ecc71;

    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    max-width: 90%;
}

.share-seed-btn {
    display: block;
    margin: 15px auto 0;
    padding: 10px 20px;
    font-size: 14px;
    font-weight: 600;
    color: #2196F3;
    background: #e3f2fd;
    border: 1px solid #bbdefb;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.2s;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}

.share-seed-btn:hover {
    background: #bbdefb;
}

.new-game-btn {
    margin-top: 20px;
    padding: 12px 30px;
    font-size: 18px;
    font-weight: 600;
    color: white;
    background-color: #2196F3;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}

.new-game-btn:hover {
    background-color: #1976D2;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(33, 150, 243, 0.4);
}

.new-game-btn:active {
    transform: translateY(0);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

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

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}
