:root {
    --cell-size: 56px;
    --gap: 6px;
    --board-max-width: 100vw;
    --primary-color: #2563eb;
    --success-color: #059669;
    --warning-color: #d97706;
    --danger-color: #dc2626;
    --surface: #fff;
    --surface-secondary: #f8fafc;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-secondary: #64748b;
    --shadow: 0 1px 3px 0 rgb(0 0 0 / .1), 0 1px 2px -1px rgb(0 0 0 / .1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / .1), 0 4px 6px -4px rgb(0 0 0 / .1)
}

* {
    -webkit-touch-callout: none;
    box-sizing: border-box
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #f8fafc 0, #e2e8f0 100%);
    color: var(--text);
    padding: 20px;
    margin: 0;
    min-height: 100vh;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none
}

.container {
    max-width: 600px;
    margin: 0 auto
}

h1 {
    font-size: 20px;
    font-weight: 600;
    margin: 0 0 20px;
    text-align: center;
    color: var(--text-secondary);
    text-shadow: none
}

select,
button,
input {
    font-size: 14px;
    padding: 10px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    font-weight: 500;
    touch-action: manipulation;
    transition: all .2s ease
}

button {
    background: var(--primary-color);
    color: white;
    border: 0;
    cursor: pointer;
    font-weight: 600;
    height: 40px
}

button:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-lg)
}

button:active {
    transform: translateY(0)
}

button.secondary {
    background: var(--surface-secondary);
    color: var(--text);
    border: 1px solid var(--border)
}

button.challenge {
    background: var(--warning-color);
    color: white
}

.stat-value.countdown {
    color: var(--warning-color);
    font-size: 24px
}

.stat-value.countdown.danger {
    color: var(--danger-color);
    animation: pulse 1s infinite
}

@keyframes pulse {

    0%,
    100% {
        transform: scale(1)
    }

    50% {
        transform: scale(1.1)
    }
}

.challenge-result-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(8px);
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 200;
    padding: 24px;
    text-align: center
}

.debug-panel {
    position: fixed;
    top: 460px;
    left: 10px;
    width: 250px;
    max-height: 400px;
    background: rgba(0, 0, 0, 0.9);
    color: #0f0;
    font-family: 'Monaco', 'Consolas', monospace;
    font-size: 11px;
    border-radius: 8px;
    padding: 0;
    overflow: hidden;
    z-index: 1000;
    display: none;
    border: 2px solid #0f0
}

.debug-panel.visible {
    display: block
}

.debug-log {
    margin: 2px 0;
    padding: 2px 0;
    border-bottom: 1px solid #333
}

/* Sticky Header für Debug-Panel */
.debug-panel-header {
    position: sticky;
    top: 0;
    background: rgba(0, 0, 0, 0.95);
    padding: 8px;
    margin: 0;
    border-bottom: 2px solid #0f0;
    border-radius: 6px 6px 0 0;
    display: flex;
    justify-content: center;
    gap: 8px;
    z-index: 10;
    box-sizing: border-box;
}

/* Debug Logs Container */
#debugLogs {
    padding: 10px;
    max-height: 340px;
    overflow-y: auto;
    box-sizing: border-box;
}

/* Neue Button-Styles */
.debug-clear-btn,
.debug-copy-btn {
    background: #f44;
    color: white;
    border: 0;
    padding: 4px 8px;
    font-size: 10px;
    cursor: pointer;
    border-radius: 3px;
    position: static;
    font-family: 'Monaco', 'Consolas', monospace;
}

.debug-copy-btn {
    background: #0a84ff;
}

.debug-clear-btn:hover {
    background: #f66;
}

.debug-copy-btn:hover {
    background: #2a9aff;
}

.challenge-result-content {
    background: var(--surface);
    padding: 40px;
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    max-width: 500px;
    width: 90%;
    animation: challengeResultSlideIn .5s ease-out
}

@keyframes challengeResultSlideIn {
    from {
        opacity: 0;
        transform: translateY(-30px)
    }

    to {
        opacity: 1;
        transform: translateY(0)
    }
}

.challenge-result-overlay h2 {
    color: var(--warning-color);
    margin: 0 0 20px 0;
    font-size: 36px;
    font-weight: 700
}

.challenge-score {
    font-size: 48px;
    font-weight: 800;
    color: var(--primary-color);
    margin: 20px 0
}

.challenge-stats {
    display: flex;
    justify-content: space-around;
    margin: 20px 0;
    padding: 20px;
    background: var(--surface-secondary);
    border-radius: 8px
}

.challenge-stat {
    text-align: center
}

.challenge-stat-label {
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .5px;
    margin-bottom: 5px
}

.challenge-stat-value {
    font-size: 20px;
    font-weight: 700;
    color: var(--text)
}

.challenge-buttons {
    display: flex;
    gap: 16px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 24px;
    padding-top: 20px;
    border-top: 1px solid var(--border)
}

.challenge-buttons .primary-action {
    font-size: 16px;
    font-weight: 700;
    padding: 14px 24px;
    min-width: 160px;
    order: 1
}

.challenge-buttons .secondary {
    padding: 12px 20px;
    min-width: 140px
}


@media(max-width:480px) {
    .challenge-buttons {
        flex-direction: column;
        gap: 12px
    }

    .challenge-buttons button {
        width: 100%;
        min-width: unset
    }
}

button:disabled {
    opacity: .5;
    cursor: not-allowed;
    pointer-events: none
}

select {
    cursor: pointer
}

input {
    min-width: 140px
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px
}

.stat-label {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .5px
}

.stat-value {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary-color)
}

.board {
    position: relative;
    display: grid;
    background: var(--surface-secondary);
    padding: 16px;
    border: 2px solid var(--border);
    border-radius: 12px;
    touch-action: none;
    max-width: calc(100vw - 80px);
    overflow: hidden;
    width: fit-content;
    margin: 0 auto
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    position: relative;
    z-index: 0;
    touch-action: none;
    transition: all .15s ease;
    font-weight: 600;
    font-size: 10px
}

.cell:hover {
    transform: scale(1.05)
}

.cell.red {
    background-color: #d9534f;
    color: white
}

.cell.yellow {
    background-color: #f0ad4e;
    color: white
}

.cell.green {
    background-color: #5cb85c;
    color: white
}

.cell.start {
    border: 3px solid var(--primary-color);
    box-shadow: 0 0 0 3px rgb(37 99 235 / .1)
}

.cell.end {
    border: 3px dashed var(--primary-color);
    box-shadow: 0 0 0 3px rgb(37 99 235 / .1)
}

.cell.start,
.cell.end {
    font-weight: 700;
    font-size: 10px;
    text-shadow: 0 1px 2px rgb(0 0 0 / .3)
}

.path-line {
    position: absolute;
    background: #000;
    z-index: 10;
    pointer-events: none;
    border-radius: 2px;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5)
}

.win-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(8px);
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
    padding: 24px;
    text-align: center;
    border-radius: 12px
}

.win-overlay h2 {
    color: var(--success-color);
    margin: 0 0 16px 0;
    font-size: 32px;
    font-weight: 700
}

.win-overlay .congrats {
    font-size: 18px;
    color: var(--text);
    margin-bottom: 20px
}

.win-overlay .share-text {
    background: var(--surface-secondary);
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: 'Monaco', 'Menlo', monospace;
    margin: 16px 0;
    white-space: pre-wrap;
    font-size: 12px;
    max-width: 100%;
    color: var(--text-secondary)
}

.win-overlay .win-buttons {
    display: flex;
    gap: 12px;
    margin-top: 20px;
    flex-wrap: wrap
}

.win-overlay button {
    padding: 12px 20px;
    font-size: 14px;
    font-weight: 600
}

.top-controls {
    display: flex;
    gap: 16px;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    padding: 12px;
    background: var(--surface);
    border-radius: 8px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border)
}

.size-control {
    display: flex;
    flex-direction: row;
    gap: 8px;
    align-items: center;
    height: 40px
}

.size-control label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap
}

.mode-control {
    display: flex;
    flex-direction: row;
    gap: 8px;
    align-items: center;
    height: 40px
}

.mode-control label {
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap
}

.go-button {
    background: var(--success-color);
    color: white;
    font-size: 16px;
    font-weight: 700;
    min-width: 100px;
    transition: all .2s ease
}

.go-button:hover {
    background: #10b981;
    transform: translateY(-1px);
    box-shadow: var(--shadow-lg)
}

.go-button:active {
    transform: translateY(0)
}

.game-stats {
    display: flex;
    gap: 20px;
    justify-content: center;
    margin-bottom: 16px;
    padding: 12px;
    background: var(--surface);
    border-radius: 8px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border)
}

.game-area {
    background: var(--surface);
    padding: 20px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    position: relative;
    margin-bottom: 16px;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center
}

.reset-path-btn {
    margin-top: 12px;
    width: 100%;
    max-width: 300px
}

.bottom-controls {
    display: flex;
    gap: 12px;
    justify-content: center
}

.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px)
}

.modal-content {
    background: var(--surface);
    margin: 10% auto;
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    max-width: 500px;
    width: 90%;
    animation: modalSlideIn .3s ease-out
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(-20px)
    }

    to {
        opacity: 1;
        transform: translateY(0)
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid var(--border)
}

.modal-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: var(--text)
}

.close-btn {
    background: 0;
    border: 0;
    font-size: 16px;
    cursor: pointer;
    padding: 4px;
    color: var(--text-secondary)
}

.modal-body {
    padding: 20px
}

.option-group {
    margin-bottom: 20px
}

.option-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: var(--text)
}

.seed-controls {
    display: flex;
    gap: 8px
}

.seed-controls input {
    flex: 1
}

.color-red {
    color: #d9534f;
    font-weight: bold
}

.color-yellow {
    color: #f0ad4e;
    font-weight: bold
}

.color-green {
    color: #5cb85c;
    font-weight: bold
}

.share {
    margin-top: 12px;
    background: var(--surface-secondary);
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: 'Monaco', 'Menlo', monospace;
    font-size: 12px;
    color: var(--text-secondary)
}

@media(prefers-color-scheme:dark) {
    :root {
        --primary-color: #3b82f6;
        --success-color: #10b981;
        --warning-color: #f59e0b;
        --danger-color: #ef4444;
        --surface: #1e293b;
        --surface-secondary: #334155;
        --border: #475569;
        --text: #f1f5f9;
        --text-secondary: #94a3b8
    }

    .modal-body p {
        color: var(--text)
    }

    .modal-body strong {
        color: var(--text)
    }

    .color-red {
        color: #ef4444 !important
    }

    .color-yellow {
        color: #f59e0b !important
    }

    .color-green {
        color: #10b981 !important
    }

    .win-overlay {
        background: rgba(30, 41, 59, 0.98)
    }

    .challenge-result-overlay {
        background: rgba(0, 0, 0, 0.9)
    }

    body {
        background: linear-gradient(135deg, #0f172a 0, #1e293b 100%)
    }
}

@media(max-width:768px) {
    body {
        padding: 8px
    }

    h1 {
        font-size: 22px;
        margin-bottom: 16px
    }

    .controls-card {
        padding: 8px
    }

    .controls {
        justify-content: center;
        gap: 6px
    }

    .control-group {
        min-width: 100px
    }

    .stats {
        width: 100%;
        justify-content: space-around;
        margin-top: 12px
    }

    .game-area {
        padding: 8px
    }

    .board {
        padding: 6px
    }

    .cell {
        font-size: 8px
    }
}

@media(max-width:480px) {
    .top-controls {
        flex-direction: column;
        align-items: stretch;
        gap: 12px
    }

    .size-control,
    .mode-control {
        width: 100%
    }

    .go-button {
        width: 100%
    }
}

@media(max-width:600px) {
    .top-controls {
        flex-direction: column;
        align-items: stretch;
        gap: 12px
    }

    .size-control,
    .mode-control {
        width: 100%
    }

    .go-button {
        width: 100%
    }

    .game-stats {
        flex-direction: row;
        margin-bottom: 12px;
        padding: 8px
    }

    .game-area {
        padding: 6px
    }

    .board {
        padding: 4px
    }

    .cell {
        font-size: 7px
    }

    .bottom-controls {
        flex-direction: column;
        gap: 8px
    }

    .modal-content {
        margin: 5% auto;
        width: 95%
    }

    .modal-header,
    .modal-body {
        padding: 16px
    }
}

@media(max-width:380px) {
    body {
        padding: 4px
    }

    .game-area {
        padding: 4px
    }

    .board {
        padding: 2px
    }

    .cell {
        font-size: 6px
    }
}

@media(max-width:480px) {
    body {
        padding: 6px
    }

    h1 {
        font-size: 20px;
        margin-bottom: 12px
    }

    .top-controls {
        flex-direction: column;
        align-items: stretch;
        gap: 12px
    }

    .size-control {
        width: 100%
    }

    .game-stats {
        flex-direction: row;
        margin-bottom: 12px;
        padding: 8px
    }

    .game-area {
        padding: 8px;
        margin: 0 auto;
        max-width: calc(100vw - 16px);
        overflow: visible
    }

    .board {
        padding: 6px;
        max-width: 100%;
        overflow: visible;
        margin: 0 auto
    }

    .cell {
        font-size: 7px
    }

    .bottom-controls {
        flex-direction: column;
        gap: 8px
    }

    .modal-content {
        margin: 5% auto;
        width: 95%
    }

    .modal-header,
    .modal-body {
        padding: 16px
    }

    .container {
        max-width: 100%;
        padding: 0
    }
}

@media(max-width:380px) {
    body {
        padding: 4px
    }

    .game-area {
        padding: 6px;
        max-width: calc(100vw - 12px)
    }

    .board {
        padding: 4px
    }

    .cell {
        font-size: 6px
    }
}

.board.success {
    border-color: var(--success-color);
    border-width: 4px;
    box-shadow: 0 0 0 4px rgb(5 150 105 / .2);
    animation: successGlow 1.5s ease-in-out
}

@keyframes successGlow {
    0% {
        box-shadow: 0 0 0 4px rgb(5 150 105 / .2);
        transform: scale(1)
    }

    50% {
        box-shadow: 0 0 0 8px rgb(5 150 105 / .4), 0 0 20px rgb(5 150 105 / .3);
        transform: scale(1.02)
    }

    100% {
        box-shadow: 0 0 0 4px rgb(5 150 105 / .2);
        transform: scale(1)
    }
}

.board.error {
    border-color: var(--danger-color);
    border-width: 3px;
    box-shadow: 0 0 0 3px rgb(220 38 38 / .2);
    animation: shake .5s ease-in-out
}

@keyframes shake {

    0%,
    100% {
        transform: translateX(0)
    }

    25% {
        transform: translateX(-4px)
    }

    75% {
        transform: translateX(4px)
    }
}

.game-disabled {
    pointer-events: none;
    opacity: .8
}

.win-overlay.fade-in {
    animation: fadeInOverlay .5s ease-out forwards
}

@keyframes fadeInOverlay {
    from {
        opacity: 0;
        transform: scale(0.95)
    }

    to {
        opacity: 1;
        transform: scale(1)
    }
}

/* Debug Legend */
.debug-legend {
    position: fixed;
    top: 10px;
    left: 10px;
    background: rgba(0, 0, 0, 0.9);
    color: #fff;
    padding: 15px;
    border-radius: 8px;
    font-family: 'Monaco', 'Consolas', monospace;
    font-size: 12px;
    z-index: 1001;
    display: none;
    border: 2px solid #0f0;
    width: 250px;
    box-sizing: border-box;
}

.debug-legend.visible {
    display: block;
}

.debug-legend h3 {
    color: #0f0;
    margin: 0 0 10px 0;
    font-size: 14px;
    text-align: center;
}

.debug-legend-item {
    display: flex;
    align-items: center;
    margin: 6px 0;
    gap: 10px;
}

.debug-legend-symbol {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
    background: #333;
    flex-shrink: 0;
}

.debug-legend-line {
    width: 20px;
    height: 3px;
    border-radius: 1px;
}

.debug-legend-cross {
    font-size: 14px;
    font-weight: bold;
}

.debug-legend-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.debug-legend-text {
    color: #ccc;
}