* {
    box-sizing: border-box;
}

:root {
    --font-lg: 1.2em;
    --font-md: 0.9em;
    --font-sm: 0.75em;
    --font-xs: 0.65em;
    --line-height: 1.6;

    color-scheme: dark;
    --bg: rgba(26, 26, 26, 1);
    --surface: rgba(38, 38, 38, 1);
    --surface-2: rgba(51, 51, 51, 1);
    --border: rgba(255, 255, 255, 0.22);
    --text: rgba(204, 204, 204, 1);
    --text-2: rgba(170, 170, 170, 1);
    --text-3: rgba(119, 119, 119, 1);
    --accent: rgba(204, 204, 204, 1);
    --link: rgba(123, 166, 222, 1);
    --danger: rgba(224, 128, 128, 1);
    --shadow: rgba(0, 0, 0, 0.5);
    --backdrop: rgba(0, 0, 0, 0.5);

    --stage-bg: rgba(0, 0, 0, 1);
    --stage-scrim: rgba(0, 0, 0, 0.85);
    --stage-toast: rgba(0, 0, 0, 0.7);
    --stage-btn: rgba(51, 51, 51, 1);
    --stage-btn-hover: rgba(68, 68, 68, 1);
    --stage-btn-border: rgba(102, 102, 102, 1);
    --stage-text: rgba(238, 238, 238, 1);
    --stage-text-2: rgba(204, 204, 204, 1);
}

body {
    /* Layout */
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    min-height: 100vh;

    /* The yaku panel pops in instantly (display) while #play-area slides left
       over 0.2s, so its right edge transiently overflows the viewport — without
       this, a horizontal scrollbar flickers in and out. `clip` (not `hidden`)
       avoids creating a scroll container, leaving vertical scrolling intact. */
    overflow-x: clip;

    /* Box Model */
    margin: 0;
    padding: 20px;

    /* Visual */
    background: var(--bg);
    color: var(--text);

    /* Typography */
    font-family: 'KoddiUDOnGothic', sans-serif;
}

a {
    color: var(--link);
    text-decoration: none;
}

a:visited {
    color: var(--link);
}

a:hover {
    text-decoration: underline;
    text-decoration-style: dotted;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#main-wrapper {
    /* Layout */
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;

    /* Box Model */
    padding: 20px;
    width: 100%;
}

#toolbar-row {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;

    /* Box Model */
    width: 640px;
    max-width: 100%;

    /* Shift in lockstep with #play-area when the yaku panel opens (see below) */
    transition: transform 0.2s ease;
}

#mute-btn {
    grid-column: 1;
    justify-self: start;
}

.toolbar-center {
    grid-column: 2;
    justify-self: center;

    /* Layout */
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 20px;
}

.toolbar-right {
    grid-column: 3;
    justify-self: end;

    /* Layout */
    display: flex;
    align-items: center;
}

@media (hover: hover) and (pointer: fine) {
    .toolbar-icon-btn.btn-touch-only {
        display: none;
    }
}

body.gamepad-on .toolbar-icon-btn.btn-touch-only {
    display: none;
}

#play-area {
    /* Layout */
    position: relative;

    /* Box Model */
    width: 640px;
    max-width: 100%;
    aspect-ratio: 640 / 480;
    height: auto;

    /* Smoothly recenter when the yaku panel opens (see rule below) */
    transition: transform 0.2s ease;
}

/* The yaku panel (380px) sits to the right of the 640px canvas. When it opens,
   shift the play-area left by half the panel width so the canvas+panel pair is
   centered as a unit. The toolbar shifts by the same amount so its edge icons
   stay aligned with the canvas, not the viewport. Only when the viewport can
   fit the 1020px combo. */
@media (min-width: 1040px) {
    #main-wrapper:has(#yaku-container:not(.hidden)) #play-area,
    #main-wrapper:has(#yaku-container:not(.hidden)) #toolbar-row {
        transform: translateX(-190px);
    }
}

#game-column {
    /* Layout */
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#toolbar {
    /* Layout */
    display: flex;
    justify-content: flex-end;
}

/* Toolbar Group */
.toolbar-group {
    /* Layout */
    display: inline-flex;
    align-items: center;
    gap: 6px;

    /* Box Model */
    margin-right: 6px;
}

.toolbar-label {
    /* Typography */
    font-family: 'KoddiUDOnGothic', sans-serif;
    font-size: 18px;
    color: var(--text);
}

.toolbar-btn {
    /* Box Model */
    width: 50px;
    height: 35px;
    border: none;

    /* Visual */
    background-color: transparent;
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
    cursor: pointer;
}

.toggle-on {
    background-image: url('assets/ui/on.png');
}

.toggle-off {
    background-image: url('assets/ui/off.png');
}

.toolbar-btn:hover {
    filter: brightness(1.2);
}

/* Disabled (e.g. difficulty locked during a battle): dimmed, no hover lift. */
.toolbar-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.toolbar-btn:disabled:hover {
    filter: none;
}

/* Mute & fullscreen use inline-SVG icon buttons (gray, hover lightens) instead
   of the on/off image toggles — matches the gensei-pc98 emulator's .btn-icon. */
.toolbar-icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 35px;
    padding: 4px 8px;
    border: none;
    background: none;
    color: var(--text-3);
    cursor: pointer;
}

.toolbar-icon-btn svg {
    display: block;
    height: 22px;
    width: auto;
}

.toolbar-icon-btn:hover {
    color: var(--accent);
}

/* Difficulty is a 3-segment image (쉬움/중간/어려움) that swaps to highlight
   the current selection. Wider than the 80px on/off toggles. */
.difficulty-btn {
    width: 75px;
}

.difficulty-easy {
    background-image: url('assets/ui/easy.png');
}

.difficulty-normal {
    background-image: url('assets/ui/normal.png');
}

.difficulty-hard {
    background-image: url('assets/ui/difficult.png');
}

#game-container {
    /* Layout */
    flex-shrink: 0;

    /* Visual */
    box-shadow: 0 0 20px var(--shadow);
}

canvas {
    /* Layout */
    display: block;

    /* Visual */
    background-color: var(--stage-bg);
    image-rendering: pixelated;

    width: 100%;
    height: 100%;

    /* Kill double-tap-to-zoom on the game surface (iOS Safari ignores
       user-scalable=no but respects this). Pinch/scroll still work. */
    touch-action: manipulation;
}

/* Same for tap targets outside the canvas (toolbar, footer links). The
   virtual gamepad keeps its own touch-action: none (higher specificity). */
button,
a {
    touch-action: manipulation;
}

#yaku-container {
    /* Layout */
    display: flex;
    flex-direction: column;
    position: absolute;
    z-index: 10;
    top: 0;
    left: 640px;

    /* Box Model */
    width: 380px;
    height: 480px;
    border: 1px solid var(--border);

    /* Visual */
    background: rgba(240, 240, 240, 1);
    box-shadow: 0 0 10px var(--shadow);
}

#yaku-container.hidden {
    display: none;
}

#yaku-frame {
    /* Layout */
    flex: 1;

    /* Box Model */
    width: 100%;
    border: none;

    /* Visual */
    background: rgba(255, 255, 255, 1);
}

#game-container:fullscreen {
    /* Layout */
    display: flex;
    justify-content: center;
    align-items: center;

    /* Box Model */
    width: 100vw;
    height: 100vh;

    /* Visual */
    background-color: var(--stage-bg);
}

#game-container:fullscreen canvas {
    /* Layout */
    display: block;

    /* Box Model */
    width: min(100vw, 133.34vh);
    height: min(100vh, 75vw);
}

.pseudo-fullscreen {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    z-index: 9999;
    background: var(--stage-bg);
    margin: 0 !important;
    padding: 0 !important;
    border-radius: 0 !important;
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
}

.pseudo-fullscreen canvas {
    width: min(100vw, 133.34vh) !important;
    height: min(100vh, 75vw) !important;
}

#main-wrapper #footer {
    color: var(--text-3);
    opacity: 1;
    margin-top: 20px;
    padding: 16px 0 0;
    border-top: 1px solid;
    border-image: linear-gradient(to right, transparent, var(--border), transparent) 1;
}

#main-wrapper #footer a {
    color: var(--text-2);
}

#main-wrapper #footer a:hover {
    color: var(--text);
}

#main-wrapper #footer .site-version {
    color: var(--text-3);
    letter-spacing: 0.05em;
}

#main-wrapper #footer .footer-sep {
    margin: 0 4px;
}

/* Mobile Optimizations */
@media (max-width: 768px) {
    body {
        padding: 10px;
    }

    #main-wrapper {
        padding: 5px;
        gap: 10px;
    }

    #toolbar-row {
        gap: 4px;
    }

    /* Shrink the toolbar so the option groups stay on ONE line (no wrap). */
    .toolbar-center {
        flex-wrap: nowrap;
        gap: 6px;
    }

    .toolbar-label {
        font-size: 12px;
    }

    .toolbar-group {
        margin-right: 0;
        margin-bottom: 0;
        gap: 3px;
    }

    .toolbar-btn {
        width: 38px;
        height: 27px;
    }

    /* 난이도는 3분할 이미지라 더 넓어야 같은 높이(27px)로 보임 (데스크톱 75:35 비율 유지). */
    .difficulty-btn {
        width: 58px;
    }

    .toolbar-icon-btn {
        padding: 4px 4px;
    }

    .toolbar-icon-btn svg {
        height: 20px;
    }

    #main-wrapper #footer {
        padding: 16px;
    }

    #main-wrapper #footer p {
        display: inline;
    }
}

#virtual-gamepad {
    display: none;
}

body.gamepad-on {
    padding-bottom: 180px;
}

body.gamepad-on #virtual-gamepad {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 16px;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    z-index: 10000;
}

#virtual-gamepad button {
    border-radius: 8px;
    background: rgba(30, 30, 30, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: rgba(230, 230, 230, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25), 0 3px 8px rgba(0, 0, 0, 0.35);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
    transition: transform 0.05s, box-shadow 0.05s;
}

#virtual-gamepad button.active {
    transform: translateY(3px);
    box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.25), 0 1px 2px rgba(0, 0, 0, 0.2);
}

.gamepad-dpad {
    position: relative;
    width: 140px;
    height: 140px;
}

.gamepad-dpad button {
    position: absolute;
    width: 48px;
    height: 48px;
    padding: 6px 14px;
}

.dpad-up { top: 0; left: 46px; }
.dpad-down { bottom: 0; left: 46px; }
.dpad-left { top: 46px; left: 0; }
.dpad-right { top: 46px; right: 0; }

#virtual-gamepad button svg {
    height: 50%;
    width: auto;
    pointer-events: none;
}

#virtual-gamepad .gamepad-buttons button svg {
    height: 30%;
}

#virtual-gamepad .gamepad-dpad button svg {
    height: 40%;
}

.gamepad-buttons {
    display: flex;
    gap: 6px;
    align-items: center;
}

.gamepad-buttons button {
    min-width: 48px;
    height: 48px;
    padding: 0 10px;
}

.gamepad-buttons button.gamepad-btn-enter {
    width: 72px;
    min-width: 72px;
}