:root {
    --primary: #009688;
    --primary-soft: #eaf2ff;
    --border: #d7e1ef;
    --bg: #f5f8fc;
    --card: #ffffff;
    --text: #102033;
    --muted: #667085;
    --slot: #f8fbff;
    --walk: #ffc400;
    --gate: #009688;
    --blocked: #475467;
    --danger: #d92d20;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: Inter, Arial, Helvetica, sans-serif;
}

body {
    background: var(--bg);
    color: var(--text);
}

.app {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 260px 1fr 350px;
    grid-template-rows: 70px 1fr;
    grid-template-areas:
        "top top top"
        "side main panel";
}

    .app.preview-mode {
        grid-template-columns: 1fr;
        grid-template-areas:
            "top"
            "main";
    }

        .app.preview-mode .sidebar,
        .app.preview-mode .panel,
        .app.preview-mode .json-card,
        .app.preview-mode .stats,
        .app.preview-mode .builder-only {
            display: none;
        }

.topbar {
    grid-area: top;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 20px;
    font-weight: 800;
}

.logo {
    width: 38px;
    height: 38px;
    border-radius: 12px;
    background: var(--primary);
    color: white;
    display: grid;
    place-items: center;
}

.top-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

button {
    border: 0;
    border-radius: 10px;
    height: 42px;
    padding: 0 16px;
    font-weight: 700;
    cursor: pointer;
    transition: .2s;
    white-space: nowrap;
}

    button:hover {
        transform: translateY(-1px);
    }

.btn-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 6px 16px rgba(20,87,217,.22);
}

.btn-light {
    background: #fff;
    color: var(--primary);
    border: 1px solid var(--border);
}

.btn-danger {
    background: #fff1f1;
    color: var(--danger);
    border: 1px solid #ffd2d2;
}

.sidebar {
    grid-area: side;
    background: var(--card);
    border-right: 1px solid var(--border);
    padding: 18px;
    overflow: auto;
}

.main {
    grid-area: main;
    padding: 20px;
    overflow: auto;
}

.panel {
    grid-area: panel;
    background: var(--card);
    border-left: 1px solid var(--border);
    padding: 20px;
    overflow: auto;
}

.section-title {
    font-size: 16px;
    font-weight: 800;
    margin-bottom: 14px;
}

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 16px;
    margin-bottom: 16px;
}

.form-group {
    margin-bottom: 13px;
}

label {
    display: block;
    font-size: 12px;
    font-weight: 800;
    color: #344054;
    margin-bottom: 7px;
}

input, select, textarea {
    width: 100%;
    height: 42px;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 9px 11px;
    outline: none;
    background: #fff;
    color: var(--text);
}

textarea {
    height: 82px;
    resize: vertical;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.tool-list {
    display: grid;
    gap: 10px;
}

.tool {
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 12px;
    background: #fbfdff;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
}

    .tool.active {
        background: var(--primary-soft);
        border-color: var(--primary);
        color: var(--primary);
    }

    .tool small {
        color: var(--muted);
        font-weight: 500;
    }

.swatch {
    width: 22px;
    height: 22px;
    border-radius: 7px;
    border: 1px solid #b9c4d5;
    flex: 0 0 auto;
}

.outer-swatch {
    background: linear-gradient(180deg, #fff 0 33%, #edf2f7 33% 66%, #fff 66%);
}

.inner-swatch {
    background: linear-gradient(90deg, #fff 0 33%, #edf2f7 33% 66%, #fff 66%);
}

.gate-swatch {
    background: var(--gate);
}

.walkway-swatch {
    background: var(--walk);
}

.blocked-swatch {
    background: var(--blocked);
}

.hint {
    font-size: 13px;
    color: var(--muted);
    line-height: 1.5;
    margin-top: 12px;
}

.page-head {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

    .page-head h2 {
        font-size: 20px;
        margin-bottom: 4px;
    }

.meta {
    color: var(--muted);
    font-size: 14px;
}

.legend-inline {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 10px;
}

.legend-pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: #fff;
}

.dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.view-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.zoom-label {
    font-size: 13px;
    color: var(--muted);
    font-weight: 700;
}

.storage-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 20px;
    overflow: auto;
}

.axis-title {
    text-align: center;
    font-weight: 800;
    margin-bottom: 14px;
}

.map-scale {
    transform-origin: top left;
    width: max-content;
}

.map-shell {
    position: relative;
    width: max-content;
    height: max-content;
}

.top-header {
    display: grid;
    gap: 6px;
    margin-left: 64px;
    margin-bottom: 6px;
}

.top-header-cell, .row-header, .cell, .zone-cell {
    width: 58px;
    height: 48px;
    border: 1px solid #cbd8ea;
    border-radius: 6px;
    display: grid;
    place-items: center;
    user-select: none;
}

.top-header-cell, .row-header {
    background: #edf2f7;
    font-weight: 800;
    color: #0f2544;
}

.grid-with-rows {
    display: flex;
    gap: 6px;
}

.row-headers {
    display: grid;
    gap: 6px;
}

.grid {
    display: grid;
    gap: 6px;
}

.cell {
    background: var(--slot);
    cursor: crosshair;
    font-size: 13px;
    font-weight: 800;
    color: #0f2544;
    position: relative;
}

    .cell:hover {
        outline: 2px solid #9bbcff;
    }

    .cell.covered {
        opacity: .08;
        pointer-events: none;
    }

    .cell .sub {
        position: absolute;
        right: 5px;
        bottom: 4px;
        font-size: 9px;
        color: #667085;
        font-weight: 700;
    }

.zone {
    position: absolute;
    z-index: 5;
    display: grid;
    gap: 6px;
}

    .zone.selected {
        outline: 3px solid var(--primary);
        outline-offset: 3px;
        border-radius: 8px;
    }

.zone-cell {
    background: var(--slot);
    font-size: 13px;
    font-weight: 800;
    cursor: pointer;
    position: relative;
}

    .zone-cell:hover {
        outline: 2px solid #9bbcff;
    }

    .zone-cell .sub {
        position: absolute;
        right: 5px;
        bottom: 4px;
        font-size: 9px;
        color: #667085;
    }

.zone-name {
    position: absolute;
    top: -20px;
    left: 0;
    background: #102033;
    color: #fff;
    border-radius: 6px;
    padding: 3px 7px;
    font-size: 11px;
    font-weight: 800;
    pointer-events: none;
}

.resize-handle {
    position: absolute;
    width: 14px;
    height: 14px;
    border-radius: 4px;
    background: #fff;
    border: 2px solid var(--primary);
    right: -8px;
    bottom: -8px;
    cursor: nwse-resize;
    z-index: 20;
}

.zone-gate, .zone-walkway, .zone-blocked {
    position: absolute;
    z-index: 6;
    border-radius: 6px;
    display: grid;
    place-items: center;
    font-weight: 800;
    border: 1px solid transparent;
}

.zone-gate {
    background: var(--gate);
    color: #fff;
}

.zone-walkway {
    background: var(--walk);
    color: #111827;
}

.zone-blocked {
    background: var(--blocked);
    color: #fff;
}

.selection-box {
    position: absolute;
    z-index: 30;
    border: 2px dashed var(--primary);
    background: rgba(20, 87, 217, .12);
    border-radius: 8px;
    pointer-events: none;
    display: none;
}

.stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
    margin-top: 16px;
}

.stat {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 15px;
}

    .stat span {
        display: block;
        color: var(--muted);
        font-size: 12px;
        margin-bottom: 5px;
    }

    .stat strong {
        font-size: 22px;
    }

.summary-list {
    display: grid;
    gap: 10px;
    font-size: 14px;
    color: #475467;
    line-height: 1.45;
}

.json-box {
    background: #101828;
    color: #d1e7ff;
    border-radius: 12px;
    padding: 14px;
    font-family: Consolas, monospace;
    font-size: 12px;
    white-space: pre-wrap;
    max-height: 240px;
    overflow: auto;
}

@media (max-width: 1100px) {
    .app {
        grid-template-columns: 1fr;
        grid-template-rows: 70px auto auto auto;
        grid-template-areas: "top" "side" "main" "panel";
    }

    .stats {
        grid-template-columns: repeat(2, 1fr);
    }
}

.loader-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255,255,255,0.85);
    z-index: 99999;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(3px);
}

.loader-content {
    text-align: center;
    background: white;
    padding: 30px;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0,0,0,.15);
    min-width: 300px;
}

.spinner {
    width: 60px;
    height: 60px;
    border: 5px solid #e5e7eb;
    border-top: 5px solid #009688;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 15px;
}

@keyframes spin {
    100% {
        transform: rotate(360deg);
    }
}


.custom-alert {
    position: fixed;
    top: 20px;
    right: 20px;
    min-width: 350px;
    max-width: 450px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0,0,0,.15);
    padding: 15px;
    display: none;
    align-items: center;
    gap: 12px;
    z-index: 100000;
    animation: slideIn .3s ease;
}

.alert-success {
    border-left: 5px solid #22c55e;
}

.alert-error {
    border-left: 5px solid #ef4444;
}

.alert-warning {
    border-left: 5px solid #f59e0b;
}

.alert-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #22c55e;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

.alert-title {
    font-weight: 700;
    margin-bottom: 4px;
}

.alert-message {
    color: #667085;
    font-size: 14px;
}

.alert-close {
    background: transparent;
    border: none;
    font-size: 18px;
    cursor: pointer;
    margin-left: auto;
    height: auto;
    padding: 0;
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}