:root {
    /* BMW M Power palette — carbon base + M tricolor (blue / violet / red) */
    --bg: #06080d;
    --bg-2: #0a0e16;
    --surface: #0e131d;
    --surface-2: #141b29;
    --surface-3: #1b2436;
    --border: #26334a;
    --border-soft: #18202f;
    --text: #eaf0fb;
    --text-dim: #9aa8c2;
    --muted: #677692;

    /* M signature colors */
    --m-blue-light: #00A0DE;
    --m-blue: #0066B1;
    --m-violet: #6F2DA8;
    --m-red: #E2001A;
    --m-grad: linear-gradient(120deg, #00A0DE 0%, #2E7BD6 32%, #6F2DA8 64%, #E2001A 100%);

    --primary: #00A0DE;
    --primary-2: #2EB6F0;
    --primary-glow: rgba(0, 160, 222, 0.40);
    --accent: #00A0DE;
    --green: #1fd187;
    --red: #E2001A;
    --radius: 14px;
    --radius-sm: 10px;
    --shadow: 0 20px 50px -20px rgba(0, 0, 0, 0.8);
    --font: 'Inter', system-ui, -apple-system, sans-serif;
    --mono: 'JetBrains Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
    font-family: var(--font);
    color: var(--text);
    background:
        radial-gradient(1100px 560px at 88% -12%, rgba(226, 0, 26, 0.12), transparent 60%),
        radial-gradient(1000px 520px at 50% -8%, rgba(111, 45, 168, 0.12), transparent 58%),
        radial-gradient(900px 500px at -8% 110%, rgba(0, 160, 222, 0.12), transparent 55%),
        var(--bg);
    -webkit-font-smoothing: antialiased;
}

.app {
    display: grid;
    grid-template-columns: 312px minmax(0, 1fr);
    gap: 18px;
    padding: 18px;
    min-height: 100vh;
    max-width: 1720px;
    margin: 0 auto;
}

/* ---------------------------------------------------------------- Sidebar */
.sidebar {
    display: flex;
    flex-direction: column;
    gap: 18px;
    background: linear-gradient(180deg, var(--surface), var(--bg-2));
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 22px 18px;
    position: sticky;
    top: 18px;
    height: calc(100vh - 36px);
    overflow-y: auto;
    overflow-x: hidden;
}
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 8px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }

.brand {
    display: flex; align-items: center; gap: 12px;
    padding-bottom: 16px;
    position: relative;
}
/* M tricolor stripe under the brand */
.brand::after {
    content: '';
    position: absolute; left: 0; right: 0; bottom: 0;
    height: 3px; border-radius: 3px;
    background: var(--m-grad);
}
.brand__logo {
    width: 46px; height: 46px;
    flex-shrink: 0;
    display: grid; place-items: center;
    border-radius: 50%;
    overflow: hidden;
}
.brand__logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}
.brand__text h1 { font-size: 17px; font-weight: 800; letter-spacing: -0.3px; }
.brand__text span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 1.4px; }

.steps { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.step {
    display: flex; align-items: center; gap: 12px;
    width: 100%;
    text-align: left;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 11px 12px;
    cursor: pointer;
    color: var(--text-dim);
    transition: all 0.18s ease;
}
.step:hover { background: var(--surface-2); color: var(--text); }
.step.is-active {
    background: var(--surface-3);
    border-color: var(--border);
    color: var(--text);
}
.step__num {
    width: 26px; height: 26px;
    flex: none;
    display: grid; place-items: center;
    border-radius: 8px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    font-size: 13px; font-weight: 700;
    color: var(--text-dim);
}
.step.is-active .step__num {
    background: var(--m-grad);
    color: #fff; border-color: transparent;
}
.step__body { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.step__title { font-size: 13.5px; font-weight: 600; }
.step__hint { font-size: 11px; color: var(--muted); }
.step__status { font-size: 10px; color: var(--muted); transition: color 0.2s; }
.step__status.is-ready { color: var(--green); }

.sidebar__foot { margin-top: auto; display: flex; gap: 14px; padding-top: 8px; border-top: 1px solid var(--border-soft); }
.link-btn { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 12.5px; padding: 8px 0; }
.link-btn:hover { color: var(--text); }

/* ------------------------------------------------------------------- Main */
.main {
    background: var(--surface);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 22px 24px;
    min-width: 0;
    display: flex; flex-direction: column;
}
.topbar {
    display: flex; align-items: flex-start; justify-content: space-between;
    gap: 16px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--border-soft);
    margin-bottom: 18px;
}
.topbar h2 { font-size: 20px; font-weight: 700; letter-spacing: -0.3px; }
.topbar__actions { display: flex; gap: 10px; flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 13px; margin-top: 3px; }

.panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.is-hidden { display: none !important; }

/* ---------------------------------------------------------------- Dropzone */
.dropzone {
    border: 1.5px dashed var(--border);
    border-radius: var(--radius);
    background: linear-gradient(180deg, var(--surface-2), transparent);
    padding: 38px 24px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
}
.dropzone:hover, .dropzone.is-drag {
    border-color: var(--primary);
    background: rgba(255, 90, 31, 0.06);
}
.dropzone__icon { font-size: 40px; margin-bottom: 10px; }
.dropzone h3 { font-size: 16px; font-weight: 600; }
.dropzone p { color: var(--muted); font-size: 13px; margin-top: 6px; }
.dropzone .pill { display: inline-block; margin-top: 14px; font-size: 11.5px; color: var(--text-dim); background: var(--surface-3); padding: 5px 12px; border-radius: 100px; border: 1px solid var(--border); }

.panel__loaded { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.file-tag { display: inline-flex; align-items: center; gap: 9px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 100px; padding: 7px 14px; font-size: 13px; }
.file-tag b { font-weight: 600; }
.file-tag .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 10px var(--green); }
.panel__meta { font-size: 12px; color: var(--muted); }

/* ------------------------------------------------------------------ Tables */
.table-scroll {
    flex: 1; min-height: 0;
    overflow: auto;
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-sm);
    background: var(--bg-2);
}
table.grid { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; font-variant-numeric: tabular-nums; }
table.grid th, table.grid td {
    border-right: 1px solid var(--border-soft);
    border-bottom: 1px solid var(--border-soft);
    padding: 0;
    text-align: center;
    font-size: 12.5px;
    font-family: var(--mono);
}
table.grid th { background: var(--surface-2); color: var(--text-dim); font-weight: 600; padding: 8px 10px; position: sticky; top: 0; z-index: 2; }
table.grid th.corner { left: 0; z-index: 3; background: var(--surface-3); color: var(--primary-2); }
table.grid th.rowhead { position: sticky; left: 0; z-index: 1; background: var(--surface-2); }
table.grid td input {
    width: 100%; min-width: 64px;
    border: none; background: transparent;
    color: var(--text);
    text-align: center;
    font-family: var(--mono); font-size: 12.5px;
    padding: 7px 8px;
    outline: none;
}
table.grid td input:focus { background: rgba(255, 90, 31, 0.12); box-shadow: inset 0 0 0 1px var(--primary); }
table.grid td.readonly { color: var(--text); padding: 7px 10px; }

/* --------------------------------------------------------------- Results */
.results-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 16px; flex-wrap: wrap; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 4px; gap: 4px; }
.seg__btn { background: transparent; border: none; color: var(--text-dim); padding: 8px 16px; border-radius: 9px; cursor: pointer; font-size: 13px; font-weight: 600; }
.seg__btn small { font-weight: 400; color: var(--muted); }
.seg__btn.is-active { background: var(--surface-3); color: var(--text); box-shadow: var(--shadow); }
.seg__btn.is-active small { color: var(--text-dim); }
.results-toolbar__right { display: flex; align-items: center; gap: 14px; }

.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-dim); cursor: pointer; user-select: none; }
.switch input { accent-color: var(--primary); width: 16px; height: 16px; }

.result-grid-wrap { flex: 1; min-height: 0; display: flex; }
.result-grid-wrap .table-scroll { flex: 1; }

.empty { margin: auto; text-align: center; color: var(--muted); }
.empty__icon { font-size: 48px; margin-bottom: 12px; opacity: 0.7; }
.empty p { font-size: 14px; }

/* --------------------------------------------------------------- Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    font-size: 13.5px; font-weight: 600;
    cursor: pointer;
    transition: all 0.16s ease;
}
.btn:hover { border-color: var(--primary); transform: translateY(-1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn--block { width: 100%; }
.btn--primary {
    background: var(--m-grad);
    border-color: transparent; color: #fff;
    box-shadow: 0 10px 24px -10px var(--primary-glow);
}
.btn--primary:hover:not(:disabled) { box-shadow: 0 14px 30px -10px var(--primary-glow); }
.btn--ghost { background: transparent; }
.btn__icon { font-size: 15px; }

/* --------------------------------------------------------------- Dropdown */
.dropdown { position: relative; }
.dropdown__menu {
    position: absolute; right: 0; top: calc(100% + 8px);
    background: var(--surface-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px;
    min-width: 150px;
    box-shadow: var(--shadow);
    display: none; z-index: 20;
}
.dropdown__menu.is-open { display: block; }
.dropdown__menu button { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--text); padding: 9px 12px; border-radius: 7px; cursor: pointer; font-size: 13px; }
.dropdown__menu button:hover { background: var(--surface-2); }

/* ------------------------------------------------------------------- Rail */
.rail { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 18px; height: fit-content; }
.rail__card {
    background: linear-gradient(180deg, var(--surface), var(--bg-2));
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    padding: 20px;
}
.rail__card--soft { background: var(--surface); }
.rail__card h3 { font-size: 15px; font-weight: 700; }
.rail__card > .muted { margin-bottom: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field span { font-size: 12.5px; font-weight: 600; color: var(--text-dim); }
.field span small { color: var(--muted); font-weight: 400; }
.field input {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    padding: 11px 14px;
    font-size: 14px;
    font-family: var(--mono);
    outline: none;
    transition: border-color 0.15s;
}
.field input:focus { border-color: var(--primary); }

.rail__lock { font-size: 11.5px; color: var(--muted); text-align: center; margin-top: 10px; }
.rail__lock.is-hidden { display: none; }

.readout { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 18px; }
.readout__item { background: var(--bg-2); border: 1px solid var(--border-soft); border-radius: var(--radius-sm); padding: 14px; }
.readout__label { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.8px; margin-bottom: 6px; }
.readout__value { font-size: 22px; font-weight: 700; font-family: var(--mono); color: var(--primary-2); }
.readout__value small { font-size: 12px; color: var(--muted); margin-left: 2px; }

.pipeline { list-style: none; counter-reset: step; display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.pipeline li { position: relative; padding-left: 30px; font-size: 12.5px; color: var(--text-dim); line-height: 1.5; }
.pipeline li::before {
    counter-increment: step; content: counter(step);
    position: absolute; left: 0; top: 0;
    width: 20px; height: 20px;
    display: grid; place-items: center;
    background: var(--surface-3); border: 1px solid var(--border);
    border-radius: 6px; font-size: 11px; font-weight: 700; color: var(--text-dim);
}
.pipeline b { color: var(--text); }
.mono { font-family: var(--mono); color: var(--accent); }

/* ----------------------------------------------------------------- Toasts */
.toasts { position: fixed; bottom: 22px; right: 22px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast {
    background: var(--surface-3);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-sm);
    padding: 13px 16px;
    font-size: 13px;
    box-shadow: var(--shadow);
    min-width: 240px; max-width: 360px;
    animation: toastIn 0.25s ease;
}
.toast.is-success { border-left-color: var(--green); }
.toast.is-error { border-left-color: var(--red); }
.toast b { display: block; margin-bottom: 2px; }
.toast span { color: var(--text-dim); }
@keyframes toastIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }

/* ------------------------------------------------------------------ Modal */
.modal { position: fixed; inset: 0; z-index: 200; display: none; align-items: center; justify-content: center; padding: 24px; }
.modal.is-open { display: flex; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(4, 7, 12, 0.7); backdrop-filter: blur(4px); }
.modal__card {
    position: relative;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 30px;
    max-width: 560px; width: 100%;
    max-height: 80vh; overflow: auto;
    box-shadow: var(--shadow);
}
.modal__close { position: absolute; top: 14px; right: 16px; background: none; border: none; color: var(--muted); font-size: 26px; cursor: pointer; line-height: 1; }
.modal__close:hover { color: var(--text); }
.modal__body h2 { font-size: 20px; margin-bottom: 14px; }
.modal__body h3 { font-size: 14px; margin: 18px 0 8px; color: var(--primary-2); }
.modal__body p, .modal__body li { color: var(--text-dim); font-size: 13.5px; line-height: 1.6; }
.modal__body ol, .modal__body ul { padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.modal__body a { color: var(--accent); }

.sample-dl {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 4px;
}
.sample-dl__item {
    display: grid;
    grid-template-columns: 56px 1fr auto;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-sm);
    text-decoration: none;
    transition: border-color .15s, background .15s;
}
.sample-dl__item:hover {
    border-color: var(--primary);
    background: var(--surface-3);
}
.sample-dl__step {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--primary-2);
}
.sample-dl__name {
    font-family: var(--mono);
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
}
.sample-dl__hint {
    font-size: 11px;
    color: var(--muted);
}

/* ----------------------------------------------------------------- Spinner */
.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --------------------------------------------------------------- Responsive */
@media (max-width: 1080px) {
    .app { grid-template-columns: 288px minmax(0, 1fr); }
}
@media (max-width: 820px) {
    .app { grid-template-columns: 1fr; padding: 12px; }
    .sidebar { position: static; height: auto; overflow: visible; }
}
