@import url('https://fonts.googleapis.com/css2?family=Nanum+Myeongjo&display=swap');

* { box-sizing: border-box; }
body { font-family: sans-serif; margin: 0; padding: 0; background: #fff; color: #000; display: flex; height: 100vh; overflow: hidden; }
button, input, select { border: 1px solid #000; background: #fff; color: #000; padding: 5px 10px; cursor: pointer; font-size: 13px; }
button:hover { background: #eee; }

/* 사이드바 (여러 챌린지 관리) */
#sidebar { width: 240px; border-right: 1px solid #000; padding: 15px; display: flex; flex-direction: column; background: #fafafa; flex-shrink: 0; }
#sidebar h2 { font-size: 16px; margin-top: 0; border-bottom: 2px solid #000; padding-bottom: 8px; font-family: 'Nanum Myeongjo', serif; }
.sidebar-btn-group { display: flex; flex-direction: column; gap: 5px; margin-bottom: 15px; }
#challenge-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; }
.challenge-item { display: flex; align-items: center; justify-content: space-between; padding: 8px; border: 1px solid #ccc; background: #fff; cursor: pointer; font-size: 13px; }
.challenge-item.active { border: 2px solid #000; font-weight: bold; background: #eee; }
.challenge-item .title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; margin-right: 5px; }
.item-actions { display: flex; gap: 2px; }
.item-actions button { padding: 2px 5px; font-size: 11px; }
.sidebar-backup-row { display: flex; gap: 5px; }
.sidebar-backup-row button { flex: 1; }

/* 메인 영역 */
#main-content { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
#controls { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; border-bottom: 1px solid #000; padding: 10px 20px; background: #fff; z-index: 5; }

#board-wrapper { flex: 1; overflow: auto; padding: 20px; }
#board-container { position: relative; display: inline-block; }

/* 보드 레이아웃 및 셀 */
.empty-challenge-message {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    font-size: 20px;
    color: #777;
    text-align: center;
    white-space: nowrap;
}

:root { --cell-size: 76px; --row-gap: 24px; --turn-overlap: 12px; }
.row {
    display: flex;
    width: max-content;
    height: var(--cell-size);
    margin-bottom: var(--row-gap);
    position: relative;
    overflow: visible;
}
.start-row {
    display: flex;
    width: max-content;
    height: var(--cell-size);
    margin-bottom: 0;
    position: relative;
}
.cell { 
    width: var(--cell-size); height: var(--cell-size); border: 1px solid #000; 
    display: flex; flex-direction: column; justify-content: flex-start; align-items: center;
    position: relative; cursor: pointer; background: #fff; flex-shrink: 0;
}
.cell:hover { background: #f0f0f0; }
.cell-number { font-size: 11px; margin-top: 2px; font-weight: bold; z-index: 2; }
.cell-theme { font-size: 10px; color: #444; margin-top: auto; margin-bottom: 4px; width: 92%; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; z-index: 2; }

/* 꺾이는 부분: 별도의 칸을 만들지 않고, 원래 그 자리에 있던 칸 자체를 늘린다.
    위 칸은 아래쪽으로, 아래 칸은 위쪽으로 각각 gap의 절반만큼 뻗는다.
    따라서 두 칸이 맞닿아 세로 연결부를 만든다. */
.cell.turn-top {
    /* 위쪽으로 뻗는 칸: 자신의 위쪽 경계가 줄 사이로 올라간다. */
    height: calc(var(--cell-size) + var(--turn-overlap));
    margin-top: calc(var(--turn-overlap) * -1);
    z-index: 1;
}
.cell.turn-bottom {
    /* 아래쪽으로 뻗는 칸: 자신의 아래쪽 경계가 줄 사이로 내려간다.
        특히 1번 칸은 0번 시작칸과 겹치지 않고 아래로만 늘어난다. */
    height: calc(var(--cell-size) + var(--turn-overlap));
    z-index: 1;
}

/* 말 */
.player-token {
    width: 68px; height: 68px;
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    z-index: 4; display: flex; align-items: center; justify-content: center; pointer-events: none;
}
.player-token img { width: 68px; height: 68px; object-fit: contain; }

/* 도장 (원형 내부 쏙 맞춤) */
.stamp-marker { 
    position: absolute; width: 60px; height: 60px; top: 50%; left: 50%; transform: translate(-50%, -50%); 
    opacity: 0.85; pointer-events: none; z-index: 3; border-radius: 50%; object-fit: contain; 
}

.stamp-toggle { display: inline-flex; align-items: center; gap: 7px; min-width: 116px; justify-content: center; }
.stamp-toggle-box {
    width: 30px; height: 30px; border: 1px solid #000; display: inline-flex;
    align-items: center; justify-content: center; background: #fff; overflow: hidden;
}
.stamp-toggle-box img { width: 100%; height: 100%; object-fit: contain; }

/* 주사위 */
#dice-display { font-size: 20px; font-weight: bold; width: 36px; height: 36px; border: 1px solid #000; display: flex; align-items: center; justify-content: center; }

/* 모달 공통 */
.modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.4); z-index: 20; }
.modal-window { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #fff; border: 2px solid #000; padding: 20px; z-index: 21; max-height: 90vh; overflow-y: auto; }

#record-modal { width: 600px; }
#editor { min-height: 180px; border: 1px solid #000; padding: 10px; font-family: 'Nanum Myeongjo', serif; overflow-y: auto; outline: none; margin-bottom: 10px; line-height: 1.6; }
#editor p { margin: 0 0 8px 0; }
#editor p:first-of-type { text-indent: 1em; }
#editor img { max-width: 100%; display: block; margin: 10px auto; border: 1px solid #ccc; }
#editor a { color: #0000ee; text-decoration: underline; }

.editor-toolbar { margin-bottom: 8px; display: flex; gap: 5px; }

#rules-modal { width: 420px; }
#preset-modal { width: 480px; }
#create-modal { width: 400px; }

.form-group { margin-bottom: 12px; display: flex; flex-direction: column; gap: 4px; }
.form-group label { font-size: 12px; font-weight: bold; }

/* =========================================================
   모바일 UI
   ========================================================= */

.mobile-sidebar-toggle {
    display: none;
}

@media (max-width: 700px) {

    /* -------------------------
       1. 보드 크게
       ------------------------- */

    :root {
        --cell-size: 60px;
        --row-gap: 28px;
        --turn-overlap: 14px;
    }    

    #main-content {
        width: 100%;
        height: 100dvh;
    }

    #board-wrapper {
        padding: 18px;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x pan-y;
    }

    #board-container {
        min-width: max-content;
    }

    .cell-number {
        font-size: 14px;
        margin-top: 4px;
    }

    .cell-theme {
        font-size: 13px;
        margin-bottom: 6px;
    }

    .player-token {
        width: 54px;
        height: 54px;
    }

    .player-token img {
        width: 54px;
        height: 54px;
    }

    .stamp-marker {
        width: 50px;
        height: 50px;
    }


    /* -------------------------
       2. 상단 메뉴
       한 줄 + 가로 스크롤
       ------------------------- */

    #controls {
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;

        gap: 10px;

        padding: 10px 12px;

        min-height: 64px;

        white-space: nowrap;

        -webkit-overflow-scrolling: touch;

        scrollbar-width: thin;
    }

    #controls > button {
        flex: 0 0 auto;

        min-height: 44px;

        padding: 9px 13px;

        font-size: 15px;
    }

    #controls > span {
        flex: 0 0 auto;
    }

    #controls input,
    #controls select {
        flex: 0 0 auto;
        min-height: 44px;
        font-size: 15px;
    }

    #dice-display {
        flex: 0 0 44px;

        width: 44px;
        height: 44px;

        font-size: 22px;
    }

    #controls #stamp-preview {
        width: 44px !important;
        height: 44px !important;
    }

    .stamp-toggle-box {
        width: 38px;
        height: 38px;
    }


    /* -------------------------
       3. 사이드바
       ------------------------- */

    #sidebar {
        position: fixed;

        left: 0;
        top: 0;

        width: min(88vw, 420px);
        height: 100dvh;

        z-index: 100;

        padding: 18px;

        border-right: 2px solid #000;

        background: #fafafa;

        box-shadow: 8px 0 24px rgba(0, 0, 0, 0.18);

        transition: transform 0.2s ease;

        transform: translateX(0);
    }

    /*
       접힌 상태
       */
    #sidebar.mobile-collapsed {
        transform: translateX(-100%);
        pointer-events: none;
    }

    #sidebar h2 {
        font-size: 20px;

        padding-right: 52px;
    }

    /*
       사이드바 오른쪽 위 햄버거
       */
    .mobile-sidebar-toggle {
        display: none;
    }

    @media (max-width: 700px) {

        .mobile-sidebar-toggle {
            display: flex;
            position: fixed;
            top: 10px;
            left: calc(min(88vw, 420px) - 56px);
            width: 35px;
            height: 35px;
            padding: 0;
            align-items: center;
            justify-content: center;
            border: 2px solid #000;
            background: #fff;
            font-size: 24px;
            line-height: 1;
            z-index: 110;
        }

        #sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: min(88vw, 420px);
            height: 100dvh;
            z-index: 100;
            padding: 18px;
            border-right: 2px solid #000;
            background: #fafafa;
            box-shadow: 8px 0 24px rgba(0, 0, 0, 0.18);
            transition: transform 0.2s ease;
            transform: translateX(0);
        }

        #sidebar.mobile-collapsed {
            transform: translateX(-100%);
            pointer-events: none;
        }

        /* 사이드바가 접히면 버튼을 화면 왼쪽에 붙임 */
        body.mobile-sidebar-collapsed .mobile-sidebar-toggle {
            left: 12px;
        }

        #sidebar h2 {
            font-size: 20px;
            padding-right: 52px;
        }
    }

    @media (min-width: 701px) {
        .mobile-sidebar-toggle {
            display: none !important;
        }
    }


    /* -------------------------
       4. 모달도 모바일에서 크게
       ------------------------- */

    .modal-window {
        width: calc(100vw - 28px) !important;

        max-width: none;

        max-height: 88dvh;

        padding: 18px;
    }

    #editor {
        min-height: 240px;

        font-size: 16px;
    }
    
}


/* PC에서는 햄버거 버튼 숨김 */
@media (min-width: 701px) {
    .mobile-sidebar-toggle {
        display: none !important;
    }
}

#more-menu {
    position: relative;
}

#more-menu-btn {
    white-space: nowrap;
}

#more-menu-panel {
    display: none;
    position: absolute;
    top: calc(100% + 5px);
    right: 0;
    z-index: 200;
    min-width: 170px;
    border: 1px solid #000;
    background: #fff;
    box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.15);
}

#more-menu.open #more-menu-panel {
    display: flex;
    flex-direction: column;
}

#more-menu-panel button {
    border: 0;
    border-bottom: 1px solid #ddd;
    text-align: left;
    background: #fff;
    padding: 10px 12px;
    white-space: nowrap;
}

#more-menu-panel button:last-child {
    border-bottom: 0;
}

#mobile-controls {
    display: none;
}

@media (max-width: 700px) {
    #sidebar.sidebar-no-transition {
        transition: none !important;
    }

    /* 데스크톱용 상단 메뉴 숨기기 */
    #controls {
        display: none;
    }

    /* 모바일 상단 메뉴 */
    #mobile-controls {
        display: flex;
        flex-direction: column;

        gap: 6px;

        padding: 8px 10px;

        min-height: 94px;

        position: relative;
        z-index: 20;

        border-bottom: 1px solid #000;
        background: #fff;
    }

    /* 각 줄 */
    .mobile-control-row {
        display: flex;
        align-items: center;

        gap: 8px;

        min-height: 42px;
    }

    /* 첫째 줄 */

    .mobile-control-row-top {
        width: 100%;
    }

    #mobile-rules-btn {
        white-space: nowrap;

        /*
         * 햄버거 버튼이 위에 있으므로
         * 규칙 보기만 조금 오른쪽으로
         */
        margin-left: 50px;
    }

    .mobile-divider {
        width: 1px;
        height: 18px;

        background: #000;

        flex: 0 0 1px;
    }

    #mobile-dice-display {
        width: 42px;
        height: 42px;

        flex: 0 0 42px;

        border: 1px solid #000;

        display: flex;
        align-items: center;
        justify-content: center;

        font-size: 20px;
        font-weight: bold;
    }

    /* 둘째 줄 */

    .mobile-control-row-bottom {
        width: 100%;
    }

    #mobile-stamp-preview {
        width: 42px;
        height: 42px;

        flex: 0 0 42px;

        border: 1px solid #000;
        background: #fff;
    }

    #mobile-player-preview {
        width: 42px;
        height: 42px;

        flex: 0 0 42px;

        border: 1px solid #000;
        background: #fff;
    }

    /*
     * 더보기는 오른쪽 끝으로 밀기
     */
    #mobile-more-menu {
        position: relative;

        margin-left: auto;
    }

    /* 더보기 메뉴 */

    #mobile-more-panel {
        display: none;

        position: absolute;

        right: 0;
        top: 48px;

        min-width: 180px;

        border: 1px solid #000;

        background: #fff;

        box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.15);

        z-index: 100;
    }

    #mobile-more-menu.open #mobile-more-panel {
        display: flex;
        flex-direction: column;
    }

    #mobile-more-panel button {
        border: 0;
        border-bottom: 1px solid #ddd;

        background: #fff;

        text-align: left;

        padding: 10px 12px;

        font-size: 14px;

        white-space: nowrap;
    }

    #mobile-more-panel button:last-child {
        border-bottom: 0;
    }
}