:root {
    --panel-bg: rgba(18, 22, 32, 1);
    --panel-border: rgba(255, 255, 255, 0.14);
    --panel-sep: rgba(255, 255, 255, 0.10);
    --text: #ffffff;
    --muted: rgba(255, 255, 255, 0.75);
    --btn-bg: rgba(255, 255, 255, 0.10);
    --btn-bg-hover: rgba(255, 255, 255, 0.16);
    --btn-border: rgba(255, 255, 255, 0.16);
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
    --radius: 12px;
    --gap: 10px;
    --pad: 12px;
    --w: min(360px, calc(100vw - 24px));
    --app-modal-title-size: 18px;
    --ui-tooltip-bg: rgba(18, 22, 32, 0.96);
    --ui-tooltip-border: var(--panel-border);
    --ui-tooltip-text: var(--text);
    --ui-tooltip-radius: 8px;
    --ui-tooltip-shadow: var(--shadow);
    --ui-tooltip-font-size: 12px;
    --ui-tooltip-offset: 8px;
    --ui-tooltip-padding: 6px 8px;
}

html, body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: #0b0e14;
    color: var(--text);
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji";
}

[hidden] {
    display: none !important;
}

/* Global scrollbar theme */
* {
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.28) rgba(255, 255, 255, 0.08);
}

*::-webkit-scrollbar {
    width: 12px;
    height: 12px;
}

*::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.08);
    border-radius: 999px;
}

*::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.28);
    border: 2px solid rgba(18, 22, 32, 0.9);
    border-radius: 999px;
}

*::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.4);
}

/* Fullscreen stage */
#stage {
    position: fixed;
    inset: 0;
    background: #0b0e14;
    touch-action: none;
    user-select: none;
}

#stage.cursor-pan {
    cursor: grab;
}

#stage.cursor-pan.is-panning {
    cursor: grabbing;
}

#stage.cursor-brush {
    cursor: url("../assets/cursors/brush.svg") 8 8, crosshair;
}

#stage.cursor-eraser {
    cursor: url("../assets/cursors/eraser.svg") 8 8, cell;
}

#stage.cursor-token-resize {
    cursor: url("../assets/cursors/token-resize.png") 16 16, nwse-resize;
}

.touchContextMenu {
    position: fixed;
    z-index: 2600;
    min-width: 220px;
    max-width: min(280px, calc(100vw - 16px));
    display: grid;
    gap: 8px;
    padding: 10px;
    background: rgba(18, 22, 32, 0.96);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    box-shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(8px);
}

.touchContextMenuItem {
    width: 100%;
    justify-content: flex-start;
    border-radius: 10px;
    min-height: 48px;
    padding: 12px 14px;
    font-size: 15px;
}

.touchContextMenuItemIcon {
    width: 18px;
    height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 18px;
}

.touchContextMenuItemIcon svg {
    width: 18px;
    height: 18px;
    display: block;
}

.touchContextMenuItemLabel {
    line-height: 1.2;
}

.touchContextMenuItem.active {
    background: rgba(122, 162, 255, 0.25);
    border-color: rgba(122, 162, 255, 0.55);
}

.touchContextMenuItem.danger {
    background: rgba(208, 63, 63, 0.2);
    border-color: rgba(232, 106, 106, 0.6);
}

.touchContextMenuItem:disabled {
    opacity: 0.6;
    cursor: default;
}

/* Brush size cursor overlay */
.brush-cursor {
    position: fixed;
    pointer-events: none;
    z-index: 9999;
    border: 2px solid rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
    display: none;
    box-sizing: border-box;
}

#map, #fog {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}

#mapLoadingOverlay {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: #0b0e14;
    color: #fff;
    font-size: 20px;
    line-height: 1;
    letter-spacing: 0.3px;
    z-index: 10;
    pointer-events: none;
}

#mapLoadingOverlay::before {
    content: "";
    display: block;
    flex: 0 0 auto;
    width: 18px;
    height: 18px;
    border-radius: 999px;
    border: 2px solid rgba(255, 255, 255, 0.35);
    border-top-color: #ffffff;
    animation: map-loading-spin 0.8s linear infinite;
}

#stage.is-loading #map,
#stage.is-loading #fog {
    opacity: 0;
}

#stage.is-loading #mapLoadingOverlay {
    display: flex;
}

@keyframes map-loading-spin {
    to {
        transform: rotate(360deg);
    }
}

.shareBadge {
    position: fixed;
    top: calc(12px + env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 1100;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.16);
    background: rgba(18, 22, 32, 0.78);
    color: var(--text);
    font-size: 12px;
    line-height: 1;
    letter-spacing: 0.1px;
    backdrop-filter: blur(8px);
    pointer-events: none;
}

.shareBadgeDot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #8d97aa;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12);
}

.shareBadge[data-state="paused"] .shareBadgeDot {
    background: #8d97aa;
}

.shareBadge[data-state="starting"] .shareBadgeDot {
    background: #5ba7ff;
}

.shareBadge[data-state="live"] .shareBadgeDot {
    background: #3fd089;
}

.shareBadge[data-state="reconnecting"] .shareBadgeDot {
    background: #f2a757;
}

.shareBadge[data-state="error"] .shareBadgeDot {
    background: #e86a6a;
}

.toolQuickOff {
    position: fixed;
    top: calc(54px + env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 1120;
    height: 44px;
    padding: 0 18px;
    gap: 8px;
    font-size: 15px;
    font-weight: 600;
    border-radius: 999px;
    border: 1px solid rgba(122, 162, 255, 0.45);
    background: rgba(18, 22, 32, 0.92);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(8px);
}

@media (max-width: 720px) {
    .toolQuickOff {
        top: auto;
        bottom: calc(64px + env(safe-area-inset-bottom));
    }

    body:has(#ui .panel.active) #toolQuickOff {
        display: none;
    }
}

.toolQuickOffIcon {
    position: relative;
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.toolQuickOffIcon svg {
    width: 20px;
    height: 20px;
    display: block;
}

.toolQuickOffIcon.is-struck::after {
    content: "";
    position: absolute;
    left: -2px;
    top: 9px;
    width: 24px;
    border-top: 2px solid rgba(255, 120, 120, 0.95);
    transform: rotate(-32deg);
    transform-origin: center;
    border-radius: 2px;
}

.toolQuickOffIcon.is-struck::before {
    content: "";
    position: absolute;
    left: -2px;
    top: 9px;
    width: 24px;
    border-top: 2px solid rgba(255, 120, 120, 0.95);
    transform: rotate(32deg);
    transform-origin: center;
    border-radius: 2px;
}

/* Panels overlay */
#ui {
    position: fixed;
    top: calc(12px + env(safe-area-inset-top));
    left: calc(12px + env(safe-area-inset-left));
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: var(--gap);
    width: var(--w);
    pointer-events: none;
}

.panel {
    pointer-events: auto;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
    overflow: hidden;
}

.panel.is-drag-scrolling {
    cursor: grabbing;
}

.panel > summary {
    list-style: none;
    cursor: pointer;
    padding: 10px var(--pad);
    font-weight: 650;
    user-select: none;
    color: var(--text);
}

.panel > summary::-webkit-details-marker {
    display: none;
}

.panel > summary::after {
    content: "▾";
    float: right;
    opacity: 0.85;
}

.panel:not([open]) > summary::after {
    content: "▸";
}

.panelBody {
    padding: 10px var(--pad) var(--pad);
    border-top: 1px solid var(--panel-sep);
    display: grid;
    gap: 10px;
}

.panelCloseRow {
    display: none;
}

.panelCloseButton {
    width: 32px;
    height: 32px;
    padding: 0;
    font-size: 16px;
    font-weight: 600;
    line-height: 1;
}

.row {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.row.devOnly,
.divider.devOnly {
    display: none !important;
}

body.isDev .row.devOnly {
    display: flex !important;
}

body.isDev .divider.devOnly {
    display: block !important;
}

.row > label {
    color: var(--muted);
    font-size: 14px;
}

.row > label.uploadButton {
    color: var(--text);
    font-size: 16px;
}

button {
    appearance: none;
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--text);
    border-radius: 10px;
    padding: 8px 10px;
    font: inherit;
    font-weight: 400;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    line-height: 1;
}

button:hover {
    background: var(--btn-bg-hover);
}

button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

button:disabled:hover {
    background: var(--btn-bg);
}

button.active {
    background: rgba(122, 162, 255, 0.25);
    border-color: rgba(122, 162, 255, 0.45);
}

button.secondary {
    background: rgba(255, 255, 255, 0.06);
}

button.danger {
    background: rgba(208, 63, 63, 0.22);
    border-color: rgba(232, 106, 106, 0.65);
    color: #ffdede;
}

button.danger:hover {
    background: rgba(208, 63, 63, 0.34);
    border-color: rgba(255, 140, 140, 0.8);
    color: #fff5f5;
}

button.ghost {
    background: transparent;
    border-color: transparent;
    color: var(--muted);
}

button.ghost:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text);
}

/* Segmented control for Pan/Cover/Reveal */
.segmented-control {
    display: flex;
    gap: 8px;
    background: var(--btn-bg);
    border: 1px solid var(--btn-border);
    border-radius: 10px;
    padding: 4px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.segmented-control button {
    flex: 1;
    border: none;
    background: transparent;
    border-radius: 8px;
    padding: 8px 12px;
    transition: all 0.15s ease;
    min-width: 44px;
}

.segmented-control button:hover {
    background: var(--btn-bg-hover);
}

.segmented-control button.active {
    background: rgba(122, 162, 255, 0.35);
    border: 2px solid rgba(122, 162, 255, 0.7);
    box-shadow: 0 0 8px rgba(122, 162, 255, 0.3);
}

/* Make fog mode buttons feel stateful (pressed/toggled). */
#toolSegmentedControl button {
    transform: translateY(-1px);
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.24);
    box-shadow:
        0 2px 0 rgba(255, 255, 255, 0.1),
        0 5px 10px rgba(0, 0, 0, 0.22);
    transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}

#toolSegmentedControl button.active {
    transform: translateY(1px);
    background: rgba(90, 142, 255, 0.42);
    border: 1px solid rgba(153, 188, 255, 0.9);
    box-shadow:
        inset 0 2px 6px rgba(0, 0, 0, 0.45),
        0 0 0 1px rgba(122, 162, 255, 0.35);
}

button svg {
    display: block;
    flex-shrink: 0;
}

input[type="range"] {
    width: 100%;
}

input[type="number"] {
    width: 55px;
    border-radius: 10px;
    border: 1px solid var(--btn-border);
    background: rgba(0, 0, 0, 0.25);
    color: var(--text);
    padding: 8px 10px;
    font: inherit;
}

input[type="file"] {
    width: 100%;
    color: var(--muted);
}

.fileInput {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.uploadButton {
    appearance: none;
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--text);
    border-radius: 10px;
    padding: 8px 10px;
    font: inherit;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    line-height: normal;
}

.uploadButton:hover {
    background: var(--btn-bg-hover);
}

button.secondary {
    background: rgba(255, 255, 255, 0.06);
}

.shareStatus {
    color: var(--muted);
    font-size: 13px;
}

.shareRow {
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
}

.shareRow .shareStatus {
    flex: 1 1 auto;
    min-width: 0;
}

.gridSettingsTable {
    display: grid;
    grid-template-columns: max-content minmax(96px, 1fr);
    align-items: center;
    column-gap: 10px;
    row-gap: 8px;
}

.gridCellLabel {
    color: var(--muted);
    font-size: 14px;
}

.gridField {
    position: relative;
    display: block;
    min-width: 0;
    width: auto;
    justify-self: start;
}

.gridFieldRow {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    width: 100%;
}

.gridFieldRow number-stepper {
    flex: 0 0 150px;
    width: 150px;
    min-width: 150px;
}

.gridSettingsTable > number-stepper {
    width: 150px;
    min-width: 150px;
}

.gridFieldInput {
    width: 100%;
    min-width: 0;
    padding-right: 0;
}

.gridFieldUnit {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--muted);
    font-size: 13px;
    pointer-events: none;
}

/* Toggle Switch */
.toggleSwitch {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    user-select: none;
}

.toggleSwitch input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.toggleSwitch .slider {
    position: relative;
    width: 44px;
    height: 24px;
    background: rgba(255, 255, 255, 0.16);
    border-radius: 24px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.toggleSwitch .slider::before {
    content: "";
    position: absolute;
    width: 18px;
    height: 18px;
    background: #fff;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    transition: all 0.2s ease;
}

.toggleSwitch input[type="checkbox"]:checked + .slider {
    background: rgba(76, 123, 217, 0.6);
    border-color: rgba(76, 123, 217, 0.8);
}

.toggleSwitch input[type="checkbox"]:checked + .slider::before {
    transform: translateX(20px);
}

.toggleSwitch .label-text {
    color: var(--text);
    font-size: 14px;
    font-weight: 500;
}

.toggleSwitch input[type="checkbox"]:disabled + .slider {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Token palette */
#tokenPalette {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 8px;
    --token-palette-cell-size: 44px;
    --token-palette-rows: 2;
    min-height: calc(
            var(--token-palette-rows) * var(--token-palette-cell-size) +
            (var(--token-palette-rows) - 1) * 8px
    );
    touch-action: none;
}

.token {
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    height: 44px;
    width: 44px;
    display: grid;
    place-items: center;
    font-size: 22px;
    background: rgba(255, 255, 255, 0.06);
    cursor: grab;
    touch-action: none;
    overflow: hidden;
}

#tokenPalette .token.is-selected {
    border-color: rgba(122, 162, 255, 0.9);
    box-shadow: 0 0 0 2px rgba(122, 162, 255, 0.35), 0 0 10px rgba(122, 162, 255, 0.28);
}

@media (pointer: coarse) {
    #tokenPalette {
        touch-action: pan-x;
    }
}

.token-image img {
    width: auto;
    height: auto;
    max-width: 80%;
    max-height: 80%;
    object-fit: contain;
    object-position: center;
    display: block;
    pointer-events: none;
    user-select: none;
}

.small {
    font-size: 13px;
    color: var(--muted);
    line-height: 1.2;
}

.panelHelp {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border: 1px solid var(--panel-border);
    border-radius: var(--radius);
    background: var(--panel-bg);
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
    pointer-events: auto;
}

#panelTokens.active + .panelHelpTokens,
#panelFog.active + .panelHelpFog,
#panelMap.active + .panelHelpMap,
#panelGrid.active + .panelHelpGrid,
#panelDev.active + .panelHelpDev {
    display: flex;
}

.panelHelpIcon {
    width: 18px;
    height: 18px;
    border-radius: 999px;
    background: rgba(122, 162, 255, 0.25);
    border: 1px solid rgba(122, 162, 255, 0.45);
    color: #d6e4ff;
    font-size: 12px;
    font-weight: 700;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 18px;
}

.panelHelpText {
    font-size: 13px;
    color: var(--muted);
    line-height: 1.2;
}

.helpQuickRef {
    display: grid;
    gap: 10px;
}

.helpQuickRefIntro {
    margin: 0;
    line-height: 1.35;
}

.helpQuickRefSection {
    display: grid;
    gap: 4px;
}

.helpQuickRefTitle {
    margin: 0;
    font-weight: 700;
    color: var(--text);
    line-height: 1.35;
}

.helpQuickRefList {
    margin: 0;
    padding-left: 16px;
    display: grid;
    gap: 3px;
    line-height: 1.35;
}

/* Divider between sections in panels */
.divider {
    border: none;
    height: 1px;
    background: var(--panel-sep);
    margin: 4px 0;
}

.modal {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    pointer-events: none;
}

.modal.isOpen {
    display: flex;
    pointer-events: auto;
}

.modalBackdrop {
    position: absolute;
    inset: 0;
    background: transparent;
    pointer-events: auto;
}

.modalPanel {
    position: relative;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    min-width: 320px;
    max-width: min(420px, calc(100vw - 32px));
    z-index: 1;
    pointer-events: auto;
}

#confirmDialog .modalPanel {
    background: var(--panel-bg);
    border-color: var(--panel-border);
}

.modalHeader {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.modalTitle {
    font-weight: 650;
}

.modalMessage {
    margin: 0 0 12px;
    color: var(--muted);
    font-size: 14px;
}

.modalActions {
    display: grid;
    gap: 8px;
}

.iconButton {
    border: 1px solid var(--btn-border);
    background: transparent;
    color: var(--text);
    border-radius: 8px;
    width: 28px;
    height: 28px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.visuallyHidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.hasTooltip {
    position: relative;
}

.hasTooltip[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    right: 0;
    top: calc(100% + var(--ui-tooltip-offset));
    z-index: 2001;
    min-width: max-content;
    max-width: 180px;
    padding: var(--ui-tooltip-padding);
    border-radius: var(--ui-tooltip-radius);
    border: 1px solid var(--ui-tooltip-border);
    background: var(--ui-tooltip-bg);
    color: var(--ui-tooltip-text);
    font-size: var(--ui-tooltip-font-size);
    line-height: 1.2;
    letter-spacing: 0.1px;
    box-shadow: var(--ui-tooltip-shadow);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translateY(-2px);
    transition: opacity 120ms ease, transform 120ms ease;
}

.hasTooltip[data-tooltip]:hover::after,
.hasTooltip[data-tooltip]:focus-visible::after {
    opacity: 1;
    transform: translateY(0);
}

.mapPreviewSection {
    display: grid;
    gap: 8px;
}

.mapPreviewList {
    display: grid;
    grid-template-columns: repeat(3, minmax(96px, 1fr));
    gap: 8px;
    justify-content: start;
}

.mapPreviewItem {
    border: 1px solid var(--btn-border);
    background: rgba(0, 0, 0, 0.25);
    border-radius: 10px;
    padding: 6px;
    display: grid;
    gap: 6px;
    position: relative;
    max-width: 100%;
}

.mapPreviewItem:hover {
    background: rgba(255, 255, 255, 0.08);
}

.mapPreviewItem.is-active {
    border-color: rgba(122, 162, 255, 0.7);
    box-shadow: 0 0 0 1px rgba(122, 162, 255, 0.35), 0 0 12px rgba(122, 162, 255, 0.25);
}

.mapPreviewImage {
    display: block;
    width: 100%;
    aspect-ratio: 1 / 1;
    object-fit: contain;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.35);
    max-height: 180px;
}

.mapPreviewImage.is-error {
    opacity: 0.35;
}

.mapPreviewSelect {
    appearance: none;
    border: none;
    background: transparent;
    padding: 0;
    margin: 0;
    display: block;
    width: 100%;
    cursor: pointer;
    text-align: left;
}

.mapPreviewDelete {
    width: 28px;
    height: 28px;
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.55);
    border-color: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(4px);
}

.mapPreviewDelete:hover {
    color: #ff9b9b;
    border-color: rgba(255, 155, 155, 0.6);
}

#tokenSettingsModal {
    --app-modal-panel-min-width: min(336px, calc(100vw - 32px));
    --app-modal-panel-width: min(336px, calc(100vw - 32px));
    --app-modal-panel-max-width: calc(100vw - 32px);
}

#assetLibraryModal {
    --app-modal-panel-width: min(760px, calc(100vw - 32px));
    --app-modal-panel-max-width: calc(100vw - 32px);
    --app-modal-panel-min-width: min(360px, calc(100vw - 32px));
    --app-modal-panel-height: min(80vh, calc(100vh - 32px));
    --app-modal-panel-max-height: calc(100vh - 32px);
    --app-modal-title-size: 14px;
    --app-modal-title-line-height: 1;
    --library-list-max-height: none;
}

.libraryModalTabs {
    display: inline-grid;
    grid-auto-flow: column;
    align-items: center;
    gap: 2px;
    padding: 2px;
    border: 1px solid var(--btn-border);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.06);
}

.libraryModalHeaderControls {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.libraryModalTab {
    border-radius: 9px;
    border-color: transparent;
    background: transparent;
    padding: 6px 14px;
    font-size: 13px;
    font-weight: 600;
    min-height: 30px;
}

.libraryModalTab.is-active {
    background: rgba(122, 162, 255, 0.35);
    border-color: rgba(122, 162, 255, 0.72);
}

.libraryModalTab:disabled {
    opacity: 1;
    cursor: default;
}

.libraryModalTab:disabled:hover {
    background: rgba(122, 162, 255, 0.35);
}

.libraryModalTabs--previewSize .libraryModalTab {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding-inline: 12px;
}

.libraryModalTabIcon {
    width: 14px;
    height: 14px;
    fill: currentColor;
    opacity: 0.9;
    flex: 0 0 auto;
}



/* Hide desktop-only on small screens */
@media (max-width: 720px) {
    .desktopOnly {
        display: none !important;
    }
}

.touchOnly {
    display: none !important;
}

/* Show touch-specific hints on touch devices and small-screen mobile layouts */
@media (max-width: 720px), (hover: none) and (pointer: coarse) {
    .touchOnly {
        display: block !important;
    }
}

/* Tab bar - visible on all screen sizes */
.mobileTabs {
    display: flex;
    pointer-events: auto;
    background: var(--panel-bg);
    backdrop-filter: blur(10px);
    border: 1px solid var(--panel-border);
    border-radius: var(--radius);
    overflow: hidden;
    order: -1; /* Show tabs first (above panels) on desktop */
}

.mobileTab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 8px 4px;
    background: transparent;
    border: none;
    border-radius: 0;
    color: var(--muted);
    font-size: 10px;
    cursor: pointer;
}

.mobileTab svg {
    width: 22px;
    height: 22px;
}

.mobileTab.active {
    color: var(--text);
    background: rgba(255, 255, 255, 0.08);
}

/* Hide all panels by default - show via tabs */
#ui .panel {
    display: none;
    pointer-events: auto;
    overflow-y: auto;
}

/* Show active panel */
#ui .panel.active {
    display: block;
}

/* Hide summary (we use tabs instead) */
#ui .panel > summary {
    display: none;
}

/* Mobile styles - bottom tabs UI */
@media (max-width: 720px) {
    :root {
        --mobile-panel-help-height: 52px;
    }

    :root {
        --w: calc(100vw - 24px);
    }

    .mapPreviewList {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scroll-snap-type: x proximity;
        touch-action: pan-x;
        padding-bottom: 4px;
    }

    .mapPreviewItem {
        flex: 0 0 96px;
        scroll-snap-align: start;
    }

    #assetLibraryModal {
        --app-modal-panel-min-width: 100vw;
        --app-modal-panel-width: 100vw;
        --app-modal-panel-max-width: 100vw;
        --app-modal-panel-height: 100vh;
        --app-modal-panel-max-height: 100vh;
        --app-modal-panel-left: 0;
        --app-modal-panel-top: 0;
        --app-modal-panel-transform: none;
        --app-modal-panel-radius: 0;
        --app-modal-panel-padding: 16px 12px calc(16px + env(safe-area-inset-bottom));
        --app-modal-header-gap: 6px;
        --library-gap: 6px;
    }

    .libraryModalTabs {
        width: 100%;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 2px;
    }

    .libraryModalHeaderControls {
        width: 100%;
        gap: 10px;
    }

    .libraryModalTabs--previewSize {
        grid-template-columns: 1fr 1fr 1fr 1fr;
    }

    .libraryModalTab {
        width: 100%;
        padding: 8px 10px;
    }

    .libraryModalTabs--previewSize .libraryModalTab {
        justify-content: center;
        gap: 6px;
    }

    @supports (height: 100dvh) {
        #assetLibraryModal {
            --app-modal-panel-height: 100dvh;
            --app-modal-panel-max-height: 100dvh;
        }
    }

    #tokenSettingsModal {
        --app-modal-panel-min-width: 100vw;
        --app-modal-panel-width: 100vw;
        --app-modal-panel-max-width: 100vw;
        --app-modal-panel-left: 0;
        --app-modal-panel-top: 100%;
        --app-modal-panel-transform: translate(0, -100%);
        --app-modal-panel-radius: 0;
    }

    #tokenPalette {
        /* 3–4 řádky: změň 3 na 4 podle preference */
        --token-palette-cell-size: 52px;
        grid-template-rows: repeat(2, 52px);
        grid-auto-flow: column; /* plní po sloupcích, pak doprava */
        grid-auto-columns: 52px; /* šířka “sloupce” = velikost tokenu */
        grid-template-columns: none; /* vypnout desktop/mobile columns setup */

        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;

        /* aby se scrolovalo uvnitř palety a nesnažilo se to panovat mapu */
        touch-action: pan-x;
        padding-top: 4px;
        padding-bottom: 6px;
    }

    #tokenPalette .token {
        width: 52px;
        height: 52px;
        font-size: 24px;
    }

    /* Tab bar at bottom */
    #ui {
        position: fixed;
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        display: flex;
        flex-direction: column;
        gap: 0;
        padding: 0;
        z-index: 1000;
        pointer-events: none;
    }

    /* Panels positioned above tab bar */
    #ui .panel {
        position: absolute;
        bottom: calc(52px + env(safe-area-inset-bottom));
        left: 0;
        right: 0;
        overflow-y: auto;
        border-radius: 0;
        border-left: none;
        border-right: none;
        margin: 0;
        z-index: 1300; /* Above fullscreen button (1200) */
    }

    #ui .panel.active {
        bottom: calc(52px + env(safe-area-inset-bottom));
    }

    #panelTokens.active,
    #panelFog.active,
    #panelMap.active,
    #panelGrid.active,
    #panelDev.active {
        bottom: calc(52px + var(--mobile-panel-help-height) + env(safe-area-inset-bottom));
    }

    #ui .panelHelp {
        display: none;
        position: absolute;
        left: 0;
        right: 0;
        bottom: calc(52px + env(safe-area-inset-bottom));
        height: var(--mobile-panel-help-height);
        min-height: 0;
        margin: 0;
        padding: 0 var(--pad);
        align-items: center;
        border-top: 1px solid var(--panel-border);
        border-radius: 0;
        border-left: none;
        border-right: none;
        z-index: 1290;
    }

    /* Panel body adjustments */
    #ui .panel > summary {
        display: none !important;
    }

    #ui .panel .panelBody {
        padding: var(--pad);
        position: relative;
    }

    #ui #panelFog .panelBody {
        padding: 25px var(--pad) var(--pad) var(--pad);
    }

    .panelCloseRow {
        display: flex;
        justify-content: flex-end;
        position: absolute;
        top: 8px;
        right: 8px;
        margin: 0;
        z-index: 2;
    }

    /* Tab bar at bottom on mobile */
    .mobileTabs {
        order: 1; /* Reset order - tabs at bottom on mobile */
        border-radius: 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
        border-top: 1px solid var(--panel-border);
        padding-bottom: env(safe-area-inset-bottom);
    }


    /* Move FAB buttons above tab bar on mobile */
    #toggleFullscreen {
        bottom: calc(60px + env(safe-area-inset-bottom));
    }

    #homeBtn {
        top: calc(12px + env(safe-area-inset-top));
        bottom: auto;
    }
}

/* Floating action buttons (bottom-right) */
.fab {
    position: fixed;
    z-index: 1200;
    width: 52px;
    height: 52px;
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    background: rgba(18, 22, 32, 0.78);
    color: #fff;
    box-shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
    backdrop-filter: blur(10px);
    display: grid;
    place-items: center;
    padding: 0;
    text-decoration: none;
}

.fab-fullscreen {
    right: calc(14px + env(safe-area-inset-right));
    bottom: calc(14px + env(safe-area-inset-bottom));
}

.fab-home {
    right: calc(12px + env(safe-area-inset-right));
    top: calc(12px + env(safe-area-inset-top));
    bottom: auto;
}

.fab-home svg {
    width: 22px;
    height: 22px;
    fill: currentColor;
}

.fab-home-logo {
    width: 50px;
    height: 50px;
    object-fit: contain;
    display: block;
    border-radius: 6px;
}

#homeBtn {
    display: grid;
}

.fab .icon {
    width: 22px;
    height: 22px;
    fill: currentColor;
}

.fab:active {
    transform: translateY(1px);
}

/* Hide FAB buttons when a panel is open on mobile */
@media (max-width: 768px) {
    body:has(#ui .panel.active) #toggleFullscreen {
        display: none;
    }
}

/* Icon toggle in fullscreen via :fullscreen */
.fab .icon-exit {
    display: none;
}

:fullscreen .fab .icon-enter {
    display: none;
}

:fullscreen .fab .icon-exit {
    display: block;
}

/* Dev panel - hidden by default, shown via JS on localhost */
#ui .panel.devPanel {
    border-color: rgba(255, 180, 50, 0.4);
    max-height: none;
    overflow: visible;
    display: none;
}

body.isDev #ui .panel.devPanel {
    display: block;
}

#ui .panel.devPanel > summary {
    display: block; /* Show summary for dev panel */
    color: #ffb432;
}

.devPanel code {
    background: rgba(255, 255, 255, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
}

body.readOnly #ui,
body.readOnly .mobileTabs {
    pointer-events: none;
}

body.readOnly #ui {
    display: none;
}

body.isRemote #ui,
body.isRemote .mobileTabs {
    pointer-events: none;
}

body.isRemote #ui {
    display: none;
}





