* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    overflow: hidden;
    width: 100%;
    height: 100%;
    height: 100dvh;
    background: #1a1614;
    font-family: "Noto Sans TC", "Microsoft JhengHei", "Heiti TC", sans-serif;
    color: #f3ecd4;
    user-select: none;
    -webkit-user-select: none;
}

#shell {
    touch-action: none;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    height: 100dvh;
}

#hud {
    flex: 0 0 auto;
    min-height: 52px;
    display: flex;
    align-items: center;
    gap: 18px;
    padding: 8px 16px;
    z-index: 6;
    background: rgba(0, 0, 0, 0.42);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.hud-brand {
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-weight: 700;
    font-size: 18px;
    letter-spacing: 0.16em;
    flex-shrink: 0;
}

.hud-stats {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 16px 22px;
    flex: 1;
    font-size: 15px;
}

.hud-stats b {
    font-weight: 700;
    margin-left: 6px;
}

.hud-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.touch-hint {
    display: none;
}

.hud-actions button {
    flex-shrink: 0;
    pointer-events: auto;
    border: 1px solid rgba(243, 236, 212, 0.35);
    background: rgba(26, 22, 20, 0.55);
    color: #f3ecd4;
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 0.16em;
    padding: 7px 12px;
    border-radius: 10px;
    cursor: pointer;
}

.save-notice {
    font-size: 13px;
    line-height: 1.6;
    margin-bottom: 16px;
}

.save-notice p {
    margin: 0 0 8px;
}

.save-notice button {
    font: inherit;
    color: inherit;
    background: transparent;
    border: 1px solid currentColor;
    border-radius: 8px;
    padding: 6px 12px;
    cursor: pointer;
}

#album-btn span {
    letter-spacing: 0;
    opacity: 0.72;
    margin-left: 4px;
}

#playfield {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: row;
    background:
        radial-gradient(circle at 50% 36%, rgba(61, 109, 140, 0.22), transparent 52%),
        #1a1614;
}

#game-container {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
}

#game-canvas {
    position: absolute;
    display: block;
    image-rendering: pixelated;
    box-shadow: 0 0 36px rgba(26, 22, 20, 0.55);
}

#poem-stack {
    position: relative;
    flex: 0 0 clamp(176px, 26vw, 280px);
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 12px 12px 0;
    pointer-events: none;
}

#poem-panel {
    position: relative;
    flex: 0 0 auto;
    color: white;
    display: flex;
    flex-direction: row-reverse;
    align-items: flex-start;
    justify-content: center;
    gap: 10px;
    padding: 10px 12px;
}

.poem-puzzle {
    --cols: 2;
    --rows: 5;
    --card-ratio: 3 / 4;
    container-type: size;
    position: relative;
    flex: 1 1 0;
    min-height: 0;
    width: 100%;
    display: grid;
    place-items: center;
    padding: 8px;
    border-radius: 12px;
    background: rgba(26, 22, 20, 0.82);
    border: 1px solid rgba(243, 236, 212, 0.28);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
    transition: opacity 0.28s ease, transform 0.28s ease;
}

.poem-puzzle.is-away {
    opacity: 0;
}

.poem-puzzle.is-complete {
    box-shadow: 0 0 0 1px rgba(255, 214, 120, 0.5), 0 12px 32px rgba(0, 0, 0, 0.45);
}

.puzzle-grid {
    display: grid;
    aspect-ratio: var(--card-ratio);
    width: min(100cqi, calc(100cqh * 3 / 4));
    height: auto;
    max-width: 100%;
    max-height: 100%;
    grid-template-columns: repeat(var(--cols), 1fr);
    grid-template-rows: repeat(var(--rows), 1fr);
    grid-auto-flow: column;
    gap: 3px;
    transition: gap 0.4s ease;
}

.poem-puzzle.is-complete .puzzle-grid {
    gap: 0;
}

.puzzle-piece {
    position: relative;
    overflow: hidden;
    border-radius: 2px;
    background:
        linear-gradient(180deg, rgba(255, 246, 214, 0.08), rgba(255, 246, 214, 0.02)),
        rgba(8, 6, 4, 0.55);
    box-shadow: inset 0 0 0 1px rgba(255, 246, 214, 0.2);
}

.puzzle-piece img {
    position: absolute;
    width: calc(var(--cols) * 100%);
    height: calc(var(--rows) * 100%);
    max-width: none;
    left: calc(var(--c) * -100%);
    top: calc(var(--r) * -100%);
    object-fit: cover;
    object-position: center;
    opacity: 0;
    transform: scale(1.06);
    transition: opacity 0.35s ease, transform 0.35s ease;
}

.puzzle-piece.is-on img,
.poem-puzzle.is-complete .puzzle-piece img {
    opacity: 1;
    transform: scale(1);
}

.puzzle-piece.is-blank {
    opacity: 0.35;
}

.panel {
    background: rgba(26, 22, 20, 0.72);
    border: 1px solid rgba(243, 236, 212, 0.22);
    padding: 12px 16px;
    border-radius: 12px;
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 16px;
    font-size: 15px;
    margin: 5px 0;
}

.row[hidden] {
    display: none !important;
}

.label {
    opacity: 0.72;
    letter-spacing: 0.5px;
    flex-shrink: 0;
}

#poem-title {
    writing-mode: vertical-rl;
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-weight: 700;
    font-size: 14px;
    letter-spacing: 0.18em;
    line-height: 1.35;
    opacity: 0.9;
}

.poem-board {
    display: flex;
    flex-direction: row-reverse;
    gap: 8px;
}

.poem-col {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.poem-slot {
    width: clamp(22px, 4.6vw, 30px);
    height: clamp(22px, 4.6vw, 30px);
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-weight: 700;
    font-size: clamp(15px, 3.4vw, 20px);
    border-radius: 4px;
}

.poem-slot.empty {
    border: 1px solid rgba(255, 255, 255, 0.22);
    background: rgba(255, 255, 255, 0.04);
}

.poem-slot.filled {
    border: 1px solid transparent;
    color: #f0c53a;
}

#lives {
    letter-spacing: 2px;
    color: #d64b3a;
}

#start-screen {
    touch-action: none;
    position: fixed;
    inset: 0;
    background: radial-gradient(circle at 50% 38%, rgba(61, 109, 140, 0.32), rgba(26, 22, 20, 0.9));
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    z-index: 10;
    text-align: center;
    padding: 24px 20px;
}

#start-screen h1 {
    margin: 0 0 8px;
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-size: clamp(32px, 6vw, 52px);
    font-weight: 700;
    letter-spacing: 0.08em;
}

.subtitle {
    margin: 0 0 18px;
    opacity: 0.88;
    font-size: 16px;
}

.howto {
    margin: 0 0 18px;
    padding: 0;
    list-style: none;
    max-width: 420px;
    text-align: left;
    opacity: 0.9;
    line-height: 1.7;
}

.howto li + li {
    margin-top: 4px;
}

#start-tip {
    margin: 0 0 20px;
    opacity: 0.75;
    font-size: 14px;
}

#start-btn {
    padding: 14px 36px;
    font-size: 18px;
    cursor: pointer;
    background: #f0c53a;
    border: 1px solid #1a1614;
    border-radius: 4px;
    color: #1a1614;
    font-weight: 700;
    font-family: inherit;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
}

#start-btn:hover {
    filter: brightness(1.06);
}

#message-banner {
    position: fixed;
    top: 64px;
    left: 50%;
    max-width: min(340px, calc(100vw - 24px));
    padding: 10px 14px;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.18);
    box-shadow: 0 10px 26px rgba(0, 0, 0, 0.35);
    opacity: 0;
    transform: translate(-50%, -6px);
    transition: opacity 0.25s ease, transform 0.25s ease;
    pointer-events: none;
    z-index: 7;
}

#message-banner.show {
    opacity: 1;
    transform: translate(-50%, 0);
}

.overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(6, 4, 2, 0.72);
    align-items: center;
    justify-content: center;
    padding: 24px 16px;
    pointer-events: auto;
}

.overlay.is-open {
    display: flex;
}

.album-sheet {
    touch-action: pan-y;
    overscroll-behavior: contain;
    width: min(920px, 100%);
    max-height: calc(100dvh - 48px);
    overflow: auto;
    background: #f3ecd4;
    color: #1a1614;
    border-radius: 16px;
    padding: 20px 22px 28px;
    box-shadow: 0 18px 48px rgba(0, 0, 0, 0.4);
}

.album-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}

.album-head h2 {
    margin: 0;
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-size: 22px;
    letter-spacing: 0.12em;
}

.album-head button,
#inspect-close {
    border: 1px solid rgba(42, 33, 24, 0.2);
    background: transparent;
    color: inherit;
    font-family: inherit;
    padding: 6px 12px;
    border-radius: 8px;
    cursor: pointer;
}

.album-section + .album-section {
    margin-top: 22px;
}

.album-section h3 {
    margin: 0 0 10px;
    font-family: "Noto Serif TC", "Songti TC", serif;
    font-size: 16px;
    letter-spacing: 0.14em;
}

.album-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.album-slot {
    width: 92px;
    padding: 0;
    border: 1px solid rgba(42, 33, 24, 0.16);
    background: #e8e0c8;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    color: #2a2118;
    font-family: "Noto Serif TC", "Songti TC", serif;
}

.album-slot img {
    display: block;
    width: 100%;
    aspect-ratio: 3 / 4;
    object-fit: cover;
}

.album-slot span {
    display: block;
    padding: 5px 6px 7px;
    font-size: 12px;
    letter-spacing: 0.04em;
}

.album-slot.is-empty {
    aspect-ratio: 3 / 4;
    width: 72px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: default;
    border-style: dashed;
    opacity: 0.55;
}

.album-slot.is-empty span {
    font-size: 22px;
}

@media (max-width: 700px) {
    #hud {
        flex-wrap: wrap;
        min-height: 46px;
        padding: 6px 10px;
        gap: 10px;
    }
    .hud-brand {
        font-size: 16px;
    }
    .hud-stats {
        order: 2;
        flex-basis: 100%;
        gap: 8px 12px;
        font-size: 13px;
    }
    .hud-actions {
        margin-left: auto;
    }
    .hud-actions button {
        padding: 7px 8px;
        letter-spacing: 0;
    }
    #playfield {
        flex-direction: column;
    }
    #poem-stack {
        flex: 0 0 auto;
        width: 100%;
        max-height: 36vh;
        padding: 8px 10px 10px;
    }
    .poem-puzzle {
        min-height: 120px;
    }
    #poem-panel {
        padding: 8px 10px;
        gap: 8px;
    }
    #poem-title {
        font-size: 12px;
    }
    .panel {
        min-width: 0;
        padding: 10px 12px;
    }
}

@media (hover: none), (pointer: coarse) {
    #hud { flex-wrap: wrap; }
    .touch-hint {
        display: block;
        order: 3;
        flex-basis: 100%;
        font-size: 12px;
        opacity: 0.75;
    }
}
