:root {
    color-scheme: light;
    --bg: #edf1f3;
    --surface: #ffffff;
    --surface-subtle: #f6f8f9;
    --line: #d9e0e4;
    --line-strong: #bdc8cf;
    --text: #172126;
    --muted: #66747c;
    --primary: #1467d2;
    --primary-hover: #0d55b1;
    --primary-soft: #eaf2fd;
    --danger: #c43d3d;
    --success: #18794e;
    --header-height: 60px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-width: 320px;
    min-height: 100%;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    font-size: 14px;
    line-height: 1.45;
}

button,
input,
select {
    color: inherit;
    font: inherit;
    letter-spacing: 0;
}

button,
select,
input[type="color"],
input[type="range"] {
    cursor: pointer;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
    outline: 3px solid rgba(20, 103, 210, 0.22);
    outline-offset: 1px;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

[hidden] {
    display: none !important;
}

.app-header {
    align-items: center;
    background: rgba(255, 255, 255, 0.96);
    border-bottom: 1px solid var(--line);
    display: flex;
    height: var(--header-height);
    justify-content: space-between;
    left: 0;
    padding: 0 max(18px, env(safe-area-inset-left));
    position: fixed;
    right: 0;
    top: 0;
    z-index: 30;
}

.brand {
    align-items: center;
    color: var(--text);
    display: flex;
    min-width: 0;
    text-decoration: none;
}

.brand img {
    display: block;
    height: 32px;
    max-width: 132px;
    object-fit: contain;
}

.brand-divider {
    background: var(--line-strong);
    height: 22px;
    margin: 0 12px;
    width: 1px;
}

.brand-product {
    font-size: 15px;
    font-weight: 700;
    white-space: nowrap;
}

.header-actions {
    align-items: center;
    display: flex;
    gap: 8px;
}

.app-shell {
    display: grid;
    grid-template-areas: "library workspace settings";
    grid-template-columns: 252px minmax(360px, 1fr) 312px;
    height: 100dvh;
    padding-top: var(--header-height);
}

.app-panel {
    background: var(--surface);
    min-height: 0;
    overflow-y: auto;
    padding: 20px;
}

.library-panel {
    border-right: 1px solid var(--line);
    grid-area: library;
}

.settings-panel {
    border-left: 1px solid var(--line);
    grid-area: settings;
}

.workspace-panel {
    display: flex;
    flex-direction: column;
    grid-area: workspace;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
}

.panel-heading,
.workspace-toolbar,
.modal-header {
    align-items: center;
    display: flex;
    justify-content: space-between;
}

.panel-heading {
    margin-bottom: 16px;
}

.panel-heading h1,
.panel-heading h2,
.modal-header h2 {
    font-size: 18px;
    line-height: 1.2;
    margin: 2px 0 0;
}

.eyebrow {
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0;
    margin: 0;
    text-transform: uppercase;
}

.count-badge {
    align-items: center;
    background: var(--surface-subtle);
    border: 1px solid var(--line);
    border-radius: 999px;
    color: var(--muted);
    display: inline-flex;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    height: 26px;
    justify-content: center;
    min-width: 26px;
    padding: 0 8px;
}

.button,
.icon-button,
.text-button,
.upload-zone,
.segmented button,
.empty-state,
.photo-action {
    border: 0;
    min-height: 40px;
}

.button {
    align-items: center;
    border-radius: 6px;
    display: inline-flex;
    font-weight: 650;
    justify-content: center;
    padding: 0 14px;
    transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease;
}

.button.primary {
    background: var(--primary);
    color: #ffffff;
}

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

.button.secondary {
    background: var(--surface);
    border: 1px solid var(--line-strong);
}

.button.secondary:hover,
.button.secondary.is-active {
    background: var(--primary-soft);
    border-color: var(--primary);
    color: var(--primary);
}

.button.small {
    min-height: 34px;
    padding: 0 10px;
}

.layout-actions {
    display: grid;
    gap: 6px;
    grid-template-columns: 1fr 1fr;
    margin-bottom: 12px;
}

.layout-actions .button:last-child {
    grid-column: 1 / -1;
}

.upload-zone {
    align-items: center;
    background: var(--surface-subtle);
    border: 1px dashed var(--line-strong);
    border-radius: 6px;
    color: var(--primary);
    display: flex;
    font-weight: 700;
    gap: 8px;
    justify-content: center;
    min-height: 72px;
    padding: 12px;
    width: 100%;
}

.upload-zone:hover,
.preview-stage.is-dragging {
    background: var(--primary-soft);
    border-color: var(--primary);
}

.upload-symbol {
    font-size: 24px;
    font-weight: 400;
    line-height: 1;
}

.photo-list {
    display: grid;
    gap: 8px;
    margin-top: 14px;
}

.photo-list-empty {
    color: var(--muted);
    padding: 18px 0;
    text-align: center;
}

.photo-list-item {
    align-items: center;
    background: var(--surface-subtle);
    border: 1px solid transparent;
    border-radius: 6px;
    display: grid;
    gap: 8px;
    grid-template-columns: 48px minmax(0, 1fr) auto;
    min-height: 58px;
    padding: 4px;
}

.photo-list-item.is-selected {
    background: var(--primary-soft);
    border-color: #93b9eb;
}

.photo-list-item.is-dragging {
    opacity: 0.45;
}

.photo-thumb {
    border-radius: 4px;
    display: block;
    height: 48px;
    object-fit: cover;
    width: 48px;
}

.photo-info {
    min-width: 0;
}

.photo-name {
    display: block;
    font-size: 12px;
    font-weight: 650;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.photo-index {
    color: var(--muted);
    display: block;
    font-size: 11px;
    margin-top: 2px;
}

.photo-item-actions {
    display: grid;
    grid-template-columns: repeat(2, 30px);
}

.photo-action {
    align-items: center;
    background: transparent;
    border-radius: 4px;
    color: var(--muted);
    display: inline-flex;
    font-size: 16px;
    justify-content: center;
    min-height: 30px;
    padding: 0;
    width: 30px;
}

.photo-action:hover {
    background: #ffffff;
    color: var(--text);
}

.photo-action.delete:hover {
    color: var(--danger);
}

.workspace-toolbar {
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    flex: 0 0 auto;
    min-height: 54px;
    padding: 8px 16px;
}

.canvas-summary {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.canvas-summary strong,
.canvas-summary span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.canvas-summary strong {
    font-size: 13px;
}

.canvas-summary span {
    color: var(--muted);
    font-size: 11px;
}

.canvas-summary span.has-warning {
    color: var(--danger);
    font-weight: 650;
}

.text-button {
    background: transparent;
    color: var(--primary);
    font-weight: 650;
    padding: 0 8px;
}

.preview-stage {
    align-items: center;
    border: 2px solid transparent;
    display: flex;
    flex: 1 1 auto;
    justify-content: center;
    min-height: 280px;
    min-width: 0;
    overflow: hidden;
    padding: 24px;
    position: relative;
}

.preview-frame {
    background: #ffffff;
    box-shadow: 0 10px 30px rgba(28, 42, 50, 0.16);
    flex: 0 0 auto;
    max-height: 100%;
    max-width: 100%;
    overflow: hidden;
    position: relative;
}

.canvas-surface,
.photos-layer,
.pattern-overlay {
    inset: 0;
    position: absolute;
}

.canvas-surface {
    overflow: hidden;
}

.pattern-overlay {
    background-repeat: repeat;
    pointer-events: none;
}

.photos-layer {
    z-index: 2;
}

.photo-cell {
    cursor: grab;
    overflow: hidden;
    position: absolute;
    touch-action: none;
}

.photo-cell:active {
    cursor: grabbing;
}

.photo-cell.is-selected {
    box-shadow: inset 0 0 0 3px var(--primary), inset 0 0 0 4px rgba(255, 255, 255, 0.85);
    z-index: 2;
}

.photo-cell img {
    display: block;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
    user-select: none;
    width: 100%;
}

.empty-state {
    align-items: center;
    background: transparent;
    color: var(--muted);
    display: flex;
    flex-direction: column;
    inset: 0;
    justify-content: center;
    margin: auto;
    position: absolute;
    width: 100%;
    z-index: 3;
}

.empty-state span {
    color: var(--primary);
    font-size: 34px;
    font-weight: 300;
    line-height: 1;
}

.empty-state strong {
    font-size: 13px;
    margin-top: 8px;
}

.photo-toolbar {
    align-items: center;
    background: var(--surface);
    border-top: 1px solid var(--line);
    display: flex;
    flex: 0 0 auto;
    justify-content: space-between;
    min-height: 54px;
    padding: 8px 16px;
}

.selected-photo-name {
    font-weight: 650;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.photo-toolbar-actions {
    display: flex;
    flex: 0 0 auto;
    gap: 6px;
    margin-left: 10px;
}

.settings-heading {
    margin-bottom: 4px;
}

.setting-section {
    border-top: 1px solid var(--line);
    padding: 16px 0;
}

.setting-section h3 {
    font-size: 13px;
    margin: 0 0 12px;
}

.field-label {
    color: var(--muted);
    display: block;
    font-size: 11px;
    font-weight: 650;
    margin-bottom: 5px;
}

.field-control {
    background: var(--surface);
    border: 1px solid var(--line-strong);
    border-radius: 5px;
    height: 40px;
    min-width: 0;
    padding: 0 10px;
    width: 100%;
}

select.field-control {
    padding-right: 28px;
}

.field-row {
    display: flex;
    gap: 10px;
    margin-top: 12px;
}

.field-row.align-end {
    align-items: flex-end;
}

.field-grow {
    flex: 1 1 0;
    min-width: 0;
}

.dpi-field {
    flex: 0 0 78px;
    min-width: 0;
}

.segmented {
    background: var(--surface-subtle);
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    display: grid;
    grid-auto-columns: 1fr;
    grid-auto-flow: column;
    overflow: hidden;
}

.segmented button {
    background: transparent;
    border-left: 1px solid var(--line);
    min-height: 38px;
    padding: 0 10px;
}

.segmented button:first-child {
    border-left: 0;
}

.segmented button.is-active {
    background: var(--surface);
    box-shadow: inset 0 -2px 0 var(--primary);
    color: var(--primary);
    font-weight: 700;
}

.segmented.compact button {
    min-height: 32px;
    padding: 0 11px;
}

.output-readout {
    background: var(--surface-subtle);
    border-radius: 4px;
    color: var(--muted);
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    margin-top: 10px;
    padding: 7px 9px;
}

.slider-field {
    display: block;
    margin-top: 12px;
}

.slider-field > span {
    align-items: center;
    color: var(--muted);
    display: flex;
    font-size: 11px;
    font-weight: 650;
    justify-content: space-between;
    margin-bottom: 5px;
}

.slider-field output {
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

input[type="range"] {
    accent-color: var(--primary);
    display: block;
    margin: 0;
    width: 100%;
}

.color-row {
    align-items: flex-end;
}

.color-control {
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--line-strong);
    border-radius: 5px;
    display: flex;
    height: 40px;
    overflow: hidden;
    padding: 4px 9px 4px 4px;
}

.color-control input[type="color"] {
    background: transparent;
    border: 0;
    height: 30px;
    margin-right: 7px;
    padding: 0;
    width: 34px;
}

.color-control span {
    font-size: 11px;
    font-variant-numeric: tabular-nums;
}

.button-field {
    overflow: hidden;
    text-align: left;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.export-section {
    padding-bottom: max(20px, env(safe-area-inset-bottom));
}

.export-actions {
    display: grid;
    gap: 8px;
    grid-template-columns: 1fr 1fr;
    margin-top: 14px;
}

.modal {
    inset: 0;
    position: fixed;
    z-index: 100;
}

.modal-backdrop {
    background: rgba(12, 21, 26, 0.58);
    inset: 0;
    position: absolute;
}

.modal-dialog {
    background: var(--surface);
    border-radius: 8px;
    left: 50%;
    max-height: min(760px, calc(100dvh - 32px));
    max-width: calc(100vw - 32px);
    overflow-y: auto;
    padding: 20px;
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 520px;
}

.modal-dialog.large {
    width: 880px;
}

.modal-header {
    margin-bottom: 14px;
}

.icon-button {
    align-items: center;
    background: transparent;
    border-radius: 4px;
    color: var(--muted);
    display: inline-flex;
    font-size: 26px;
    justify-content: center;
    line-height: 1;
    min-height: 40px;
    width: 40px;
}

.icon-button:hover {
    background: var(--surface-subtle);
    color: var(--text);
}

.search-control {
    margin-bottom: 14px;
}

.layout-grid {
    display: grid;
    gap: 10px;
    grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
}

.layout-option {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
    min-height: 138px;
    padding: 8px;
    text-align: left;
}

.layout-option:hover,
.layout-option.is-active {
    background: var(--primary-soft);
    border-color: var(--primary);
}

.layout-miniature {
    aspect-ratio: 1;
    background: #e7ecef;
    border-radius: 4px;
    display: block;
    overflow: hidden;
    position: relative;
    width: 100%;
}

.layout-mini-cell {
    background: #8c9aa2;
    border: 1px solid #e7ecef;
    display: block;
    position: absolute;
}

.layout-mini-cell:nth-child(4n + 2) {
    background: #6e99c7;
}

.layout-mini-cell:nth-child(4n + 3) {
    background: #79a991;
}

.layout-mini-cell:nth-child(4n) {
    background: #d09b62;
}

.layout-mini-cell.is-polygon {
    border: 0;
    transform: scale(0.985);
}

.layout-option.is-active .layout-miniature {
    box-shadow: inset 0 0 0 2px var(--primary);
}

.layout-option-name {
    display: block;
    font-size: 11px;
    font-weight: 650;
    margin-top: 7px;
    overflow: hidden;
    text-align: center;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.pattern-option {
    aspect-ratio: 1;
    background-color: #eef1f2;
    background-repeat: repeat;
    border: 2px solid transparent;
    border-radius: 6px;
    min-height: 62px;
    padding: 0;
    position: relative;
}

.pattern-option:hover,
.pattern-option.is-active {
    border-color: var(--primary);
}

.pattern-option.none::before {
    background: var(--danger);
    content: "";
    height: 2px;
    left: 12%;
    position: absolute;
    top: 50%;
    transform: rotate(-45deg);
    width: 76%;
}

.pattern-controls {
    border-top: 1px solid var(--line);
    display: grid;
    gap: 12px;
    grid-template-columns: 1fr 1fr 1fr;
    margin-top: 16px;
    padding-top: 14px;
}

.pattern-controls .slider-field {
    margin-top: 0;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 18px;
}

.busy-overlay {
    align-items: center;
    background: rgba(255, 255, 255, 0.88);
    display: flex;
    flex-direction: column;
    inset: 0;
    justify-content: center;
    position: fixed;
    z-index: 150;
}

.spinner {
    animation: spin 800ms linear infinite;
    border: 3px solid var(--line);
    border-radius: 50%;
    border-top-color: var(--primary);
    height: 34px;
    margin-bottom: 12px;
    width: 34px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.toast {
    background: var(--text);
    border-radius: 6px;
    bottom: max(18px, env(safe-area-inset-bottom));
    color: #ffffff;
    left: 50%;
    max-width: calc(100vw - 32px);
    opacity: 0;
    padding: 10px 14px;
    pointer-events: none;
    position: fixed;
    transform: translate(-50%, 12px);
    transition: opacity 160ms ease, transform 160ms ease;
    z-index: 200;
}

.toast.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

.toast.is-error {
    background: var(--danger);
}

@media (max-width: 1160px) {
    .app-shell {
        grid-template-areas:
            "workspace workspace"
            "library settings";
        grid-template-columns: minmax(280px, 0.9fr) minmax(320px, 1.1fr);
        height: auto;
        min-height: 100dvh;
    }

    .workspace-panel {
        border-bottom: 1px solid var(--line);
        min-height: 580px;
    }

    .preview-stage {
        height: 56dvh;
        min-height: 420px;
    }

    .library-panel,
    .settings-panel {
        border-left: 0;
        border-right: 0;
        overflow: visible;
    }

    .library-panel {
        border-right: 1px solid var(--line);
    }

    .photo-list {
        grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
    }
}

@media (max-width: 720px) {
    :root {
        --header-height: 54px;
    }

    .app-header {
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }

    .brand img {
        height: 27px;
        max-width: 112px;
    }

    .brand-divider {
        margin: 0 8px;
    }

    .app-shell {
        display: flex;
        flex-direction: column;
        padding-bottom: calc(68px + env(safe-area-inset-bottom));
    }

    .workspace-panel {
        min-height: 430px;
        order: 1;
    }

    .library-panel {
        border-bottom: 1px solid var(--line);
        border-right: 0;
        order: 2;
    }

    .settings-panel {
        order: 3;
    }

    .app-panel {
        overflow: visible;
        padding: 16px;
    }

    .workspace-toolbar {
        min-height: 48px;
        padding: 6px 12px;
    }

    .preview-stage {
        flex: 0 0 auto;
        height: min(52dvh, 470px);
        min-height: 320px;
        padding: 14px;
    }

    .photo-toolbar {
        align-items: stretch;
        flex-direction: column;
        gap: 7px;
        padding: 8px 12px;
    }

    .photo-toolbar-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        margin-left: 0;
    }

    .photo-list {
        grid-template-columns: 1fr;
    }

    .export-actions {
        background: rgba(255, 255, 255, 0.97);
        border-top: 1px solid var(--line);
        bottom: 0;
        gap: 8px;
        left: 0;
        margin: 0;
        padding: 10px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
        position: fixed;
        right: 0;
        z-index: 25;
    }

    .modal-dialog,
    .modal-dialog.large {
        border-radius: 8px 8px 0 0;
        bottom: 0;
        left: 0;
        max-height: calc(100dvh - 28px);
        max-width: 100%;
        padding: 16px;
        top: auto;
        transform: none;
        width: 100%;
    }

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

    .layout-option {
        min-height: 116px;
        padding: 6px;
    }

    .pattern-grid {
        grid-template-columns: repeat(5, 1fr);
    }

    .pattern-option {
        min-height: 50px;
    }

    .pattern-controls {
        grid-template-columns: 1fr;
    }

    .toast {
        bottom: calc(76px + env(safe-area-inset-bottom));
    }
}

@media (max-width: 390px) {
    .brand-product,
    .brand-divider {
        display: none;
    }

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

    .field-row {
        gap: 8px;
    }
}

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