/* ========================================
   Mobile & Tablet Responsive Styles
   ========================================
   Breakpoints:
   - Phone:   < 768px
   - Tablet:  768px – 1023px
   - Desktop: >= 1024px (unchanged)
   ======================================== */

/* ========================================
   Shared: Phone + Tablet (< 1024px)
   ======================================== */

@media (max-width: 1023px) {

    /* --- Disable tap highlight flash on mobile --- */
    * {
        -webkit-tap-highlight-color: transparent;
    }

    /* --- Body: fixed viewport, no scroll (prevents drag-to-scroll on pieces) --- */
    body {
        height: 100vh;
        height: 100dvh; /* dynamic viewport height — excludes mobile browser chrome & nav bar */
        overflow: hidden;
    }

    html {
        height: 100%;
        overflow: hidden;
    }

    /* --- Hide desktop ribbon, show mobile header --- */
    .ribbon {
        display: none !important;
    }

    .mobile-header {
        display: flex !important;
    }

    /* --- Mobile header --- */
    .mobile-header {
        height: 48px;
        min-height: 48px;
        background-color: #18461D;
        background-image: url('../img/mainHeaderBar.png');
        background-size: 100% 100%;
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 12px;
        flex-shrink: 0;
        z-index: 200;
    }

    .mobile-header-title {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 18px;
        font-weight: lighter;
    }

    .mobile-header-title img {
        width: 28px;
        height: 28px;
    }

    .mobile-header-actions {
        display: flex;
        align-items: center;
        gap: 4px;
    }

    .mobile-header-btn {
        width: 24px;
        height: 24px;
        cursor: pointer;
        opacity: 0.85;
        padding: 4px;
    }

    .mobile-header-btn:active {
        opacity: 1;
    }

    .mobile-hamburger {
        background: none;
        border: none;
        cursor: pointer;
        padding: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .mobile-hamburger svg {
        width: 24px;
        height: 24px;
        fill: #fff;
    }

    /* --- Mobile menu (dropdown) --- */
    .mobile-menu {
        display: none;
        position: fixed;
        top: 48px;
        left: 0;
        right: 0;
        background: #1e5925;
        z-index: 199;
        flex-direction: column;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .mobile-menu.open {
        display: flex;
    }

    .mobile-menu-item {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 14px 20px;
        color: #fff;
        font-size: 15px;
        border: none;
        background: none;
        cursor: pointer;
        text-align: left;
        width: 100%;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }

    .mobile-menu-item:hover,
    .mobile-menu-item:active {
        background: rgba(255, 255, 255, 0.1);
    }

    .mobile-menu-item.active {
        background: rgba(255, 255, 255, 0.15);
        font-weight: bold;
    }

    .mobile-menu-item img {
        width: 24px;
        height: 24px;
        filter: brightness(0) invert(1);
    }

    .mobile-menu-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        top: 48px;
        background: rgba(0, 0, 0, 0.4);
        z-index: 198;
    }

    .mobile-menu-backdrop.open {
        display: block;
    }

    /* --- Content layout: stacked vertical, fills remaining height --- */
    .content-container {
        flex-direction: column;
        overflow: hidden;
        flex: none;
        height: calc(100vh - 48px);
        height: calc(100dvh - 48px); /* dynamic viewport height minus mobile header */
    }

    /* --- Hide left panel and divider (panels via menu overlays) --- */
    .left-panel,
    .left-panel.visible {
        width: 0 !important;
        min-width: 0 !important;
        max-width: 0 !important;
        overflow: hidden !important;
        display: none !important;
    }

    .divider,
    .divider.visible {
        display: none !important;
    }

    /* --- Right panel: stacked, board on top, fills remaining height --- */
    .right-panel {
        position: relative;
        flex: 1 1 0;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        min-height: 0;
        height: 100%;
    }

    /* --- Board panel dock: inline, full width --- */
    #panelDock {
        position: static !important;
        top: auto !important;
        right: auto !important;
        left: auto !important;
        width: 100% !important;
        z-index: 1;
        order: -1; /* Board above canvas */
        flex-shrink: 0;
    }

    #boardPanel {
        width: 100% !important;
    }

    /* Hide Position top bar on mobile (buttons moved to header) */
    #boardPanelTopBar {
        display: none;
    }

    /* Hide expand/collapse button on mobile (board is always full-width) */
    #expandBoardButton {
        display: none;
    }

    /* Hide move buttons on mobile to save vertical space */
    #moveButtons {
        display: none;
    }

    /* --- Hide desktop notes, outcome bar, info box on mobile (replaced by mobile bottom bar) --- */
    #notesBox {
        display: none !important;
    }

    #outcomeBarRow {
        display: none !important;
    }

    #bottomInfoBox {
        display: none !important;
    }

    /* --- Canvas area: fills remaining space below board --- */
    #canvasFrame {
        flex: 1 1 auto;
        min-height: 0;
    }

    #canvas_container {
        touch-action: none; /* Let panzoom handle all touch */
    }

    /* --- Panel overlays (tree browser, search, engine, feedback) --- */
    .mobile-panel-overlay {
        display: none;
        position: fixed;
        inset: 0;
        top: 48px; /* below mobile header */
        z-index: 150;
        background: #fff;
        flex-direction: column;
        overflow: hidden;
    }

    .mobile-panel-overlay.open {
        display: flex;
    }

    #mobilePanelContent {
        flex: 1 1 0;
        min-height: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .mobile-panel-overlay .panel-view {
        display: block !important;
    }

    /* Hide the panel's own title bar inside mobile overlay (overlay has its own header) */
    .mobile-panel-overlay .panel-title {
        display: none;
    }

    /* Make tree list scrollable inside overlay */
    .mobile-panel-overlay #treelist-scroll-container {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        max-height: none;
    }

    .mobile-panel-overlay #treebrowser {
        height: 100%;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    .mobile-panel-overlay #treelist-wrapper {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        min-height: 0;
    }

    .mobile-panel-overlay-header {
        height: 40px;
        min-height: 40px;
        background: #1e5925;
        color: #fff;
        display: flex;
        align-items: center;
        padding: 0 12px;
        gap: 12px;
        flex-shrink: 0;
    }

    .mobile-panel-close {
        background: none;
        border: none;
        color: #fff;
        font-size: 24px;
        cursor: pointer;
        padding: 4px 8px;
        line-height: 1;
    }

    .mobile-panel-title {
        flex: 1;
        font-size: 15px;
        font-weight: 600;
    }

    /* --- Hide jQuery UI resize handles on mobile --- */
    .ui-resizable-handle {
        display: none !important;
    }

    /* --- Context menu adjustments --- */
    .context-menu-list {
        font-size: 15px;
    }

    .context-menu-item {
        padding: 10px 16px !important;
    }

    /* --- Mobile bottom bar (comment preview + info) --- */
    #mobileBottomBar {
        display: block;
        flex-shrink: 0;
        background: #f5f5f0;
        border-top: 1px solid #ccc;
        font-size: 12px;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    #mobileCommentPreview {
        display: none; /* shown via JS when comment exists */
        padding: 6px 40px 6px 10px; /* right padding for edit button */
        font-size: 13px;
        color: #333;
        line-height: 1.4;
        max-height: calc(1.4em * 3); /* 3 lines max */
        overflow: hidden;
        cursor: pointer;
        border-bottom: 1px solid #ddd;
    }

    #mobileCommentPreview.has-comment {
        display: block;
    }

    #mobileInfoBar {
        padding: 2px 10px 4px;
    }

    /* Row 1: outcome bar + game count */
    #mobileInfoRow1 {
        display: flex;
        align-items: center;
        gap: 8px;
        height: 20px;
    }

    #mobileOutcomeBar {
        display: flex;
        align-items: center;
        flex: 1;
    }

    #mobileOutcomeLabel {
        font-size: 10px;
        color: #666;
        margin-right: 4px;
        white-space: nowrap;
    }

    #mobileOutcomeSegments {
        display: flex;
        flex: 1;
        height: 14px;
        border-radius: 2px;
        overflow: hidden;
    }

    .mobile-outcome-seg {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 9px;
        overflow: hidden;
    }

    #mobileOutcome-white { background: #fff; border: 1px solid #ccc; color: #333; }
    #mobileOutcome-tie   { background: #888; color: #fff; }
    #mobileOutcome-black { background: #333; color: #fff; }

    #mobileGameCount {
        font-size: 10px;
        color: #888;
        white-space: nowrap;
    }

    /* Row 2: move text */
    #mobileInfoRow2 {
        height: 18px;
        overflow: hidden;
    }

    #mobileMoveText {
        font-size: 11px;
        color: #555;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* --- Mobile edit note button (floating) --- */
    #mobileEditNoteBtn {
        display: flex;
        position: absolute;
        bottom: 52px; /* above the bottom bar */
        right: 8px;
        width: 36px;
        height: 36px;
        border-radius: 6px;
        background: rgba(30, 89, 37, 0.85);
        border: none;
        cursor: pointer;
        align-items: center;
        justify-content: center;
        z-index: 10;
        box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    }

    #mobileEditNoteBtn svg {
        width: 20px;
        height: 20px;
        fill: #fff;
    }

    /* --- Mobile comment expanded view --- */
    #mobileCommentExpanded {
        display: none;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0; /* covers entire right-panel area above bottom bar */
        background: rgba(255, 255, 255, 0.97);
        z-index: 9;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 12px;
        font-size: 14px;
        line-height: 1.5;
        color: #333;
    }

    #mobileCommentExpanded.open {
        display: block;
    }

    #mobileCommentExpandedText {
        white-space: pre-wrap;
        word-wrap: break-word;
    }

    /* --- Mobile note edit overlay --- */
    #mobileNoteEditOverlay {
        display: none;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        background: #fff;
        z-index: 11;
        flex-direction: column;
    }

    #mobileNoteEditOverlay.open {
        display: flex;
    }

    #mobileNoteEditHeader {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 8px 12px;
        background: #1e5925;
        color: #fff;
        font-size: 15px;
        font-weight: 600;
        flex-shrink: 0;
    }

    #mobileNoteEditClose {
        background: none;
        border: none;
        color: #fff;
        font-size: 24px;
        cursor: pointer;
        padding: 0 4px;
        line-height: 1;
    }

    #mobileNoteEditContent {
        flex: 1;
        display: flex;
        flex-direction: column;
        padding: 12px;
        gap: 8px;
        min-height: 0;
        overflow: hidden;
    }

    #mobileNoteTitle {
        font-size: 15px;
        padding: 8px;
        border: 1px solid #ccc;
        border-radius: 4px;
        flex-shrink: 0;
        box-sizing: border-box;
        width: 100%;
    }

    #mobileNoteComment {
        flex: 1;
        font-size: 14px;
        padding: 8px;
        border: 1px solid #ccc;
        border-radius: 4px;
        resize: none;
        min-height: 0;
        box-sizing: border-box;
        width: 100%;
        line-height: 1.5;
    }
}

/* ========================================
   Phone only (< 768px)
   ======================================== */

@media (max-width: 767px) {

    /* Hide eval column on phone */
    #evalColumn {
        display: none !important;
    }

    /* Canvas fills remaining space (handled by flex in shared block) */

    /* Full-width search input */
    #searchInput {
        width: calc(100% - 80px);
    }
}

/* ========================================
   Tablet (768px – 1023px)
   ======================================== */

@media (min-width: 768px) and (max-width: 1023px) {

    /* Board stays full-width on tablet too */

    /* Canvas fills remaining space (handled by flex in shared block) */
}

/* ========================================
   Desktop: hide mobile elements
   ======================================== */

.mobile-header {
    display: none;
}

.mobile-menu {
    display: none;
}

.mobile-menu-backdrop {
    display: none;
}

.mobile-panel-overlay {
    display: none;
}

@media (min-width: 1024px) {
    #mobileBottomBar {
        display: none;
    }

    #mobileEditNoteBtn {
        display: none;
    }

    #mobileCommentExpanded {
        display: none;
    }

    #mobileNoteEditOverlay {
        display: none;
    }
}
