/* Nester styles — DESIGN-TOKEN CONTRACT (the skin API, house convention):
   components read ONLY tokens, never raw color. Color roles: -face (fill), -edge (hairline),
   -ink (text). State suffix last (-hover/-active/-disabled). One skin for now ("studio":
   silver machine chassis around a black CRT screen, like DDCS Studio); a new skin = a new
   [data-theme] block redefining tokens, no component edits. */

:root, [data-theme="studio"] {
    --bg: #c4bfb5;            /* silver chassis */
    --panel-face: #cfcabf;    /* silver plate */
    --panel-edge: #6f6a60;
    --panel-ink: #101010;
    --panel-ink-dim: #4b4843;

    --screen-face: #0a0a0a;   /* the readout */
    --screen-edge: #3a3a36;
    --screen-grid: #1c221c;
    --phosphor: #15e000;      /* CRT green */
    --cyan: #1fd4d4;
    --amber: #ff9e1b;
    --red: #ff3838;
    --dro-white: #f4f4f4;

    --btn-face: linear-gradient(#e2ddd2, #c9c4b9);
    --btn-face-hover: linear-gradient(#ece7dc, #d3cec3);
    --btn-face-active: linear-gradient(#bfbaaf, #cfcabf);
    --btn-edge: #7a7568;
    --btn-ink: #101010;
    --btn-primary-face: linear-gradient(#3f83ff, #1668ff);
    --btn-primary-face-hover: linear-gradient(#5b95ff, #2f79ff);
    --btn-primary-ink: #ffffff;
    --btn-primary-edge: #0f4ecc;

    --field-face: #f4f2ec;
    --field-edge: #8a857a;
    --field-ink: #101010;

    --select: #1668ff;
    --radius: 6px;
    --mono: "DejaVu Sans Mono", "Consolas", ui-monospace, monospace;
    --sans: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--panel-ink);
    font-family: var(--sans);
    font-size: 14px;
    display: flex;
    flex-direction: column;
    height: 100dvh; /* mobile address-bar safe */
    overflow: hidden;
}

/* ---------- header ---------- */
.app-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    background: var(--panel-face);
    border-bottom: 1px solid var(--panel-edge);
    flex-wrap: wrap;
}
.brand {
    font-family: var(--mono);
    font-weight: bold;
    font-size: 18px;
    letter-spacing: 3px;
}
.hdr-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.hdr-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.btn-icon { padding: 0; min-width: 44px; font-size: 18px; line-height: 1; }

/* ---------- buttons: 44px touch targets ---------- */
.btn {
    min-height: 44px;
    padding: 0 16px;
    border: 1px solid var(--btn-edge);
    border-radius: var(--radius);
    background: var(--btn-face);
    color: var(--btn-ink);
    font-size: 14px;
    font-family: var(--sans);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--btn-face-hover); }
.btn:active { background: var(--btn-face-active); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-primary {
    background: var(--btn-primary-face);
    color: var(--btn-primary-ink);
    border-color: var(--btn-primary-edge);
    font-weight: 600;
}
.btn-primary:hover { background: var(--btn-primary-face-hover); }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: 13px; }

/* ---------- job strip (top bar settings) ---------- */
.tool-bar {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
    padding: 7px 12px;
    background: var(--panel-face);
    border-bottom: 1px solid var(--panel-edge);
}
.bar-field { display: flex; align-items: center; gap: 7px; }
.bar-field > label {
    font-size: 12px;
    font-family: var(--mono);
    letter-spacing: 1px;
    text-transform: uppercase;
    color: var(--panel-ink-dim);
    white-space: nowrap;
}
.bar-field .unit { text-transform: none; letter-spacing: 0; }
.bar-field input[type="number"], .bar-field select {
    min-height: 40px;
    padding: 0 8px;
    font-size: 15px;
    font-family: var(--mono);
    background: var(--field-face);
    color: var(--field-ink);
    border: 1px solid var(--field-edge);
    border-radius: var(--radius);
}
.bar-field input[type="number"] { width: 96px; }

/* ---------- segmented toggle (mm | in) ---------- */
.seg {
    display: inline-flex;
    border: 1px solid var(--btn-edge);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--field-face);
}
.seg-btn {
    min-height: 44px;
    padding: 0 14px;
    border: none;
    background: transparent;
    color: var(--panel-ink-dim);
    font-family: var(--mono);
    font-size: 13px;
    white-space: nowrap; /* "Imperial in" must never break mid-button */
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.seg-btn + .seg-btn { border-left: 1px solid var(--btn-edge); }
.seg-btn.active {
    background: var(--btn-primary-face);
    color: var(--btn-primary-ink);
    font-weight: 600;
}

/* ---------- switch (checkbox) ---------- */
.switch { display: inline-flex; align-items: center; min-height: 44px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
    width: 48px; height: 26px;
    border-radius: 13px;
    background: var(--field-face);
    border: 1px solid var(--field-edge);
    position: relative;
    transition: background 120ms ease;
}
.switch-thumb {
    position: absolute; top: 2px; left: 2px;
    width: 20px; height: 20px;
    border-radius: 50%;
    background: var(--panel-ink-dim);
    transition: transform 120ms ease, background 120ms ease;
}
.switch input:checked + .switch-track { background: var(--select); border-color: var(--btn-primary-edge); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(22px); background: #fff; }

/* ---------- layout ---------- */
main { flex: 1; display: flex; min-height: 0; }
#sidebar {
    width: 300px;
    min-width: 200px;
    flex: 0 0 auto;
    overflow-y: auto;
    background: var(--panel-face);
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.panel-title {
    margin: 0 0 8px;
    font-size: 12px;
    font-family: var(--mono);
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--panel-ink-dim);
}

/* Drag handle: a real 10px grab strip (a 1px border is not a touch target) with a visible
   grip mark, sitting between the sidebar and the viewport. */
.sidebar-grip {
    flex: 0 0 auto;
    width: 10px;
    background: var(--panel-face);
    border-left: 1px solid var(--panel-edge);
    border-right: 1px solid var(--panel-edge);
    cursor: col-resize;
    touch-action: none;
    position: relative;
}
.sidebar-grip::after {
    content: "";
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    width: 2px; height: 34px;
    border-left: 2px dotted var(--panel-edge);
}
.sidebar-grip:hover, .sidebar-grip.dragging { background: var(--btn-face-hover); }

/* ---------- parts list ---------- */
.parts-list { display: flex; flex-direction: column; gap: 6px; }
.empty-hint {
    color: var(--panel-ink-dim);
    font-size: 13px;
    line-height: 1.5;
    padding: 10px;
    border: 1px dashed var(--panel-edge);
    border-radius: var(--radius);
    text-align: center;
}
.part-row {
    display: grid;
    grid-template-columns: 44px 1fr auto auto;
    align-items: center;
    gap: 8px;
    padding: 6px;
    background: var(--field-face);
    border: 1px solid var(--field-edge);
    border-radius: var(--radius);
}
.part-thumb {
    width: 44px; height: 44px;
    background: var(--screen-face);
    border: 1px solid var(--screen-edge);
    border-radius: 4px;
}
.part-meta { min-width: 0; }
.part-name {
    font-size: 13px; font-weight: 600;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.part-dims {
    font-size: 11px;
    font-family: var(--mono);
    color: var(--panel-ink-dim);
    white-space: nowrap;      /* an inch reading is long; one line, clipped, beats three lines */
    overflow: hidden;
    text-overflow: ellipsis;
}
.part-qty {
    width: 56px; min-height: 44px;
    text-align: center;
    font-size: 15px; font-family: var(--mono);
    background: var(--field-face);
    color: var(--field-ink);
    border: 1px solid var(--field-edge);
    border-radius: var(--radius);
}
.part-remove {
    min-width: 44px; min-height: 44px;
    border: none; background: none;
    color: var(--red);
    font-size: 18px;
    cursor: pointer;
}

/* ---------- settings form (rendered from SETTINGS_SCHEMA) ---------- */
.settings-form { display: flex; flex-direction: column; gap: 8px; }
.form-row {
    display: grid;
    grid-template-columns: 1fr 110px;
    align-items: center;
    gap: 8px;
}
.form-row label { font-size: 13px; }
.form-row .unit { color: var(--panel-ink-dim); font-size: 12px; }
.form-row input[type="number"], .form-row select {
    min-height: 44px;
    padding: 0 8px;
    font-size: 15px;
    font-family: var(--mono);
    background: var(--field-face);
    color: var(--field-ink);
    border: 1px solid var(--field-edge);
    border-radius: var(--radius);
    width: 100%;
}
.form-row .seg { width: 100%; }
.form-row .seg-btn { flex: 1; padding: 0 6px; }
.form-row.has-help { grid-template-areas: "label control" "help help"; }
.form-row.has-help > label { grid-area: label; }
.form-row.has-help > :not(label):not(.form-help) { grid-area: control; }
.form-help {
    grid-area: help;
    margin: 2px 0 6px;
    font-size: 12px;
    line-height: 1.45;
    color: var(--panel-ink-dim);
}
.link-btn {
    background: none;
    border: none;
    color: var(--select);
    font-size: 13px;
    padding: 10px 2px;
    min-height: 44px;
    cursor: pointer;
    text-align: left;
}
.link-btn:hover { text-decoration: underline; }

/* ---------- settings page ---------- */
.settings-page[hidden] { display: none; }
.settings-page {
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(16, 16, 16, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.settings-sheet {
    background: var(--panel-face);
    border: 1px solid var(--panel-edge);
    border-radius: var(--radius);
    width: min(560px, 100%);
    max-height: 100%;
    display: flex;
    flex-direction: column;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}
.settings-head, .settings-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 16px;
}
.settings-head { border-bottom: 1px solid var(--panel-edge); }
.settings-foot { border-top: 1px solid var(--panel-edge); }
.settings-head h2 {
    margin: 0;
    font-size: 15px;
    font-family: var(--mono);
    letter-spacing: 2px;
    text-transform: uppercase;
}
.settings-form.full {
    overflow-y: auto;
    padding: 8px 16px 16px;
    gap: 4px;
}
.settings-form.full .form-row { grid-template-columns: 1fr 180px; }
.section-title {
    margin: 18px 0 6px;
    font-size: 12px;
    font-family: var(--mono);
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--panel-ink-dim);
    border-bottom: 1px solid var(--panel-edge);
    padding-bottom: 5px;
}
.section-title:first-child { margin-top: 4px; }

/* ---------- viewport ---------- */
#viewport { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.sheet-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 10px;
    background: var(--panel-face);
    border-bottom: 1px solid var(--panel-edge);
    flex-wrap: wrap;
}
.sheet-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.sheet-tab {
    min-height: 36px;
    padding: 0 14px;
    font-family: var(--mono);
    font-size: 13px;
    background: var(--btn-face);
    border: 1px solid var(--btn-edge);
    border-radius: var(--radius);
    cursor: pointer;
}
.sheet-tab.active {
    background: var(--btn-primary-face);
    color: var(--btn-primary-ink);
    border-color: var(--btn-primary-edge);
}
.sheet-bar-right { display: flex; align-items: center; gap: 10px; }

/* The verdict line: did everything fit? Read from across the shop, so it carries its own
   colour band rather than sitting as one more grey stat. */
.fit-status {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    font-family: var(--mono);
    font-size: 14px;
    border-bottom: 1px solid var(--panel-edge);
}
.fit-status:empty { display: none; }
.fit-status .fit-icon { font-size: 16px; line-height: 1; }
.fit-status .fit-detail { color: var(--panel-ink-dim); font-size: 13px; }
.fit-status.ok { background: #d8e8d2; color: #14520c; }
.fit-status.ok .fit-detail { color: #2f6b28; }
.fit-status.bad { background: #f2d5d5; color: #8a1212; font-weight: 600; }
.fit-status.bad .fit-detail { color: #a33; font-weight: 400; }
.fit-status.idle { background: var(--panel-face); color: var(--panel-ink-dim); }
.stat-line { font-family: var(--mono); font-size: 13px; color: var(--panel-ink-dim); }

#nestCanvas {
    flex: 1;
    width: 100%;
    min-height: 0;
    background: var(--screen-face);
    touch-action: none; /* we own pan/zoom gestures */
    display: block;
    cursor: grab;
}

.drop-overlay[hidden] { display: none; } /* class display:flex would defeat [hidden] otherwise */
.drop-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(22, 104, 255, 0.25);
    border: 3px dashed var(--select);
    color: var(--dro-white);
    font-size: 22px;
    font-family: var(--mono);
    pointer-events: none;
    z-index: 10;
}

/* ---------- busy screen ---------- */
/* Covers the sheet view only (not the header), so Cancel and the settings stay reachable and
   it is obvious WHAT is busy. Real progress, not a spinner: the worker reports parts placed. */
.busy-overlay[hidden] { display: none; }
.busy-overlay {
    position: absolute;
    inset: 0;
    z-index: 12;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(10, 10, 10, 0.72);
}
.busy-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    min-width: min(340px, 80vw);
    padding: 22px 26px;
    background: var(--screen-face);
    border: 1px solid var(--screen-edge);
    border-radius: var(--radius);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.busy-title {
    font-family: var(--mono);
    font-size: 14px;
    letter-spacing: 4px;
    color: var(--phosphor);
}
.busy-bar {
    width: 100%;
    height: 10px;
    background: var(--screen-grid);
    border: 1px solid var(--screen-edge);
    border-radius: 5px;
    overflow: hidden;
}
.busy-fill {
    height: 100%;
    width: 0%;
    background: var(--phosphor);
    transition: width 100ms linear;
}
/* Before the first progress report there is nothing honest to show as a percentage, so the
   bar sweeps instead of sitting at a fake 0%. */
.busy-fill.indeterminate {
    width: 35%;
    animation: busy-sweep 1.1s ease-in-out infinite;
}
@keyframes busy-sweep {
    0%   { transform: translateX(-100%); }
    100% { transform: translateX(286%); }
}
.busy-line {
    font-family: var(--mono);
    font-size: 13px;
    color: var(--dro-white);
    text-align: center;
}

.toast {
    position: fixed;
    bottom: 18px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--screen-face);
    color: var(--dro-white);
    border: 1px solid var(--screen-edge);
    border-radius: var(--radius);
    padding: 10px 18px;
    font-family: var(--mono);
    font-size: 13px;
    z-index: 20;
    max-width: 90vw;
}
.toast.warn { color: var(--amber); }

/* ---------- small screens: stack ---------- */
@media (max-width: 720px) {
    main { flex-direction: column; }
    #sidebar {
        width: 100%;
        max-height: 40dvh;
        border-right: none;
        border-bottom: 1px solid var(--panel-edge);
    }
}
