:root {
    --bg: #f4f5f7;
    --panel: #ffffff;
    --border: #d8dce2;
    --text: #1f2733;
    --muted: #6b7785;
    --primary: #2f6fb0;
    --primary-dark: #245890;
    --danger: #c0392b;
    --sheet-bg: #fbf7ec;
    --sheet-border: #b9a677;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color: var(--text);
    background: var(--bg);
    line-height: 1.45;
}

.app-header {
    padding: 1rem 1.5rem;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
}

.app-header h1 {
    margin: 0 0 0.25rem;
    font-size: 1.4rem;
}

.app-header p {
    margin: 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.app-header-link {
    flex: none;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.85rem;
    white-space: nowrap;
}

.app-header-link:hover {
    color: var(--text);
    text-decoration: underline;
}

.layout {
    display: grid;
    grid-template-columns: minmax(360px, 1fr) minmax(0, 1.6fr);
    gap: 1rem;
    padding: 1rem;
    align-items: start;
}

@media (max-width: 900px) {
    .layout {
        grid-template-columns: 1fr;
    }
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem 1.25rem 1.25rem;
}

.panel h2 {
    font-size: 1rem;
    margin: 1.25rem 0 0.5rem;
}

.panel h2:first-of-type {
    margin-top: 0.25rem;
}

.field-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
    align-items: flex-end;
}

label {
    display: flex;
    flex-direction: column;
    font-size: 0.78rem;
    color: var(--muted);
    gap: 0.2rem;
    white-space: nowrap; /* "Breite (cm)" nicht hinter der Klammer umbrechen */
}

/*
 * Caption als EIN Inline-Element: ohne diesen Wrapper würden in der Flex-Spalte
 * der Text, das <span class="unit-label"> und die ")" zu je eigenen Flex-Items
 * und somit auf drei Zeilen umbrechen.
 */
.cap {
    white-space: nowrap;
}

/* hidden-Attribut muss `label { display:flex }` überstimmen (sonst bleibt das
   Feld trotz [hidden] sichtbar – z. B. Spaltmaß bei Schublade/Einlegeböden). */
label[hidden] {
    display: none;
}

/* Inline-Label für Checkboxen (Label-Text neben der Box) */
label.check {
    flex-direction: row;
    align-items: center;
    gap: 0.35rem;
}

/* Kopfzeile eines Panels mit Aktion rechts (z. B. Vollbild-Button) */
.panel-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
}

.panel-head h2 {
    margin-top: 0.25rem;
}

.panel-head-actions {
    display: flex;
    gap: 0.5rem;
    flex: none;
}

.btn.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

/* Hinweise der Möbel-Vorlage (Rückwand, Bauweise …) */
.tpl-notes {
    margin: 0.4rem 0 0.25rem;
    font-size: 0.8rem;
    color: var(--muted);
}

.tpl-notes ul {
    margin: 0;
    padding-left: 1.1rem;
}

/* Explosionszeichnung der Möbel-Vorlage (three.js, drehbar) */
.exploded {
    margin: 0.5rem 0 0.25rem;
    width: 100%;
    max-width: 380px;
    height: 300px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #f0f2f5;
    cursor: grab;
    overflow: hidden;
}

.exploded:active {
    cursor: grabbing;
}

.exploded canvas {
    display: block;
}

/* SVG-Fallback, falls three.js nicht geladen werden kann */
.exploded-svg {
    display: block;
    width: 100%;
    height: 100%;
}

/* Restflächen-Infozeile */
.rest-info {
    font-size: 0.82rem;
    color: var(--muted);
    margin-bottom: 0.5rem;
}

/* CSV-Eingabe */
.csv-input {
    width: 100%;
    box-sizing: border-box;
    font: inherit;
    font-size: 0.85rem;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 5px;
    resize: vertical;
}

input,
select {
    font: inherit;
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: #fff;
    color: var(--text);
}

input[type="number"] {
    width: 6.5rem;
}

input[type="color"] {
    padding: 0.15rem;
    width: 3rem;
    height: 2.2rem;
}

.btn {
    font: inherit;
    cursor: pointer;
    padding: 0.45rem 0.9rem;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: #eef1f4;
    color: var(--text);
}

.btn:hover {
    background: #e2e6eb;
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.actions {
    margin-top: 1rem;
    display: flex;
    gap: 0.6rem;
}

.board-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.board-table th,
.board-table td {
    text-align: left;
    padding: 0.35rem 0.4rem;
    border-bottom: 1px solid var(--border);
}

.board-table th {
    color: var(--muted);
    font-weight: 600;
}

.swatch {
    display: inline-block;
    width: 0.95rem;
    height: 0.95rem;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.2);
}

/*
 * Aktionen-Zelle bewusst NICHT als Flex-Container: ein flex-td erzeugt mit
 * border-collapse eine sub-pixel ungenaue Zellenhöhe, wodurch die untere
 * Trennlinie unter den Buttons je nach Pixel-Position minimal nach oben springt.
 * Inline-Buttons mit geerbter line-height verhalten sich wie normaler Zelltext.
 */
.row-actions {
    white-space: nowrap;
}

.link-action,
.link-danger {
    background: none;
    border: none;
    cursor: pointer;
    font: inherit;
    line-height: inherit;
    vertical-align: baseline;
    padding: 0;
}

.link-action {
    color: var(--primary);
    margin-right: 0.75rem;
}

.link-danger {
    color: var(--danger);
}

.board-table tr.editing {
    background: #eef4fb;
}

.hint {
    color: var(--muted);
    font-size: 0.8rem;
    margin: 0.5rem 0 0;
}

.stats {
    font-size: 0.85rem;
    margin-bottom: 0.6rem;
    min-height: 1.2rem;
}

.stats .warn {
    color: var(--danger);
    font-weight: 600;
}

.canvas-wrap {
    border: 1px solid var(--border);
    border-radius: 6px;
    background:
        linear-gradient(45deg, #eceff3 25%, transparent 25%),
        linear-gradient(-45deg, #eceff3 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #eceff3 75%),
        linear-gradient(-45deg, transparent 75%, #eceff3 75%);
    background-size: 18px 18px;
    background-position: 0 0, 0 9px, 9px -9px, -9px 0;
    padding: 12px;
    overflow: auto;
    /* Verhindert das Markieren der Teil-Beschriftungen beim Verschieben */
    user-select: none;
    -webkit-user-select: none;
}

#canvas {
    display: block;
    width: 100%;
    height: auto;
    background: var(--sheet-bg);
    border: 2px solid var(--sheet-border);
    touch-action: none; /* eigenes Drag-Handling statt Scrollen/Zoom */
}

/* Vollbildmodus: Zeichenbrett zentriert und maximal groß einpassen */
#canvas-wrap:fullscreen {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2vmin;
    overflow: hidden;
}

#canvas-wrap:fullscreen #canvas {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
}

/* Verschnittkante (außen umlaufender Verschnitt) */
#canvas .waste {
    fill: #d9c9a0;
}

/* nutzbare Fläche innerhalb der Verschnittkante; gestrichelte Kante = Schnittkante (rot) */
#canvas .usable-area {
    fill: var(--sheet-bg);
    stroke: #e02424;
    stroke-width: 1.5;
    stroke-dasharray: 6 4;
}

/* Schnittfuge zwischen Teilen als durchgehende rote Linie */
#canvas .kerf-line {
    stroke: #e02424;
    stroke-width: 1.5;
    vector-effect: non-scaling-stroke;
}

/* rechteckige Restflächen (schraffiert, Fill kommt vom Muster im Markup) */
#canvas .rest-rect {
    stroke: #2f8f55;
    stroke-width: 1.5;
    stroke-dasharray: 8 5;
    vector-effect: non-scaling-stroke;
    pointer-events: none;
}

#canvas .rest-label {
    fill: #1f5a36;
    font-family: system-ui, sans-serif;
    font-weight: 600;
    paint-order: stroke;
    stroke: rgba(255, 255, 255, 0.85);
    stroke-width: 0.6;
    vector-effect: non-scaling-stroke;
}

.piece {
    cursor: grab;
}

.piece.dragging {
    cursor: grabbing;
}

.piece rect {
    stroke: rgba(0, 0, 0, 0.35);
    stroke-width: 1;
}

/* Beschriftung im foreignObject – bricht innerhalb des Bretts um */
.piece-label {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1px 2px;
    text-align: center;
    line-height: 1.05;
    color: #1f2733;
    font-family: system-ui, sans-serif;
    overflow: hidden;
    overflow-wrap: anywhere; /* auch lange/zusammenhängende Wörter umbrechen */
    word-break: break-word;
    hyphens: auto;
    pointer-events: none;
}
