:root{
    --bg:#0b0f14;
    --panel:#121926;
    --card:#0f1622;
    --border:#223044;
    --text:#e7eefc;
    --muted:#92a4c0;
    --btn:#1a2638;
    --btn2:#24334a;
    --accent:#77a7ff;

    --side-w: clamp(260px, 26vw, 360px);
    --catalog-w: clamp(380px, 30vw, 460px);

    /* Keep the first two work areas viewport-based:
       top half = information grid, bottom half = visual preview.
       Segmentation and retrofit intentionally live below the first viewport. */
    --top-workspace-h: calc(100vh - 104px);
    --info-grid-h: calc(var(--top-workspace-h) * 0.5);
    --preview-card-h: calc(var(--top-workspace-h) * 0.5);
}

*{box-sizing:border-box}
html,body{height:100%}
body{
    margin:0;
    background:var(--bg);
    color:var(--text);
    font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, "Apple Color Emoji","Segoe UI Emoji";
}

.app{height:100%; display:flex; flex-direction:column;}
.topbar{
    display:flex; align-items:center; justify-content:space-between;
    padding:12px 16px;
    border-bottom:1px solid var(--border);
    background:linear-gradient(180deg, rgba(18,25,38,.95), rgba(18,25,38,.8));
}
.brand{font-weight:700; letter-spacing:.2px}
.muted{color:var(--muted)}
.status{color:var(--muted); font-size:14px}

.main{
    flex:1;
    display:grid;
    grid-template-columns: var(--catalog-w) minmax(0, 1fr);
    min-height:0;
    gap:12px;
    padding:12px;
}

.panel{
    background:var(--panel);
    border:1px solid var(--border);
    border-radius:14px;
    overflow:hidden;
    display:flex;
    flex-direction:column;
    min-height:0;
    min-width:0;
}

.catalog{
    min-width:0;
}

.catalog .list{
    scrollbar-gutter:stable;
}

.panel-title{
    padding:10px 12px;
    border-bottom:1px solid var(--border);
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:8px;
    font-weight:600;
}

.list{
    overflow-y:auto;
    overflow-x:hidden;
    padding:8px;
    display:flex;
    flex-direction:column;
    gap:6px;
    flex:1;
    min-height:0;
}
.run-item{
    padding:10px 10px;
    border:1px solid var(--border);
    border-radius:12px;
    background:rgba(15,22,34,.8);
    cursor:pointer;
    display:flex;
    flex-direction:column;
    align-items:stretch;
    gap:6px;
}
.run-item:hover{border-color:#2d3f5c}
.run-item.active{
    border-color:var(--accent);
    box-shadow:0 0 0 2px rgba(119,167,255,.18) inset;
}
.run-item-main {
    min-width: 0;
}

.run-item-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: left;
}

.run-item-badges {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    min-height: 20px;
}

.badge-annotation-full {
    color: #d7f7df;
    border-color: rgba(110, 210, 140, 0.55);
    background: rgba(50, 110, 65, 0.28);
}

.badge-annotation-partial {
    color: #ffe3b0;
    border-color: rgba(245, 170, 60, 0.60);
    background: rgba(130, 80, 20, 0.32);
}

.badge{
    font-size:12px;
    color:var(--muted);
    padding:2px 8px;
    border-radius:999px;
    border:1px solid var(--border);
}

.content{
    padding-bottom:12px;
    min-width:0;
    overflow:auto;
}

.grid{
    display:grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    grid-template-areas:
    "meta joined"
    "asp  eval";
    gap:12px;
    padding:12px;
    height: var(--info-grid-h);
    min-width:0;
}
.card-meta{ grid-area: meta; }
.card-asp{ grid-area: asp; }
.card-joined{ grid-area: joined; }
.card-eval{ grid-area: eval; }

.card{
    background:var(--card);
    border:1px solid var(--border);
    border-radius:14px;
    padding:10px;
    min-height:0;
    min-width:0;
    display:flex;
    flex-direction:column;
}
.card-title{ font-weight:600; color:#d7e5ff; }
.card-head{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:10px;
    margin-bottom:8px;
}

.mono{
    margin:0;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    font-size:12px;
    line-height:1.35;
    color:#cfe0ff;
    border:1px dashed rgba(146,164,192,.22);
    border-radius:12px;
    padding:8px;
    background:rgba(10,14,20,.6);
    flex:1;
    min-height:0;
    overflow:auto;
}


.eval-json{
    white-space:pre;
    tab-size:2;
}

.icon-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    width:30px;
    height:30px;
    border-radius:10px;
    border:1px solid var(--border);
    background:rgba(15,22,34,.55);
    cursor:pointer;
    padding:0;
}
.icon-btn:hover{ background:rgba(36,51,74,.65); }
.icon{ width:16px; height:16px; fill:rgba(231,238,252,0.88); }
.icon-btn.copied{
    border-color: rgba(119,167,255,0.85);
    box-shadow:0 0 0 2px rgba(119,167,255,.18) inset;
}

.player{
    margin:0 12px;
    min-width:0;
    height:var(--preview-card-h);
    flex:0 0 var(--preview-card-h);
    overflow:hidden;
}

/* The player card is now only the preview stage: controls, visuals and scrubber.
   Segmentation and retrofit live in their own cards below it. */
.player-top{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:10px;
    min-width:0;
    flex:0 0 auto;
}
.preview-speed{
    margin-left:6px;
    font-size:13px;
    font-weight:600;
    color:var(--muted);
    opacity:.95;
}

.btn{
    background:var(--btn);
    border:1px solid var(--border);
    border-radius:12px;
    padding:8px 10px;
    color:var(--text);
    cursor:pointer;
}
.btn:hover{background:var(--btn2)}
.btn.small{padding:6px 8px; font-size:13px}
.btn:disabled{ opacity:0.55; cursor:not-allowed; }

.viewport-row{
    display:grid;
    /* Camera gets half the preview width; minimap + local map share the other half. */
    grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr);
    gap:12px;
    margin-top:10px;
    align-items:stretch;
    min-width:0;
    min-height:0;
    width:100%;
    overflow:hidden;
    flex:1 1 auto;
}

.viewport{
    position:relative;
    border-radius:14px;
    overflow:hidden;
    border:1px solid var(--border);
    background:#070a0f;
    height:100%;
    min-height:0;
    display:flex;
    align-items:center;
    justify-content:center;
    min-width:0;
}
.viewport img{ max-width:100%; max-height:100%; display:block; }

.mini-map{
    height:100%;
    min-height:0;
    border-radius:14px;
    border:1px solid var(--border);
    background:#070a0f;
    overflow:hidden;
    min-width:0;
    position:relative;
}
.mini-map canvas{
    width:100% !important;
    height:100% !important;
}
.mini-map .map-overlay{
    position:absolute;
    inset:0;
    display:flex;
    align-items:center;
    justify-content:center;
    font:13px ui-sans-serif, system-ui;
    color:#cfe0ff;
    background:#070a0f;
    z-index:2;
    pointer-events:none;
    text-align:center;
    padding:10px;
}

.proj-box{
    height:100%;
    min-height:0;
    border-radius:14px;
    border:1px solid var(--border);
    background:#070a0f;
    overflow:hidden;
    min-width:0;
    position:relative;
}
.proj-box .local-map{
    width:100%;
    height:100%;
    display:block;
    border:none !important;
    border-radius:0 !important;
}

.backend-minimap-layer{
    position:absolute;
    inset:0;
    display:none;
    align-items:center;
    justify-content:center;
    background:#070a0f;
    overflow:hidden;
    z-index:3;
}
.backend-minimap-img{
    width:100%;
    height:100%;
    object-fit:contain;
    display:block;
}
.backend-minimap-msg{
    position:absolute;
    inset:0;
    display:flex;
    align-items:center;
    justify-content:center;
    text-align:center;
    padding:12px;
    color:#cfe0ff;
    font:13px ui-sans-serif, system-ui;
    background:#070a0f;
}
#projBox.backend-mode .backend-minimap-layer{ display:flex; }
#projBox.backend-mode canvas{ display:none !important; }
#projBox.backend-mode .backend-minimap-layer.has-image .backend-minimap-msg{ display:none; }

.overlay{
    position:absolute;
    left:10px;
    top:10px;
    background:rgba(0,0,0,.55);
    border:1px solid rgba(255,255,255,.14);
    border-radius:12px;
    padding:6px 10px;
    font-size:13px;
    color:#f2f6ff;
    z-index:2;
    white-space: pre-line;
}

.eval-overlay{
    position:absolute;
    left:10px;
    bottom:10px;
    max-width:min(360px, calc(100% - 20px));
    background:rgba(0,0,0,.68);
    border:1px solid rgba(255,255,255,.14);
    border-radius:12px;
    padding:8px 10px;
    font-size:12px;
    line-height:1.35;
    color:#f2f6ff;
    z-index:2;
    white-space: pre-line;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    box-shadow:0 4px 18px rgba(0,0,0,.25);
    pointer-events:none;
}

/* loading layer */
.frame-loading{
    position:absolute;
    inset:0;
    z-index:4;
    display:flex;
    align-items:center;
    justify-content:center;
    background:rgba(3, 7, 12, 0.62);
    backdrop-filter: blur(1px);
    pointer-events:none;
    overflow:hidden;
}
.frame-loading.hidden{
    display:none;
}

.frame-loading-panel.hidden,
.frame-loading-spinner.hidden{
    display:none;
}
.frame-loading-panel{
    position:relative;
    min-width:min(420px, calc(100% - 48px));
    max-width:min(520px, calc(100% - 48px));
    padding:18px 20px 16px 20px;
    border:1px solid rgba(119,167,255,0.28);
    border-radius:12px;
    background:rgba(7, 12, 18, 0.82);
    box-shadow:0 10px 30px rgba(0,0,0,.28);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}
.frame-loading-panel::before,
.frame-loading-panel::after{
    content:"";
    position:absolute;
    width:18px;
    height:18px;
    border-color:rgba(119,167,255,0.72);
    pointer-events:none;
}
.frame-loading-panel::before{
    top:8px;
    left:8px;
    border-top:2px solid rgba(119,167,255,0.72);
    border-left:2px solid rgba(119,167,255,0.72);
}
.frame-loading-panel::after{
    right:8px;
    bottom:8px;
    border-right:2px solid rgba(119,167,255,0.72);
    border-bottom:2px solid rgba(119,167,255,0.72);
}
.frame-loading-kicker{
    font-size:11px;
    letter-spacing:.18em;
    color:#8ea4c7;
    margin-bottom:8px;
}
.frame-loading-title{
    font-size:18px;
    font-weight:700;
    letter-spacing:.04em;
    color:#edf4ff;
    margin-bottom:8px;
}
.frame-loading-sub{
    font-size:12px;
    line-height:1.4;
    color:#b5c7e3;
    letter-spacing:.08em;
}

.scrub{ display:flex; flex-direction:column; gap:8px; padding-top:10px; flex:0 0 auto; }
.scrub input[type="range"]{ width:100%; accent-color: var(--accent); }

.scrub-under{
    display:flex;
    align-items:flex-start;
    justify-content:space-between;
    gap:12px;
    flex-wrap:wrap;
    min-width:0;
}

.scrub-info{
    display:flex;
    gap:14px;
    flex-wrap:wrap;
    font-size:13px;
    color:#cfe0ff;
    min-width:0;
}

.speed-controls{
    display:flex;
    gap:8px;
    justify-content:flex-end;
    flex-wrap:wrap;
    flex:0 1 auto;
    min-width:0;
}
.btn.small.speed{ padding:6px 10px; white-space:nowrap; }
.btn.speed.active{
    border:2px solid var(--accent);
    box-shadow:0 0 0 2px rgba(119,167,255,.14) inset;
    filter: brightness(1.08);
}

.player-controls{
    display:flex;
    gap:8px;
    align-items:center;
    justify-content:flex-end;
    flex-wrap:wrap;
    min-width:0;
}

.switch{
    display:flex;
    align-items:center;
    gap:10px;
    padding:6px 10px;
    border:1px solid var(--border);
    border-radius:12px;
    background:rgba(15,22,34,.55);
    cursor:pointer;
    user-select:none;
}

.switch input{
    position:absolute;
    opacity:0;
    width:1px;
    height:1px;
}

.switch .slider{
    width:42px;
    height:22px;
    border-radius:999px;
    background:rgba(255,255,255,0.18);
    border:1px solid rgba(255,255,255,0.16);
    position:relative;
    transition:background .15s ease, border-color .15s ease;
}

.switch .slider::after{
    content:"";
    position:absolute;
    top:50%;
    left:3px;
    width:16px;
    height:16px;
    border-radius:50%;
    background:rgba(255,255,255,0.88);
    transform:translateY(-50%);
    transition:transform .15s ease;
    box-shadow:0 1px 6px rgba(0,0,0,0.35);
}

.switch input:checked + .slider{
    background:rgba(119,167,255,0.55);
    border-color:rgba(119,167,255,0.75);
}

.switch input:checked + .slider::after{
    transform:translateY(-50%) translateX(20px);
}

.switch-text{
    font:12px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    color:rgba(255,255,255,0.86);
    white-space:nowrap;
}

.segment-section{
    margin:12px;
    margin-top:12px;
    gap:8px;
    overflow:hidden;
    flex:0 0 auto;
}

.segment-head{
    margin-bottom:0;
    flex:0 0 auto;
}

.segment-tools {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-top: 0;
    flex-wrap: wrap;
    min-width:0;
    flex:0 0 auto;
}

.segment-label {
    flex:1 1 220px;
    min-width:180px;
    max-width:420px;
    padding:8px 10px;
    border-radius:10px;
    border:1px solid var(--border);
    background:rgba(10,14,20,.72);
    color:var(--text);
}

.segment-status {
    margin-top:0;
    min-width:0;
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
}

.run-filter-bar {
    display: flex;
    gap: 8px;
    align-items: center;
    margin: 10px 0 8px 0;
    flex-wrap: wrap;
}

.run-filter-input {
    flex: 1 1 180px;
    min-width: 160px;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid #2f3640;
    background: #11161c;
    color: #e8eef7;
}

.run-filter-status {
    margin-bottom: 8px;
}

.overlay-fresh { outline: 2px solid rgba(0, 180, 0, 0.6); }
.overlay-stale { outline: 2px solid rgba(220, 160, 0, 0.7); }
.overlay-missing { outline: 2px solid rgba(220, 0, 0, 0.7); }

.retrofit-section{
    margin:12px;
    margin-top:0;
    gap:8px;
    overflow:hidden;
    flex:0 0 auto;
}

.retrofit-head{
    align-items:center;
    margin-bottom:0;
    flex:0 0 auto;
}

.retrofit-btn{
    white-space:nowrap;
    flex:0 0 auto;
}

.retrofit-destination{display:flex;flex-direction:column;gap:6px;}
.retrofit-coordinate-fields{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:10px;}
.retrofit-coordinate-fields label{display:flex;flex-direction:column;gap:4px;font-size:12px;}
.retrofit-coordinate-fields input{min-width:0;padding:7px 9px;border:1px solid var(--border);border-radius:6px;background:#11161c;color:#e8eef7;}
.retrofit-destination-help{font-size:11px;}

.retrofit-panel{
    display:flex;
    flex-direction:column;
    gap:6px;
    min-height:0;
    overflow:hidden;
    flex:0 0 auto;
}

.retrofit-panel-title{
    font-size:12px;
    font-weight:600;
    letter-spacing:.2px;
    flex:0 0 auto;
}

.retrofit-output{
    flex:0 1 auto;
    min-height:56px;
    max-height:110px;
    width:100%;
    margin:0;
    overflow:auto;
}


/* Keep preview visuals inside the preview card. Mapbox likes absolute children;
   these containment rules prevent them from visually bleeding into segmentation/retrofit. */
.player .viewport,
.player .mini-map,
.player .proj-box{
    max-width:100%;
    flex:0 0 auto;
}

.player .mapboxgl-map,
.player .mapboxgl-canvas-container,
.player .mapboxgl-canvas{
    max-width:100% !important;
    max-height:100% !important;
}

.segment-section,
.retrofit-section{
    position:relative;
    z-index:1;
}

@media (max-width: 1650px){
    :root{
        --top-workspace-h: calc(100vh - 104px);
    }

    .viewport-row{
        gap:10px;
    }

    .switch{
        padding:5px 8px;
        gap:8px;
    }

    .btn{
        padding:7px 9px;
    }

    .btn.small{
        padding:5px 7px;
    }
}

@media (max-width: 1350px){
    .grid{
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto auto;
        grid-template-areas:
      "meta"
      "asp"
      "joined"
      "eval";
        height:auto;
    }
    .grid .card{
        min-height:320px;
    }
    .viewport-row{ grid-template-columns: 1fr; }
    .player{ height:auto; flex-basis:auto; overflow:visible; }
    .viewport, .mini-map, .proj-box{ height:240px; }
    .scrub-under{ flex-wrap:wrap; }
    .speed-controls{ flex-wrap:wrap; }
    .player{ margin:0 12px; }
    .retrofit-section{ margin:12px; }
    .frame-loading-panel{
        min-width:calc(100% - 32px);
        max-width:calc(100% - 32px);
    }
    .frame-loading-title{
        font-size:16px;
    }
}

body[data-mode="live"] .main{ grid-template-columns: 1fr; }
body[data-mode="live"] #catalogPanel{ display:none !important; }
body[data-mode="live"] .panel.content{ grid-column: 1 / -1; }
.replay-vocalizer-status{
    color:#cfe0ff;
}

.replay-vocalizer-control input:disabled + .slider{
    opacity:0.45;
}

.replay-vocalizer-control input:disabled ~ .switch-text{
    color:rgba(255,255,255,0.45);
}
