/* Normalize CSS */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html {
    font-size: 100%;
    line-height: 1.15;
}
body {
    font-family: Arial, sans-serif;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea {
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
}
button, [role="button"] {
    cursor: pointer;
}
a {
    color: inherit;
    text-decoration: none;
}
/* Base Styles */
body {
    overflow: hidden;
    background-color: #1E3A8A;
    touch-action: manipulation;
}
.three-canvas {
    display: block;
    width: 100%;
    height: 100vh;
    touch-action: none; /* Prevent default browser touch behaviors */
}
#controls {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    max-width: 100%;
    max-height: 60vh;
    padding: 6px;
    background: transparent;
    border-radius: 0 0 8px 8px;
    transition: transform 0.3s ease-in-out;
    z-index: 10;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
#controls.collapsed {
    transform: translateY(-100%);
}
.control-group {
    padding: 4px 8px;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
#box-group {
    background: rgba(248, 248, 248, 0.8);
}
#members-group {
    background: rgba(248, 248, 248, 0.6);
}
#downloads-group {
    background: rgba(248, 248, 248, 0.4);
    align-items: flex-start;
    border-bottom: none; /* Added to remove the thin grey line */
}
.control-group:not(:last-child) {
    border-bottom: 1px solid #ccc;
}
#settings-group {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: rgba(255, 255, 255, 0.95);
    padding: 6px;
    z-index: 10;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 6px;
    box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.2);
}
#box-type-toggle {
    position: fixed;
    bottom: 160px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 11;
    width: 48px;
    height: 48px;
    touch-action: none;
}
#box-type-toggle[data-mode="outside"] rect {
    fill: #00FF00;
    stroke: none;
}
#box-type-toggle[data-mode="inside"] rect {
    fill: #00FF00;
    stroke: #FFFFFF;
    stroke-width: 33.33;
}
#box-type-toggle:focus {
    outline: none;
}
#controls label {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin: 4px 0;
    padding-left: 8px;
    font-size: 16px;
    flex-wrap: nowrap;
    gap: 8px;
}
#controls input[type="number"],
#controls select {
    width: 100px;
    padding: 6px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
    touch-action: auto;
}
#rail-inputs {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: nowrap;
    padding-left: 8px;
}
#rail-inputs input[type="number"] {
    width: 80px;
    padding: 6px;
}
button,
#bom-modal-buttons button {
    margin: 4px 2px;
    padding: 8px 12px;
    font-size: 16px;
    border: none;
    background: #007bff;
    color: white;
    border-radius: 4px;
    min-width: 90px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}
button:hover,
button:focus,
#bom-modal-buttons button:hover,
#bom-modal-buttons button:focus {
    background: #0056b3;
    outline: 2px solid #00FF00;
}
#settings-group button.highlighted {
    background: #0056b3;
    outline: 2px solid #00FF00;
}
#downloads-group .button-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 4px;
    width: 100%;
    max-width: 450px;
    margin: 0 auto;
    padding-left: 0;
}
.error {
    color: red;
    font-size: 14px;
    margin: 4px 0;
    padding-left: 8px;
    flex: 1 1 100%;
    text-align: left;
}
.invalid-input {
    border: 2px solid red;
}
.unit {
    font-size: 16px;
    min-width: 24px;
    width: auto;
}
.tooltip {
    position: relative;
    display: inline-flex;
    align-items: center;
}
.tooltip .tooltiptext {
    visibility: hidden;
    width: 140px;
    background-color: black;
    color: white;
    text-align: center;
    border-radius: 6px;
    padding: 6px;
    position: absolute;
    z-index: 13;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0;
    transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext,
.tooltip:focus-within .tooltiptext {
    visibility: visible;
    opacity: 1;
}
#members-group label[for="railThickness"] {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: 8px;
}
#members-group label[for="railThickness"] .unit {
    grid-column: 2;
}
#downloads-group label[for="exportFormat"] {
    display: flex;
    align-items: center;
    gap: 8px;
}
/* Modal Styles */
#bomModal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    max-height: 100vh;
    padding: 8px;
    padding-bottom: calc(2 * 8px);
    background: rgba(255, 255, 255, 0.4);
    border-radius: 8px;
    z-index: 15;
    display: none;
    overflow-y: auto;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    flex-direction: column;
    gap: 8px;
}
#bomModal.show {
    display: flex;
}
#bomModal::-webkit-scrollbar {
    width: 10px;
}
#bomModal::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 5px;
}
#bomModal::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 5px;
}
#bomModal::-webkit-scrollbar-thumb:hover {
    background: #555;
}
#bomModal {
    scrollbar-width: thin;
    scrollbar-color: #888 #f1f1f1;
}
.bom-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    background: rgba(255, 255, 255, 0.6);
    padding: 12px;
    border-radius: 4px;
}
.bom-title {
    font-size: 18px;
    text-align: center;
    margin-bottom: 8px;
}
.bom-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.bom-form label {
    display: flex;
    flex-direction: column;
    font-size: 14px;
}
.bom-form input[type="text"],
.bom-form input[type="tel"] {
    padding: 6px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 4px;
    width: 100%;
}
.bom-form .name-group {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.bom-form .name-group label {
    flex: 1;
    min-width: 120px;
}
.bom-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 8px;
    margin-bottom: 16px;
}
.bom-table th,
.bom-table td {
    border: 1px solid #ccc;
    padding: 8px;
    text-align: center;
    font-size: 14px;
}
.bom-table th {
    background: rgba(0, 0, 0, 0.1);
}
.bom-table-title {
    font-size: 16px;
    font-weight: bold;
    margin-top: 12px;
    text-align: center;
}
.bom-total {
    font-size: 14px;
    font-weight: bold;
    text-align: center;
    margin: 8px 0;
    padding: 4px;
    background: rgba(0, 0, 0, 0.05);
    border-radius: 4px;
}
#bom-modal-buttons {
    display: none;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 8px;
    position: sticky;
    bottom: 0;
    background: inherit;
}
#bomModal.show #bom-modal-buttons {
    display: flex;
}
/* Side Toggle Styles */
.side-toggles {
    display: flex;
    gap: 2px;
    flex-wrap: wrap;
    justify-content: flex-start; /* Changed to left justify */
    padding: 2px;
}
.side-toggle {
    width: 32px;
    height: 32px;
    border: 2px solid #808080;
    border-radius: 4px;
    background: #FFFFFF;
    color: #000000;
    font-weight: bold;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    min-width: auto;
}
.side-toggle.active {
    background: #808080;
    color: #FFFFFF;
    border-color: #000000;
}
.side-toggle:hover:not(:disabled),
.side-toggle:focus {
    outline: none;
    border-color: #000000;
    box-shadow: 0 0 0 2px rgba(128, 128, 128, 0.25);
}
/* Dimension Label Alignment */
.dim-label {
    display: inline-block;
    width: 70px;
    text-align: right;
}
/* Dark Mode */
@media (prefers-color-scheme: dark) {
    body {
        background-color: #121212;
    }
    #controls {
        color: white;
    }
    #box-group {
        background: rgba(40, 40, 40, 0.8);
    }
    #members-group {
        background: rgba(40, 40, 40, 0.6);
    }
    #downloads-group {
        background: rgba(40, 40, 40, 0.4);
    }
    #settings-group {
        background: rgba(30, 30, 30, 0.95);
        color: white;
    }
    #bomModal {
        background: rgba(30, 30, 30, 0.4);
        color: white;
    }
    #controls label,
    .unit,
    .error,
    .bom-title,
    .bom-table th,
    .bom-table td,
    .bom-table-title,
    .bom-total {
        color: white;
    }
    #controls input[type="number"],
    #controls select,
    .bom-form input[type="text"],
    .bom-form input[type="tel"] {
        background: #333;
        color: white;
        border-color: #555;
    }
    button,
    #bom-modal-buttons button {
        background: #005f73;
    }
    button:hover,
    button:focus,
    #bom-modal-buttons button:hover,
    #bom-modal-buttons button:focus {
        background: #007bff;
    }
    #settings-group button.highlighted {
        background: #007bff;
    }
    .control-group:not(:last-child) {
        border-bottom: 1px solid #555;
    }
    .tooltip .tooltiptext {
        background-color: #333;
        color: white;
    }
    .bom-container {
        background: rgba(40, 40, 40, 0.6);
    }
    .bom-table {
        border-color: #555;
    }
    .bom-table th {
        background: rgba(255, 255, 255, 0.1);
    }
    #bom-modal-buttons {
        background: rgba(30, 30, 30, 0.95);
    }
    #bomModal::-webkit-scrollbar-track {
        background: #f1f1f1;
    }
    #bomModal::-webkit-scrollbar-thumb {
        background: #666;
    }
    #bomModal::-webkit-scrollbar-thumb:hover {
        background: #888;
    }
    #bomModal {
        scrollbar-color: #666 #222;
    }
    /* Dark mode adjustments for side toggles */
    .side-toggle {
        background: #FFFFFF;
        color: #000000;
        border-color: #808080;
    }
    .side-toggle.active {
        background: #808080;
        color: #FFFFFF;
        border-color: #000000;
    }
    .side-toggle:hover:not(:disabled),
    .side-toggle:focus {
        border-color: #000000;
        box-shadow: 0 0 0 2px rgba(128, 128, 128, 0.3);
    }
    .bom-total {
        background: rgba(255, 255, 255, 0.1);
    }
}
/* Media Queries */
@media (max-width: 768px) {
    #controls {
        padding: 4px;
        max-height: 50vh;
        gap: 3px;
    }
    .control-group {
        padding: 4px 6px;
    }
    #downloads-group {
        gap: 3px;
    }
    #downloads-group .button-container {
        gap: 3px;
        max-width: 100%;
    }
    #controls label,
    #rail-inputs,
    #downloads-group .button-container,
    .error {
        padding-left: 6px;
    }
    #box-type-toggle {
        bottom: 162px;
        width: 40px;
        height: 40px;
    }
    #settings-group {
        padding: 4px;
        gap: 4px;
    }
    #controls label {
        font-size: 14px;
        margin: 3px 0;
        gap: 6px;
    }
    #controls input[type="number"],
    #controls select {
        width: 90px;
        padding: 5px;
        font-size: 14px;
    }
    #rail-inputs input[type="number"] {
        width: 70px;
        padding: 5px;
    }
    button,
    #bom-modal-buttons button {
        font-size: 14px;
        padding: 6px 10px;
        min-width: 80px;
    }
    .error {
        font-size: 12px;
        margin: 3px 0;
    }
    .unit {
        font-size: 14px;
        min-width: 20px;
    }
    .tooltip .tooltiptext {
        width: 120px;
        top: -36px;
        font-size: 12px;
        padding: 5px;
    }
    .bom-title,
    .bom-form label,
    .bom-table th,
    .bom-table td,
    .bom-table-title,
    .bom-total {
        font-size: 12px;
    }
    .bom-table {
        margin-bottom: 12px;
    }
    .bom-form .name-group label {
        min-width: 100px;
    }
    .side-toggles {
        gap: 1px;
        padding: 1px;
    }
    .side-toggle {
        width: 28px;
        height: 28px;
        font-size: 12px;
    }
    /* Tighten BOM vertical padding for tablets */
    .bom-container {
        padding: 8px;
        gap: 6px;
    }
    .bom-title {
        margin-bottom: 6px;
    }
    .bom-form {
        gap: 6px;
    }
    .bom-table {
        margin-top: 6px;
        margin-bottom: 12px;
    }
    .bom-table th,
    .bom-table td {
        padding: 6px;
    }
    .bom-table-title {
        margin-top: 8px;
    }
    .bom-total {
        margin: 6px 0;
        padding: 3px;
    }
    #bom-modal-buttons {
        padding: 6px;
    }
    .dim-label {
        width: 110px;
    }
}
@media (max-width: 480px) {
    #controls {
        padding: 2px;
        max-height: 75vh; /* Increased for iPhone 13 mini */
        gap: 2px;
    }
    .control-group {
        padding: 2px 4px;
    }
    #downloads-group {
        gap: 2px;
    }
    #downloads-group .button-container {
        gap: 2px;
        max-width: 100%;
    }
    #controls label,
    #rail-inputs,
    #downloads-group .button-container,
    .error {
        padding-left: 4px;
    }
    #box-type-toggle {
        bottom: 154px;
        width: 36px;
        height: 36px;
    }
    #settings-group {
        padding: 3px;
        gap: 3px;
    }
    #controls label {
        font-size: 12px;
        margin: 2px 0;
        gap: 4px;
    }
    #controls input[type="number"],
    #controls select {
        width: 80px;
        padding: 4px;
        font-size: 12px;
    }
    #rail-inputs input[type="number"] {
        width: 60px;
        padding: 4px;
    }
    button,
    #bom-modal-buttons button {
        font-size: 12px;
        padding: 5px 8px;
        min-width: 70px;
    }
    .error {
        font-size: 10px;
        margin: 2px 0;
    }
    .unit {
        font-size: 12px;
        min-width: 18px;
    }
    .tooltip .tooltiptext {
        width: 100px;
        top: -32px;
        font-size: 10px;
        padding: 4px;
    }
    .bom-title,
    .bom-form label,
    .bom-table th,
    .bom-table td,
    .bom-table-title,
    .bom-total {
        font-size: 9px; /* Reduced for iPhone 13 mini */
        padding: 6px;
    }
    .bom-container {
        padding: 8px; /* Reduced for iPhone 13 mini */
    }
    .bom-table {
        margin-bottom: 10px; /* Reduced for iPhone 13 mini */
    }
    .bom-table th,
    .bom-table td {
        padding: 6px; /* Reduced for iPhone 13 mini */
    }
    .bom-form .name-group label {
        min-width: 70px; /* Reduced for iPhone 13 mini */
    }
    .side-toggles {
        gap: 1px;
        padding: 0;
    }
    .side-toggle {
        width: 24px;
        height: 24px;
        font-size: 11px;
    }
    /* Further tighten BOM vertical padding for smartphones */
    .bom-container {
        padding: 6px;
        gap: 4px;
    }
    .bom-title {
        margin-bottom: 4px;
    }
    .bom-form {
        gap: 4px;
    }
    .bom-table {
        margin-top: 4px;
        margin-bottom: 8px;
    }
    .bom-table th,
    .bom-table td {
        padding: 4px;
    }
    .bom-table-title {
        margin-top: 6px;
    }
    .bom-total {
        margin: 4px 0;
        padding: 2px;
    }
    #bom-modal-buttons {
        padding: 4px;
    }
    .dim-label {
        width: 95px;
    }
}
#downloads-group {
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}
#downloads-group label {
    margin: 0;
    padding-left: 0;
}
#downloads-group .button-container {
    padding-left: 0;
}
@media print {
    body * {
        visibility: hidden; /* Hide everything by default */
    }
    #bomModal, #bomModal * {
        visibility: visible; /* Show only the BOM modal and its children */
    }
    #bomModal {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: auto; /* Let it flow naturally for print */
        overflow: visible; /* Avoid clipping */
    }
    #bom-modal-buttons {
        display: none; /* Hide close/print buttons if not needed on paper */
    }
    /* Optional: Add page breaks or margins */
    .bom-table {
        page-break-inside: avoid; /* Keep tables on one page if possible */
    }
    @page {
        margin: 1cm; /* Set page margins */
    }
}