:root {
    --bg-top: #ead6bd;
    --bg-bottom: #f7efe4;
    --panel: rgba(251, 246, 238, 0.78);
    --panel-border: rgba(255, 255, 255, 0.62);
    --ink: #544237;
    --muted: #8f7d70;
    --shadow: 0 24px 70px rgba(112, 80, 53, 0.16);
    --radius-xl: 32px;
    --radius-lg: 24px;
    --ease: cubic-bezier(0.22, 1, 0.36, 1);
    font-family: "Trebuchet MS", "PingFang SC", "Microsoft YaHei", sans-serif;
    color: var(--ink);
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
}

body {
    overflow: hidden;
    background:
            radial-gradient(circle at 10% 8%, rgba(255, 255, 255, 0.8), transparent 28%),
            radial-gradient(circle at 84% 14%, rgba(255, 232, 201, 0.86), transparent 24%),
            linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
}

button {
    font: inherit;
}

canvas {
    display: block;
}

.glass {
    background: var(--panel);
    border: 1px solid var(--panel-border);
    box-shadow: var(--shadow);
    backdrop-filter: blur(18px);
}

.app-shell {
    height: 100vh;
    height: 100dvh;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
    border-radius: var(--radius-xl);
    flex-shrink: 0;
}

.glass-lite {
    background: rgba(255, 255, 255, 0.38);
    border: 1px solid rgba(255, 255, 255, 0.5);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.24);
}

.eyebrow,
.section-kicker {
    margin: 0;
    font-size: 11px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--muted);
}

h1 {
    margin: 4px 0 0;
    font-size: clamp(28px, 2.6vw, 40px);
    line-height: 1;
    font-family: "Georgia", "STKaiti", serif;
    font-weight: 400;
}

.topbar-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-shrink: 0;
}

.top-log {
    flex: 1;
    min-width: 280px;
    max-width: 760px;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px;
    padding: 10px 14px;
    border-radius: 22px;
}

.top-progress {
    min-width: 220px;
    width: 280px;
    display: grid;
    gap: 8px;
    padding: 10px 14px;
    border-radius: 22px;
    flex-shrink: 0;
}

.compact-meter {
    margin-top: 0;
}

.compact-meter span {
    font-size: 12px;
}

.score-pill {
    min-width: 116px;
    padding: 10px 14px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.6);
    text-align: right;
}

.score-pill span {
    display: block;
    font-size: 11px;
    color: var(--muted);
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.score-pill strong {
    display: block;
    margin-top: 2px;
    font-size: 28px;
}

.icon-btn {
    border: 0;
    border-radius: 999px;
    padding: 10px 14px;
    background: rgba(255, 255, 255, 0.6);
    color: var(--ink);
    cursor: pointer;
    transition: transform 160ms var(--ease), background 160ms ease;
}

.icon-btn.small {
    padding: 8px 12px;
    font-size: 13px;
}

.workspace {
    position: relative;
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 248px;
    gap: 12px;
}

.stage-panel,
.control-panel {
    border-radius: var(--radius-xl);
    min-height: 0;
}

.stage-panel {
    position: relative;
    padding: 14px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.stage-backdrop {
    position: absolute;
    inset: 0;
    background:
            radial-gradient(circle at 50% 16%, rgba(255, 255, 255, 0.7), transparent 28%),
            linear-gradient(180deg, rgba(255, 255, 255, 0.15), transparent 26%);
    pointer-events: none;
}

.board-frame {
    position: relative;
    z-index: 1;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    border-radius: 28px;
    background: linear-gradient(180deg, rgba(255, 252, 248, 0.9), rgba(244, 232, 220, 0.8));
    border: 1px solid rgba(255, 255, 255, 0.58);
}

.board-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
    flex-shrink: 0;
    flex-wrap: wrap;
}

.board-header strong,
.reference-head strong {
    display: block;
    margin-top: 3px;
    font-size: 18px;
}

.board-mode-switcher {
    display: inline-flex;
    gap: 8px;
    align-items: center;
    padding: 8px;
    border-radius: 18px;
    flex-shrink: 0;
    order: -1;
    margin: 0 auto;
}

.legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
}

.legend-chip {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 6px 9px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.68);
    color: var(--muted);
    font-size: 12px;
}

.legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.legend-dot.target { background: rgba(117, 96, 81, 0.24); }
.legend-dot.filled { background: linear-gradient(180deg, #ffd86b, #f29a5f); }
.legend-dot.wrong { background: linear-gradient(180deg, #eca29b, #d27363); }

.board-wrap {
    position: relative;
    flex: 1;
    min-height: 0;
    border-radius: 24px;
    background:
            linear-gradient(180deg, rgba(255, 255, 255, 0.26), rgba(255, 255, 255, 0.08)),
            linear-gradient(180deg, rgba(238, 221, 206, 0.58), rgba(243, 231, 219, 0.48));
    display: grid;
    overflow: auto;
}

.board-wrap.pan-ready {
    cursor: grab;
}

.board-wrap.panning {
    cursor: grabbing;
}

.board-wrap::before {
    content: "";
    position: absolute;
    inset: 16px;
    border: 1px dashed rgba(160, 128, 104, 0.18);
    border-radius: 22px;
    pointer-events: none;
}

#boardCanvas,
#referenceCanvas {
    image-rendering: pixelated;
}

.board-viewport {
    min-width: 100%;
    min-height: 100%;
    display: grid;
    place-items: center;
    padding: 12px;
}

#boardCanvas {
    position: relative;
    z-index: 1;
    border-radius: 18px;
    touch-action: none;
}

.board-wrap::-webkit-scrollbar {
    width: 0;
    height: 0;
}

.board-wrap {
    scrollbar-width: none;
}

.control-panel {
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    position: relative;
    font-size: 13px;
}

.card {
    padding: 10px;
    border-radius: var(--radius-lg);
    background: rgba(255, 255, 255, 0.42);
    border: 1px solid rgba(255, 255, 255, 0.45);
}

.size-grid,
.palette {
    display: grid;
    gap: 8px;
}

.size-grid {
    grid-template-columns: 1fr;
    margin-top: 8px;
}

.zoom-row {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 6px;
    align-items: center;
    margin-top: 6px;
}

.zoom-readout {
    min-width: 52px;
    text-align: center;
    color: var(--muted);
    font-size: 12px;
}

.full-btn {
    width: 100%;
    margin-top: 8px;
}

.size-btn,
.tool-btn,
.primary-btn,
.secondary-btn {
    border: 0;
    border-radius: 14px;
    cursor: pointer;
    transition: transform 160ms var(--ease), background 160ms ease, color 160ms ease, opacity 160ms ease;
    font-size: 13px;
}

.size-btn,
.tool-btn,
.secondary-btn {
    padding: 8px 9px;
    background: rgba(255, 255, 255, 0.56);
    color: var(--ink);
}

.size-btn.active,
.tool-btn.active {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(255, 225, 215, 0.94));
    color: #8c4f3d;
}

.palette {
    grid-template-columns: repeat(4, 1fr);
    margin-top: 0;
}

.color-chip {
    display: grid;
    gap: 4px;
    justify-items: center;
    padding: 7px 4px;
    border: 1px solid transparent;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 16px;
    cursor: pointer;
    font-size: 11px;
}

.palette-toggle {
    margin-top: 6px;
}

.palette-popover {
    position: fixed;
    left: 18px;
    right: 18px;
    top: 92px;
    width: auto;
    max-width: none;
    padding: 10px;
    border-radius: 16px;
    background: rgba(255, 249, 243, 0.96);
    border: 1px solid rgba(255, 255, 255, 0.72);
    box-shadow: 0 18px 34px rgba(105, 76, 53, 0.18);
    z-index: 8;
}

.palette {
    grid-template-columns: repeat(6, minmax(0, 1fr));
}

.color-chip.active {
    border-color: rgba(219, 145, 120, 0.6);
    background: rgba(255, 255, 255, 0.78);
}

.color-chip-code {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 22px;
    height: 18px;
    padding: 0 4px;
    border-radius: 999px;
    background: rgba(84, 66, 55, 0.1);
    color: var(--ink);
    font-size: 10px;
    line-height: 1;
}

.color-chip-name {
    text-align: center;
    line-height: 1.1;
}

.color-dot {
    width: 18px;
    height: 18px;
    border-radius: 50%;
}

.tool-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
    margin-top: 6px;
}

.info-line {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    font-size: 12px;
    color: var(--muted);
}

.info-line strong {
    color: var(--ink);
    text-align: right;
}

.meter {
    display: grid;
    gap: 6px;
    margin-top: 6px;
}

.meter span,
.hint-text,
.reference-copy {
    color: var(--muted);
    font-size: 12px;
}

.meter-track {
    height: 12px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.62);
    overflow: hidden;
}

.meter-track i {
    display: block;
    width: 0;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, #efbc77, #df7a61);
    transition: width 220ms ease;
}

.meter-track.warm i {
    background: linear-gradient(90deg, #ffc66c, #ed8e6f);
}

.actions-card {
    display: grid;
    gap: 6px;
}

.sticky-actions {
    margin-top: auto;
}

.primary-btn {
    padding: 9px 10px;
    background: linear-gradient(180deg, #d78268, #c9604b);
    color: #fff7f3;
}

.primary-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}

.floating-reference {
    position: absolute;
    left: 14px;
    top: 14px;
    width: min(320px, 32vw);
    padding: 12px;
    border-radius: 24px;
    z-index: 5;
    user-select: none;
}

.help-panel {
    position: absolute;
    left: 14px;
    bottom: 14px;
    width: min(360px, 34vw);
    padding: 12px;
    border-radius: 22px;
    z-index: 4;
}

.help-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 8px;
}

.help-head strong {
    display: block;
    margin-top: 3px;
    font-size: 16px;
}

.help-list {
    display: grid;
    gap: 6px;
}

.help-list p {
    margin: 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
}

.reference-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
    cursor: grab;
}

.reference-actions {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
}

.reference-head.dragging {
    cursor: grabbing;
}

#referenceCanvas {
    width: 100%;
    aspect-ratio: 1;
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.58);
}

.iron-overlay {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    gap: 18px;
    background: rgba(250, 243, 234, 0.72);
    backdrop-filter: blur(8px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 260ms ease;
}

.iron-overlay.active {
    opacity: 1;
}

.iron-sheet {
    width: min(72%, 380px);
    height: min(72%, 380px);
    border-radius: 28px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(255, 250, 243, 0.68));
    animation: iron-press 1400ms var(--ease) infinite alternate;
}

.iron-glow {
    position: absolute;
    width: 60%;
    height: 18px;
    border-radius: 999px;
    background: rgba(255, 197, 108, 0.42);
    filter: blur(16px);
    animation: iron-glow 1400ms ease infinite alternate;
}

.iron-overlay p {
    position: absolute;
    bottom: 54px;
    margin: 0;
    color: #8f6758;
    font-size: 18px;
}

.finish-card {
    position: absolute;
    right: 18px;
    bottom: 18px;
    width: min(300px, calc(100% - 36px));
    padding: 16px;
    border-radius: 22px;
    z-index: 3;
}

.finish-card strong {
    display: block;
    margin: 8px 0 10px;
    font-size: 22px;
}

.finish-card p {
    margin: 0 0 14px;
    color: var(--muted);
    line-height: 1.5;
}

@keyframes iron-press {
    from { transform: translateY(-8px) scale(1); }
    to { transform: translateY(8px) scale(1.02); }
}

@keyframes iron-glow {
    from { opacity: 0.35; transform: scaleX(0.92); }
    to { opacity: 0.68; transform: scaleX(1.08); }
}

@media (max-width: 1100px), (max-height: 780px) {
    .workspace {
        grid-template-columns: minmax(0, 1fr) 220px;
    }

    .floating-reference {
        width: min(280px, 34vw);
    }

    .help-panel {
        width: min(320px, 36vw);
    }

    .board-header {
        flex-direction: column;
    }

    .legend {
        justify-content: flex-start;
    }

    .top-log {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        max-width: 520px;
    }

    .top-progress {
        width: 240px;
    }
}

@media (max-width: 860px) {
    body {
        overflow: auto;
    }

    .app-shell {
        height: auto;
    }

    .workspace {
        grid-template-columns: 1fr;
    }

    .topbar {
        align-items: stretch;
        flex-wrap: wrap;
    }

    .top-log {
        order: 3;
        min-width: 100%;
        max-width: none;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .top-progress {
        order: 4;
        width: 100%;
    }

    .control-panel {
        order: 2;
    }

    .stage-panel {
        min-height: 68vh;
    }

    .floating-reference {
        position: relative;
        left: auto;
        top: auto;
        width: 100%;
        margin-bottom: 10px;
    }

    .help-panel {
        position: relative;
        left: auto;
        bottom: auto;
        width: 100%;
        margin-top: 10px;
    }

    .palette-popover {
        left: 12px;
        right: 12px;
        width: auto;
        max-width: none;
        top: 150px;
    }

    .palette {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}