:root {
    --bg-primary: #f0f2f5;
    --bg-secondary: #ffffff;
    --bg-sidebar: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border: #e2e8f0;
    --accent: #3b82f6;
    --accent-light: #eff6ff;
    --danger: #dc2626;
    --success: #16a34a;
    --warning: #d97706;
    --radius: 4px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: #f0f2f5;
    color: #333;
    overflow: hidden;
    height: 100vh;
    display: flex;
    flex-direction: row;
}

/* === LAYOUT === */
#sidebar {
    width: 380px;
    height: 100%;
    background: #fff;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    box-shadow: 4px 0 20px rgba(0,0,0,0.03);
    z-index: 20;
    flex-shrink: 0;
}

#render-area {
    flex-grow: 1;
    height: 100%;
    position: relative;
    overflow: hidden;
    background: #f4f6f9;
}

#render-area canvas { display: block; width: 100% !important; height: 100% !important; }

/* === HEADER === */
.header {
    padding: 10px 12px;
    border-bottom: 1px solid #f0f0f0;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 50px;
    box-sizing: border-box;
    flex-shrink: 0;
}

h1 {
    margin: 0;
    font-size: 1.0rem;
    color: #1e293b;
    display: flex;
    align-items: center;
    gap: 4px;
    font-weight: 700;
    white-space: nowrap;
}

.title-text { color: #1e293b; letter-spacing: -0.3px; }

.version-span {
    font-size: 0.75em;
    color: #94a3b8;
    font-weight: 400;
    margin-left: 2px;
    background: #f1f5f9;
    padding: 1px 5px;
    border-radius: 4px;
}

.header-actions { display: flex; gap: 5px; align-items: center; flex-shrink: 0; margin-left: auto; }

.btn-header {
    background: transparent;
    border: 1px solid #cbd5e1;
    color: #475569;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
}

.btn-header:hover { border-color: #3b82f6; color: #3b82f6; }
#btn-reset:hover { border-color: #d32f2f; color: #d32f2f; }

.btn-hamburger {
    background: transparent;
    border: 1px solid #cbd5e1;
    color: #475569;
    font-size: 1rem;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
}

.btn-hamburger:hover { border-color: #3b82f6; color: #3b82f6; }

/* === DROPDOWN === */
.dropdown { position: relative; }
.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    top: 100%;
    margin-top: 4px;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.12);
    min-width: 220px;
    z-index: 100;
}
.dropdown-content.show { display: block; }
.dropdown-content a {
    display: block;
    padding: 8px 12px;
    font-size: 0.78rem;
    color: #334155;
    text-decoration: none;
    border-bottom: 1px solid #f1f5f9;
}
.dropdown-content a:last-child { border-bottom: none; }
.dropdown-content a:hover { background: #f8fafc; color: #3b82f6; }

/* === CONTROLS === */
#controls-container {
    flex: 1;
    overflow-y: auto;
    scroll-behavior: smooth;
}

#controls-container::-webkit-scrollbar { width: 6px; }
#controls-container::-webkit-scrollbar-track { background: transparent; }
#controls-container::-webkit-scrollbar-thumb { background: #cbd5e0; border-radius: 3px; }

.group-box {
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    margin: 10px 12px;
    padding: 10px;
}

.section-title {
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #64748b;
    margin-bottom: 8px;
    padding-bottom: 4px;
    border-bottom: 1px solid #f1f5f9;
}

.control-item { margin-bottom: 6px; }
.control-item label {
    display: block;
    font-size: 0.72rem;
    font-weight: 600;
    color: #475569;
    margin-bottom: 2px;
}
.control-item input[type="number"],
.control-item input[type="text"],
.control-item select {
    width: 100%;
    padding: 5px 8px;
    font-size: 0.8rem;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius);
    background: #fff;
    color: #1e293b;
    font-family: inherit;
}
.control-item input:focus,
.control-item select:focus { border-color: #3b82f6; outline: none; box-shadow: 0 0 0 2px #eff6ff; }

.control-item input[type="range"] {
    width: 100%;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: #e2e8f0;
    border-radius: 2px;
    outline: none;
}

.control-item input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #3b82f6;
    cursor: pointer;
    border: 2px solid #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.control-item input[type="color"] {
    width: 32px;
    height: 24px;
    border: 1px solid #cbd5e1;
    border-radius: 3px;
    cursor: pointer;
    padding: 1px;
}

.full-width { width: 100%; }

.cell-physical-grid,
.cell-electrical-grid,
.cell-rating-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px 8px;
    margin-bottom: 6px;
}

.cell-electrical-grid { grid-template-columns: 1fr 1fr; }
.cell-rating-grid { grid-template-columns: 1fr 1fr; }

.cell-cost-block { grid-column: span 2; }
.cell-cost-input-row { display: flex; gap: 4px; align-items: center; }
.cell-cost-input-row input { flex: 1; }

.controls-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 8px; }
.controls-grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px 8px; }

.layer-control-wrap { display: flex; gap: 6px; align-items: center; }
.layer-input-group { flex: 1; }
.layer-check-group { display: flex; align-items: center; gap: 4px; }
.layer-check-label { font-size: 0.68rem; color: #64748b; cursor: pointer; }
.layer-check-group input:disabled + .layer-check-label { opacity: 0.4; cursor: default; }

.toggle-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 0;
    font-size: 0.78rem;
    color: #475569;
}

/* === TOGGLE (INLINE) === */
.inline-toggle {
    display: inline-flex;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    overflow: hidden;
}
.it-opt {
    padding: 3px 10px;
    font-size: 0.7rem;
    font-weight: 600;
    cursor: pointer;
    background: #f8fafc;
    color: #94a3b8;
    border-right: 1px solid #cbd5e1;
    user-select: none;
}
.it-opt:last-child { border-right: none; }
.it-opt.active { background: #eff6ff; color: #3b82f6; }
.it-opt.active.warn { background: #fef2f2; color: #dc2626; }
.it-opt.active.good { background: #f0fdf4; color: #16a34a; }

/* === SMART INPUT === */
.smart-input-wrap {
    position: relative;
    display: flex;
    align-items: center;
}
.smart-input-wrap input {
    width: 100%;
    padding: 4px 24px 4px 6px;
    font-size: 0.78rem;
    border: 1px solid #cbd5e1;
    border-radius: var(--radius);
    font-family: 'SF Mono', 'Consolas', monospace;
}
.smart-input-wrap .unit {
    position: absolute;
    right: 6px;
    font-size: 0.68rem;
    color: #94a3b8;
    pointer-events: none;
}

.mini-input input { padding: 3px 22px 3px 6px; font-size: 0.75rem; }

/* === SEARCH RESULTS === */
#db-results {
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
    background: #fff;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    box-shadow: 0 8px 20px rgba(0,0,0,0.12);
    max-height: 220px;
    overflow-y: auto;
    z-index: 100;
    display: none;
}
#db-results .db-item {
    padding: 6px 10px;
    font-size: 0.72rem;
    cursor: pointer;
    border-bottom: 1px solid #f1f5f9;
}
#db-results .db-item:last-child { border-bottom: none; }
#db-results .db-item:hover { background: #eff6ff; }
#db-results .db-name { font-weight: 600; color: #1e293b; }
#db-results .db-info { color: #94a3b8; font-size: 0.68rem; }

#db-search-wrap { position: relative; }

/* === HELP ICONS === */
.help-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #e2e8f0;
    color: #64748b;
    font-size: 9px;
    font-weight: 700;
    cursor: help;
    position: relative;
    vertical-align: middle;
    margin-left: 3px;
}
.help-icon.warn { background: #fef3c7; color: #92400e; }

.help-icon .tip-content {
    display: none;
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #1e293b;
    color: #f1f5f9;
    padding: 8px 10px;
    border-radius: 6px;
    font-size: 0.68rem;
    font-weight: 400;
    line-height: 1.4;
    width: 200px;
    z-index: 200;
    pointer-events: none;
    margin-bottom: 6px;
}
.help-icon:hover .tip-content { display: block; }
.tip-content.tip-left { left: auto; right: 0; transform: none; }
.tip-content.tip-right { left: 0; transform: none; }
.tip-content.tip-centered { left: 50%; transform: translateX(-50%); }

/* === SIMULATION RESULTS === */
#sim-results {
    display: flex;
    gap: 0;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    padding: 8px;
    margin-top: 4px;
}

.res-col { flex: 1; min-width: 0; }

.res-v-sep {
    width: 1px;
    background: #e2e8f0;
    margin: 0 8px;
}

.res-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 0;
    font-size: 0.72rem;
}

.res-lbl { color: #64748b; font-weight: 500; }
.res-val { font-weight: 700; font-family: 'SF Mono', monospace; font-size: 0.72rem; }

.c-red { color: #dc2626; }
.c-blue { color: #3b82f6; }
.c-green { color: #16a34a; }
.c-orange { color: #ea580c; }
.c-teal { color: #0d9488; }
.c-indigo { color: #4f46e5; }
.c-violet { color: #7c3aed; }
.c-purp { color: #9333ea; }
.c-pink { color: #db2777; }
.c-dark { color: #1e293b; }
.c-gray { color: #64748b; }

#row-status {
    margin-top: 4px;
    padding-top: 4px;
    border-top: 1px dashed #e2e8f0;
}
#status-text { font-size: 0.7rem; color: #64748b; }
#status-text.status-alert { color: #dc2626; font-weight: 700; }
#status-text.status-caution { color: #d97706; font-weight: 600; }
#status-text.status-note { color: #64748b; }

/* === MATERIAL SETTINGS === */
details summary {
    font-size: 0.75rem;
    padding: 8px 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    color: #475569;
}

.details-content {
    padding: 8px;
    border: 1px solid #e2e8f0;
    border-top: none;
    border-radius: 0 0 6px 6px;
}

.mat-header-row {
    display: grid;
    grid-template-columns: 80px 1fr 60px;
    font-size: 0.68rem;
    font-weight: 700;
    color: #94a3b8;
    padding: 2px 0 4px;
    border-bottom: 1px solid #f1f5f9;
    margin-bottom: 4px;
}

.mat-row {
    display: grid;
    grid-template-columns: 80px 1fr 60px;
    align-items: center;
    padding: 3px 0;
}

.mat-label { font-size: 0.72rem; color: #475569; font-weight: 500; }
.color-wrap { display: flex; align-items: center; gap: 6px; }
.color-wrap input[type="range"] { flex: 1; }
.perc-label { font-size: 0.68rem; color: #94a3b8; text-align: right; font-weight: 600; }

/* === STATS PANEL === */
#stats-panel {
    border-top: 1px solid #e5e7eb;
    padding: 8px 12px;
    background: #f8fafc;
    display: flex;
    gap: 0;
    flex-shrink: 0;
}

.stats-col { flex: 1; min-width: 0; }
.stats-v-sep { width: 1px; background: #e2e8f0; margin: 0 8px; }

.stat-header {
    font-size: 0.68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #94a3b8;
    margin-bottom: 4px;
    padding-bottom: 2px;
    border-bottom: 1px solid #e2e8f0;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.72rem;
    padding: 1px 0;
    min-width: 0;
}

.stat-row > span:first-child {
    color: #64748b;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stat-row .val {
    font-weight: 700;
    margin-left: 6px;
    min-width: 0;
    white-space: nowrap;
}

.stat-divider {
    border-top: 1px solid #e2e8f0;
    margin: 3px 0;
}

/* === SNAPSHOT AREA === */
#snapshot-area {
    padding: 6px 12px;
    border-top: 1px solid #e5e7eb;
    background: #fff;
    flex-shrink: 0;
}

.snap-controls { display: flex; gap: 6px; margin-bottom: 4px; }

.btn-action {
    flex: 1;
    padding: 5px 8px;
    font-size: 0.72rem;
    font-weight: 600;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    cursor: pointer;
    background: #fff;
    color: #475569;
    font-family: inherit;
}
.btn-action:hover { border-color: #3b82f6; color: #3b82f6; }
.btn-add { border-color: #16a34a; color: #16a34a; }
.btn-add:hover { background: #f0fdf4; }
.btn-comp { border-color: #7c3aed; color: #7c3aed; }
.btn-comp:hover { background: #f5f3ff; }

#snap-list {
    max-height: 80px;
    overflow-y: auto;
    font-size: 0.68rem;
}

/* === OVERLAYS === */
.overlay-text {
    position: absolute;
    pointer-events: none;
    user-select: none;
    color: #94a3b8;
    font-size: 0.7rem;
    font-weight: 500;
    z-index: 10;
}

#watermark { bottom: 10px; right: 15px; font-weight: 600; opacity: 0.6; }
#controls-hint { bottom: 10px; left: 15px; display: flex; flex-direction: column; gap: 2px; }

#perf-toast {
    position: absolute;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(255,255,255,0.95);
    border: 1px solid #eab308;
    border-left: 4px solid #eab308;
    padding: 8px 16px;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    font-size: 0.75rem;
    color: #854d0e;
    font-weight: 600;
    display: none;
    align-items: center;
    gap: 8px;
    z-index: 50;
    pointer-events: none;
}

#validation-toast {
    position: absolute;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(255,255,255,0.95);
    border: 1px solid #f59e0b;
    border-left: 4px solid #f59e0b;
    padding: 8px 16px;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    font-size: 0.75rem;
    color: #92400e;
    display: none;
    z-index: 50;
    pointer-events: none;
}

/* === MODALS === */
.modal-base-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

#comp-modal {
    background: #fff;
    width: 90%;
    max-width: 900px;
    max-height: 85vh;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 50px rgba(0,0,0,0.3);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #e2e8f0;
}

.modal-body {
    padding: 16px;
    overflow-y: auto;
    flex: 1;
}

.comp-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.75rem;
}
.comp-table th, .comp-table td {
    padding: 6px 10px;
    border: 1px solid #e2e8f0;
    text-align: left;
}
.comp-table th { background: #f8fafc; font-weight: 700; color: #475569; }
.comp-table .best-val { background: #f0fdf4; color: #16a34a; font-weight: 700; }

/* === DISCLAIMER === */
#disclaimer-box {
    background: #fff;
    width: 90%;
    max-width: 600px;
    max-height: 85vh;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}

.disc-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: #fef2f2;
    border-bottom: 1px solid #fecaca;
    font-weight: 700;
    font-size: 0.85rem;
    color: #991b1b;
    border-radius: 8px 8px 0 0;
}

.disc-content {
    padding: 16px;
    overflow-y: auto;
    font-size: 0.78rem;
    color: #475569;
    line-height: 1.6;
}

.disc-content p { margin-bottom: 10px; }
.disc-content ul { padding-left: 20px; margin-bottom: 10px; }

.disc-footer {
    padding: 12px 16px;
    border-top: 1px solid #e2e8f0;
}

.disc-footer button {
    padding: 8px 16px;
    background: #dc2626;
    color: #fff;
    border: none;
    border-radius: 6px;
    font-weight: 700;
    cursor: pointer;
    font-size: 0.85rem;
}
.disc-footer button:hover { background: #b91c1c; }

/* === 3D LABELS === */
.label-tag {
    font-size: 10px;
    font-weight: 600;
    padding: 3px 7px;
    border: 1px solid #4a90d9;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.92);
    color: #4a90d9;
    white-space: nowrap;
    pointer-events: none;
    user-select: none;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.label-tag.inter {
    border-color: #eab308;
    color: #b45309;
}

.dim-tag {
    font-size: 11px;
    font-weight: 600;
    color: #333;
    background: rgba(255, 255, 255, 0.8);
    padding: 1px 4px;
    border-radius: 3px;
    pointer-events: none;
    user-select: none;
    white-space: nowrap;
}

/* === INPUT INVALID === */
.input-invalid {
    border-color: #dc2626 !important;
    box-shadow: 0 0 0 2px #fef2f2 !important;
}

/* === RESPONSIVE === */
@media (max-width: 800px) and (orientation: portrait) {
    body { flex-direction: column; }
    #render-area { height: 25vh; width: 100%; border-top: 1px solid #e5e7eb; order: 2; }
    #sidebar { width: 100%; height: 75vh; border-right: none; order: 1; }
}

@media (max-height: 600px) and (orientation: landscape) {
    #sidebar { width: 45vw; max-width: 380px; min-width: 320px; }
}

@media (max-width: 800px), (max-height: 600px) {
    #controls-hint { display: none !important; }
    .label-tag { font-size: 10px !important; padding: 4px 7px !important; }
    .dim-tag { font-size: 10px !important; }
}

#wiring-canvas-top, #wiring-canvas-bottom {
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    max-width: 100%;
    height: auto;
}
