html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    font-family: system-ui, "Hiragino Sans", "Yu Gothic UI", sans-serif;
}

#app {
    display: flex;
    height: 100%;
    width: 100%;
}

#mapWrap {
    position: relative;
    flex: 1;
    min-width: 0;
    height: 100%;
}

#map {
    width: 100%;
    height: 100%;
    background: #0b2740;
}


/*  Zoom Controls  */

.ol-zoom {
    top: auto !important;
    /* bottom: 3.8em !important; */
    bottom: 2.2em !important;
    /* bottom: 0.5em !important; */
    left: 0.4em !important;
    z-index: 10 !important;
}


/*  Download Buttons  */

#btnDownloadData,
#btnDownloadVoxy {
    position: absolute;
    left: 0.5em;
    z-index: 20;
    background: rgba(20, 24, 30, 0.92);
    color: #9fb6d4;
    border: 1px solid #444;
    border-radius: 4px;
    height: 26px;
    font-size: 11px;
    cursor: pointer;
    white-space: nowrap;
    /* display: none; */
}

#btnDownloadData {
    bottom: 0.5em;
}

/* #btnDownloadVoxy {
    bottom: 0.5em;
} */

#btnDownloadData:hover,
#btnDownloadVoxy:hover {
    background: #2e3646;
    color: #eee;
}


/*  Top-Left Info Panel  */

#panel {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 10;
    background: rgba(20, 24, 30, 0.92);
    color: #eee;
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 13px;
    width: 194px;
}

#panel label {
    display: block;
    margin: 4px 0;
    cursor: pointer;
}

#panel .row {
    margin-top: 6px;
    display: flex;
    gap: 5px;
}

#panel button {
    flex: 1;
    cursor: pointer;
    font-size: 12px;
    padding: 4px;
    background: #2a313d;
    color: #eee;
    border: 1px solid #444;
    border-radius: 4px;
}

#panel button:hover {
    background: #3d4a5c;
}

#panel select {
    flex: 1;
    background: #2a313d;
    color: #eee;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 3px;
    font-size: 12px;
}

#btnDiscord {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    background: #5865F2;
    color: #fff;
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 600;
    text-decoration: none;
    transition: background 0.15s;
    cursor: pointer;
}

#btnDiscord:hover {
    background: #4752c4;
}

#btnDiscord svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

#panelCredit {
    margin-top: 6px;
    color: #556;
    font-size: 10px;
    line-height: 1.5;
}

#serverVersion {
    position: absolute;
    margin-top: 6px;
    color: #666688;
    font-size: 12px;
    line-height: 1.5;
    top: 106px; /* beside the screenshot button, below #coordBar */
    left: 264px;
}


/*  Online Players Panel  */

#playersPanel {
    position: absolute;
    top: calc(10px + var(--panel-h, 300px) + 2.6px);
    left: 10px;
    z-index: 10;
    background: rgba(20, 24, 30, 0.92);
    color: #eee;
    border-radius: 8px;
    font-size: 13px;
    width: 194px;
    overflow: hidden;
}

#playersPanelToggle {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: none;
    border: none;
    color: #eee;
    font-size: 12px;
    cursor: pointer;
    padding: 8px 12px;
    text-align: left;
}

#playersPanelToggle:hover {
    background: rgba(255, 255, 255, 0.05);
}

#playersPanelTitle {
    font-weight: 600;
    color: #9fb6d4;
}

#playersPanelChevron {
    font-size: 10px;
    color: #556;
    transition: transform 0.2s;
}

#playersPanelChevron.open {
    transform: rotate(90deg);
}

#playersPanelBody {
    padding: 4px 8px 8px;
    max-height: 220px;
    overflow-y: auto;
}

#playersLoading {
    font-size: 11px;
    color: #556;
    padding: 4px 0;
    display: block;
}

.player-row {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 4px 2px;
    border-bottom: 1px solid #1e2530;
}

.player-row:last-child {
    border-bottom: none;
}

.player-name {
    font-size: 12px;
    color: #eee;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.players-empty {
    font-size: 11px;
    color: #556;
    padding: 4px 2px;
}


/*  Coordinate Input Bar  */

#coordBar {
    position: absolute;
    top: 10px;
    left: 230px;
    z-index: 10;
    background: rgba(20, 24, 30, 0.92);
    color: #eee;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 12px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    min-width: 200px;
}

#coordBar input {
    background: #2a313d;
    color: #eee;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 12px;
    width: 100%;
    box-sizing: border-box;
}

#coordBar input::placeholder {
    color: #668;
}

#coordBar .tileRow {
    display: flex;
    gap: 5px;
}

#coordBar .tileRow #inTileZ {
    flex: 0 0 56px;
}

#coordBar .tileRow #inTileXZ {
    flex: 1 1 auto;
    min-width: 0;
}


/*  Top Center Buttons  */

#topBtns {
    position: absolute;
    top: 5px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 22;
    display: flex;
    gap: 6px;
}

#topBtns button {
    background: rgba(20, 24, 30, 0.92);
    color: #eee;
    border: 1px solid #444;
    border-radius: 8px;
    padding: 6px 14px;
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s;
}

#topBtns button:hover {
    background: #2e3646;
}

#topBtns button.active {
    background: #2e3646;
    border-color: #9fb6d4;
    color: #fff;
}


/*  Warps Toggle Button  */

#btnSidePanel {
    position: absolute;
    top: 5px;
    right: 5px;
    z-index: 22;
    background: rgba(20, 24, 30, 0.92);
    color: #eee;
    border: 1px solid #444;
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
    transition: transform 0.22s ease;
}

#sidePanel.open ~ #btnSidePanel,
#btnSidePanel.open {
    transform: translateX(-280px);
}

#btnSidePanel:hover {
    background: #2e3646;
}


/*  Side Panel  */

#sidePanel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 280px;
    z-index: 17;
    background: rgba(20, 24, 30, 0.97);
    color: #eee;
    display: flex;
    flex-direction: column;
    font-size: 13px;
    transform: translateX(100%);
    transition: transform 0.22s ease;
    box-shadow: -4px 0 16px rgba(0, 0, 0, 0.4);
}

#sidePanel.open {
    transform: translateX(0);
}

#spHeader {
    display: flex;
    align-items: center;
    background: #12151b;
    padding: 0;
    flex-wrap: nowrap;
}

#spHeader button {
    flex: 1;
    padding: 10px 2px;
    background: none;
    border: 0;
    color: #9fb6d4;
    cursor: pointer;
    font-size: 11px;
    font-weight: 600;
    min-width: 0;
}

#spHeader button.active {
    background: #2e3646;
    color: #fff;
}

#spClose {
    flex: none !important;
    padding: 10px 10px !important;
    font-size: 15px !important;
    color: #9fb6d4 !important;
    background: none !important;
    flex: 0 0 auto !important;
}

#spClose:hover {
    color: #fff !important;
}

#spSearch {
    margin: 7px 8px;
    padding: 5px 8px;
    background: #2a313d;
    border: 1px solid #444;
    color: #eee;
    border-radius: 4px;
    width: calc(100% - 30px);
}

#spSortRow {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: -2px 8px 6px;
    font-size: 11px;
    color: #9fb6d4;
}

#spSort {
    flex: 1;
    min-width: 0;
    background: #2a313d;
    color: #eee;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 3px 4px;
    font-size: 12px;
}

#spList {
    overflow-y: auto;
    flex: 1;
    padding: 0 4px 8px;
}

.sp-item {
    padding: 6px 8px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
}

.sp-item:hover {
    background: #2e3646;
}

.dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    flex: none;
    border: 1px solid rgba(0, 0, 0, 0.4);
}

.nm {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.cp {
    flex: none;
    opacity: 0.55;
    cursor: pointer;
    font-size: 14px;
}

.cp:hover {
    opacity: 1;
}

.sp-back {
    padding: 6px 8px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    color: #9fb6d4;
    font-size: 12px;
    margin-bottom: 2px;
}

.sp-back:hover {
    background: #2e3646;
}

.sp-player {
    padding: 7px 8px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sp-player:hover {
    background: #2e3646;
}

.sp-player-name {
    font-weight: 600;
    color: #b388ff;
}

.sp-player-count {
    font-size: 11px;
    color: #9fb6d4;
}

.sp-warp-closest {
    font-size: 10px;
    color: #556;
    margin-left: auto;
    padding-right: 2px;
    white-space: nowrap;
}
.sp-warp-closest-link {
    cursor: pointer;
    color: #7a9cbf;
    text-decoration: underline;
}
.sp-warp-closest-link:hover {
    color: #9fb6d4;
}


/*  Context Menu  */

#ctx {
    position: absolute;
    display: none;
    z-index: 25;
    background: #1d222b;
    color: #eee;
    border: 1px solid #444;
    border-radius: 6px;
    overflow: hidden;
    font-size: 13px;
    min-width: 220px;
}

#ctx .hdr {
    padding: 6px 10px;
    background: #12151b;
    color: #9fb6d4;
    font-size: 11px;
    line-height: 1.5;
}

#ctx button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 7px 10px;
    background: none;
    border: 0;
    color: #eee;
    cursor: pointer;
}

#ctx button:hover {
    background: #2e3646;
}

.sep {
    border-top: 1px solid #333;
}


/*  Compass Directions  */

.nesw {
    position: absolute;
    z-index: 11;
    pointer-events: none;
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    text-shadow: 0 0 4px #000, 0 0 8px #000;
    font-family: monospace;
}

#dir-n {
    top: 38px;
    left: 50%;
    transform: translateX(-50%);
}

#dir-s {
    bottom: 6px;
    left: 50%;
    transform: translateX(-50%);
}

#dir-e {
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
}

#dir-w {
    left: 6px;
    top: 50%;
    transform: translateY(-50%);
}

.edge-dot {
    position: absolute;
    z-index: 12;
    pointer-events: none;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.5);
}


/*  Hover Tooltip  */

#hover {
    position: absolute;
    display: none;
    z-index: 15;
    pointer-events: none;
    background: rgba(15, 18, 24, 0.95);
    color: #fff;
    padding: 4px 9px;
    border-radius: 5px;
    font-size: 12px;
    border: 1px solid #555;
}


/*  Toast Notification  */

#toast {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: #2e7d32;
    color: #fff;
    padding: 6px 14px;
    border-radius: 6px;
    display: none;
    z-index: 30;
    font-size: 13px;
    max-width: 70%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}


/*  Language Selection Modal  */

#langModal {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, 0.6);
    display: none;
    align-items: center;
    justify-content: center;
}

#langModal .box {
    background: #1d222b;
    color: #eee;
    padding: 26px 32px;
    border-radius: 10px;
    text-align: center;
}

#langModal button {
    margin: 12px 8px 0;
    padding: 10px 26px;
    font-size: 15px;
    border-radius: 6px;
    border: 1px solid #555;
    background: #2e3646;
    color: #fff;
    cursor: pointer;
}

#langModal button:hover {
    background: #3d4a5c;
}


/*  Welcome Modal  */

#welcomeModal {
    position: fixed;
    inset: 0;
    z-index: 99;
    background: rgba(0, 0, 0, 0.55);
    display: none;
    align-items: center;
    justify-content: center;
}

#welcomeModal .wbox {
    background: #1d222b;
    color: #eee;
    padding: 32px 38px;
    border-radius: 10px;
    text-align: center;
    max-width: 400px;
    line-height: 1.75;
}

#welcomeModal h2 {
    font-size: 17px;
    font-weight: 700;
    margin-bottom: 16px;
    color: #fff;
}

#welcomeModal p {
    font-size: 13px;
    color: #b0bec5;
    margin: 0 0 6px;
}

#welcomeModal .wok {
    margin-top: 18px;
    padding: 9px 34px;
    font-size: 14px;
    background: #5865F2;
    border: none;
    color: #fff;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}

#welcomeModal .wok:hover {
    background: #4752c4;
}


/*  Image Panel  */

#imagePanel {
    width: 0;
    overflow: hidden;
    height: 100%;
    background: #0d1117;
    display: flex;
    flex-direction: column;
    transition: width 0.3s ease;
    border-left: 1px solid #222;
    flex-shrink: 0;
    position: relative;
    z-index: 18;
}

#imagePanel.open {
    width: 50%;
}

#imgHeader {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 10px;
    background: #12151b;
    border-bottom: 1px solid #222;
    min-height: 38px;
    flex-shrink: 0;
}

#imgHeader span {
    font-size: 11px;
    color: #9fb6d4;
    white-space: nowrap;
    flex: none;
}

#imgAuthorSel {
    flex: 1;
    background: #2a313d;
    color: #eee;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 4px 6px;
    font-size: 12px;
    min-width: 0;
}

#imgSortBtn {
    flex: none;
    background: #2a313d;
    border: 1px solid #444;
    color: #9fb6d4;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    padding: 3px 7px;
    white-space: nowrap;
}

#imgSortBtn:hover {
    background: #3a4350;
    color: #fff;
}

#imgClose {
    flex: none;
    background: none;
    border: none;
    color: #9fb6d4;
    font-size: 16px;
    cursor: pointer;
    padding: 2px 6px;
    line-height: 1;
}

#imgClose:hover {
    color: #fff;
}

#imgView {
    flex: 1;
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #08090d;
    min-height: 0;
}

#imgBig {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    cursor: pointer;
    border: none;
    display: none;
}

#imgGeoTag {
    position: absolute;
    top: 8px;
    left: 10px;
    z-index: 4;
    pointer-events: none;
    background: rgba(0, 0, 0, 0.6);
    color: #9fb6d4;
    padding: 3px 7px;
    border-radius: 4px;
    font-size: 10px;
    display: none;
}

#imgMeta {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.85));
    padding: 22px 14px 10px;
    color: #eee;
    font-size: 12px;
    pointer-events: none;
    display: none;
}

#imgMeta .img-title {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 2px;
}

#imgMeta .img-sub {
    color: #9fb6d4;
    font-size: 11px;
}

#imgDownload {
    position: absolute;
    bottom: 10px;
    right: 12px;
    z-index: 5;
    background: rgba(20, 24, 30, 0.88);
    color: #eee;
    border: 1px solid #555;
    border-radius: 5px;
    padding: 5px 11px;
    font-size: 12px;
    cursor: pointer;
    display: none;
}

#imgDownload:hover {
    background: #2e3646;
}

#imgEmpty {
    color: #555;
    font-size: 14px;
    text-align: center;
}

#imgCarousel {
    height: 20%;
    min-height: 90px;
    max-height: 170px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    padding: 6px 10px;
    background: #0d1117;
    border-top: 1px solid #1e2530;
    scroll-behavior: smooth;
}

#imgCarousel::-webkit-scrollbar {
    height: 4px;
}

#imgCarousel::-webkit-scrollbar-thumb {
    background: #333;
    border-radius: 2px;
}

.car-thumb {
    flex: none;
    height: calc(100% - 8px);
    aspect-ratio: 4 / 3;
    object-fit: cover;
    border-radius: 4px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.15s, opacity 0.15s;
    opacity: 0.55;
}

.car-thumb:hover {
    opacity: 0.85;
}

.car-thumb.active {
    border-color: #b388ff;
    opacity: 1;
}

.car-empty {
    color: #444;
    font-size: 12px;
    margin: auto;
}


/*  Screenshot Tool Button  */

#ssBtn {
    position: absolute;
    z-index: 10;
    left: 230px;
    top: 113px; /* sits below #coordBar (moved down one row for the tile search inputs) */
}

#btnScreenshot {
    background: rgba(20, 24, 30, 0.92);
    border: 1px solid #2a313d;
    color: #9fb6d4;
    border-radius: 8px;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    box-sizing: border-box;
}

#btnScreenshot:hover {
    background: rgba(30, 37, 48, 0.98);
}

#btnScreenshot.active {
    background: #1a2d1a;
    border-color: #3a6a3a;
    color: #6fcf6f;
}


/*  Screenshot Drag Selection  */

#ssDragOverlay {
    position: absolute;
    inset: 0;
    z-index: 9;
    cursor: crosshair;
    display: none;
}

#ssDragBox {
    position: absolute;
    border: 2px solid #6fcf6f;
    background: rgba(111, 207, 111, 0.12);
    pointer-events: none;
    z-index: 9;
    display: none;
}


/*  Screenshot Modal  */

#ssModal {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 300px;
    z-index: 50;
    display: none;
    flex-direction: column;
    pointer-events: none;
}

#ssModalInner {
    background: rgba(20, 24, 30, 0.97);
    border-left: 1px solid #2a313d;
    display: flex;
    flex-direction: column;
    width: 100%;
    pointer-events: all;
    overflow: hidden;
    flex: 1;
}

#ssModalHeader {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid #2a313d;
    font-size: 13px;
    font-weight: 700;
    color: #9fb6d4;
    flex-shrink: 0;
}

#ssClose {
    background: none;
    border: none;
    color: #778;
    cursor: pointer;
    font-size: 16px;
    padding: 2px 6px;
}

#ssModalBody {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.ss-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    gap: 8px;
}

.ss-row label {
    color: #9fb6d4;
    flex-shrink: 0;
}

.ss-row span {
    color: #eee;
    font-size: 11px;
    text-align: right;
}

.ss-row select {
    background: #2a313d;
    border: 1px solid #444;
    color: #eee;
    border-radius: 4px;
    padding: 3px 0px;
    font-size: 12px;
}

.ss-row input[type="checkbox"] {
    width: 15px;
    height: 15px;
    accent-color: #6fcf6f;
    cursor: pointer;
}

.ss-sep {
    height: 1px;
    background: #2a313d;
    margin: 3px 0;
}

#ssDl {
    background: #2d4a2d;
    border: 1px solid #3a6a3a;
    color: #6fcf6f;
    border-radius: 5px;
    padding: 8px;
    font-size: 13px;
    cursor: pointer;
    margin-top: 4px;
}

#ssDl:disabled {
    background: #1a2020;
    border-color: #2a313d;
    color: #445;
    cursor: not-allowed;
}

#ssDl:not(:disabled):hover {
    background: #3a6a3a;
}

/* Category groups in warp/waypoint sidebar */
.sp-cat-header {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    font-weight: 700;
    color: #9fb6d4;
    padding: 5px 8px;
    cursor: pointer;
    user-select: none;
    border-top: 1px solid #1e2530;
    margin-top: 2px;
}
.sp-cat-header:hover { background: rgba(255,255,255,.04); }
.sp-cat-arrow { font-size: 9px; color: #556; flex-shrink: 0; }
.sp-cat-label { flex: 1; }
.sp-cat-count { font-size: 10px; color: #556; }
.sp-cat-group { padding-left: 8px; border-left: 2px solid #1e2530; margin-left: 10px; }

/* Linked image highlight in carousel */
.car-thumb-linked {
    outline: 2px solid #9fb6d4;
    outline-offset: 1px;
}
.car-linked-badge {
    position: absolute;
    top: 4px;
    right: 2px;
    font-size: 16px;
    background: rgba(20,24,30,.85);
    border-radius: 3px;
    padding: 2px;
    pointer-events: none;
    z-index: 2;
    line-height: 1;
    width: 16px;
    height: 16px;
    text-align: center;
}


/*  Disputed territory info (hover the faded coastline)  */

#disputeInfo {
    position: absolute;
    display: none;
    z-index: 16;
    pointer-events: none;
    max-width: 300px;
    background: rgba(15, 18, 24, 0.96);
    color: #dde3ec;
    border: 1px solid rgba(255, 196, 92, 0.55);
    border-radius: 8px;
    padding: 9px 12px 10px;
    font-size: 12px;
    line-height: 1.45;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

#disputeInfo .di-tag {
    color: #ffc45c;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    margin-bottom: 2px;
}

#disputeInfo .di-title {
    color: #fff;
    font-weight: 700;
    font-size: 13px;
    margin-bottom: 4px;
}

@media (max-width: 640px), (pointer: coarse) and (max-height: 500px) {
    #disputeInfo {
        max-width: min(320px, calc(100vw - 32px));
        font-size: 13px;
    }
}


/*  Search toggle (phone only; hidden on desktop)  */

#btnSearchToggle {
    display: none;
}

/* Long-pressing the map must not trigger the browser's own text/image menus. */
#map {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

#ssDragOverlay {
    touch-action: none;
}


/* =========================================================================
   Phone layout
   Same controls as desktop, rearranged and resized for small touch screens.
   Applies to portrait phones (<= 640px wide) and landscape phones
   (touch screen, <= 500px tall). The breakpoint matches PHONE_LAYOUT_QUERY
   in app.js.
   ========================================================================= */

@media (max-width: 640px), (pointer: coarse) and (max-height: 500px) {

    :root {
        --safe-top: env(safe-area-inset-top, 0px);
        --safe-right: env(safe-area-inset-right, 0px);
        --safe-bottom: env(safe-area-inset-bottom, 0px);
        --safe-left: env(safe-area-inset-left, 0px);
        --phone-gap: 8px;
        --phone-btn: 38px;      /* round tool buttons */
        --phone-warps-w: 78px;  /* fixed so the Images button can sit next to it */
    }

    html, body, #app {
        height: 100%;
        height: 100dvh;
    }

    /* iOS zooms the whole page when focusing inputs smaller than 16px. */
    input, select, textarea {
        font-size: 16px !important;
    }

    /* ---- Info panel: collapsible card in the top-left ---- */
    #panel {
        top: calc(var(--phone-gap) + var(--safe-top));
        left: calc(var(--phone-gap) + var(--safe-left));
        box-sizing: border-box;
        padding: 9px 12px;
        font-size: 14px;
        max-height: calc(100% - 120px - var(--safe-top) - var(--safe-bottom));
        overflow-y: auto;
        z-index: 21;
    }

    #panel #BTEJapan {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 6px;
        cursor: pointer;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-tap-highlight-color: transparent;
    }

    #panel #BTEJapan::after {
        content: "▾";
        font-size: 12px;
        color: #9fb6d4;
        transition: transform 0.2s;
    }

    #panel.collapsed #BTEJapan::after {
        transform: rotate(-90deg);
    }

    #panel.collapsed {
        width: auto;
        max-width: calc(100vw - var(--phone-warps-w) - 120px - var(--safe-left) - var(--safe-right));
    }

    #panel.collapsed > *:not(#BTEJapan) {
        display: none !important;
    }

    /* Opened: a dropdown that sits above the top-right buttons if it has to overlap them. */
    #panel:not(.collapsed) {
        width: 226px;
        max-width: calc(100vw - 2 * var(--phone-gap) - var(--safe-left) - var(--safe-right));
        z-index: 24;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    }

    #panel label {
        margin: 2px 0;
        padding: 5px 0;
    }

    #panel input[type="checkbox"] {
        width: 17px;
        height: 17px;
        vertical-align: -3px;
        margin: 0 6px 0 0;
    }

    #panel button {
        padding: 8px 4px;
        font-size: 13px;
    }

    #panel select {
        padding: 6px;
    }

    #btnDiscord {
        padding: 9px 10px;
        font-size: 13px;
    }

    /* ---- Online players: stays directly under the info panel ---- */
    #playersPanel {
        top: calc(var(--phone-gap) + var(--safe-top) + var(--panel-h, 36px) + 6px);
        left: calc(var(--phone-gap) + var(--safe-left));
        width: 170px;
        z-index: 20;
    }

    #playersPanelToggle {
        padding: 9px 12px;
        font-size: 13px;
    }

    #playersPanelBody {
        max-height: 40vh;
    }

    .player-name {
        font-size: 13px;
    }

    /* ---- Top-right: [Images] [Warps] ---- */
    #btnSidePanel {
        top: calc(var(--phone-gap) + var(--safe-top));
        right: calc(var(--phone-gap) + var(--safe-right));
        width: var(--phone-warps-w);
        height: var(--phone-btn);
        padding: 0 6px;
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    #topBtns {
        top: calc(var(--phone-gap) + var(--safe-top));
        left: auto;
        right: calc(var(--phone-gap) * 2 + var(--phone-warps-w) + var(--safe-right));
        transform: none;
    }

    #topBtns button {
        height: var(--phone-btn);
        padding: 0 12px;
        font-size: 14px;
    }

    /* ---- Right-hand tool column: [search] [screenshot] ---- */
    #btnSearchToggle,
    #btnScreenshot {
        width: var(--phone-btn);
        height: var(--phone-btn);
        border-radius: 10px;
    }

    #btnSearchToggle {
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        z-index: 21;
        top: calc(var(--phone-gap) * 2 + var(--phone-btn) + var(--safe-top));
        right: calc(var(--phone-gap) + var(--safe-right));
        padding: 0;
        background: rgba(20, 24, 30, 0.92);
        border: 1px solid #2a313d;
        color: #9fb6d4;
        cursor: pointer;
    }

    #btnSearchToggle[aria-expanded="true"] {
        border-color: #9fb6d4;
        color: #fff;
    }

    #ssBtn {
        left: auto;
        top: calc(var(--phone-gap) * 3 + var(--phone-btn) * 2 + var(--safe-top));
        right: calc(var(--phone-gap) + var(--safe-right));
        z-index: 21;
    }

    /* ---- Coordinate / tile search: drops down when the search button is tapped ---- */
    #coordBar {
        display: none;
        top: calc(var(--phone-gap) * 2 + var(--phone-btn) + var(--safe-top));
        left: calc(var(--phone-gap) + var(--safe-left));
        right: calc(var(--phone-gap) * 2 + var(--phone-btn) + var(--safe-right));
        min-width: 0;
        max-width: 420px;
        margin-left: auto;
        padding: 8px;
        gap: 6px;
        z-index: 22;
        box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
    }

    #coordBar.open {
        display: flex;
    }

    #coordBar input {
        padding: 7px 8px;
    }

    #coordBar .tileRow #inTileZ {
        flex: 0 0 74px;
    }

    #serverVersion {
        top: auto;
        left: auto;
        bottom: calc(6px + var(--safe-bottom));
        right: calc(var(--phone-gap) + var(--safe-right));
        margin: 0;
        font-size: 10px;
        z-index: 10;
        pointer-events: none;
    }

    /* ---- Compass letters clear of the top bar ---- */
    #dir-n {
        top: calc(var(--phone-gap) * 2 + var(--phone-btn) + var(--safe-top));
    }

    #dir-s {
        bottom: calc(6px + var(--safe-bottom));
    }

    #dir-e {
        right: calc(4px + var(--safe-right));
    }

    #dir-w {
        left: calc(4px + var(--safe-left));
    }

    /* ---- Bottom-left: zoom buttons + download ---- */
    .ol-zoom {
        bottom: calc(44px + var(--safe-bottom)) !important;
        left: calc(var(--phone-gap) + var(--safe-left)) !important;
    }

    .ol-zoom button {
        width: 38px !important;
        height: 38px !important;
        font-size: 22px !important;
    }

    #btnDownloadData {
        left: calc(var(--phone-gap) + var(--safe-left));
        bottom: calc(var(--phone-gap) + var(--safe-bottom));
        height: 30px;
        font-size: 12px;
    }

    /* ---- Warp / waypoint / builds list: slides in from the right ---- */
    #sidePanel {
        width: min(330px, 88vw);
        padding-top: var(--safe-top);
        padding-bottom: var(--safe-bottom);
        z-index: 23;
        font-size: 14px;
    }

    /* the panel has its own ✕, so hide the buttons it would cover */
    #sidePanel.open ~ #btnSidePanel,
    #btnSidePanel.open,
    #sidePanel.open ~ #topBtns,
    #sidePanel.open ~ #btnSearchToggle,
    #sidePanel.open ~ #ssBtn {
        display: none;
    }

    #spHeader button {
        padding: 13px 2px;
        font-size: 12px;
    }

    #spClose {
        padding: 10px 14px !important;
        font-size: 18px !important;
    }

    #spSearch {
        width: calc(100% - 16px);
        box-sizing: border-box;
        padding: 8px 10px;
    }

    #spSortRow {
        font-size: 13px;
    }

    #spSort {
        padding: 6px;
    }

    .sp-item,
    .sp-player,
    .sp-back {
        padding: 10px 8px;
    }

    .sp-cat-header {
        padding: 11px 8px;
        font-size: 13px;
    }

    .sp-cat-arrow {
        font-size: 11px;
    }

    .sp-cat-count {
        font-size: 12px;
    }

    .cp {
        font-size: 18px;
        padding: 0 4px;
        opacity: 0.7;
    }

    /* ---- Context menu (long-press) ---- */
    #ctx {
        min-width: 230px;
        max-width: calc(100vw - 16px);
        font-size: 15px;
        border-radius: 10px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
        z-index: 40;
    }

    #ctx .hdr {
        padding: 9px 14px;
        font-size: 12px;
    }

    #ctx button {
        padding: 11px 14px;
        font-size: 15px;
    }

    /* ---- Toast ---- */
    #toast {
        bottom: calc(52px + var(--safe-bottom));
        max-width: calc(100vw - 32px);
        white-space: normal;
        text-align: center;
        overflow-wrap: anywhere;
        font-size: 14px;
    }

    /* ---- Modals ---- */
    #langModal .box,
    #welcomeModal .wbox {
        max-width: calc(100vw - 32px);
        box-sizing: border-box;
        padding: 24px 20px;
    }

    #langModal button {
        margin: 12px 6px 0;
        padding: 11px 22px;
    }

    /* ---- Screenshot settings: bottom sheet, so the map above stays draggable ---- */
    #ssModal {
        top: auto;
        width: 100%;
        height: min(58%, 440px);
    }

    #ssModalInner {
        border-left: 0;
        border-top: 1px solid #2a313d;
        border-radius: 12px 12px 0 0;
        padding-bottom: var(--safe-bottom);
    }

    .ss-row {
        font-size: 14px;
        min-height: 32px;
    }

    .ss-row input[type="checkbox"] {
        width: 20px;
        height: 20px;
    }

    #ssDl {
        padding: 11px;
        font-size: 15px;
    }

    /* ---- Image viewer ---- */
    #imgHeader {
        min-height: 44px;
        padding: 6px 10px;
    }

    #imgClose {
        font-size: 20px;
        padding: 6px 8px;
    }

    #imgSortBtn {
        padding: 6px 8px;
        font-size: 12px;
    }

    #imgDownload {
        padding: 8px 12px;
        font-size: 13px;
    }

    .car-thumb {
        opacity: 0.7;
    }
}

/* Portrait phones: the image viewer opens as a sheet under the map instead of
   taking half the (already narrow) width. */
@media (max-width: 640px) {
    #app {
        flex-direction: column;
    }

    #mapWrap {
        flex: 1;
        height: auto;
        min-height: 0;
    }

    #imagePanel {
        width: 100%;
        height: 0;
        border-left: 0;
        border-top: 1px solid #222;
        transition: height 0.3s ease;
    }

    #imagePanel.open {
        width: 100%;
        height: 52%;
        padding-bottom: var(--safe-bottom);
        box-sizing: border-box;
    }

    #imgCarousel {
        min-height: 76px;
        height: 22%;
    }
}
