/* Construction Calculator Layout */
.calc-container {
    display: grid;
    grid-template-columns: 1fr 250px;
    gap: 20px;
    align-items: start;
}

.calc-tools-close {
    position: absolute;
    right: 8px;
    top: 8px;
    width: 36px;
    height: 36px;
    padding: 0;
    font-size: 1.8rem;
    line-height: 1;
    border-radius: 6px;
    background-color: #fff;
    border: 2px solid #dc3545;
    color: #dc3545;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.15);
    transition: all 0.2s ease;
}

.calc-tools-close:hover {
    background-color: #dc3545;
    color: #fff;
    transform: scale(1.05);
    box-shadow: 0 3px 6px rgba(220, 53, 69, 0.3);
}

.calc-tools-close:active {
    transform: scale(0.95);
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

/* Allow scrolling - content shifts instead of overlaying */
/* body.tools-open { overflow: hidden; } */

.calc-main-panel {
    background-color: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.calculator-grid {
    display: grid;
    grid-template-areas:
        "functions functions functions"
        "keypad    spacer    units";
    grid-template-columns: 1fr 24px auto;
    gap: 15px;
}

.calc-function-keys {
    grid-area: functions;
    display: grid;
    grid-template-columns: repeat(5, 1fr); /* AC | DEL | Length | Area | Volume */
    gap: 10px;
}

.calc-function-keys > .btn { width: 100%; }

.calc-keypad {
    grid-area: keypad;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

.calc-unit-keys {
    grid-area: units;
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
}

.calc-keypad .zero-btn {
    grid-column: auto;
}

.calculator-grid .btn {
    height: 50px;
    font-size: 1.2rem;
}

.calc-stack-panel {
    background-color: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    height: 100%;
}

.calculation-history-stack {
    max-height: 400px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.stack-item {
    background-color: #fff;
    padding: 10px;
    border-radius: 5px;
    border: 1px solid #dee2e6;
    cursor: pointer;
    transition: background-color 0.2s;
}

.stack-item:hover {
    background-color: #e9ecef;
    border-left: 3px solid #007bff;
}

.stack-item.active {
    background-color: #d4edda;
    border-left: 3px solid #28a745;
}

.stack-expression {
    font-size: 0.8rem;
    color: #6c757d;
    word-wrap: break-word;
}

.stack-result {
    font-size: 1.1rem;
    font-weight: bold;
    text-align: right;
}
/* NOT SURE WHAT THEE THREE LINES ARE FROM:
background-color: #eee;
padding: 1rem;
border-bottom: 1px solid #ccc;
}
*/
.calculator-grid {
    max-width: 450px;
    margin: 0 auto;
}

.calc-main-panel {
    max-width: 500px;
    margin: 0 auto;
    order: 1;
}

/* Header tools toggle sizing (no absolute positioning here) */
.calc-menu-header {
    position: relative;
    padding-right: 52px;
}
.calc-menu-header-title {
    padding-top: 8px;
}
.calc-tools-toggle { height: 44px; font-size: 2rem; padding: 0.375rem 0.75rem; }
.calc-tools-toggle.btn { font-size: 2rem; padding: 0.375rem 0.75rem; }

/* Stack row: Tools button left of a centered Stack (desktop), above it (mobile) */
.stack-row {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    width: 100%;
    order: -2;
}
.stack-row .calc-stack-panel { width: 100%; max-width: 390px; margin: 0 auto; }

.btn {
    height: 50px;
    font-size: 1.2rem;
    border-radius: 0.5rem;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.btn:active {
    box-shadow: none;
    transform: translateY(1px);
}

/* Main layout container for sidebar, calculator, and stack */
.main-layout-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
}

/* Ensure correct order on narrow screens: sidebar → stack → calculator */
.calc-sidebar {
    order: 1;
}

.stack-row {
    order: 2;
}

.calculator-container {
    order: 3;
}

/* Container for calculator and stack */
.calculator-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
}

/* Tools button group - order changes based on screen size */
.tools-button-group {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    order: 0;
    width: 100%;
    max-width: 500px;
    margin-left: auto;
    margin-right: auto;
    padding: 0 20px;
    justify-content: center;
}

.tools-toggle-btn {
    height: 36px !important;
    font-size: 0.9rem !important;
    padding: 0.25rem 0.75rem !important;
    white-space: nowrap;
}

/* Stack styling */
.calc-stack-panel {
    border-top: none;
    margin-top: 0;
    margin-bottom: 10px;
    padding-top: 0;
    width: 100%;
    max-width: 390px;
    margin-left: auto;
    margin-right: auto;
    order: -2;
    display: flex;
    flex-direction: column;
}

.stack-row {
    min-height: 0;
}

.stack-header-bar {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-bottom: 12px;
}

.stack-title {
    margin: 4px; /* replace mb-0 */
}

.stack-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.stack-actions .output-unit-label {
    font-size: 1rem;
    margin-right: 8px; /* replace mr-2 */
}

.stack-actions .output-unit-select {
    width: auto;
    font-size: 1rem;
    padding: 0.25rem 0.5rem;
    height: calc(1.5em + 0.5rem + 2px);
    margin-right: 8px; /* space before Clear button */
}

.calculation-stack {
    flex: 1;
    min-height: 120px;
    overflow: auto;
    resize: vertical;
    padding: 10px;
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    background-color: #f9f9f9;
    scroll-behavior: smooth;
}

.stack-header {
    display: grid;
    grid-template-columns: 1fr 160px 36px 36px; /* Input | Result | Assign | Delete */
    column-gap: 8px;
    padding: 10px;
    position: sticky;
    top: -10px;
    z-index: 1;
    background-color: #f9f9f9; 
}

.stack-entry {
    padding: 10px;
    border-radius: 5px;
    background-color: white;
    border: 1px solid #e0e0e0;
    transition: all 0.2s;
    display: grid;
    grid-template-columns: 1fr 160px 36px 36px; /* Input | Result | Assign | Delete */
    column-gap: 8px;
    align-items: center;
}

.stack-entry:not(.stack-break) {
    cursor: pointer;
}

.stack-input-original {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stack-converted {
    flex: 1;
    min-width: 0;
    text-align: right;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stack-entry:hover {
    background-color: #f0f0f0;
}

.stack-break {
    background-color: #f8f9fa;
    border-style: dashed;
    position: relative;
}

.stack-break span {
    margin-left: 0; /* align under Result column */
    overflow: visible;
    text-overflow: clip;
    white-space: normal;
}

.stack-break::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 10px;
    height: 2px;
    background-color: #007bff;
    border-radius: 2px 2px 0 0;
}

.selected-entry {
    border-color: #007bff;
    background-color: #e6f2ff;
    box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

.stack-value-container {
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stack-col {
    min-width: 0;
}

.col-label {
    font-size: 0.7rem;
    color: #6c757d;
    text-transform: uppercase;
    line-height: 1;
    margin-bottom: 2px;
}

.stack-col + .stack-col {
    border-left: 1px dashed #e0e0e0;
    padding-left: 8px;
}

/* Delete column should not have a left divider/padding */
.assign-col,
.delete-col,
.delete-col.stack-col,
.stack-header .delete-col,
.stack-entry .delete-col,
.stack-header .assign-col,
.stack-entry .assign-col {
    border-left: none !important;
    padding-left: 0 !important;
}

.stack-value {
    font-weight: 500;
}

.stack-operator {
    font-weight: 700;
    color: #007bff;
    margin-left: 10px;
    display: inline-block;
    min-width: 1.25em; /* keeps a gap even when operator is hidden */
}

.stack-subtotal {
    flex: 1;
    text-align: right;
    padding-right: 10px;
    font-weight: 700;
    color: #28a745;
}

.subtotal-value {
    font-weight: 600;
    color: #000000;
    cursor: default;
}

.stack-entry.has-subtotal {
    background-color: #f8f9fa;
    border-style: dashed;
    position: relative;
}

.stack-entry.has-subtotal .stack-value-container {
    margin-left: 0;
}

.stack-entry.has-subtotal::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 10px;
    height: 1px;
    background-color: #6a6a6c;
    border-radius: 2px 2px 0 0;
}

.final-total-value {
    font-weight: 700;
    color: #28a745;
    cursor: default;
}

.stack-delete-btn {
    height: 30px;
    width: 30px;
    padding: 0;
    line-height: 1;
    font-size: 0.8rem;
    justify-self: end;
    align-self: center;
}

.stack-send-btn {
    height: 30px;
    width: 30px;
    padding: 0;
    font-size: 0.8rem;
    border-radius: 4px;
    position: relative;
}

/* tool selection dropdown */
.tool-selection-dropdown {
    position: absolute;
    right: 0;
    top: 100%;
    margin-top: 4px;
    background: white;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    z-index: 1000;
    min-width: 150px;
    display: none;
    overflow: visible;
    color: #000;
}

.tool-selection-dropdown.show {
    display: block;
}

.tool-selection-dropdown button {
    display: block;
    width: 100%;
    padding: 8px 12px;
    border: none;
    background: none;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s;
}

.tool-selection-dropdown button:hover {
    background-color: #f0f0f0;
}

.tool-selection-dropdown button:first-child {
    border-radius: 4px 4px 0 0;
}

.tool-selection-dropdown button:last-child {
    border-radius: 0 0 4px 4px;
}

.stack-header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.stack-title {
    margin: 0;
    flex: 1;
}

.stack-assign-btn {
    height: 30px;
    width: 30px;
    padding: 0;
    line-height: 1;
    font-size: 0.8rem;
    justify-self: end;
    align-self: center;
}

/* Reusable Output Unit styles and display actions container */
.calc-display-actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
    margin-top: 6px;
}
.output-unit-label {
    font-size: 1.2rem;
    margin-right: 8px;
}
.output-unit-select {
    width: auto;
    font-size: 1.2rem;
    padding: 0.25rem 0.5rem;
    height: calc(1.5em + 0.5rem + 2px);
}

/* Geometry tool: keep unit select and send button inline with input */
.calc-sidebar .geometry-tab {
    --geom-send-col: 40px;
}
.calc-sidebar .geometry-tab .form-group {
    display: grid;
    grid-template-columns: auto 1fr auto var(--geom-send-col);
    column-gap: 8px;
    align-items: center;
}
.calc-sidebar .geometry-tab .form-group > label {
    margin-bottom: 0; /* inline label */
    white-space: nowrap;
    width: 60px; /* Fixed width for consistent label alignment */
    text-align: left;
}

.calc-sidebar .geometry-tab .form-group .input-group,
.calc-sidebar .geometry-tab .form-group .input-group-append {
    display: contents;
}
.calc-sidebar .geometry-tab .form-group .input-group {
    flex-wrap: nowrap;
}
.calc-sidebar .geometry-tab .form-group input.form-control {
    min-width: 0; /* allow shrink within column */
    width: 100%;
}
.calc-sidebar .geometry-tab .form-group select.form-control {
    width: auto; /* don't stretch unit selects */
}
/* Send-to-calc button column sizing */
.calc-sidebar .geometry-tab .form-group .btn {
    white-space: nowrap;
    height: 36px;
    padding: 0.25rem 0.5rem;
    font-size: 0.9rem;
    justify-self: end; /* align to the right of its column */
    width: var(--geom-send-col);
}
/* Optional: slightly tighten the sidebar controls */
/*
.calc-sidebar .geometry-tab .input-group .form-control,
.calc-sidebar .geometry-tab .input-group .btn {
    height: 36px;
    padding: 0.25rem 0.5rem;
    font-size: 0.9rem;
}
*/
/* Conversion tab: inline Output row (Label | Input | Unit | Send) */
.calc-sidebar .conversion-tab { --conv-send-col: 40px; }
.calc-sidebar .conversion-tab .inline-row {
    display: grid;
    grid-template-columns: auto 1fr auto var(--conv-send-col);
    column-gap: 8px;
    align-items: center;
}
.calc-sidebar .conversion-tab .inline-row > label {
    margin-bottom: 0;
    white-space: nowrap;
    width: 100px; /* Fixed width for consistent label alignment */
    text-align: left;
}
.calc-sidebar .conversion-tab .inline-row .input-group,
.calc-sidebar .conversion-tab .inline-row .input-group-append {
    display: contents;
}
/* Fallback: prevent wrapping if contents isn't supported */
.calc-sidebar .conversion-tab .inline-row .input-group { flex-wrap: nowrap; }
.calc-sidebar .conversion-tab .inline-row input.form-control { min-width: 44px; width: 100%; }
.calc-sidebar .conversion-tab .inline-row select.form-control { width: auto; }
.calc-sidebar .conversion-tab .inline-row .btn {
    white-space: nowrap;
    height: 36px;
    padding: 0.25rem 0.5rem;
    font-size: 0.9rem;
    justify-self: end;
    width: var(--conv-send-col);
}

/* Board Feet tab: inline Output rows (Label | Input | Unit | Send) */
.calc-sidebar .boardfeet-tab { --bdft-send-col: 40px; }
.calc-sidebar .boardfeet-tab .inline-row {
    display: grid;
    grid-template-columns: auto 1fr auto var(--bdft-send-col);
    column-gap: 8px;
    align-items: center;
}
.calc-sidebar .boardfeet-tab .inline-row > label {
    margin-bottom: 0;
    white-space: nowrap;
    width: 140px; /* Fixed width for consistent label alignment - wider for output labels */
    text-align: left;
}
.calc-sidebar .boardfeet-tab .inline-row .input-group,
.calc-sidebar .boardfeet-tab .inline-row .input-group-append {
    display: contents;
}
/* Fallback: prevent wrapping if contents isn't supported */
.calc-sidebar .boardfeet-tab .inline-row .input-group { flex-wrap: nowrap; }
.calc-sidebar .boardfeet-tab .inline-row input.form-control { min-width: 0; width: 100%; }
.calc-sidebar .boardfeet-tab .inline-row select.form-control { width: auto; }
.calc-sidebar .boardfeet-tab .inline-row .btn {
    white-space: nowrap;
    height: 36px;
    padding: 0.25rem 0.5rem;
    justify-self: end;
    width: var(--bdft-send-col);
}

/* Board Feet tab: ensure input unit selects can grow (inputs: Thickness/Width/Length) */
.calc-sidebar .boardfeet-tab .form-group .input-group {
    flex-wrap: nowrap; /* keep input + unit select on one line */
}
.calc-sidebar .boardfeet-tab .form-group .input-group > .form-control {
    min-width: 44px; /* allow shrink but not too small */
    flex: 1 1 auto;  /* text input takes remaining space */
}
.calc-sidebar .boardfeet-tab .form-group .input-group-append .form-control {
    width: auto;     /* size select by its content */
    flex: 0 0 auto;  /* prevent select from stretching or shrinking */
}

@media (min-width: 850px) {
    /* Wider columns on medium+ screens (stack still above keypad at this breakpoint) */
    .stack-header {
        grid-template-columns: 1fr 160px 40px 26px; /* Input | Result | Assign | Delete */
    }
    .stack-entry {
        grid-template-columns: 1fr 160px 40px 26px
    }
}

/* On medium screens, 2-column layout: sidebar | stack over calculator */
@media (min-width: 850px) and (max-width: 1299px) {
    .main-layout-container {
        display: grid;
        grid-template-areas:
            "sidebar stack"
            "sidebar calc";
        grid-template-columns: auto 1fr;
        gap: 20px;
    }
    
    .calc-sidebar {
        grid-area: sidebar;
        width: 440px;
        order: initial; /* Reset flex order for grid layout */
    }
    
    .stack-row {
        grid-area: stack;
        order: initial; /* Reset flex order for grid layout */
    }
    
    .calculator-container {
        grid-area: calc;
        order: initial; /* Reset flex order for grid layout */
    }
}

/* On large screens, create 3-column layout: sidebar | calculator | stack */
@media (min-width: 1300px) {
    .main-layout-container {
        display: grid;
        grid-template-areas:
            "sidebar calc stack";
        grid-template-columns: auto 1fr 430px;
        gap: 20px;
        align-items: start;
    }
    
    .calc-sidebar {
        grid-area: sidebar;
        width: 440px;
        order: initial; /* Reset flex order for grid layout */
    }
    
    .calculator-container {
        grid-area: calc;
        display: flex;
        flex-direction: column;
        gap: 20px;
        order: initial; /* Reset flex order for grid layout */
    }
    
    .stack-row {
        grid-area: stack;
        order: initial; /* Reset flex order for grid layout */
    }
    body.tools-open .calc-sidebar {
        margin-bottom: 20px;
        box-shadow: 0 2px 12px rgba(0,0,0,0.2);
    }
    
    body.tools-open .calc-sidebar .tools-content {
        max-height: 60vh;
        overflow-y: auto;
    }
    .calc-drawer-backdrop {
        display: none; /* No longer needed since sidebar opens below header */
    }
    .calc-main-panel {
        grid-area: calc;
        order: 0;
    }
    .stack-row {
        grid-area: stack;
        order: 0;
        display: flex;
        width: auto;
        align-items: stretch;
        height: 100%;
    }
    .stack-row .calc-stack-panel {
        order: 0;
        width: 100%;
        max-width: none;
        margin: 0;
        height: 100%;
    }
}

/* Hide per-entry delete buttons in narrow single-column layout and collapse the column */
@media (max-width: 767.98px) {
    .stack-header { grid-template-columns: 1fr 160px 36px; } /* drop delete col */
    .stack-entry { grid-template-columns: 1fr 160px 36px; } /* drop delete col */
    .stack-col.delete-col, .stack-header .delete-col { display: none !important; }
}

/* Sidebar for tools - pushes content down on mobile */
.calc-sidebar {
    background-color: #f8f9fa;
    padding: 16px;
    border-radius: 8px;
    box-shadow: none;
    max-height: none;
    overflow: visible;
    transition: box-shadow 0.3s ease;
    margin-bottom: 0;
    border-radius: 8px;
}

.calc-sidebar .tools-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease, margin 0.3s ease;
}

body.tools-open .calc-sidebar {
    margin-bottom: 20px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.2);
}

body.tools-open .calc-sidebar .tools-content {
    max-height: 60vh;
    overflow-y: auto;
}

/* Drawer backdrop - only on large screens where drawer overlays */
.calc-drawer-backdrop {
    display: none;
    order: -4;
}

@media (min-width: 850px) {
    .calc-drawer-backdrop {
        display: none; /* No longer needed since sidebar opens below header */
    }
}

.calc-sidebar .btn.active {
    background-color: #0a94d6;
    color: #fff;
}

/* Tools buttons in sidebar */
.calc-sidebar .tools-button-group {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.calc-sidebar .tools-button-group .btn {
    flex: 1;
    min-width: 0;
    padding: 0.5rem 0.75rem;
}

.calc-sidebar .tools-button-group .btn.active {
    background-color: #0a94d6;
    color: #fff;
    border-color: #0a94d6;
}

.calc-tools-close {
    width: 32px !important;
    height: 32px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 4px !important;
}

.btn[title*="Send"],
.btn[title*="Send output to calculator"],
.btn[title*="Send to calculator"],
.btn[title*="Send to sidebar"],
.stack-send-btn {
    font-weight: bold !important;
    font-size: 1.2em !important;
}

.btn[title*="Send output to calculator"],
.btn[title*="Send to calculator"] {
    height: 30px !important;
    width: 30px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

/* Make Conversion and Board Feet behave like Geometry for rows with input-group */
.calc-sidebar .conversion-tab .form-group:has(.input-group),
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) {
    display: grid;
    grid-template-columns: auto 1fr auto 40px; /* Label | Input | Unit | Send */
    column-gap: 8px;
    align-items: center;
}
.calc-sidebar .conversion-tab .form-group:has(.input-group) > label {
    margin-bottom: 0;
    white-space: nowrap;
    width: 100px; /* Fixed width for consistent label alignment */
    text-align: left;
}
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) > label {
    margin-bottom: 0;
    white-space: nowrap;
    width: 140px; /* Fixed width for consistent label alignment - wider for output labels */
    text-align: left;
}
/* Flatten wrappers so children become grid items */
.calc-sidebar .conversion-tab .form-group:has(.input-group) .input-group,
.calc-sidebar .conversion-tab .form-group:has(.input-group) .input-group-append,
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) .input-group,
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) .input-group-append {
    display: contents;
}
/* Inputs/selects sizing inside grid */
.calc-sidebar .conversion-tab .form-group:has(.input-group) input.form-control,
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) input.form-control {
    min-width: 44px;
    width: 100%;
}
.calc-sidebar .conversion-tab .form-group:has(.input-group) select.form-control,
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) select.form-control {
    width: auto !important;
    min-width: max-content;
    flex: 0 0 auto;
}
.calc-sidebar .conversion-tab .form-group:has(.input-group) .btn,
.calc-sidebar .boardfeet-tab .form-group:has(.input-group) .btn {
    white-space: nowrap;
    height: 36px;
    padding: 0.25rem 0.5rem;
    justify-self: end;
    width: 40px;
}

.calc-spacer { grid-area: spacer; }
.calc-keypad .key-spacer { visibility: hidden; }
