/* AlgoLand - 2D Grid Block Coding Game - CodeGen-style layout */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #0f0f1a;
    --panel-bg: #1a1a2e;
    --panel-border: #2a2a40;
    --text: #e0e0e0;
    --muted: #8892b0;
    --accent: #4a8fd4;
    --accent-hover: #3a7abe;
    --success: #27ae60;
    --warning: #f59e0b;
    --danger: #e74c3c;
    --code-block: #6366f1;
    --pool-bg: #1e1e30;
}

html, body {
    width: 100%;
    height: 100%;
    font-family: 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
}

/* ============ HEADER ============ */
#game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--panel-border);
    flex-shrink: 0;
}

#header-left { display: flex; align-items: baseline; gap: 16px; }
#header-left h1 { font-size: 1.4rem; color: #ffd700; letter-spacing: 1px; }
#level-info { font-size: 0.9rem; color: var(--muted); }

#header-controls { display: flex; gap: 8px; }

.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    color: white;
}
.btn:hover { transform: translateY(-1px); opacity: 0.9; }
.btn:active { transform: translateY(0); }
.btn-run { background: var(--success); }
.btn-run:hover { background: #2ecc71; }
.btn-step { background: var(--warning); }
.btn-step:hover { background: #d97706; }
.btn-reset { background: #7f8c8d; }
.btn-reset:hover { background: #95a5a6; }
.btn-clear { background: var(--danger); }
.btn-clear:hover { background: #c0392b; }
.btn-icon { background: #2c3e7a; padding: 8px 12px; }
.btn-icon:hover { background: #3a4f9e; }

/* ============ LEVEL SELECT ============ */
#level-select-screen {
    position: absolute;
    inset: 0;
    z-index: 100;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    overflow-y: auto;
    padding: 40px 20px;
}
#level-select-screen.hidden { display: none; }

#level-select-header h2 {
    font-size: 1.8rem;
    font-weight: 700;
    color: #a0d8ef;
}

#level-select-grid {
    display: grid;
    grid-template-columns: repeat(var(--cols, 5), 1fr);
    gap: 14px;
    width: 100%;
    max-width: 720px;
}

.level-slot {
    aspect-ratio: 1;
    background: var(--pool-bg);
    border: 2px solid var(--panel-border);
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.15s, border-color 0.15s, background 0.15s;
    padding: 4px;
    text-align: center;
}
.level-slot:hover {
    transform: translateY(-3px);
    border-color: var(--accent);
    background: #252540;
}
.level-slot .slot-number { font-size: 1.3rem; font-weight: 700; color: #a0d8ef; }
.level-slot .slot-name { font-size: 0.75rem; color: #8888a8; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.level-slot.empty { visibility: hidden; pointer-events: none; }
.level-slot.locked { opacity: 0.4; cursor: not-allowed; border-color: var(--panel-bg); background: #141420; }
.level-slot.locked:hover { transform: none; border-color: var(--panel-bg); background: #141420; }
.level-slot.completed { border-color: #3a7a3a; background: #1a2e1a; }
.level-slot.completed:hover { border-color: #5aaa5a; background: #224422; }
.level-slot .slot-stars { font-size: 0.7rem; margin-top: 2px; line-height: 1; }

#level-select-dots {
    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: center;
}
.level-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #2a2a40;
    cursor: pointer;
    transition: background 0.2s, transform 0.2s;
}
.level-dot:hover { background: #5a5a8e; }
.level-dot.active { background: var(--accent); transform: scale(1.3); }

/* ============ GAME MAIN LAYOUT ============ */
#game-main {
    display: flex;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}
#game-main.hidden { display: none; }

/* Left: Code Pool */
#code-pool {
    width: 420px;
    min-width: 390px;
    flex-shrink: 0;
    background: var(--panel-bg);
    border-right: 1px solid var(--panel-border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
#code-pool.hidden { display: none; }

#pool-header {
    padding: 10px 14px;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--muted);
    border-bottom: 1px solid var(--panel-border);
}

#pool-blocks {
    flex: 1;
    overflow-y: auto;
    padding: 12px 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
}

/* Center: Game Area + Description */
#center-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

#grid-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 16px;
    overflow: auto;
}

#objectives-bar {
    padding: 8px 16px;
    width: 100%;
    max-width: 600px;
    margin-bottom: 12px;
}

#objectives-list {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
}

.objective {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--pool-bg);
    border-radius: 20px;
    font-size: 0.85rem;
    border: 1px solid var(--panel-border);
}
.objective img { width: 20px; height: 20px; object-fit: contain; }
.objective.completed { border-color: var(--success); color: var(--success); }

.grid {
    display: grid;
    grid-template-columns: repeat(10, 62px);
    grid-template-rows: repeat(10, 62px);
    gap: 2px;
    background: var(--panel-border);
    padding: 4px;
    border-radius: 8px;
    position: relative;
}

.cell {
    width: 62px;
    height: 62px;
    background: var(--pool-bg);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    font-size: 32px;
    overflow: hidden;
}

.cell .asset-img {
    width: 80%;
    height: 80%;
    object-fit: contain;
    pointer-events: none;
}

.cell .count-badge {
    position: absolute;
    bottom: 2px;
    right: 2px;
    background: var(--danger);
    color: #fff;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cell.finish { background: rgba(34, 197, 94, 0.15); }
.cell.portal { background: rgba(59, 130, 246, 0.15); }
.cell.start { background: rgba(245, 158, 11, 0.15); }

#character-element {
    position: absolute;
    width: 62px;
    height: 62px;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 10;
    transition: left 0.3s ease-in-out, top 0.3s ease-in-out;
}
#character-element img {
    width: 80%;
    height: 80%;
    object-fit: contain;
}

/* Description Panel (below game area) */
#description-panel {
    flex-shrink: 0;
    max-height: 360px;
    overflow-y: auto;
    background: var(--panel-bg);
    border-top: 1px solid var(--panel-border);
}
#description-header {
    padding: 12px 18px;
    font-weight: 600;
    font-size: 1.7rem;
    color: var(--muted);
    border-bottom: 1px solid var(--panel-border);
}
#description-body {
    padding: 16px 18px;
    line-height: 1.5;
    font-size: 1.8rem;
}
#description-body img {
    width: 48px;
    height: 48px;
    object-fit: contain;
    vertical-align: middle;
    margin: 0 4px;
}

/* Zoom controls - CodeGen style */
#tab-zoom-controls {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-left: auto;
    padding: 4px 8px;
}
.btn-zoom {
    width: 28px;
    height: 28px;
    border: 1px solid var(--panel-border);
    background: var(--bg);
    color: var(--accent);
    border-radius: 4px;
    cursor: pointer;
    font-size: 18px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    transition: all 0.2s;
}
.btn-zoom:hover {
    background: var(--panel-border);
    border-color: var(--accent);
}
.btn-zoom:active {
    transform: scale(0.95);
}
#zoom-level {
    font-size: 0.8rem;
    color: var(--muted);
    min-width: 42px;
    text-align: center;
}

/* Right: Code Area */
#right-area {
    width: 360px;
    min-width: 300px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    border-left: 1px solid var(--panel-border);
    overflow: hidden;
}

#tab-headers {
    display: flex;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--panel-border);
}

.tab-btn {
    flex: 1;
    padding: 10px 8px;
    background: transparent;
    border: none;
    color: var(--muted);
    cursor: pointer;
    font-size: 0.85rem;
    border-bottom: 2px solid transparent;
    transition: color 0.15s, border-color 0.15s;
}
.tab-btn.active { color: var(--text); border-color: var(--accent); }
.tab-btn:hover { color: var(--text); }

#tab-content {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.tab-panel {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.tab-panel[style*="display:none"] { display: none !important; }

#workspace-header, .function-header {
    padding: 8px 14px;
    font-size: 0.85rem;
    color: var(--muted);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--panel-border);
}

#cmd-counter {
    font-size: 0.8rem;
    font-weight: 500;
    color: #a0a0c0;
    background: #0f0f1a;
    padding: 3px 10px;
    border-radius: 10px;
    border: 1px solid #2a2a40;
}

#cmd-counter.limit-reached {
    color: #ff4444;
    border-color: #ff4444;
    font-weight: 700;
}

.function-header span:last-child {
    font-size: 0.75rem;
    font-weight: 500;
    color: #a0a0c0;
    background: #0f0f1a;
    padding: 2px 8px;
    border-radius: 10px;
    border: 1px solid #2a2a40;
}

.function-header span:last-child.limit-reached {
    color: #ff4444;
    border-color: #ff4444;
    font-weight: 700;
}

#status-bar {
    padding: 8px;
    text-align: center;
    font-size: 0.9rem;
    color: #8892b0;
    min-height: 36px;
}

#workspace-dropzone, .function-dropzone {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    min-height: 80px;
}

#workspace-placeholder, .function-placeholder {
    color: var(--muted);
    font-size: 0.85rem;
    text-align: center;
    padding: 20px;
    opacity: 0.5;
}

/* ============ CODE BLOCKS (div + SVG background, CodeGen-style) ============ */
.code-block {
    --block-color: #2980b9;
    position: relative;
    width: 132px;
    height: 92px;
    color: #fff;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: grab;
    user-select: none;
    touch-action: none;
    background: transparent;
    flex-shrink: 0;
}
.code-block:active { cursor: grabbing; }

.block-shape {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 0;
    overflow: visible;
    pointer-events: none;
    filter: drop-shadow(0 1px 1.5px rgba(0,0,0,0.32));
}
.block-label {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 92px;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    text-align: center;
    padding: 0 6px;
}
.block-label svg, .block-label img { width: auto; height: auto; max-width: 100%; max-height: 76px; display: block; }
.hex-block .block-label svg, .hex-block .block-label img { max-height: 56px; }
.repeat-title svg, .repeat-title img, .if-title svg, .if-title img, .ifelse-title svg, .ifelse-title img { max-width: 56px; max-height: 56px; vertical-align: middle; display: inline-block; }
.ifelse-sep-text svg { max-width: 16px; max-height: 16px; vertical-align: middle; display: inline-block; }

.pool-block { transition: transform 0.12s ease; }
.pool-block:hover { transform: translateY(-3px); }

#workspace-blocks, #function1-blocks, #function2-blocks,
.repeat-inner, .if-inner, .ifelse-true-inner, .ifelse-false-inner {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}
#workspace-blocks, #function1-blocks, #function2-blocks {
    align-items: flex-start;
    min-height: 100%;
    padding-left: 10px;
}

.workspace-block { margin-bottom: 0; }
.workspace-block.dragging {
    opacity: 0.92;
    z-index: 1000;
    position: fixed;
    pointer-events: none;
}
.workspace-block.dragging .block-shape,
.workspace-block.dragging .repeat-shape,
.workspace-block.dragging .if-shape,
.workspace-block.dragging .ifelse-shape { filter: drop-shadow(0 10px 18px rgba(0,0,0,0.5)); }

.snap-target > .block-shape > path,
.snap-target > .repeat-shape > path,
.snap-target > .if-shape > path,
.snap-target > .ifelse-shape > path,
.snap-target > .hex-shape > path {
    stroke: #ffd700;
    stroke-width: 2.5px;
}

/* Drop preview / active container highlight */
.drop-preview {
    position: relative;
    width: 100%;
    height: 0;
    pointer-events: none;
    z-index: 5;
}
.drop-preview::before {
    content: '';
    position: absolute;
    left: 0;
    top: -3px;
    width: var(--drop-width, 100%);
    height: 6px;
    background: #ffd700;
    border-radius: 3px;
    box-shadow: 0 0 8px rgba(255,215,0,0.6);
}
.drop-active {
    outline: 2px dashed #ffd700;
    outline-offset: -2px;
    border-radius: 0 4px 4px 0;
}

/* ============ REPEAT (Tekrarla) C-BLOCK ============ */
.repeat-block { width: 200px; height: auto; }
.repeat-shape {
    position: absolute; top: 0; left: 0; z-index: 0;
    overflow: visible; pointer-events: none;
    filter: drop-shadow(0 1px 1.5px rgba(0,0,0,0.32));
}
.repeat-head {
    position: relative; z-index: 1;
    display: flex; align-items: center; justify-content: space-between;
    gap: 6px; padding: 12px 12px 12px 14px;
    pointer-events: none;
}
.repeat-title { font-size: 0.85rem; white-space: nowrap; }
.repeat-count {
    display: inline-flex; align-items: center; gap: 3px;
    background: rgba(0,0,0,0.22); border-radius: 6px; padding: 2px;
    pointer-events: auto;
}
.rc-btn {
    width: 20px; height: 20px; border: none; border-radius: 4px;
    background: rgba(255,255,255,0.22); color: #fff; cursor: pointer;
    font-size: 14px; font-weight: 700; line-height: 1;
    display: inline-flex; align-items: center; justify-content: center; padding: 0;
}
.rc-btn:hover { background: rgba(255,255,255,0.38); }
.rc-btn:disabled { opacity: 0.35; cursor: default; }
.rc-val { min-width: 16px; text-align: center; font-size: 0.95rem; font-weight: 700; }

.repeat-mid { position: relative; z-index: 1; display: flex; align-items: stretch; }
.repeat-arm { width: 18px; flex-shrink: 0; }
.repeat-inner { flex: 1; min-height: 40px; }
.repeat-inner > .workspace-block { margin-bottom: 0; width: 132px; align-self: flex-start; }
.repeat-inner-placeholder {
    color: #ffffff66; font-size: 0.72rem; margin: auto;
    pointer-events: none; padding: 12px 0; align-self: center;
}
.repeat-foot { position: relative; z-index: 1; width: 100%; height: 16px; pointer-events: none; }

/* ============ IF BLOCK (C-Block) ============ */
.if-block { position: relative; width: 200px; height: auto; }
.if-shape {
    position: absolute; top: 0; left: -2px; z-index: 0;
    overflow: visible; pointer-events: none;
    filter: drop-shadow(0 1px 1.5px rgba(0,0,0,0.32));
}
.if-head {
    position: relative; z-index: 1; height: 92px; width: 100%;
    box-sizing: border-box; display: flex; align-items: center;
    padding-left: 10px; padding-right: 10px; gap: 6px;
}
.if-title { color: #fff; font-weight: bold; font-size: 13px; flex-shrink: 0; }
.if-condition-slot {
    position: relative; width: 120px; height: 64px;
    margin-top: 4px; margin-left: 6px; margin-right: 10px;
    display: flex; align-items: center; justify-content: flex-start; flex-shrink: 0;
}
.if-condition-slot:empty::before {
    content: ''; position: absolute; width: 100%; height: 100%;
    clip-path: polygon(20% 0%, 80% 0%, 100% 50%, 80% 100%, 20% 100%, 0% 50%);
    background: rgba(0,0,0,0.15); border: 2px dashed rgba(255,255,255,0.3); box-sizing: border-box;
}
.if-condition-slot.filled::before { display: none; }
.if-condition-slot .hex-block { position: absolute; top: 0; left: 0; }
.if-condition-slot.slot-highlight::before {
    border-color: rgba(255,255,255,0.8); background: rgba(255,255,255,0.15);
    box-shadow: 0 0 8px rgba(255,255,255,0.6);
}
.if-mid { position: relative; z-index: 1; display: flex; }
.if-arm { width: 18px; flex-shrink: 0; }
.if-inner { flex: 1; min-height: 40px; padding: 4px 4px 4px 0; }
.if-foot { position: relative; z-index: 1; height: 16px; }

/* ============ IF-ELSE BLOCK (Double C-Block) ============ */
.ifelse-block { position: relative; width: 200px; height: auto; }
.ifelse-shape {
    position: absolute; top: 0; left: -2px; z-index: 0;
    overflow: visible; pointer-events: none;
    filter: drop-shadow(0 1px 1.5px rgba(0,0,0,0.32));
}
.ifelse-head {
    position: relative; z-index: 1; height: 92px; width: 100%;
    box-sizing: border-box; display: flex; align-items: center;
    padding-left: 10px; padding-right: 10px; gap: 6px;
}
.ifelse-title { color: #fff; font-weight: bold; font-size: 13px; flex-shrink: 0; }
.ifelse-condition-slot {
    position: relative; width: 120px; height: 64px;
    margin-top: 4px; margin-left: 6px; margin-right: 10px;
    display: flex; align-items: center; justify-content: flex-start; flex-shrink: 0;
}
.ifelse-condition-slot:empty::before {
    content: ''; position: absolute; width: 100%; height: 100%;
    clip-path: polygon(20% 0%, 80% 0%, 100% 50%, 80% 100%, 20% 100%, 0% 50%);
    background: rgba(0,0,0,0.15); border: 2px dashed rgba(255,255,255,0.3); box-sizing: border-box;
}
.ifelse-condition-slot.filled::before { display: none; }
.ifelse-condition-slot .hex-block { position: absolute; top: 0; left: 0; }
.ifelse-condition-slot.slot-highlight::before {
    border-color: rgba(255,255,255,0.8); background: rgba(255,255,255,0.15);
    box-shadow: 0 0 8px rgba(255,255,255,0.6);
}
.ifelse-true-mid, .ifelse-false-mid { position: relative; z-index: 1; display: flex; }
.ifelse-arm { width: 18px; flex-shrink: 0; }
.ifelse-true-inner, .ifelse-false-inner { flex: 1; min-height: 40px; padding: 4px 4px 4px 0; }
.ifelse-separator { position: relative; z-index: 1; height: 28px; display: flex; align-items: center; padding-left: 24px; }
.ifelse-sep-text { color: #fff; font-weight: bold; font-size: 12px; }
.ifelse-foot { position: relative; z-index: 1; height: 16px; }

/* ============ HEX BLOCKS (Boolean sensors) ============ */
.hex-block {
    position: relative; width: 120px; height: 64px;
    display: flex; align-items: center; justify-content: center;
}
.hex-block > .block-label { font-size: 12px; font-weight: bold; height: 64px; line-height: 1.1; padding: 0 12px; }
.hex-shape {
    position: absolute; top: 0; left: 0; z-index: 0;
    overflow: visible; pointer-events: none;
    filter: drop-shadow(0 1px 1.5px rgba(0,0,0,0.32));
}

/* ============ CONTEXT MENU ============ */
.context-menu {
    position: fixed;
    z-index: 9999;
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 8px;
    padding: 6px 0;
    min-width: 200px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
}
.context-menu.hidden { display: none; }
.context-item {
    padding: 8px 16px;
    color: #e2e8f0;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.15s;
    user-select: none;
}
.context-item:hover { background: #334155; }
.context-item.disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; }
.context-danger { color: #ef4444; }
.context-danger:hover { background: #450a0a; }
.context-separator { height: 1px; background: #334155; margin: 4px 8px; }

/* ============ MODALS ============ */
#complete-overlay, #fail-overlay, #description-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.hidden { display: none !important; }

.modal {
    background: var(--panel-bg);
    padding: 32px 40px;
    border-radius: 16px;
    text-align: center;
    border: 1px solid var(--panel-border);
    max-width: 720px;
}

#description-title {
    font-size: 2rem;
    margin-bottom: 8px;
}

#description-modal-body {
    margin: 20px 0;
    line-height: 1.5;
    font-size: 2rem;
    text-align: left;
    max-height: 70vh;
    overflow-y: auto;
}
#description-modal-body img {
    width: 48px;
    height: 48px;
    object-fit: contain;
    vertical-align: middle;
    margin: 0 4px;
}

.modal h2 { margin-top: 0; margin-bottom: 12px; }
.modal p { margin-bottom: 16px; color: var(--muted); }
.modal .btn { margin: 4px; }
