/* Design Hours (Shop › Tools) — js/tools/design-hours.js.
 * Scope colors are a validated categorical set (CVD-safe in this order:
 * walls, ewp, stairs). Stairs is under 3:1 on white, so every
 * chart also carries a legend, total labels, hover tooltips and a table view. */

#designHoursModal {
    /* Three scopes = the first three slots of the validated palette, which
       pass every check all-pairs (orange + yellow did not). */
    --dh-walls: #2a78d6;
    --dh-ewp:   #eb6834;
    --dh-stairs: #1baf7a;
    /* Floor Production pairs Floors with EWP only, so Floors takes the
       walls slot (walls never shares a chart with it). */
    --dh-floors: #2a78d6;
    --dh-track: var(--ras-paper-3);
}

/* Fixed 4-column grid (2 on tablets, 1 on phones) so fields line up row by
 * row instead of auto-fit reflowing them into odd widths. */
.dh-form {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: var(--ras-space-3);
    align-items: end;
}
.dh-form .dh-span-2 { grid-column: span 2; }
.dh-form .dh-span-3 { grid-column: span 3; }
@media (max-width: 760px) {
    .dh-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .dh-form .dh-span-3 { grid-column: span 2; }
}
@media (max-width: 480px) {
    .dh-form { grid-template-columns: minmax(0, 1fr); }
    .dh-form .dh-span-2, .dh-form .dh-span-3 { grid-column: auto; }
}

/* Scope selector: fills its column, four equal buttons — none gets clipped. */
.dh-scope-seg { display: flex; width: 100%; }
.dh-scope-seg > button {
    flex: 1 1 0;
    min-width: 0;
    height: var(--ras-control-h);
    padding: 0 var(--ras-space-2);
    font-size: var(--ras-fs-md);
    white-space: nowrap;
}

/* Project / designer picker — the list is exactly as wide as its input. */
.dh-combo { position: relative; width: 100%; }
.dh-combo > .ras-input { width: 100%; box-sizing: border-box; }
.dh-combo__list {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 2px);
    z-index: 20;
    max-height: 260px;
    overflow-y: auto;
    background: var(--ras-surface);
    border: 1px solid var(--ras-line-strong);
    border-radius: var(--ras-radius);
    box-shadow: 0 8px 24px rgba(0,0,0,0.12);
}
.dh-combo__list.is-open { display: block; }
.dh-combo__opt {
    padding: 8px 12px;
    font-size: var(--ras-fs-base);
    color: var(--ras-text);
    cursor: pointer;
    border-bottom: 1px solid var(--ras-line-soft);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.dh-combo__opt:last-child { border-bottom: none; }
.dh-combo__opt:hover, .dh-combo__opt.is-active { background: var(--ras-green-soft); }
.dh-combo__opt b { color: var(--ras-ink); }
.dh-combo__empty { padding: 10px 12px; font-size: var(--ras-fs-sm); color: var(--ras-muted); }

.dh-filters { display: flex; flex-wrap: wrap; gap: var(--ras-space-2); align-items: center; }
.dh-filters .ras-input, .dh-filters .ras-select { width: auto; min-width: 150px; }

.dh-kpis {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: var(--ras-space-3);
}
.dh-kpi { padding: var(--ras-space-3) var(--ras-space-4); }
.dh-kpi .ras-stat__value { color: var(--ras-ink); }
.dh-kpi--danger .ras-stat__value { color: var(--ras-danger); }

.dh-grid-2 {
    display: grid;
    /* Two columns only when each gets ≥ 460px — below that the bar labels and
       the change tables get cramped, so the cards stack instead. */
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr));
    gap: var(--ras-space-4);
}
@media (max-width: 420px) {
    .dh-grid-2 { grid-template-columns: 1fr; }
}

.dh-legend { display: flex; flex-wrap: wrap; gap: var(--ras-space-3); font-size: var(--ras-fs-sm); color: var(--ras-text); }
.dh-legend span { display: inline-flex; align-items: center; gap: 6px; }
.dh-swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* Horizontal stacked bars: label | bar | total */
.dh-hbars { display: flex; flex-direction: column; gap: 8px; }
.dh-hbar {
    display: grid;
    grid-template-columns: minmax(110px, 38%) 1fr auto;
    gap: var(--ras-space-2);
    align-items: center;
    font-size: var(--ras-fs-sm);
}
.dh-hbar__label { color: var(--ras-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dh-hbar__label b { color: var(--ras-ink); }
.dh-hbar__track { display: flex; gap: 2px; height: 16px; min-width: 0; }
.dh-hbar__seg { height: 100%; min-width: 3px; cursor: default; }
.dh-hbar__seg:last-child { border-radius: 0 4px 4px 0; }
.dh-hbar__total { color: var(--ras-ink); font-weight: 700; font-variant-numeric: tabular-nums; min-width: 48px; text-align: right; }


/* Budget meter */
.dh-meter { position: relative; height: 10px; background: var(--dh-track); border-radius: 4px; min-width: 90px; }
.dh-meter > div { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; background: var(--ras-green); }
.dh-meter--warn > div { background: var(--ras-warn-strong); }
.dh-meter--over > div { background: var(--ras-danger); }

.dh-num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.dh-scope-pill { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }

#dhTip {
    position: fixed;
    z-index: 3000;
    pointer-events: none;
    background: var(--ras-ink);
    color: var(--ras-on-dark);
    font-size: var(--ras-fs-sm);
    padding: 6px 9px;
    border-radius: var(--ras-radius);
    box-shadow: 0 4px 14px rgba(0,0,0,0.2);
    white-space: pre-line;
    display: none;
    max-width: 260px;
}

/* .ras-field sets display:flex, which beats the UA [hidden] rule. */
.dh-form [hidden] { display: none; }

.dh-budget-inputs { display: grid; grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); gap: var(--ras-space-2); }
.dh-budget-inputs .ras-input { width: 100%; box-sizing: border-box; }

/* ---------- sample-data toggle + comparison period ---------- */
#designHoursModal { --dh-cmp: #A8A79F; }   /* comparison period: neutral grey, never a scope color */

.dh-tabbar { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--ras-space-3); flex-wrap: wrap; border-bottom: 1px solid var(--ras-line); }
.dh-tabbar .ras-tabs { border-bottom: none; }
.dh-demo-toggle { display: inline-flex; align-items: center; gap: 6px; font-size: var(--ras-fs-sm); color: var(--ras-muted); cursor: pointer; padding-bottom: 8px; user-select: none; }

.dh-compare-toggle { display: inline-flex; align-items: center; gap: 6px; font-size: var(--ras-fs-sm); font-weight: 600; color: var(--ras-text); cursor: pointer; user-select: none; }

.dh-delta { font-size: var(--ras-fs-sm); font-weight: 600; color: var(--ras-text); white-space: nowrap; font-variant-numeric: tabular-nums; }
.dh-hbar__total .dh-delta { font-size: var(--ras-fs-xs); font-weight: 500; color: var(--ras-muted); }
.dh-hbar__total { min-width: 64px; }

.dh-hbar__bars { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.dh-hbar__cmp { height: 6px; }
.dh-hbar__cmp > div { height: 100%; min-width: 2px; background: var(--dh-cmp); border-radius: 0 3px 3px 0; }
.dh-swatch--cmp { background: var(--dh-cmp); }

/* Grid items default to min-width:auto, so a wide table inside a card would
   push the card past the screen on phones; let it shrink and scroll inside. */
.dh-grid-2 > *, .dh-kpis > * { min-width: 0; }

/* Projects status table: budgeted / executed / completion / status + period hours */
.dh-status td { vertical-align: middle; }
.dh-status tfoot td { border-top: 2px solid var(--ras-line); background: var(--ras-paper-2); }
.dh-status .dh-col-period { background: var(--ras-green-softer); }
.dh-status tfoot .dh-col-period { background: var(--ras-green-soft); }
.dh-expand {
    border: none; background: none; cursor: pointer; padding: 0 4px 0 0;
    color: var(--ras-muted); font-size: 12px; width: 18px; text-align: left;
}
.dh-expand:hover { color: var(--ras-green); }
.dh-prow__sub { margin-left: 18px; }
.dh-prow__sub:empty { display: none; }
.dh-srow td { background: var(--ras-paper-2); font-size: var(--ras-fs-sm); padding-top: 6px; padding-bottom: 6px; }
.dh-srow > td:first-child:not(.dh-pcell) { padding-left: 32px; }
.dh-compl { display: flex; flex-direction: column; gap: 3px; min-width: 80px; }
.dh-compl__pct { font-weight: 700; color: var(--ras-ink); font-variant-numeric: tabular-nums; }
.dh-compl .dh-meter { height: 6px; min-width: 0; }

/* Compare mode: each project = period row on top + compared row under it
   (grey, lighter text), the Change cell spans both. */
.dh-pcell { white-space: nowrap; }
.dh-pcell > span { display: block; }
.dh-ptag { font-size: var(--ras-fs-xs); font-weight: 700; text-transform: uppercase; letter-spacing: 0.3px; color: var(--ras-green-deep); }
.dh-ptag--cmp { color: var(--ras-muted); display: inline-flex !important; align-items: center; gap: 5px; }
.dh-status--cmp tr.dh-pair-top > td { border-bottom: none; }
.dh-status--cmp tr.dh-pair-cmp > td:not([rowspan]) { background: var(--ras-paper-2); color: var(--ras-muted); }
.dh-status--cmp tr.dh-pair-cmp .dh-compl__pct { color: var(--ras-muted); font-weight: 600; }
.dh-status--cmp tr.dh-pair-cmp .ras-badge { opacity: 0.75; }
.dh-status--cmp tr.dh-pair-cmp .dh-col-period { background: var(--ras-paper-3); }
.dh-status--cmp tr.dh-pair-cmp > td { border-bottom: 2px solid var(--ras-line); }
.dh-change-td { vertical-align: middle; border-left: 1px solid var(--ras-line-soft); }
.dh-change { display: flex; flex-direction: column; gap: 3px; min-width: 110px; }
.dh-change__hours { font-weight: 700; color: var(--ras-ink); }
.dh-status--cmp td[rowspan] { vertical-align: middle; background: var(--ras-surface); }
.dh-status tfoot tr.dh-pair-cmp > td:not([rowspan]) { background: var(--ras-paper-3); }

.dh-status thead th:first-child { min-width: 150px; }

/* Projects table: scrolls inside its own box so the header row can stay put.
   (The wrapper already scrolls sideways, which makes it the sticky container —
   sticking to the modal's scroll isn't possible, so it gets its own height.) */
.dh-status-wrap { max-height: 72vh; overflow: auto; }
.dh-status thead th { position: sticky; top: 0; z-index: 3; box-shadow: inset 0 -1px 0 var(--ras-line); }
.dh-status thead th.dh-col-period { background: #E9F3EE; }   /* opaque: rows scroll under it */

/* (i) next to a column name — explanation in the #dhTip tooltip. */
.dh-info {
    display: inline-flex; align-items: center; justify-content: center;
    width: 14px; height: 14px; margin-left: 4px; vertical-align: 1px;
    border-radius: 50%; border: 1px solid var(--ras-muted-soft);
    font: italic 700 9px/1 Georgia, serif; text-transform: none; letter-spacing: 0;
    color: var(--ras-muted); background: var(--ras-surface); cursor: help;
}
.dh-info:hover, .dh-info:focus { border-color: var(--ras-green); color: var(--ras-green); outline: none; }
.dh-status tfoot tr.dh-trow--nobudget > td { background: var(--ras-surface); color: var(--ras-muted); border-top: 1px dashed var(--ras-line-strong); }

/* Wider window than the other tools: the Projects table has up to 8 columns
   and shouldn't need sideways scrolling on a normal monitor. */
#designHoursModal .dh-modal-content { width: 96vw; max-width: 1480px; }
/* Headers wrap onto two lines instead of forcing the table wider. */
.dh-status thead th { white-space: normal; line-height: 1.25; vertical-align: bottom; }

/* ---------- Progress (work done) ---------- */
.dh-meter--progress > div { background: var(--ras-info); }
.dh-forecast { display: flex; flex-direction: column; gap: 2px; white-space: nowrap; }
.dh-forecast__h { font-weight: 700; color: var(--ras-ink); font-variant-numeric: tabular-nums; }
.dh-forecast--over .dh-forecast__h, .dh-forecast--over .ras-meta { color: var(--ras-danger); }

.dh-pg-table td { vertical-align: middle; }
.dh-pg-in { width: 74px; text-align: right; padding-right: 6px; }
.dh-pg-bhead td { background: var(--ras-paper-3); font-weight: 700; color: var(--ras-text); font-size: var(--ras-fs-sm); }
.dh-pg-table tfoot td { border-top: 2px solid var(--ras-line); background: var(--ras-paper-2); vertical-align: top; }
.dh-wsum { font-weight: 700; font-size: var(--ras-fs-md); font-variant-numeric: tabular-nums; }
.dh-wsum--ok { color: var(--ras-green); }
.dh-wsum--bad { color: var(--ras-danger); }
.dh-pg-big { font-size: 20px; color: var(--ras-ink); }
.dh-pg-forecast { margin-top: 12px; padding: 10px 12px; background: var(--ras-paper-2); border-radius: var(--ras-radius); display: flex; flex-direction: column; gap: 4px; }
.dh-pg-verdict { font-size: var(--ras-fs-md); color: var(--ras-text); }
.dh-pg-verdict--over { color: var(--ras-danger); }
.dh-pg-current td { background: var(--ras-green-softer); }

/* ---------- Budget Dashboard: department bar + ADP upload ---------- */
.dh-deptbar { display: flex; flex-wrap: wrap; gap: var(--ras-space-2); }
.dh-dept {
    height: var(--ras-control-h);
    padding: 0 var(--ras-space-4);
    border: 1px solid var(--ras-line-strong);
    border-radius: var(--ras-radius);
    background: var(--ras-surface);
    color: var(--ras-text);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}
.dh-dept:hover { border-color: var(--ras-green); color: var(--ras-green); background: var(--ras-green-softer); }
.dh-dept.is-active { background: var(--ras-green); border-color: var(--ras-green); color: var(--ras-on-dark); }
.dh-dept:focus-visible { outline: 2px solid var(--ras-green); outline-offset: 2px; }

.dh-drop {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--ras-space-2);
    padding: var(--ras-space-5) var(--ras-space-4);
    border: 2px dashed var(--ras-line-strong);
    border-radius: var(--ras-radius);
    background: var(--ras-surface);
    text-align: center;
    cursor: pointer;
}
.dh-drop:hover { border-color: var(--ras-green); background: var(--ras-green-softer); }
.dh-drop input[type="file"] { display: none; }
.dh-warn-list { margin: var(--ras-space-1) 0 0; padding-left: 18px; }
