:root {
    color-scheme: dark;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --background: #0e1013;
    --panel: #181b20;
    --panel-raised: #20242a;
    --border: rgba(255, 255, 255, 0.13);
    --muted: #a4abb3;
    --text: #f4f5f6;
    --accent: #79c2ad;
    --danger: #ef8d8d;
}

* {
    box-sizing: border-box;
}

body {
    min-width: 20rem;
    margin: 0;
    color: var(--text);
    background: var(--background);
}

button,
input,
select {
    font: inherit;
}

button,
.file-button {
    min-height: 2.75rem;
    padding: 0.6rem 0.85rem;
    border: 1px solid #44505c;
    border-radius: 0.65rem;
    color: var(--text);
    background: var(--panel-raised);
    cursor: pointer;
}

button:hover,
button:focus-visible,
.file-button:hover,
.file-button:focus-within {
    border-color: var(--accent);
}

button:disabled {
    opacity: 0.42;
    cursor: default;
}

.primary-button {
    border-color: var(--accent);
    color: #07110e;
    background: var(--accent);
    font-weight: 700;
}

.quiet-button {
    background: transparent;
}

.danger-button {
    border-color: rgba(239, 141, 141, 0.55);
    color: var(--danger);
    background: transparent;
}

.page-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    min-height: 5rem;
    padding: 0.8rem clamp(1rem, 3vw, 2.5rem);
    border-bottom: 1px solid var(--border);
    background: #111419;
}

.page-header h1,
.page-header p {
    margin: 0;
}

.eyebrow {
    color: var(--accent);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.back-link {
    color: var(--muted);
    text-decoration: none;
}

.save-state {
    margin-left: auto !important;
    color: var(--muted);
    font-size: 0.85rem;
}

.app-shell {
    display: grid;
    grid-template-columns: minmax(18rem, 23rem) minmax(0, 1fr);
    gap: 1rem;
    padding: 1rem;
}

.sidebar,
.workspace-column {
    display: grid;
    align-content: start;
    gap: 1rem;
    min-width: 0;
}

.panel,
.toolbar,
.workspace {
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 0.9rem;
    background: var(--panel);
}

.panel {
    padding: 1rem;
}

details.panel {
    padding: 0;
}

details.panel > summary {
    padding: 1rem;
    font-weight: 700;
    cursor: pointer;
}

details.panel > :not(summary) {
    margin-right: 1rem;
    margin-left: 1rem;
}

details.panel > :last-child {
    margin-bottom: 1rem;
}

.field-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.field-grid label {
    display: grid;
    gap: 0.35rem;
    color: var(--muted);
    font-size: 0.85rem;
}

.field-grid label span {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.field-grid input,
.field-grid select,
.toolbar select {
    width: 100%;
    min-width: 0;
    padding: 0.65rem;
    border: 1px solid #414952;
    border-radius: 0.5rem;
    color: var(--text);
    background: #0d1014;
}

.field-grid label span input {
    flex: 1;
}

.field-grid small {
    color: var(--muted);
}

.field-wide {
    grid-column: 1 / -1;
}

.check-field {
    display: flex !important;
    grid-template-columns: auto 1fr;
    align-items: center;
}

.check-field input {
    width: auto;
}

.compact-form {
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.material-list {
    display: grid;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.material-card {
    display: grid;
    grid-template-columns: 2.25rem 1fr auto;
    align-items: center;
    gap: 0.65rem;
    padding: 0.55rem;
    border: 1px solid var(--border);
    border-radius: 0.65rem;
    background: var(--panel-raised);
}

.material-card[draggable="true"] {
    cursor: grab;
}

.material-card span:nth-child(2) {
    display: grid;
    min-width: 0;
}

.material-card strong,
.material-card small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.material-card small {
    color: var(--muted);
}

.material-card button {
    min-height: 2.25rem;
    padding: 0.4rem 0.55rem;
}

.material-swatch {
    width: 2.25rem;
    aspect-ratio: 1;
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 0.4rem;
}

.empty-note,
.workspace-hint {
    color: var(--muted);
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.55rem;
    padding: 0.65rem;
}

.toolbar label {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    margin-left: auto;
    color: var(--muted);
}

.workspace {
    min-height: 24rem;
    padding: 0.5rem;
    overflow: hidden;
    background-color: #111419;
    background-image: radial-gradient(rgba(255, 255, 255, 0.08) 1px, transparent 1px);
    background-size: 18px 18px;
}

#board-canvas {
    display: block;
    width: 100%;
    height: clamp(24rem, 58vw, 48rem);
    touch-action: none;
    user-select: none;
}

.board-background {
    fill: #e8d8b9;
}

.board-outline {
    fill: none;
    stroke: #f5f7f9;
    stroke-width: 7;
    pointer-events: none;
}

.division-line {
    stroke: #5e4d3b;
    stroke-width: 5;
    stroke-dasharray: 28 20;
    pointer-events: none;
}

.piece {
    cursor: grab;
}

.piece rect {
    stroke: rgba(33, 25, 18, 0.78);
    stroke-width: 4;
}

.piece text {
    fill: #1c1712;
    font-size: 30px;
    font-weight: 700;
    pointer-events: none;
}

.piece.is-selected rect {
    stroke: #ffffff;
    stroke-width: 10;
}

.piece.has-overlap rect {
    stroke: #db3030;
    stroke-width: 10;
}

.piece.is-selected.has-overlap rect {
    stroke: #ffdb66;
}

.workspace-hint {
    margin: -0.45rem 0 0;
    font-size: 0.85rem;
}

.panel h2 {
    margin: 0 0 0.8rem;
    font-size: 1.05rem;
}

.panel-heading {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
}

#validation-summary {
    color: var(--muted);
    font-size: 0.85rem;
}

#validation-summary[data-tone="warning"] {
    color: #ffdb66;
}

#validation-summary[data-tone="success"] {
    color: var(--accent);
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.65rem;
    margin-bottom: 1rem;
}

.summary-grid > div {
    display: grid;
    gap: 0.25rem;
    padding: 0.75rem;
    border-radius: 0.65rem;
    background: var(--panel-raised);
}

.summary-grid span {
    color: var(--muted);
    font-size: 0.78rem;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    padding: 0.55rem;
    border-bottom: 1px solid var(--border);
    text-align: left;
    white-space: nowrap;
}

th {
    color: var(--muted);
    font-size: 0.78rem;
}

.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.55rem;
}

.file-button {
    display: inline-flex;
    align-items: center;
}

.file-button input {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
}

@media (max-width: 900px) {
    .app-shell {
        grid-template-columns: minmax(0, 1fr);
    }

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

    .sidebar > :last-child {
        grid-column: 1 / -1;
    }
}

@media (max-width: 640px) {
    body {
        overflow-x: hidden;
    }

    .page-header {
        align-items: flex-start;
        flex-wrap: wrap;
    }

    .page-header > div {
        min-width: 0;
    }

    .page-header h1 {
        font-size: clamp(1.55rem, 9vw, 2rem);
    }

    .save-state {
        width: 100%;
        margin-left: 0 !important;
    }

    .app-shell {
        padding: 0.65rem;
    }

    .sidebar {
        grid-template-columns: 1fr;
    }

    .sidebar > :last-child {
        grid-column: auto;
    }

    .field-grid {
        grid-template-columns: 1fr;
    }

    .field-wide {
        grid-column: auto;
    }

    .material-card {
        grid-template-columns: 2.25rem minmax(0, 1fr);
    }

    .material-card button {
        grid-column: 1 / -1;
        width: 100%;
    }

    .toolbar label {
        width: 100%;
        margin-left: 0;
    }

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

@media print {
    body {
        color: #111;
        background: #fff;
    }

    .page-header,
    .sidebar,
    .toolbar,
    .workspace-hint,
    .file-panel {
        display: none;
    }

    .app-shell {
        display: block;
        padding: 0;
    }

    .workspace,
    .panel {
        break-inside: avoid;
        border-color: #aaa;
        color: #111;
        background: #fff;
    }

    #board-canvas {
        height: auto;
        max-height: 60vh;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
    }
}
