* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --pri: #ffa500;
    --pri-h: #a16900;
    --bg: #F6DBB9;
    --sf: #ffffff;
    --sf-h: #f1f5f9;
    --tx: #1e293b;
    --tx2: #64748b;
    --bd: #e2e8f0;
    --r: 5px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: var(--bg);
    color: var(--tx);
    min-height: 100vh;
}

.hidden {
    display: none !important;
}

/* ── Header ── */
header {
    background: var(--sf);
    border-bottom: 1px solid var(--bd);
    padding: 14px 24px;
    text-align: center;
}

.header-top {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

header h1 {
    font-size: 22px;
    font-weight: 700;
}

header .subtitle {
    color: var(--tx2);
    font-size: 13px;
    margin-top: 2px;
}

/* ── Layout ── */
main {
    display: flex;
    max-width: 1440px;
    margin: 0 auto;
    padding: 16px;
    gap: 16px;
    height: calc(100vh - 16px);
}

/* ── Sidebar ── */
.controls {
    width: 270px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
}

/* ── 右侧 ── */
.controls-right {
    width: 270px;
}

.pattern-area {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

/* 手机端：仍上下排列 */
@media (max-width: 900px) {
    main {
        flex-direction: column;
        height: auto;
    }

    .controls,
    .controls-right {
        width: 100%;
    }

    .pattern-container {
        min-height: 360px;
    }
}

.controls section {
    background: var(--sf);
    border-radius: var(--r);
    border: 1px solid var(--bd);
    padding: 14px;
}

.controls h3 {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 10px;
    color: var(--tx2);
    text-transform: uppercase;
    letter-spacing: .5px;
}

/* ── Drop zone ── */
.drop-zone {
    border: 2px dashed var(--bd);
    border-radius: var(--r);
    padding: 28px 12px;
    text-align: center;
    cursor: pointer;
    transition: .2s;
    color: var(--tx2);
    font-size: 13px;
    line-height: 1.7;
}

.drop-zone:hover,
.drop-zone.dragover {
    border-color: var(--pri);
    background: rgba(99, 102, 241, .04);
    color: var(--pri);
}

/* ── Preview ── */
.preview-container {
    margin-top: 10px;
    border-radius: var(--r);
    overflow: hidden;
    border: 1px solid var(--bd);
}

.preview-container img {
    width: 100%;
    display: block;
}

.restore-btn {
    margin: 6px 6px 0;
    width: calc(100% - 12px);
    font-size: 12px;
    padding: 7px 10px;
}

.restore-status {
    margin: 6px;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 12px;
    line-height: 1.5;
}

.restore-status-info {
    background: #eff6ff;
    color: #1d4ed8;
    border: 1px solid #bfdbfe;
}

.restore-status-success {
    background: #f0fdf4;
    color: #166534;
    border: 1px solid #bbf7d0;
}

.restore-status-error {
    background: #fef2f2;
    color: #b91c1c;
    border: 1px solid #fecaca;
}

.restore-panel-status {
    margin: 0 20px 16px;
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 12px;
    line-height: 1.6;
}

.restore-panel-status-info {
    background: #eff6ff;
    color: #1d4ed8;
    border: 1px solid #bfdbfe;
}

.restore-panel-status-success {
    background: #f0fdf4;
    color: #166534;
    border: 1px solid #bbf7d0;
}

.restore-panel-status-error {
    background: #fef2f2;
    color: #b91c1c;
    border: 1px solid #fecaca;
}

/* ── Form controls ── */
.control-group {
    margin-bottom: 10px;
}

.control-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 3px;
}

.control-group small {
    font-size: 11px;
    color: var(--tx2);
}

.control-group select,
.control-group input[type="range"] {
    width: 100%;
}

select {
    padding: 5px 8px;
    border: 1px solid var(--bd);
    border-radius: 4px;
    font-size: 13px;
    background: var(--sf);
}

input[type="range"] {
    margin-top: 3px;
}

.control-group label input[type="checkbox"] {
    margin-right: 5px;
}

/* ── Buttons ── */
.btn {
    width: 100%;
    padding: 9px 14px;
    border: none;
    border-radius: var(--r);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: .2s;
    margin-bottom: 6px;
}

.btn:disabled {
    opacity: .45;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--pri);
    color: #fff;
}

.btn-primary:hover:not(:disabled) {
    background: var(--pri-h);
}

.btn-secondary {
    background: var(--sf);
    color: var(--tx);
    border: 1px solid var(--bd);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--sf-h);
}

.btn-sm {
    padding: 4px 12px;
    font-size: 12px;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--pri);
    color: var(--pri);
    border-radius: 6px;
    cursor: pointer;
    transition: .2s;
}

.btn-outline:hover {
    background: var(--pri);
    color: #fff;
}

/* ── Stats ── */
.stats-section .stat-item {
    display: flex;
    justify-content: space-between;
    padding: 5px 0;
    font-size: 13px;
    border-bottom: 1px solid var(--bd);
}

.stats-section .stat-item:last-child {
    border-bottom: none;
}

.stat-value {
    font-weight: 600;
}

/* ── Legend ── */
.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    font-size: 13px;
}

.legend-color {
    width: 18px;
    height: 18px;
    border-radius: 4px;
    border: 1px solid var(--bd);
    flex-shrink: 0;
}

.legend-code {
    font-weight: 600;
    min-width: 36px;
}

.legend-count {
    color: var(--tx2);
    margin-left: auto;
}

/* ── Pattern area ── */
.pattern-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-width: 0;
    overflow-y: auto;
    min-height: 0;
}

.pattern-container {
    flex: 1;
    position: relative;
    background: var(--sf);
    border: 1px solid var(--bd);
    border-radius: var(--r);
    overflow: hidden;
    min-height: 320px;
}

.pattern-container.has-pattern {
    background: #fafafa;
}

#patternCanvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: grab;
}

#patternCanvas.canvas-edit-mode {
    cursor: crosshair;
}

.empty-state {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--tx2);
    font-size: 14px;
    pointer-events: none;
    padding: 24px;
    text-align: center;
}

.pattern-toolbar {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    gap: 6px;
    z-index: 5;
}

.pattern-toolbar .btn {
    width: auto;
    margin: 0;
    padding: 6px 10px;
    font-size: 12px;
}

/* ── Crop ── */
.crop-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, .25);
    cursor: crosshair;
    z-index: 10;
}

.crop-box {
    position: absolute;
    border: 2px solid var(--pri);
    background: rgba(99, 102, 241, .12);
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, .35);
}

.crop-actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.crop-actions .btn {
    margin: 0;
}

/* ── Tooltip ── */
.bead-tooltip {
    position: fixed;
    z-index: 1000;
    background: rgba(15, 23, 42, .92);
    color: #fff;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 12px;
    line-height: 1.5;
    pointer-events: none;
    max-width: 200px;
}

.bead-tooltip .tip-swatch {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 3px;
    margin-right: 6px;
    vertical-align: -1px;
    border: 1px solid rgba(255, 255, 255, .35);
}

/* ── Edit mode ── */
.edit-mode-hint {
    position: absolute;
    left: 12px;
    bottom: 12px;
    background: rgba(15, 23, 42, .85);
    color: #fff;
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 12px;
    z-index: 6;
}

.bead-picker {
    position: fixed;
    z-index: 1100;
    width: 260px;
    max-height: 360px;
    background: #fff;
    border: 1px solid var(--bd);
    border-radius: 12px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, .18);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.bead-picker-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-bottom: 1px solid var(--bd);
    font-size: 13px;
    font-weight: 600;
}

.bead-picker-header button {
    border: none;
    background: none;
    font-size: 16px;
    cursor: pointer;
    color: var(--tx2);
}

.bead-picker-search {
    padding: 8px 12px;
    border-bottom: 1px solid var(--bd);
}

.bead-picker-search input {
    width: 100%;
    padding: 7px 10px;
    border: 1px solid var(--bd);
    border-radius: 8px;
    font-size: 13px;
}

.bead-picker-colors {
    padding: 10px;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 6px;
}

.bp-swatch {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 6px;
    border: 2px solid transparent;
    cursor: pointer;
}

.bp-swatch:hover {
    transform: scale(1.08);
}

.bp-swatch.bp-active {
    border-color: var(--pri);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, .25);
}

/* ── Compare overlay ── */
.compare-overlay {
    position: absolute;
    inset: 0;
    z-index: 4;
    pointer-events: none;
}

.compare-overlay img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    opacity: .55;
}

.compare-slider-wrap {
    position: absolute;
    left: 12px;
    bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, .92);
    border: 1px solid var(--bd);
    border-radius: 8px;
    padding: 6px 10px;
    pointer-events: auto;
    font-size: 12px;
}

.compare-slider-wrap input[type="range"] {
    width: 120px;
}

/* ── Fullscreen ── */
.pattern-container.fake-fullscreen {
    position: fixed;
    inset: 0;
    z-index: 999;
    border-radius: 0;
    border: none;
}

/* ── Pixel restore modal ── */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 16px;
}

.modal-box {
    background: #fff;
    border-radius: 14px;
    width: min(920px, 96vw);
    max-height: 90vh;
    overflow: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
    position: relative;
}

.modal-close {
    position: absolute;
    top: 10px;
    right: 14px;
    border: none;
    background: none;
    font-size: 24px;
    cursor: pointer;
    color: var(--tx2);
    line-height: 1;
}

.restore-modal-body {
    padding: 20px;
}

.restore-preview-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.restore-preview-col {
    border: 1px solid var(--bd);
    border-radius: 10px;
    overflow: hidden;
    background: #fafafa;
}

.restore-preview-col h4 {
    font-size: 13px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--bd);
    background: #fff;
}

.restore-preview-col img,
.restore-preview-col canvas {
    width: 100%;
    display: block;
    image-rendering: pixelated;
    background: #fff;
}

.restore-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    margin: 14px 0;
}

.restore-controls button.active {
    background: var(--pri);
    color: #fff;
    border-color: var(--pri);
}

/* ── Responsive ── */
@media (max-width: 800px) {
    main {
        flex-direction: column;
        height: auto;
        min-height: calc(100vh - 80px);
    }

    .controls {
        width: 100%;
        max-height: none;
    }

    .pattern-area {
        min-height: 50vh;
    }

    .pattern-container {
        min-height: 360px;
    }

    .restore-preview-row {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    header {
        padding: 12px 14px;
    }

    header h1 {
        font-size: 18px;
    }

    main {
        padding: 10px;
        gap: 10px;
    }

    .controls section {
        padding: 12px;
    }

    .bead-picker {
        width: min(280px, 92vw);
    }
}