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

/* Disable page zoom */
html, body {
    overscroll-behavior: none;
}

/* iOS PWA safe area support */
html {
    height: 100%;
    height: -webkit-fill-available;
}

:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-cell: #0f0f23;
    --text-primary: #ffffff;
    --text-secondary: #a0a0a0;
    --accent: #ffd700;
    --accent-hover: #ffed4a;
    --error: #ff4757;
    --success: #2ed573;
    --border-thin: #4a4a6a;
    --border-thick: #ffffff;
    --cell-hover: #252550;
    --cell-x: #666688;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    min-height: -webkit-fill-available;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    /* iOS safe area insets for standalone PWA mode */
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
}

.container {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    position: relative;
    z-index: 1;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

h1 {
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: 1px;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 15px;
}

.timer {
    font-size: 1.5rem;
    font-weight: 600;
    font-family: 'Courier New', monospace;
    color: var(--accent);
    min-width: 70px;
    text-align: right;
}

.help-btn,
.settings-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 2px solid var(--text-primary);
    background: transparent;
    color: var(--text-primary);
    font-size: 1.2rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s ease;
    touch-action: manipulation; /* Disable double-tap zoom */
}

.settings-btn {
    font-weight: 600;
}

.help-btn:hover,
.settings-btn:hover {
    background: var(--text-primary);
    color: var(--bg-primary);
}

main {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.game-info {
    display: flex;
    justify-content: space-between;
    font-size: 1rem;
    color: var(--text-secondary);
}

.game-info span {
    color: var(--text-primary);
    font-weight: 600;
}

.grid-container {
    display: flex;
    justify-content: center;
}

.grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    gap: 0;
    border: 3px solid var(--border-thick);
    background: var(--border-thin);
    width: 100%;
    max-width: 400px;
    aspect-ratio: 1;
    touch-action: none; /* Disable pan + pinch on game surface */
}

.cell {
    background: var(--bg-cell);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.8rem;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s ease;
    border: 1px solid var(--border-thin);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    position: relative;
}

.cell:hover {
    background: var(--cell-hover);
}

.cell.star {
    color: var(--accent);
    text-shadow: 0 0 10px var(--accent);
}

.cell.star.invalid {
    color: var(--error);
    text-shadow: 0 0 10px var(--error);
    animation: shake 0.3s ease;
}

.cell.x-mark {
    color: var(--cell-x);
}

/* Region borders - these will be added dynamically */
.cell.border-top {
    border-top: 3px solid var(--border-thick);
}

.cell.border-right {
    border-right: 3px solid var(--border-thick);
}

.cell.border-bottom {
    border-bottom: 3px solid var(--border-thick);
}

.cell.border-left {
    border-left: 3px solid var(--border-thick);
}

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

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

.btn {
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    touch-action: manipulation; /* Disable double-tap zoom on buttons */
}

.btn-primary {
    background: var(--accent);
    color: var(--bg-primary);
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-2px);
}

.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 2px solid var(--border-thin);
}

.btn-secondary:hover {
    background: var(--cell-hover);
    border-color: var(--text-primary);
}

/* Win Message */
.win-message {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 100;
    animation: fadeIn 0.3s ease;
}

.win-message.show {
    display: flex;
}

.win-content {
    background: var(--bg-secondary);
    padding: 40px;
    border-radius: 16px;
    text-align: center;
    border: 3px solid var(--accent);
    animation: popIn 0.3s ease;
}

.win-content h2 {
    font-size: 2rem;
    margin-bottom: 15px;
}

.win-content p {
    font-size: 1.2rem;
    margin-bottom: 10px;
    color: var(--text-secondary);
}

.win-content .new-record {
    color: var(--accent);
    font-weight: 700;
    font-size: 1.3rem;
}

.win-content .btn {
    margin-top: 20px;
}

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

@keyframes popIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* Help Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 200;
    padding: 20px;
}

.modal.show {
    display: flex;
}

.modal-content {
    background: var(--bg-secondary);
    padding: 30px;
    border-radius: 16px;
    max-width: 450px;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
    border: 2px solid var(--border-thick);
}

.modal-close {
    position: absolute;
    top: 10px;
    right: 15px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 2rem;
    cursor: pointer;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-content h2 {
    margin-bottom: 20px;
    padding-right: 30px;
}

.help-section {
    margin-bottom: 20px;
}

.help-section h3 {
    color: var(--accent);
    margin-bottom: 10px;
    font-size: 1.1rem;
}

.help-section p,
.help-section ul {
    color: var(--text-secondary);
    line-height: 1.6;
}

.help-section ul {
    padding-left: 20px;
}

.help-section li {
    margin-bottom: 8px;
}

.help-section strong {
    color: var(--text-primary);
}

/* Settings Modal */
.settings-section {
    margin-bottom: 20px;
}

.settings-section h3 {
    color: var(--accent);
    margin-bottom: 12px;
    font-size: 1.1rem;
}

.toggle {
    display: flex;
    align-items: center;
    gap: 12px;
    user-select: none;
    -webkit-user-select: none;
    cursor: pointer;
    color: var(--text-secondary);
}

.toggle input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.toggle-track {
    width: 46px;
    height: 26px;
    border-radius: 999px;
    border: 2px solid var(--border-thin);
    background: var(--bg-cell);
    position: relative;
    flex: 0 0 auto;
    transition: background 0.2s ease, border-color 0.2s ease;
}

.toggle-track::after {
    content: "";
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--text-primary);
    position: absolute;
    top: 50%;
    left: 4px;
    transform: translateY(-50%);
    transition: transform 0.2s ease, background 0.2s ease;
}

.toggle input:checked + .toggle-track {
    background: var(--accent);
    border-color: var(--accent);
}

.toggle input:checked + .toggle-track::after {
    transform: translate(20px, -50%);
    background: var(--bg-primary);
}

.toggle-label {
    color: var(--text-primary);
    font-weight: 600;
}

.toggle input:focus-visible + .toggle-track {
    outline: 3px solid rgba(255, 255, 255, 0.35);
    outline-offset: 2px;
}

/* Optional Christmas theme */
body.theme-christmas {
    --bg-primary: #0b1b14;
    --bg-secondary: #112c21;
    --bg-cell: #07140f;
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.72);
    --accent: #ffd56a;        /* warm gold */
    --accent-hover: #ffe49b;
    --error: #ff6b6b;
    --success: #2ed573;
    --border-thin: #2b5f49;   /* evergreen */
    --border-thick: #ffffff;
    --cell-hover: #123a2b;
    --cell-x: #6aa38a;
}

body.theme-christmas::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    opacity: 0.22;
    background:
        radial-gradient(circle at 10% 20%, rgba(255,255,255,0.55) 0 2px, transparent 4px),
        radial-gradient(circle at 70% 30%, rgba(255,255,255,0.5) 0 2px, transparent 4px),
        radial-gradient(circle at 30% 80%, rgba(255,255,255,0.45) 0 2px, transparent 4px),
        radial-gradient(circle at 90% 75%, rgba(255,255,255,0.45) 0 2px, transparent 4px);
    background-size: 140px 140px, 180px 180px, 220px 220px, 260px 260px;
    animation: snowDrift 18s linear infinite;
}

body.theme-christmas::after {
    content: "";
    position: fixed;
    inset: -20%;
    pointer-events: none;
    z-index: 0;
    opacity: 0.14;
    background:
        radial-gradient(closest-side at 20% 25%, rgba(255, 0, 70, 0.30), transparent 65%),
        radial-gradient(closest-side at 78% 18%, rgba(0, 255, 140, 0.26), transparent 62%),
        radial-gradient(closest-side at 82% 78%, rgba(255, 214, 80, 0.26), transparent 60%);
    filter: blur(2px);
}

body.theme-christmas h1::after {
    content: " 🎄";
}

body.theme-christmas .grid {
    box-shadow: 0 10px 35px rgba(0, 0, 0, 0.35);
}

@keyframes snowDrift {
    from { transform: translateY(-40px); }
    to { transform: translateY(40px); }
}

@media (prefers-reduced-motion: reduce) {
    body.theme-christmas::before {
        animation: none;
    }
}

/* Responsive adjustments */
@media (max-width: 480px) {
    body {
        padding: 10px;
        padding-top: max(10px, env(safe-area-inset-top));
        padding-bottom: max(10px, env(safe-area-inset-bottom));
        padding-left: max(10px, env(safe-area-inset-left));
        padding-right: max(10px, env(safe-area-inset-right));
    }
    
    h1 {
        font-size: 1.4rem;
    }
    
    .timer {
        font-size: 1.2rem;
    }
    
    .cell {
        font-size: 1.4rem;
    }
    
    .btn {
        padding: 10px 18px;
        font-size: 0.9rem;
    }
    
    .modal-content {
        padding: 20px;
    }
}

/* PWA Install prompt styling */
.install-prompt {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-secondary);
    padding: 15px 25px;
    border-radius: 12px;
    border: 2px solid var(--accent);
    display: none;
    align-items: center;
    gap: 15px;
    z-index: 50;
}

.install-prompt.show {
    display: flex;
}

.install-prompt button {
    padding: 8px 16px;
    font-size: 0.9rem;
}

.install-dismiss {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.5rem;
}

/* Update Banner - visas när ny service worker finns */
.update-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-top: 2px solid var(--accent);
    padding: 12px 20px;
    display: none;
    justify-content: space-between;
    align-items: center;
    z-index: 1000;
    animation: slideUp 0.3s ease;
}

.update-banner.show {
    display: flex;
}

.update-banner span {
    font-weight: 500;
}

.update-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn-small {
    padding: 8px 16px;
    font-size: 0.85rem;
}

.update-dismiss {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 5px;
    line-height: 1;
}

.update-dismiss:hover {
    color: var(--text-primary);
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}
