:root {
    --ink: #f7f4e7;
    --muted: rgba(247, 244, 231, .68);
    --panel: rgba(5, 14, 18, .72);
    --line: rgba(247, 244, 231, .16);
    --coin: #f4c95d;
    --coin-dark: #8d641d;
    --water: #42b7c7;
    --jade: #54d3a0;
    --red: #df6258;
    --violet: #a98cff;
    --shadow: rgba(0, 0, 0, .44);
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    min-height: 100%;
    margin: 0;
    overflow: hidden;
    background: #061016;
    color: var(--ink);
    font-family: Inter, "Noto Sans TC", "Microsoft JhengHei", Arial, sans-serif;
    letter-spacing: 0;
}

button {
    border: 0;
    color: inherit;
    font: inherit;
}

.coin-game {
    position: relative;
    width: 100vw;
    height: 100dvh;
    min-height: 560px;
    overflow: hidden;
    isolation: isolate;
    background: #061016;
    touch-action: none;
}

#gameCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    background: #061016;
    touch-action: none;
}

.scene-shade {
    position: absolute;
    inset: 0;
    z-index: 1;
    pointer-events: none;
    background:
        linear-gradient(to bottom, rgba(0, 0, 0, .38), transparent 30%, transparent 66%, rgba(0, 0, 0, .58)),
        linear-gradient(90deg, rgba(0, 0, 0, .38), transparent 24%, transparent 76%, rgba(0, 0, 0, .4));
}

.hud {
    position: absolute;
    top: max(14px, env(safe-area-inset-top));
    left: 14px;
    z-index: 4;
    display: flex;
    gap: 8px;
    pointer-events: none;
}

.status-chip,
.result-card,
.play-panel,
.history-strip {
    border: 1px solid var(--line);
    background: var(--panel);
    box-shadow: 0 16px 38px var(--shadow);
    backdrop-filter: blur(12px);
}

.status-chip {
    min-width: 96px;
    padding: 9px 10px 8px;
    border-radius: 8px;
}

.status-chip span {
    display: block;
    color: var(--muted);
    font-size: 10px;
    font-weight: 800;
}

.status-chip strong {
    display: block;
    margin-top: 2px;
    font-size: 21px;
    line-height: 1;
}

.result-card {
    position: absolute;
    top: max(14px, env(safe-area-inset-top));
    right: 14px;
    z-index: 4;
    width: min(300px, calc(100vw - 32px));
    padding: 12px 14px;
    border-radius: 8px;
    transform-origin: top right;
    transition: border-color .2s ease, transform .2s ease;
    pointer-events: none;
}

.result-card.is-great {
    border-color: rgba(244, 201, 93, .7);
}

.result-card.is-good {
    border-color: rgba(84, 211, 160, .62);
}

.result-card.is-calm {
    border-color: rgba(66, 183, 199, .58);
}

.result-card.is-rough {
    border-color: rgba(223, 98, 88, .58);
}

.result-card.is-pop {
    transform: scale(1.025);
}

.result-card span,
.result-card small {
    display: block;
    color: var(--muted);
    font-size: 10px;
    font-weight: 800;
}

.result-card strong {
    display: block;
    margin: 4px 0;
    font-size: clamp(22px, 4vw, 34px);
    line-height: 1.08;
}

.history-strip {
    position: absolute;
    right: 14px;
    top: calc(max(14px, env(safe-area-inset-top)) + 104px);
    z-index: 4;
    padding: 9px;
    border-radius: 8px;
    pointer-events: none;
}

.history-dots {
    display: flex;
    gap: 7px;
}

.history-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: rgba(247, 244, 231, .2);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .24);
}

.history-dot.is-great {
    background: var(--coin);
}

.history-dot.is-good {
    background: var(--jade);
}

.history-dot.is-calm {
    background: var(--water);
}

.history-dot.is-rough {
    background: var(--red);
}

.orbit-controls {
    position: absolute;
    inset: 0;
    z-index: 4;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 max(14px, env(safe-area-inset-left)) 0 max(14px, env(safe-area-inset-right));
    pointer-events: none;
}

.orbit-button {
    position: relative;
    width: 62px;
    height: 58px;
    border: 0;
    background: transparent;
    color: transparent;
    cursor: pointer;
    font-size: 0;
    pointer-events: auto;
    filter: drop-shadow(0 18px 20px rgba(0, 0, 0, .46));
    transform-style: preserve-3d;
    transition: transform .14s ease, filter .14s ease;
    animation: orbitFloat 2.8s ease-in-out infinite;
}

.orbit-button::before,
.orbit-button::after {
    position: absolute;
    inset: 0;
    content: "";
    clip-path: inherit;
}

.orbit-button::before {
    z-index: 2;
    background:
        linear-gradient(145deg, rgba(255, 255, 255, .42), transparent 36%),
        linear-gradient(180deg, #6bffb2 0%, #31d27e 55%, #179b58 100%);
    box-shadow:
        inset -6px -8px 0 rgba(0, 71, 38, .22),
        inset 7px 7px 0 rgba(255, 255, 255, .2);
}

.orbit-button::after {
    z-index: 1;
    background: linear-gradient(180deg, #0d7f4a 0%, #065232 100%);
    opacity: .96;
}

.orbit-button-left,
.orbit-button-left::before,
.orbit-button-left::after {
    clip-path: polygon(100% 0, 0 50%, 100% 100%);
}

.orbit-button-right,
.orbit-button-right::before,
.orbit-button-right::after {
    clip-path: polygon(0 0, 100% 50%, 0 100%);
}

.orbit-button-left::after {
    transform: translate(8px, 8px);
}

.orbit-button-right::after {
    transform: translate(-8px, 8px);
}

.orbit-button:hover {
    filter: drop-shadow(0 20px 24px rgba(57, 217, 138, .3)) drop-shadow(0 18px 20px rgba(0, 0, 0, .42));
}

.orbit-button:hover::before {
    background:
        linear-gradient(145deg, rgba(255, 255, 255, .5), transparent 36%),
        linear-gradient(180deg, #83ffc1 0%, #45df8f 55%, #1aa763 100%);
}

.orbit-button:active {
    transform: translateY(5px) scale(.96);
    filter: drop-shadow(0 10px 12px rgba(0, 0, 0, .4));
}

@keyframes orbitFloat {
    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-5px);
    }
}

.play-panel {
    position: absolute;
    left: 50%;
    bottom: max(16px, env(safe-area-inset-bottom));
    z-index: 4;
    display: grid;
    grid-template-columns: 1fr 96px;
    gap: 10px;
    width: min(520px, calc(100vw - 28px));
    padding: 12px;
    border-radius: 8px;
    transform: translateX(-50%);
    pointer-events: none;
}

.gesture-readout {
    grid-column: 1;
    grid-row: 1;
    min-width: 0;
}

.gesture-readout span {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.gesture-readout strong {
    display: block;
    margin-top: 3px;
    overflow: hidden;
    color: var(--ink);
    font-size: 19px;
    line-height: 1.1;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.throw-meter {
    grid-column: 1 / -1;
    grid-row: 2;
    height: 7px;
    overflow: hidden;
    border-radius: 8px;
    background: rgba(255, 255, 255, .11);
}

.throw-meter i {
    display: block;
    width: 0%;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--water), var(--coin));
    transition: width .12s ease;
}

.reset-button {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    min-height: 46px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, .08);
    color: var(--ink);
    cursor: pointer;
    font-weight: 900;
    pointer-events: auto;
    transition: transform .14s ease, background .14s ease;
}

.reset-button:active {
    transform: translateY(1px);
}

.gesture-trace {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 3;
    width: 0;
    height: 6px;
    border-radius: 8px;
    background: linear-gradient(90deg, rgba(66, 183, 199, .1), rgba(244, 201, 93, .92));
    box-shadow: 0 0 18px rgba(244, 201, 93, .42);
    opacity: 0;
    pointer-events: none;
    transform-origin: left center;
    transition: opacity .14s ease;
}

.gesture-trace.is-active {
    opacity: 1;
}

.toast-layer {
    position: absolute;
    left: 50%;
    bottom: calc(max(16px, env(safe-area-inset-bottom)) + 96px);
    z-index: 5;
    display: grid;
    gap: 8px;
    width: min(420px, calc(100vw - 32px));
    transform: translateX(-50%);
    pointer-events: none;
}

.toast {
    justify-self: center;
    max-width: 100%;
    padding: 9px 12px;
    border: 1px solid rgba(247, 244, 231, .18);
    border-radius: 8px;
    background: rgba(5, 14, 18, .84);
    color: var(--ink);
    font-size: 13px;
    font-weight: 800;
    box-shadow: 0 16px 32px rgba(0, 0, 0, .38);
    animation: toastIn .18s ease-out both, toastOut .22s ease-in 1.95s forwards;
}

.loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 8;
    display: grid;
    place-items: center;
    gap: 12px;
    align-content: center;
    background: #061016;
    transition: opacity .3s ease, visibility .3s ease;
}

.loading-overlay.is-hidden {
    display: none;
    visibility: hidden;
    opacity: 0;
}

.loader-mark {
    width: 46px;
    height: 46px;
    border: 4px solid rgba(247, 244, 231, .16);
    border-top-color: var(--coin);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}

.loading-overlay p {
    margin: 0;
    color: var(--muted);
    font-size: 12px;
    font-weight: 900;
}

@keyframes spin {
    to {
        transform: rotate(1turn);
    }
}

@keyframes toastIn {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
}

@keyframes toastOut {
    to {
        opacity: 0;
        transform: translateY(-8px);
    }
}

@media (max-width: 760px) {
    .hud {
        right: 14px;
        width: calc(100vw - 28px);
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .status-chip {
        min-width: 0;
        padding: 8px 9px;
    }

    .result-card {
        top: calc(max(14px, env(safe-area-inset-top)) + 66px);
        left: 14px;
        right: auto;
        width: min(270px, calc(100vw - 28px));
    }

    .history-strip {
        top: calc(max(14px, env(safe-area-inset-top)) + 67px);
        left: min(242px, calc(100vw - 116px));
        right: auto;
    }

    .orbit-controls {
        align-items: flex-end;
        padding-bottom: calc(max(16px, env(safe-area-inset-bottom)) + 102px);
    }

    .orbit-button {
        width: 52px;
        height: 48px;
    }
}

@media (max-width: 520px) {
    .coin-game {
        min-height: 520px;
    }

    .status-chip span,
    .result-card span,
    .result-card small {
        font-size: 9px;
    }

    .status-chip strong {
        font-size: 18px;
    }

    .hud {
        width: min(362px, calc(100vw - 28px));
    }

    .result-card strong {
        font-size: 22px;
    }

    .play-panel,
    .toast-layer {
        left: 14px;
        width: min(362px, calc(100vw - 28px));
        transform: none;
    }

    .play-panel {
        grid-template-columns: 1fr 74px;
        padding: 10px;
    }

    .orbit-controls {
        display: block;
        padding: 0;
        pointer-events: none;
    }

    .orbit-button {
        position: absolute;
        bottom: calc(max(16px, env(safe-area-inset-bottom)) + 112px);
    }

    #rotateLeftButton {
        left: 14px;
    }

    #rotateRightButton {
        right: auto;
        left: calc(14px + min(362px, calc(100vw - 28px)) - 52px);
    }

    .gesture-readout strong {
        font-size: 17px;
    }

    .reset-button {
        min-height: 42px;
        font-size: 14px;
    }

    .history-strip {
        left: min(220px, calc(100vw - 104px));
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: .01ms !important;
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
    }
}

@media (prefers-reduced-motion: reduce) {
    .orbit-button {
        animation: none;
    }
}
