:root {
    --bg: #f2f0eb;
    --surface: #fffdf8;
    --surface-2: #e8e2d8;
    --text: #24211c;
    --muted: #6d675f;
    --line: #c9c0b4;
    --accent: #2f6f65;
    --accent-2: #b47f3b;
    --danger: #a63f3f;
    --shadow: 0 16px 40px rgba(36, 33, 28, 0.14);
}

* {
    box-sizing: border-box;
}

html {
    height: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: Arial, Helvetica, sans-serif;
    overflow: hidden;
}

button,
input,
select {
    font: inherit;
}

button {
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text);
    border-radius: 6px;
    padding: 6px 8px;
    cursor: pointer;
}

.icon-button {
    display: inline-grid;
    place-items: center;
    width: 34px;
    height: 32px;
    min-width: 34px;
    padding: 0;
    vertical-align: middle;
}

.icon {
    width: 18px;
    height: 18px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.9;
    stroke-linecap: round;
    stroke-linejoin: round;
}

button:hover {
    border-color: var(--accent);
}

button.active,
.tab.active,
.tool.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

button.danger {
    border-color: var(--danger);
    color: var(--danger);
}

button.danger:hover {
    background: var(--danger);
    color: #fff;
}

button.danger-soft {
    border-color: rgba(166, 63, 63, 0.55);
    background: #fff7f5;
    color: var(--danger);
}

button.danger-soft:hover {
    border-color: var(--danger);
}

input,
select {
    width: 100%;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    border-radius: 6px;
    padding: 4px 6px;
    min-height: 29px;
}

input[type="color"] {
    min-height: 29px;
    padding: 3px;
}

.app-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 390px;
    height: 100vh;
    max-height: 100vh;
}

.workspace {
    min-width: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
    overflow: hidden;
}

.topbar {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 12px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.project-line {
    display: grid;
    grid-template-columns: minmax(180px, 320px) minmax(180px, 280px);
    gap: 10px;
}

.project-name {
    font-weight: 600;
}

.top-actions,
.row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.danger-menu {
    position: relative;
}

.danger-menu summary {
    display: inline-grid;
    place-items: center;
    width: 34px;
    height: 32px;
    min-width: 34px;
    list-style: none;
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--muted);
    border-radius: 6px;
    padding: 0;
    cursor: pointer;
    user-select: none;
}

.danger-menu summary::-webkit-details-marker {
    display: none;
}

.danger-menu[open] summary {
    border-color: var(--danger);
    color: var(--danger);
}

.danger-menu button {
    margin-top: 6px;
}

.top-actions .danger-menu[open] {
    z-index: 5;
}

.top-actions .danger-menu button {
    position: absolute;
    right: 0;
    top: 100%;
    width: 38px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.quick-tooltip {
    position: fixed;
    z-index: 10000;
    pointer-events: none;
    padding: 6px 8px;
    border-radius: 5px;
    background: rgba(36, 33, 28, 0.94);
    color: #fff;
    font-size: 12px;
    line-height: 1.25;
    max-width: 240px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}

.field-note {
    margin: -4px 0 4px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.35;
}

.stage {
    position: relative;
    min-height: 0;
    overflow: hidden;
    background:
        linear-gradient(45deg, rgba(0, 0, 0, 0.035) 25%, transparent 25%),
        linear-gradient(-45deg, rgba(0, 0, 0, 0.035) 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, rgba(0, 0, 0, 0.035) 75%),
        linear-gradient(-45deg, transparent 75%, rgba(0, 0, 0, 0.035) 75%);
    background-size: 26px 26px;
    background-position: 0 0, 0 13px, 13px -13px, -13px 0;
}

#planCanvas,
#threeMount {
    display: block;
    width: 100%;
    height: 100%;
}

#planCanvas {
    cursor: grab;
    touch-action: none;
}

#planCanvas[data-mode="rect"],
#planCanvas[data-mode="lasso"],
#planCanvas[data-mode="contour"],
#planCanvas[data-mode="cutout"],
#planCanvas[data-mode="wall"],
#planCanvas[data-mode="insert"],
#planCanvas[data-mode="scale"] {
    cursor: crosshair;
}

#planCanvas[data-mode="edit"] {
    cursor: move;
}

#planCanvas.is-panning {
    cursor: grabbing;
}

.three-stage {
    position: relative;
    background: #1d1e22;
}

.three-view-menu {
    position: absolute;
    left: 12px;
    top: 12px;
    z-index: 3;
    display: grid;
    gap: 6px;
    min-width: 160px;
    padding: 9px 10px;
    border: 1px solid rgba(255, 255, 255, 0.28);
    border-radius: 8px;
    background: rgba(250, 247, 240, 0.9);
    box-shadow: var(--shadow);
    backdrop-filter: blur(6px);
    font-size: 13px;
}

.three-view-menu-title {
    font-weight: 700;
}

.three-view-menu label {
    display: flex;
    align-items: center;
    gap: 7px;
    white-space: nowrap;
    cursor: pointer;
}

.three-view-menu input {
    width: auto;
    min-height: 0;
    margin: 0;
}

.sidebar {
    min-width: 0;
    height: 100vh;
    overflow: auto;
    border-left: 1px solid var(--line);
    background: var(--surface);
    padding: 8px;
}

.tabs {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 5px;
    margin-bottom: 9px;
}

.tab {
    padding: 6px 5px;
}

.tabs .icon-button {
    width: 100%;
    min-width: 0;
}

.panel {
    display: none;
}

.panel.active {
    display: grid;
    gap: 7px;
}

.panel h2 {
    margin: 0;
    font-size: 16px;
}

.field {
    display: grid;
    gap: 2px;
    color: var(--muted);
    font-size: 12px;
}

.wall-field input[type="file"] {
    width: 100%;
}

.wall-label {
    color: var(--text);
    font-weight: 600;
}

.wall-status {
    display: flex;
    align-items: center;
    gap: 7px;
    min-height: 28px;
    color: var(--muted);
    font-size: 12px;
}

.wall-status.has-image {
    color: var(--text);
}

.wall-status-thumb {
    width: 42px;
    height: 26px;
    flex: 0 0 auto;
    border: 1px solid var(--line);
    border-radius: 4px;
    background-color: #eee8dc;
    background-position: center;
    background-size: cover;
}

.opening-list {
    display: grid;
    gap: 5px;
}

.opening-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 5px 6px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: rgba(255, 253, 248, 0.72);
    color: var(--text);
    font-size: 12px;
}

.opening-item button {
    padding: 3px 6px;
    min-height: 0;
    font-size: 12px;
}

.opening-empty {
    color: var(--muted);
    font-size: 12px;
}

.checkline {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
    font-size: 13px;
}

.checkline input {
    width: auto;
}

.grid2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.grid1 {
    display: grid;
    gap: 6px;
}

.grid3 {
    display: grid;
    grid-template-columns: 0.65fr 0.65fr 1.7fr;
    gap: 6px;
}

.tool-panel-title {
    margin: 2px 0 0;
    font-size: 16px;
}

.tool-section {
    display: grid;
    gap: 5px;
    padding: 6px;
    border: 1px solid rgba(201, 192, 180, 0.7);
    border-radius: 7px;
    background: rgba(255, 253, 248, 0.68);
}

.tool-section-title {
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.tool-grid,
.tool-row {
    display: flex;
    align-items: center;
    align-content: flex-start;
    flex-wrap: wrap;
    gap: 5px;
}

.tool,
.compact-command {
    display: grid;
    place-items: center;
    width: 34px;
    height: 32px;
    min-width: 34px;
    padding: 0;
}

.tool-row .danger-menu {
    display: inline-grid;
}

.cut-row {
    gap: 4px;
}

.tool-row-label {
    align-self: center;
    color: var(--muted);
    font-size: 12px;
    line-height: 1;
}

.panel[data-panel="tools"] .row,
.panel[data-panel="tools"] .tool-row {
    gap: 5px;
}

.panel[data-panel="tools"] .tool-row button:not(.compact-command):not(.icon-button),
.panel[data-panel="tools"] .row button:not(.compact-command):not(.icon-button) {
    padding: 5px 7px;
    font-size: 14px;
}

.hint {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
}

.tile-list,
.stairs-list,
.summary {
    display: grid;
    gap: 6px;
}

.tile-list {
    max-height: 360px;
    overflow: auto;
    padding-right: 2px;
}

.tile-palette {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
}

.mask-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
}

.panel[data-panel="tiles"] {
    gap: 6px;
}

.panel[data-panel="tiles"] .row {
    gap: 5px;
}

.panel[data-panel="tiles"] .row button {
    min-height: 28px;
    padding: 4px 7px;
    font-size: 13px;
}

.panel[data-panel="tiles"] input,
.panel[data-panel="tiles"] select {
    min-height: 28px;
    padding: 3px 6px;
    font-size: 13px;
}

.mask-source {
    display: inline-block;
    width: 11px;
    height: 11px;
    border: 1px solid rgba(0, 0, 0, 0.25);
    border-radius: 2px;
    vertical-align: -1px;
}

.mask-source.green {
    background: #00ff00;
}

.mask-source.magenta {
    background: #ff00ff;
}

.mask-source.cyan {
    background: #00ffff;
}

.mask-source.yellow {
    background: #ffff00;
}

.palette-chip {
    display: grid;
    grid-template-columns: 17px 1fr;
    align-items: center;
    gap: 4px;
    padding: 3px;
    min-width: 0;
}

.palette-chip span {
    display: block;
    width: 17px;
    height: 17px;
    border: 1px solid rgba(0, 0, 0, 0.18);
    border-radius: 3px;
}

.palette-chip strong {
    font-size: 12px;
    line-height: 1;
}

.palette-chip.active {
    border-color: var(--accent);
    box-shadow: inset 0 0 0 1px var(--accent);
}

.pattern-gallery {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
    max-height: 218px;
    overflow: auto;
    padding-right: 2px;
}

.pattern-card {
    display: grid;
    gap: 2px;
    padding: 4px;
    min-width: 0;
    text-align: left;
}

.pattern-card.active {
    border-color: var(--accent);
    box-shadow: inset 0 0 0 1px var(--accent);
}

.pattern-thumb {
    display: block;
    width: 100%;
    aspect-ratio: 1;
    border: 1px solid var(--line);
    border-radius: 4px;
    background-color: #f4f0e8;
    background-size: cover;
    background-position: center;
}

.pattern-card span:not(.pattern-thumb) {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
    font-size: 11px;
}

.pattern-card small {
    color: var(--muted);
    font-size: 10px;
    line-height: 1.1;
}

.tile-item {
    display: grid;
    grid-template-columns: 34px minmax(0, 1fr) 28px;
    align-items: center;
    gap: 6px;
    padding: 5px;
    border: 1px solid var(--line);
    border-radius: 6px;
    min-height: 46px;
}

.tile-item.active {
    border-color: var(--accent);
    box-shadow: inset 0 0 0 1px var(--accent);
}

.empty-state {
    border: 1px dashed var(--line);
    border-radius: 6px;
    padding: 10px;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.35;
    background: rgba(255, 255, 255, 0.45);
}

.tile-swatch {
    width: 32px;
    height: 32px;
    border: 1px solid var(--line);
    border-radius: 4px;
    background-size: cover;
    background-position: center;
}

.tile-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
    font-size: 14px;
    line-height: 1.15;
}

.tile-subtitle {
    color: var(--muted);
    font-size: 10.5px;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stairs-item,
.summary-item {
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 8px;
    background: rgba(255, 255, 255, 0.55);
}

.calc-section-title,
.area-calc h3 {
    margin: 6px 0 0;
    font-size: 15px;
}

.area-breakdown {
    display: grid;
    gap: 12px;
}

.area-calc {
    display: grid;
    gap: 7px;
    padding-top: 10px;
    border-top: 1px solid var(--line);
}

.area-calc-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    color: var(--muted);
    font-size: 12px;
}

.area-calc-meta span {
    padding: 4px 6px;
    border: 1px solid rgba(201, 192, 180, 0.75);
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.45);
}

.calc-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.calc-table th,
.calc-table td {
    border-bottom: 1px solid var(--line);
    padding: 7px 4px;
    text-align: left;
    vertical-align: top;
}

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

.calc-table.compact {
    font-size: 12px;
}

.calc-table .empty-cell {
    color: var(--muted);
    text-align: center;
}

.print-report {
    display: none;
}

.print-report-header {
    display: flex;
    justify-content: space-between;
    gap: 24px;
    align-items: flex-start;
    padding-bottom: 16px;
    border-bottom: 2px solid #222;
}

.print-report-header h1 {
    margin: 0;
    font-size: 26px;
}

.print-report-header p,
.print-report-date {
    margin: 4px 0 0;
    color: #555;
    font-size: 13px;
}

.print-plan {
    margin: 18px 0;
    text-align: center;
}

.print-plan img {
    max-width: 100%;
    max-height: 52vh;
    object-fit: contain;
}

.print-report-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    margin: 14px 0 18px;
}

.print-report-grid div {
    border: 1px solid #aaa;
    padding: 8px;
    background: #f7f7f7;
}

.print-report-grid span {
    display: block;
    color: #555;
    font-size: 11px;
}

.print-report-grid strong {
    display: block;
    margin-top: 3px;
    font-size: 16px;
}

.print-report h2 {
    margin: 18px 0 8px;
    font-size: 18px;
}

.print-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.print-table th,
.print-table td {
    border: 1px solid #999;
    padding: 6px 7px;
    text-align: left;
    vertical-align: top;
}

.print-table th {
    background: #e9e9e9;
    font-weight: 700;
}

.context-menu {
    position: absolute;
    z-index: 10;
    width: auto;
    padding: 6px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.context-menu button {
    width: 100%;
    border: 0;
    text-align: left;
    background: transparent;
}

.context-menu button.icon-button {
    width: 36px;
    min-width: 36px;
    height: 34px;
    border: 1px solid transparent;
    text-align: center;
}

.context-menu button:hover {
    background: var(--surface-2);
}

@media (max-width: 1050px) {
    body {
        overflow: auto;
    }

    .app-shell {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(580px, 65vh) auto;
        height: auto;
        max-height: none;
        min-height: 100vh;
    }

    .workspace,
    .sidebar {
        height: auto;
    }

    .sidebar {
        border-left: 0;
        border-top: 1px solid var(--line);
    }
}

@media (max-width: 640px) {
    .topbar,
    .project-line,
    .grid2 {
        grid-template-columns: 1fr;
    }

    .topbar {
        display: grid;
    }

    .top-actions button,
    .row button {
        flex: 1 1 120px;
    }

    .tabs {
        grid-template-columns: repeat(2, 1fr);
    }

    .sidebar {
        padding: 10px;
    }
}

@media print {
    @page {
        size: A4 landscape;
        margin: 10mm;
    }

    body {
        background: #fff;
        overflow: visible;
    }

    body.printing-report .app-shell,
    body.printing-report .quick-tooltip,
    body.printing-report .context-menu {
        display: none !important;
    }

    body.printing-report .print-report {
        display: block;
        color: #111;
    }

    body:not(.printing-report) .sidebar,
    body:not(.printing-report) .topbar,
    body:not(.printing-report) .context-menu {
        display: none !important;
    }

    body:not(.printing-report) .app-shell,
    body:not(.printing-report) .workspace {
        display: block;
        height: auto;
    }
}
