:root {
    --tom-viewport-height: 100vh;
    --tom-safe-top: env(safe-area-inset-top, 0px);
    --tom-safe-right: env(safe-area-inset-right, 0px);
    --tom-safe-bottom: env(safe-area-inset-bottom, 0px);
    --tom-safe-left: env(safe-area-inset-left, 0px);
}

@supports (height: 100dvh) {
    :root { --tom-viewport-height: 100dvh; }
}

body.responsive-core {
    min-width: 0;
    min-height: var(--tom-viewport-height);
    height: var(--tom-viewport-height);
    padding-top: max(12px, var(--tom-safe-top));
    padding-right: max(12px, var(--tom-safe-right));
    padding-bottom: max(12px, var(--tom-safe-bottom));
    padding-left: max(12px, var(--tom-safe-left));
}

.responsive-core *, .responsive-core *::before, .responsive-core *::after {
    box-sizing: border-box;
}

.responsive-core .workspace {
    height: 100%;
    min-width: 0;
    min-height: 0;
    gap: clamp(8px, 1.3vw, 20px);
}

.responsive-core .main-area { min-width: 0; min-height: 0; }
.responsive-core .site-dialog-overlay,
.responsive-core .trade-modal {
    padding: max(12px, var(--tom-safe-top)) max(12px, var(--tom-safe-right)) max(12px, var(--tom-safe-bottom)) max(12px, var(--tom-safe-left));
}
.responsive-core .modal-content,
.responsive-core .site-dialog-box { max-width: min(900px, 100%); max-height: 100%; }
.responsive-core button,
.responsive-core input,
.responsive-core select,
.responsive-core textarea { font: inherit; }

/* Binder camera: the 1550x1000 stage and every registered child stay intact. */
.responsive-core.responsive-monsterbox.has-responsive-camera .main-area {
    overflow: hidden;
    overscroll-behavior: contain;
}
.responsive-core.responsive-monsterbox.has-responsive-camera .diorama-stage {
    top: 0;
    left: 0;
    transform-origin: 0 0;
    will-change: transform;
}
.responsive-core.responsive-monsterbox.has-responsive-camera .main-area.portrait-binder-camera {
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
    touch-action: pan-x;
    scrollbar-width: thin !important;
    -webkit-overflow-scrolling: touch;
}
.responsive-camera-spacer { position: relative; pointer-events: none; }
.responsive-camera-diagnostic {
    position: fixed;
    z-index: 99999;
    top: max(5px, var(--tom-safe-top));
    left: max(5px, var(--tom-safe-left));
    padding: 5px 7px;
    border: 1px solid #ff8800;
    background: rgba(0,0,0,.9);
    color: #ff8800;
    font: 10px/1.35 var(--tom-font-mono, monospace);
    pointer-events: none;
    white-space: pre;
}
.responsive-preview-badge {
    position: fixed;
    z-index: 99998;
    top: max(6px, var(--tom-safe-top));
    right: max(6px, var(--tom-safe-right));
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border: 2px solid #ff8800;
    background: rgba(0,0,0,.92);
    color: #ff8800;
    font: 900 10px/1 var(--tom-font-mono, monospace);
    text-transform: uppercase;
    box-shadow: 0 0 12px rgba(255,136,0,.35);
}
.responsive-preview-badge a {
    min-height: 30px;
    display: inline-flex;
    align-items: center;
    padding: 0 8px;
    border: 1px solid #ff8800;
    color: #ff8800;
    text-decoration: none;
}
.responsive-preview-badge a:hover,
.responsive-preview-badge a:focus-visible { background: #ff8800; color: #000; }

/* Standard content pages reflow; the shelf remains outside page content. */
.responsive-core.responsive-content-page .main-area { overflow: auto; }
.responsive-core.responsive-content-page .inbox-list { min-width: 0; }
.responsive-core.responsive-content-page .trade-row,
.responsive-core.responsive-content-page .mini-desk,
.responsive-core.responsive-content-page .tab-heading { min-width: 0; }
.responsive-core.responsive-content-page .file-tabs { overflow-x: auto; padding-bottom: 2px; }
.responsive-core.responsive-content-page .file-tab { flex: 0 0 auto; }

body.responsive-core.responsive-login {
    overflow: auto;
    justify-content: center;
    padding: max(18px, var(--tom-safe-top)) max(16px, var(--tom-safe-right)) max(18px, var(--tom-safe-bottom)) max(16px, var(--tom-safe-left));
}
.responsive-login #typewriter-container { width: min(100%, 600px); margin-bottom: clamp(18px, 5vh, 40px); }
.responsive-login .login-form { width: min(100%, 340px); gap: clamp(18px, 4vh, 30px); }
.responsive-login .input-group { width: 100%; }
.responsive-login .input-group input { min-height: 48px; }
.mobile-actions-strip { display: none; }

/* Shelf presentation is viewport-driven. Touchscreen desktops must retain the
   full desktop shelf instead of inheriting the compact phone controls. */
@media (max-width: 900px) {
    body.responsive-core:not(.responsive-login) { padding: 6px; }
    .responsive-core .workspace { flex-direction: column; padding-bottom: 148px; gap: 6px; }
    .responsive-core .main-area { width: 100%; height: auto; flex: 1 1 auto; }
    .responsive-core .shelf-panel {
        position: fixed;
        z-index: 5000;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: calc(148px + var(--tom-safe-bottom));
        padding: 6px 8px max(8px, var(--tom-safe-bottom));
        overflow: hidden;
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: 64px 70px;
        gap: 4px;
    }
    .responsive-core .shelf-panel .pack-row {
        position: absolute;
        top: 70px;
        left: 8px;
        width: calc(25% - 8px);
        min-height: 64px;
        margin: 0;
        z-index: 2;
        display: grid;
        grid-template-columns: repeat(2, minmax(48px, 1fr));
        gap: 0;
    }
    .responsive-core .shelf-panel .pack-row .vessel-wrapper,
    .responsive-core .shelf-panel .pack-row .rpg-console-entry { width: 100%; min-width: 0; }
    .responsive-core .shelf-panel .pack-row .pack-img { width: 38px; }
    .responsive-core .shelf-panel .scrollable-shelf {
        grid-row: 1;
        display: flex;
        align-items: flex-start;
        gap: 8px;
        width: 100%;
        min-width: 0;
        margin: 0;
        padding: 3px 6px 5px;
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x proximity;
        touch-action: pan-x;
    }
    .responsive-core .shelf-panel .scrollable-shelf .vessel-wrapper {
        flex: 0 0 52px;
        width: 52px;
        scroll-snap-align: start;
    }
    .responsive-core .shelf-panel .scrollable-shelf .vessel-icon { width: 38px; margin-bottom: 1px; }
    .responsive-core .shelf-panel .scrollable-shelf .vessel-label {
        width: 52px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 6px;
        line-height: 8px;
    }
    .responsive-core .shelf-panel .shelf-nav-grid {
        grid-row: auto !important;
        grid-column: auto !important;
        position: absolute !important;
        top: 70px !important;
        right: 0;
        bottom: auto;
        left: auto;
        grid-template-columns: repeat(6, minmax(42px, 1fr));
        align-items: start;
        gap: 0;
        margin: 0;
        padding: 0;
        width: 75% !important;
        height: 64px;
        overflow-x: auto;
        overflow-y: hidden;
        touch-action: pan-x;
        scroll-snap-type: x proximity;
    }
    .responsive-core .shelf-panel .shelf-nav-grid .vessel-wrapper { width: 42px; justify-self: center; }
    .responsive-core .shelf-panel .shelf-nav-grid .vessel-icon { width: 26px; margin-bottom: 1px; }
    .responsive-core .shelf-panel .shelf-nav-grid .vessel-label { font-size: 6px; line-height: 8px; }
    .responsive-core .shelf-panel .shelf-organize-control,
    .responsive-core .shelf-panel .mobile-binder-carousel { display: none !important; }
    .responsive-core .shelf-panel .pack-row,
    .responsive-core .shelf-panel .shelf-nav-grid { display: none !important; }
    .responsive-core .shelf-panel .mobile-actions-strip {
        position: absolute;
        right: 6px;
        bottom: max(4px, var(--tom-safe-bottom));
        left: 6px;
        height: 66px;
        display: flex;
        align-items: flex-start;
        gap: 4px;
        padding: 2px 4px 4px;
        overflow-x: auto;
        overflow-y: hidden;
        scroll-snap-type: x proximity;
        touch-action: pan-x;
        -webkit-overflow-scrolling: touch;
    }
    .responsive-core .mobile-actions-strip .vessel-wrapper,
    .responsive-core .mobile-actions-strip .rpg-console-entry {
        flex: 0 0 52px;
        width: 52px;
        min-width: 52px;
        height: 58px;
        align-items: center;
        justify-content: flex-start;
        scroll-snap-align: start;
    }
    .responsive-core .mobile-actions-strip .vessel-icon { width: 38px; margin-bottom: 1px; }
    .responsive-core .mobile-actions-strip .vessel-label {
        width: 52px;
        margin-top: 1px !important;
        font-size: 6px;
        line-height: 8px;
        text-align: center;
    }
    .responsive-core .mobile-actions-strip .pack-stack-container {
        width: 40px;
        height: 40px;
        min-height: 40px;
        margin: 0;
    }
    .responsive-core .mobile-actions-strip .pack-img { width: 36px; height: 40px; }
    .responsive-core .mobile-actions-strip .rpg-console-entry img { width: 38px; height: 38px; object-fit: contain; }
    .responsive-core.responsive-content-page .trade-row,
    .responsive-core.responsive-content-page .mini-desk,
    .responsive-core.responsive-content-page .tab-heading { flex-wrap: wrap; }
    .responsive-core.responsive-content-page .trade-row { align-items: flex-start; gap: 12px; }
    .responsive-core.responsive-content-page .inbox-list { margin: -2px 8px 8px; padding: 18px 10px 12px; }
    .responsive-core.responsive-content-page .header-bar { padding: 12px 10px 0; }
    .responsive-core.responsive-monsterbox .modal-overlay {
        align-items: flex-start;
        justify-content: center;
        overflow-x: auto;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding: 12px 8px calc(160px + var(--tom-safe-bottom));
    }
    .responsive-core.responsive-monsterbox .modal-overlay > * {
        flex: 0 0 auto;
        max-width: 100%;
    }
    .responsive-core.responsive-monsterbox .sort-box {
        width: min(450px, calc(100vw - 16px));
    }
    .responsive-core.responsive-trade .main-area {
        flex-direction: column;
        overflow-y: auto;
        align-items: stretch;
    }
    .responsive-core.responsive-trade .trade-panel {
        flex: 0 0 auto;
        width: 100%;
        min-width: 0;
        min-height: min(440px, 62vh);
    }
}

@media (orientation: landscape) and (max-height: 600px) and (max-width: 1100px) {
    body.responsive-core:not(.responsive-login) { padding: 0; }
    .responsive-core .workspace {
        flex-direction: row;
        padding: 0 176px 0 0;
        gap: 0;
    }
    .responsive-core .main-area { height: 100%; border-width: 1px; }
    .responsive-core .shelf-panel {
        top: 0;
        right: 0;
        bottom: 0;
        left: auto;
        width: calc(176px + var(--tom-safe-right));
        height: 100%;
        display: grid;
        grid-template-columns: 88px 88px;
        grid-template-rows: minmax(0, 1fr);
        padding: 4px max(4px, var(--tom-safe-right)) 4px 4px;
        background-position: center 42%;
    }
    .responsive-core .shelf-panel .scrollable-shelf {
        position: relative;
        grid-column: 1;
        grid-row: 1;
        width: 84px;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 5px;
        padding: 4px 2px;
        overflow-x: hidden;
        overflow-y: auto;
        scroll-snap-type: y proximity;
        touch-action: pan-y;
        border-right: 1px dashed rgba(85,255,85,.35);
    }
    .responsive-core .shelf-panel .scrollable-shelf .vessel-wrapper { flex: 0 0 50px; width: 76px; }
    .responsive-core .shelf-panel .scrollable-shelf .vessel-icon { width: 34px; }
    .responsive-core .shelf-panel .pack-row {
        top: 4px;
        right: 4px;
        left: auto;
        width: 84px;
        min-height: 100px;
        display: grid;
        grid-template-columns: 1fr;
        grid-template-rows: repeat(2, 50px);
    }
    .responsive-core .shelf-panel .pack-row .pack-img { width: 31px; }
    .responsive-core .shelf-panel .pack-row .rpg-console-entry img { width: 36px; height: 31px; }
    .responsive-core .shelf-panel .shelf-nav-grid {
        top: 104px !important;
        right: 4px;
        left: auto;
        width: 84px !important;
        height: calc(100% - 108px);
        display: grid;
        grid-template-columns: 1fr;
        grid-auto-rows: 50px;
        align-content: start;
        overflow-x: hidden;
        overflow-y: auto;
        scroll-snap-type: y proximity;
        touch-action: pan-y;
    }
    .responsive-core .shelf-panel .shelf-nav-grid .vessel-wrapper { width: 76px; scroll-snap-align: start; }
    .responsive-core .shelf-panel .shelf-nav-grid .vessel-icon { width: 27px; }
    .responsive-core .shelf-panel .mobile-actions-strip {
        top: 4px;
        right: 4px;
        bottom: 4px;
        left: auto;
        width: 84px;
        height: auto;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 4px;
        padding: 3px 2px;
        overflow-x: hidden;
        overflow-y: auto;
        scroll-snap-type: y proximity;
        touch-action: pan-y;
    }
    .responsive-core.responsive-monsterbox .modal-overlay {
        padding: 8px calc(188px + var(--tom-safe-right)) 8px 8px;
    }
    .responsive-core .mobile-actions-strip .vessel-wrapper,
    .responsive-core .mobile-actions-strip .rpg-console-entry {
        flex: 0 0 48px;
        width: 76px;
        min-width: 76px;
        height: 48px;
    }
    .responsive-core .mobile-actions-strip .vessel-icon { width: 34px; }
    .responsive-core .mobile-actions-strip .pack-stack-container { width: 36px; height: 36px; min-height: 36px; }
    .responsive-core .mobile-actions-strip .pack-img { width: 33px; height: 36px; }
    .responsive-core .mobile-actions-strip .rpg-console-entry img { width: 34px; height: 34px; }
}

@media (max-width: 680px) {
    .responsive-core.responsive-content-page .main-area { padding: 8px; }
    .responsive-core.responsive-content-page .file-tab { min-width: 130px; padding: 11px 14px 10px; }
    .responsive-core.responsive-content-page .trade-actions { width: 100%; justify-content: flex-start; }
    .responsive-core.responsive-content-page .modal-content { width: 100%; padding: 16px; }
}

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