:root {
    --bg: #111111;
    --panel: #1e1e1e;
    --panel2: #141414;
    --line: #424242;
    --line2: #2a2a2a;
    --text: #e8e8e8;
    --muted: #888888;
    --bad: #f48771;
    --ok: #89d185;
    --btn: #333333;
    --btn2: #2d2d2d;
    --btnHover: #444444;
    --primary: #666666;
    --primary2: #4d4d4d;
    --shadow: rgba(0, 0, 0, 0.5);
    --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, SF Mono, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New";
    --sans: 'Inter', ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    font-size: 13px;
    line-height: 1.25;

    padding: 10px;
    height: 100vh;
    overflow: hidden;
    /* prevents page scrollbar */
    display: flex;
    flex-direction: column;
    gap: 10px;
    /* space between banner and app */
}


/* Banner CTA */
.mrg-cta {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    border: 1px solid var(--line);
    background: var(--btn);
    color: var(--text);
    padding: 7px 10px;
    border-radius: 0;
    cursor: pointer;
    font-size: 12px;
}

.mrg-cta:hover {
    background: var(--btnHover);
}

.mrg-cta .tag {
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
    opacity: 0.9;
}

.mrg-cta.primary {
    border-color: var(--primary);
    color: #e0e0e0;
    background: transparent;
}

.mrg-cta.primary:hover {
    background: #333333;
}

.mrg-cta .badge {
    font-family: var(--mono);
    font-size: 10px;
    padding: 2px 6px;
    border: 1px solid rgba(120, 120, 120, 0.65);
    color: #e0e0e0;
    background: rgba(120, 120, 120, 0.10);
}

/* 2-line CTA text */
.mrg-cta {
    flex-direction: column;
    align-items: flex-end;
    /* right aligned in the banner */
    gap: 2px;
    line-height: 1.05;
}

.mrg-cta .cta-main {
    font-weight: 700;
}

.mrg-cta .cta-sub {
    font-family: var(--mono);
    font-size: 10px;
    color: var(--muted);
    opacity: 0.95;
}


.app {
    display: grid;
    grid-template-columns: minmax(0, 48fr) minmax(0, 52fr);
    gap: 10px;

    flex: 1 1 auto;
    /* fill remaining space under banner */
    min-height: 0;
    /* critical: allows children to shrink without overflow */
}


.col-left,
.col-right {
    display: grid;
    gap: 10px;
    min-height: 0;
}

.col-left {
    grid-template-rows: 180px 1fr;
}

/* right column uses dynamic template rows set by JS */
.col-right {
    grid-template-rows: 46% calc(54% - 10px);
    gap: 10px;
    min-height: 0;
}


.field[hidden] {
    display: none !important;
}

.panel {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.03), rgba(255, 255, 255, 0)), var(--panel);
    border: 1px solid var(--line);
    border-radius: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0;
    box-shadow: 0 10px 24px var(--shadow);
}

/* OLD BEHAVIOR: minimize collapses body but keeps header visible */
.panel.minimized {
    min-height: auto;
}

.panel.minimized .panel-body {
    display: none !important;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    border-bottom: 1px solid var(--line2);
    background: rgba(0, 0, 0, 0.12);
    flex: 0 0 auto;
}

.panel.minimized .panel-header {
    border-bottom: 0;
}

.panel.minimized .panel-header .actions>*:not(#btnMinViewer):not(#btnMinGcode) {
    display: none !important;
}

.title {
    font-weight: 700;
    letter-spacing: 0.2px;
}

.actions {
    display: flex;
    gap: 6px;
    align-items: center;
}

button,
select {
    font-family: inherit;
    font-size: 12px;
}

button {
    border: 1px solid var(--line);
    background: var(--btn);
    color: var(--text);
    padding: 7px 10px;
    border-radius: 0;
    cursor: pointer;
}

button:hover {
    background: var(--btnHover);
}

button.small {
    padding: 6px 8px;
}

button.green {
    background: rgba(38, 208, 124, 0.08);
    /* subtle green fill (visible but not loud) */
    border-color: rgba(38, 208, 124, 0.55);
    /* brighter than the dark green border */
    color: #c6ffe4;
    /* same mint text as your ok pill */
    box-shadow: 0 0 0 1px rgba(38, 208, 124, 0.10) inset;
}

button.green:hover {
    background: rgba(38, 208, 124, 0.10);
    /* subtle fill (matches .pill.ok) */
    border-color: rgba(25, 211, 122, 0.90);
    /* a bit more â€œeye-catchingâ€ on hover */
}

button.green:active {
    background: rgba(46, 204, 113, 0.20);
}




/* Primary buttons: neutral */
button.primary {
    background: transparent;
    border-color: var(--primary);
    color: #e0e0e0;
}

button.primary:hover {
    background: #333333;
}




button.ghost {
    background: rgba(255, 255, 255, 0.05);
}

button.ghost:hover {
    background: rgba(255, 255, 255, 0.1);
}

button.danger {
    background: rgba(255, 85, 85, 0.08);
    border-color: #6b2a2a;
    color: #ffb3b3;
}

button.danger:hover {
    background: rgba(255, 85, 85, 0.16);
}




.tabs {
    display: inline-flex;
    border: 1px solid var(--line);
    background: var(--btn2);
    background: var(--btn2);
}

.tab {
    border: 0;
    border-radius: 0;
    padding: 6px 10px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.tab.active {
    background: #333333;
    color: #e0e0e0;
    outline: 1px solid var(--primary);
    outline-offset: -1px;
}





select {
    background: var(--btn2);
    color: var(--text);
    border: 1px solid var(--line);
    padding: 6px 8px;
    border-radius: 0;
    outline: none;
}

.panel-body {
    padding: 10px;
    overflow: auto;
    min-height: 0;
    flex: 1 1 auto;
}

.tube-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 10px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.label {
    color: var(--muted);
    font-size: 11px;
    letter-spacing: 0.2px;
}

input {
    background: var(--panel2);
    color: var(--text);
    border: 1px solid var(--line);
    padding: 7px 8px;
    border-radius: 0;
    outline: none;
    width: 100%;
    accent-color: var(--ok);
}

input:focus {
    border-color: var(--ok);
    box-shadow: 0 0 0 2px rgba(43, 178, 104, 0.18);
}

input.invalid {
    border-color: var(--bad);
    background: rgba(255, 85, 85, 0.08);
}

input.invalid:focus {
    box-shadow: 0 0 0 2px rgba(255, 85, 85, 0.18);
}

/* --- Tube panel: compact, no horizontal scrolling --- */
#panelTube .tube-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    /* allow columns to shrink */
    gap: 6px 10px;
    align-items: center;
}

/* each row becomes: [label][input] */
#panelTube .field {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    min-width: 0;
    /* critical to prevent overflow in CSS grid */
}

#panelTube .label {
    margin: 0;
    white-space: nowrap;
}

/* inputs fill available space, but won't force overflow */
#panelTube input {
    width: 100%;
    min-width: 0;
    /* critical: allows shrinking instead of overflowing */
    padding: 6px 6px;
    text-align: right;
}


.status {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.pill {
    padding: 4px 8px;
    border: 1px solid var(--line);
    background: rgba(0, 0, 0, 0.18);
    border-radius: 0;
    color: var(--muted);
    font-family: var(--mono);
    font-size: 11px;
}

.pill.ok {
    color: #c6ffe4;
    border-color: #1e3c2d;
    background: rgba(38, 208, 124, 0.10);
}

.pill.bad {
    color: #ffd2d2;
    border-color: #3d1e1e;
    background: rgba(255, 77, 77, 0.12);
}

.cutlist-scroll {
    border: 1px solid var(--line2);
    background: rgba(0, 0, 0, 0.10);
    overflow: auto;
    max-height: calc(100% - 0px);
}

.scrollbar::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

.scrollbar::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.18);
}

.scrollbar::-webkit-scrollbar-thumb {
    background: #333333;
    border: 1px solid #444444;
}

.scrollbar::-webkit-scrollbar-thumb:hover {
    background: #4a4a4a;
}

.scrollbar {
    scrollbar-color: #333333 rgba(0, 0, 0, 0.18);
    scrollbar-width: thin;
}

table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    font-size: 12px;
}

th,
td {
    border-bottom: 1px solid var(--line2);
    padding: 7px 8px;
    vertical-align: top;
}

th {
    position: sticky;
    top: 0;
    background: var(--panel2);
    /* opaque */
    border-bottom: 1px solid var(--line);
    text-align: left;
    color: var(--muted);
    font-weight: 600;
    z-index: 2;
}


td.small {
    color: var(--muted);
}

td.op {
    white-space: normal;
    overflow-wrap: anywhere;
}

td.op .opId {
    margin-top: 3px;
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
    opacity: 0.9;
}


td.op .opId {
    margin-top: 3px;
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
    opacity: 0.9;
}


td.details {
    white-space: normal;
    overflow-wrap: anywhere;
    color: var(--muted);
}

.row-actions {
    display: flex;
    gap: 6px;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
}

.row-actions .split {
    position: relative;
    display: inline-flex;
    gap: 0;
    border: 1px solid var(--line);
    border: 1px solid var(--line);
}

.row-actions .split button {
    border: 0;
    border-right: 1px solid var(--line);
    background: var(--btn);
    padding: 6px 8px;
    border-radius: 0;
}

.row-actions .split button:last-child {
    border-right: 0;
}

.row-actions .split .menu {
    position: absolute;
    right: 0;
    top: 100%;
    background: var(--panel2);
    border: 1px solid var(--line);
    min-width: 140px;
    box-shadow: 0 10px 24px var(--shadow);
    z-index: 50;
}

.row-actions .split .menu button {
    width: 100%;
    text-align: left;
    border: 0;
    border-bottom: 1px solid var(--line2);
    padding: 8px 10px;
    background: transparent;
}

.row-actions .split .menu button:hover {
    background: rgba(255, 255, 255, 0.04);
}

.viewer-wrap {
    position: relative;
    height: 100%;
    min-height: 0;
    border: 1px solid var(--line2);
    background: #070a0f;
}

iframe {
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
    background: #070a0f;
}

textarea {
    width: 100%;
    flex: 1 1 auto;
    /* <-- key: fill remaining space */
    height: auto;
    /* <-- don't force 100% in flex column */
    min-height: 180px;
    resize: none;
    border: 1px solid var(--line2);
    background: #070a0f;
    color: #e8f0ff;
    font-family: var(--mono);
    font-size: 11.5px;
    line-height: 1.25;
    padding: 10px;
    outline: none;
    border-radius: 0;
}

.statusline {
    margin-top: 8px;
    color: var(--muted);
    font-family: var(--mono);
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

dialog {
    border: 1px solid var(--line);
    border-radius: 0;
    background: var(--panel);
    color: var(--text);
    box-shadow: 0 20px 60px var(--shadow);
    padding: 0;
    width: min(720px, calc(100vw - 40px));
}

dialog::backdrop {
    background: rgba(0, 0, 0, 0.55);
}

.dlg-head {
    padding: 10px 12px;
    border-bottom: 1px solid var(--line2);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    background: rgba(0, 0, 0, 0.18);
}

.dlg-title {
    font-weight: 700;
}

.dlg-body {
    padding: 12px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.dlg-foot {
    border-top: 1px solid var(--line2);
    padding: 10px 12px;
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    background: rgba(0, 0, 0, 0.12);
}

.span2 {
    grid-column: 1 / span 2;
}

.hint {
    color: var(--muted);
    font-size: 11px;
}

.row-actions .split>button[data-act="copyMenu"] {
    padding: 6px 6px !important;
    min-width: 26px !important;
    letter-spacing: 2px;
}


tr.other-tube {
    opacity: 0.45;
}


/* Cutlist: prevent the panel-body from scrolling so only the table scrolls */
#panelCuts .panel-body {
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#panelCuts .cutlist-scroll {
    flex: 1 1 auto;
    min-height: 0;
}


/* highlight the selected cutlist row */
#tb tr.selected td {
    background: rgba(25, 211, 122, 0.10);
}

tr.selected td {
    background: rgba(0, 255, 0, 0.10);
}

tr.selected td:first-child {
    box-shadow: inset 3px 0 0 rgba(0, 255, 0, 0.55);
}


/* ===== Mr. Gusset Top Banner ===== */
#mrg-banner {
    display: flex;
    justify-content: space-between;
    align-items: baseline;

    padding: 8px 10px;
    margin-bottom: 0;

    background: rgba(0, 0, 0, 0.18);
    border: 1px solid var(--line);
    border-bottom-color: var(--line2);

    box-shadow: 0 10px 24px var(--shadow);
}

#mrg-banner .mrg-title {
    font-weight: 800;
    letter-spacing: 0.3px;
    /* reset browser h1 defaults so layout is unchanged */
    margin: 0;
    padding: 0;
    font-size: inherit;
}

#mrg-banner .mrg-sub {
    color: var(--muted);
    font-size: 11px;
    margin-left: 8px;
    font-weight: 600;
}

#mrg-banner .mrg-version {
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
}




/* =========================
   Responsive: 1-column layout
   (stack Viewer + G-code under Cutlist)
   ========================= */
@media (max-width: 980px) and (orientation: portrait) {


    #mrg-banner {
        margin-bottom: 10px;
    }

    #panelTube .tube-grid {
        grid-template-columns: 1fr !important;
    }


    .col-right {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
    }

    #panelViewer,
    #panelGcode {
        height: auto !important;
    }

    /* keep viewer at a reasonable fixed height in portrait */
    #panelViewer .viewer-wrap {
        height: 380px !important;
        min-height: 280px !important;
    }



    /* IMPORTANT: allow panels to expand in portrait (no clipping) */
    .panel {
        overflow: visible !important;
    }

    /* stack everything */
    .app {
        grid-template-columns: 1fr;
        height: auto;
        /* let the page flow */
        min-height: 0;
    }

    /* stop forcing the left column to be 180px + 1fr */
    .col-left {
        grid-template-rows: auto auto;
        min-height: 0;
    }

    /* right column becomes normal flow too */
    .col-right {
        grid-template-rows: auto auto;
        min-height: 0;
    }





    /* Allow the PAGE to scroll (override the base lock) */
    html {
        height: auto !important;
    }

    /* In portrait: disable the desktop flex layout so the page can grow */
    body {
        display: block !important;
    }

    /* .app was a flex child (flex:1) on desktop â€” kill that in portrait */
    .app {
        flex: none !important;
    }


    body {
        height: auto !important;
        overflow: auto !important;
    }

    /* Disable internal panel scrolling (so only the PAGE scrolls) */
    .panel-body {
        overflow: visible !important;
        flex: none !important;
        min-height: auto !important;
    }

    /* Cutlist: remove its internal scroller so it flows with page */
    .cutlist-scroll {
        overflow: visible !important;
        max-height: none !important;
    }



    #panelGcode textarea {
        flex: none !important;
        display: block;
        width: 100%;
        overflow: hidden !important;
        /* no internal scroll */
        resize: none !important;
        height: 100%;
        /* JS will set px height */
        min-height: 220px;

        /* never cap it */
        box-sizing: border-box;
        margin: 0;
        padding: 10px;
        flex: 1 1 auto;
    }

    /* ===== Main scrollbar styling (match Cutlist) ===== */
    @supports (scrollbar-color: #333333 rgba(0, 0, 0, 0.18)) {
        body {
            scrollbar-color: #333333 rgba(0, 0, 0, 0.18);
            scrollbar-width: thin;
        }
    }

    body::-webkit-scrollbar {
        width: 10px;
        height: 10px;
    }

    body::-webkit-scrollbar-track {
        background: rgba(0, 0, 0, 0.18);
    }

    body::-webkit-scrollbar-thumb {
        background: #333333;
        border: 1px solid #444444;
    }

    body::-webkit-scrollbar-thumb:hover {
        background: #4a4a4a;
    }



    /* === Portrait: Viewer must contain (clip) the iframe === */
    #panelViewer .panel-body {
        overflow: hidden !important;
        /* <-- stop bleed */
        flex: 1 1 auto !important;
        /* allow it to size properly */
        min-height: 0 !important;
        /* critical for nested flex */
        padding: 10px !important;
        /* keep even margins */
        display: flex;
        flex-direction: column !important;
    }

    #panelViewer .viewer-wrap {
        flex: 1 1 auto !important;
        min-height: 280px !important;
        /* your portrait target */
        height: 380px !important;
        /* force a real box */
        overflow: hidden !important;
        /* <-- also clip here */
        box-sizing: border-box !important;
    }

    #panelViewer iframe {
        width: 100% !important;
        height: 100% !important;
        display: block !important;
    }




    #panelTube .tube-grid {
        grid-template-columns: 1fr !important;
    }



}