:root {
    --bg-primary: #f8fafc;
    --bg-surface: #ffffff;
    --bg-board: #fdf5e6; /* Old Lace from original WinForms */
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: #cbd5e1;
    --border-dark: #334155;
    --accent-wood: #c68a4c; /* BurlyWood floor indicator */
    --accent-wood-glow: rgba(198, 138, 76, 0.4);
    --wall-color: #475569; /* DarkGray stone walls */
    --wall-shadow: #1e293b;
    --target-color: #2563eb; /* Target / Exit ring */
    --target-fill: rgba(37, 99, 235, 0.15);
    --btn-primary-bg: #dc2626; /* Apple red */
    --btn-primary-hover: #b91c1c;
    --btn-secondary-bg: #e2e8f0;
    --btn-secondary-text: #1e293b;
    --btn-secondary-hover: #cbd5e1;
    --success-color: #16a34a;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-surface: #1e293b;
    --bg-board: #1c1917;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --border-color: #334155;
    --border-dark: #64748b;
    --accent-wood: #d97706;
    --accent-wood-glow: rgba(217, 119, 6, 0.4);
    --wall-color: #334155;
    --wall-shadow: #0f172a;
    --target-color: #60a5fa;
    --target-fill: rgba(96, 165, 250, 0.2);
    --btn-primary-bg: #ef4444;
    --btn-primary-hover: #dc2626;
    --btn-secondary-bg: #334155;
    --btn-secondary-text: #f8fafc;
    --btn-secondary-hover: #475569;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    touch-action: manipulation;
}

.app-wrapper {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    max-width: 1000px;
    margin: 0 auto;
    width: 100%;
}

/* --- WinForms-style Menu Bar --- */
.winforms-menubar {
    display: flex;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
    position: relative;
    user-select: none;
    z-index: 100;
}

.menu-item {
    position: relative;
}

.menu-button {
    background: transparent;
    border: none;
    color: var(--text-primary);
    padding: 0.25rem 0.6rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.menu-button:hover, .menu-button.active {
    background: var(--btn-secondary-bg);
}

.menu-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    box-shadow: var(--shadow-lg);
    min-width: 200px;
    padding: 0.35rem 0;
    display: flex;
    flex-direction: column;
    z-index: 1000;
}

.dropdown-item {
    background: transparent;
    border: none;
    text-align: left;
    padding: 0.45rem 1rem;
    font-size: 0.85rem;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.dropdown-item:hover {
    background: var(--btn-secondary-bg);
}

.dropdown-item.checked::after {
    content: "✓";
    color: var(--btn-primary-bg);
    font-weight: bold;
}

.dropdown-divider {
    height: 1px;
    background: var(--border-color);
    margin: 0.35rem 0;
}

/* --- Action / Parameter Bar --- */
.controls-bar {
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    padding: 0.75rem 1rem;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.params-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem 1rem;
    align-items: center;
}

.param-field {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.825rem;
    font-weight: 500;
}

.param-input {
    width: 48px;
    padding: 0.25rem 0.35rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 0.825rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    text-align: center;
}

.param-input:focus {
    outline: 2px solid var(--btn-primary-bg);
    border-color: transparent;
}

.actions-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn {
    padding: 0.4rem 0.85rem;
    border-radius: 6px;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    transition: all 0.15s ease;
}

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

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

.btn-primary:hover:not(:disabled) {
    background: var(--btn-primary-hover);
}

.btn-secondary {
    background: var(--btn-secondary-bg);
    color: var(--btn-secondary-text);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--btn-secondary-hover);
}

.btn-accent {
    background: var(--accent-wood);
    color: #ffffff;
}

.btn-accent:hover:not(:disabled) {
    filter: brightness(1.1);
}

.btn-toggle-active {
    background: #16a34a !important;
    color: #ffffff !important;
}

/* --- HUD Status Bar --- */
.status-hud {
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    padding: 0.5rem 1rem;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.875rem;
}

.hud-metrics {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 1.25rem;
}

.metric-item {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.metric-label {
    color: var(--text-secondary);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.metric-value {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.winner-banner {
    color: #dc2626;
    font-weight: 800;
    font-size: 1.05rem;
    animation: bounce 0.6s ease infinite alternate;
}

@keyframes bounce {
    from { transform: scale(1); }
    to { transform: scale(1.06); }
}

/* --- Accelerometer Live Widget --- */
.accelerometer-hud {
    background: rgba(34, 197, 94, 0.1);
    border: 1px solid rgba(34, 197, 94, 0.3);
    padding: 0.35rem 0.75rem;
    border-radius: 6px;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.8rem;
}

.accel-indicator {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 8px #22c55e;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* --- Game Board Area --- */
.board-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    position: relative;
    overflow: hidden;
}

.board-container {
    position: relative;
    max-width: min(85vw, 560px);
    max-height: min(85vw, 560px);
    width: 100%;
    aspect-ratio: 1 / 1;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    touch-action: none;
}

/* Board Outer Frame & Grid */
.puzzle-board {
    width: 100%;
    height: 100%;
    position: relative;
    background-color: var(--bg-board);
    box-shadow: var(--shadow-lg), inset 0 0 10px rgba(0, 0, 0, 0.1);
    display: grid;
    cursor: pointer;
    border: 2px solid var(--border-dark);
}

.board-rotating {
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Outer border wall cells */
.board-cell {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.outer-wall {
    background-color: var(--wall-color);
    box-shadow: inset 1px 1px 2px rgba(255, 255, 255, 0.2), inset -1px -1px 2px rgba(0, 0, 0, 0.4);
}

.outer-wall.active-floor {
    background-color: var(--accent-wood);
    box-shadow: 0 0 12px var(--accent-wood-glow), inset 1px 1px 3px rgba(255, 255, 255, 0.3), inset -1px -1px 3px rgba(0, 0, 0, 0.4);
}

.inner-wall {
    background-color: var(--wall-color);
    box-shadow: inset 1px 1px 2px rgba(255, 255, 255, 0.2), inset -1px -1px 2px rgba(0, 0, 0, 0.4);
    border-radius: 2px;
}

.exit-target {
    position: absolute;
    width: 78%;
    height: 78%;
    border-radius: 50%;
    border: 3.5px solid var(--target-color);
    background-color: var(--target-fill);
    pointer-events: none;
    z-index: 2;
    box-shadow: 0 0 6px rgba(37, 99, 235, 0.3);
}

/* --- Fruit Floating Sprites & Sliding Animation --- */
.fruit-sprite {
    position: absolute;
    z-index: 10;
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: left var(--anim-duration, 220ms) cubic-bezier(0.5, 0, 1, 1),
                top var(--anim-duration, 220ms) cubic-bezier(0.5, 0, 1, 1);
    will-change: left, top;
}

.puzzle-board[style*="--anim-duration: 0ms"] .fruit-sprite {
    transition: none !important;
}

.fruit-img {
    width: 90%;
    height: 90%;
    object-fit: contain;
    user-select: none;
    pointer-events: none;
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
}

.fruit-img-2x2 {
    width: 95%;
    height: 95%;
}

/* Rotating overlay controls */
.rotation-controls {
    display: flex;
    gap: 1rem;
    margin-top: 1rem;
    justify-content: center;
}

.rotate-btn {
    width: 54px;
    height: 54px;
    border-radius: 50%;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
}

/* Accelerometer Virtual Tilt Simulator for Desktop */
.tilt-simulator-panel {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    margin: 0.75rem 1rem 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    box-shadow: var(--shadow-sm);
}

/* Strictly hide manual tilt simulator on mobile, touch, or narrow screens */
@media (pointer: coarse), (hover: none), (max-width: 768px) {
    .tilt-simulator-panel {
        display: none !important;
    }
}

.tilt-dial-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.tilt-needle {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid var(--border-dark);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tilt-pointer {
    position: absolute;
    width: 2px;
    height: 10px;
    background: #dc2626;
    top: 2px;
    transform-origin: bottom center;
}

/* Toast notifications */
.toast-alert {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.95);
    color: #ffffff;
    padding: 0.6rem 1.25rem;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    z-index: 2000;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translate(-50%, 10px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(2px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    z-index: 1500;
}

.modal-content {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    max-width: 500px;
    width: 100%;
    padding: 1.5rem;
    box-shadow: var(--shadow-lg);
    max-height: 90vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.modal-title {
    font-size: 1.25rem;
    font-weight: 700;
}

.modal-close {
    background: transparent;
    border: none;
    font-size: 1.25rem;
    color: var(--text-secondary);
    cursor: pointer;
}

.modal-body {
    font-size: 0.9rem;
    line-height: 1.6;
    color: var(--text-primary);
}

.modal-body pre {
    background: var(--bg-primary);
    padding: 0.75rem;
    border-radius: 6px;
    overflow-x: auto;
    font-family: monospace;
    font-size: 0.85rem;
    margin: 0.5rem 0;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.25rem;
}

/* Spinner */
.spinner-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(1px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    z-index: 50;
}

[data-theme="dark"] .spinner-backdrop {
    background: rgba(15, 23, 42, 0.75);
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border-color);
    border-top-color: var(--btn-primary-bg);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* --- Collapsible Header for Phone / Small Screens --- */
.header-collapsible-wrapper {
    transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s ease;
}

.hud-mobile-actions {
    display: none;
}

@media (max-width: 768px) {
    .hud-mobile-actions {
        display: flex;
        align-items: center;
        gap: 0.35rem;
        margin-left: auto;
    }

    .btn-mobile-quick, .btn-mobile-expand {
        padding: 0.25rem 0.55rem;
        font-size: 0.8rem;
        border-radius: 4px;
        white-space: nowrap;
    }

    .btn-mobile-expand {
        background: var(--btn-secondary-bg);
        border: 1px solid var(--border-color);
        font-weight: 600;
        cursor: pointer;
    }

    .btn-mobile-expand:hover {
        background: var(--btn-secondary-hover);
    }

    .header-collapsible-wrapper.is-collapsed {
        max-height: 0;
        opacity: 0;
        overflow: hidden;
        pointer-events: none;
        border: none;
    }

    .header-collapsible-wrapper.is-expanded {
        max-height: 560px;
        opacity: 1;
        overflow: visible;
        pointer-events: auto;
        border-bottom: 2px solid var(--border-color);
    }

    .controls-bar {
        padding: 0.5rem;
    }

    .params-grid {
        justify-content: center;
        gap: 0.4rem 0.6rem;
    }

    .param-field {
        font-size: 0.775rem;
    }

    .param-input {
        width: 42px;
        padding: 0.2rem;
    }

    .actions-group {
        width: 100%;
        justify-content: center;
        margin-top: 0.25rem;
    }

    .status-hud {
        padding: 0.35rem 0.65rem;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        gap: 0.4rem;
        font-size: 0.8rem;
    }

    .hud-metrics {
        gap: 0.6rem;
        font-size: 0.8rem;
    }

    .metric-label {
        font-size: 0.72rem;
    }

    .metric-value {
        font-size: 0.8rem;
    }

    .board-wrapper {
        padding: 0.4rem;
    }

    .board-container {
        max-width: min(94vw, calc(100vh - 145px), 560px);
        max-height: min(94vw, calc(100vh - 145px), 560px);
    }
}

