:root {
    --bg-color: #1e1e1e;
    --sidebar-bg: #252526;
    --text-color: #d4d4d4;
    --accent: #0e639c;
    --grid-line: #333;
    --cell-size: 32px;
}

body {
    /* Lexend would be cool here, maybe consider in-game retro gaming font as well*/
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    display: flex;
    height: 100vh;
    overflow: hidden;
}

#sidebar {
    width: 333px;
    min-width: 150px;
    max-width: 800px;
    flex: 0 0 333px;
    background-color: var(--sidebar-bg);
    padding: 20px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #000;
    overflow: hidden;
    position: relative;
}

#sidebar-resize {
    position: absolute;
    right: 0;
    top: 0;
    width: 5px;
    height: 100%;
    cursor: col-resize;
    background: transparent;
    user-select: none;
}

#sidebar-resize:hover {
    background: var(--accent);
}

h2, h3 { margin-top: 0; }

.control-group { margin-bottom: 20px; border-bottom: 1px solid #444; padding-bottom: 10px; }

label { display: block; margin-bottom: 5px; font-size: 0.9em; }
input[type="number"] {
    background: #3c3c3c;
    border: 1px solid #555;
    color: white;
    padding: 5px;
    width: 60px;
}

input[type="text"] {
    background: #3c3c3c;
    border: 1px solid #555;
    color: white;

}

button {
    background-color: var(--accent);
    color: white;
    border: none;
    padding: 8px 12px;
    cursor: pointer;
    margin-top: 5px;
    border-radius: 4px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-size: 14px;
    transition: opacity 0.2s ease;
}
button:hover { opacity: 0.9; }
button:active { opacity: 0.8; }

.tiles-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.layer-toggle {
    display: inline-flex;
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 6px;
    overflow: hidden;
}

.layer-toggle button {
    margin: 0;
    border-radius: 0;
    padding: 6px 10px 8px 10px;
    background: #2b2b2b;
    border-right: 1px solid #333;
    margin-top: 0;
    font-size: 12px;
    transition: opacity 0.2s ease;
    color: white;
}

.layer-toggle button:last-child {
    border-right: none;
}

.layer-toggle button.active {
    color: white;
}

.layer-toggle #toggle-foreground.active {
    background: #2d7d46;
}

.layer-toggle #toggle-background.active {
    background: #0066CC;
}

.layer-toggle button:hover {
    opacity: 0.9;
}

.layer-toggle button:active {
    opacity: 0.8;
}

#palette-wrapper {
    overflow-y: auto;
    max-height: calc(100dvh - 100px);
    flex: 1;
    overflow-x: visible;
}

#palette {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(48px, 1fr));
    gap: 5px;
}

.tile-btn.hidden-bg {
    display: none;
}

@media (max-width: 900px) {
    #palette {
        grid-template-columns: repeat(auto-fit, minmax(48px, 1fr));
        max-width: 100%;
        padding-right: 35px;
    }
}

#rightbar {
    width: 320px;
    min-width: 150px;
    max-width: 800px;
    flex: 0 0 320px;
    background-color: var(--sidebar-bg);
    padding: 20px;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #000;
    overflow: hidden;
    gap: 12px;
    position: relative;
}

#rightbar-resize {
    position: absolute;
    left: 0;
    top: 0;
    width: 5px;
    height: 100%;
    cursor: col-resize;
    background: transparent;
    user-select: none;
}

#rightbar-resize:hover {
    background: var(--accent);
}

.rightbar-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 12px;
    border-bottom: 1px solid #444;
}

.rightbar-tab {
    padding: 8px 16px;
    background: #1e1e1e;
    border: 1px solid #444;
    border-bottom: none;
    cursor: pointer;
    color: #888;
    font-size: 14px;
    transition: all 0.2s;
    border-radius: 4px 4px 0 0;
}

.rightbar-tab:hover {
    background: #2a2a2a;
    color: #aaa;
}

.rightbar-tab.active {
    background: var(--sidebar-bg);
    color: var(--text-color);
    border-bottom: 1px solid var(--sidebar-bg);
    margin-bottom: -1px;
}

.rightbar-tab-content {
    display: none;
    flex-direction: column;
    gap: 12px;
    flex: 1;
    overflow-y: auto;
}

.rightbar-tab-content.active {
    display: flex;
}

.tile-btn {
    width: 48px;
    height: 48px;
    border: 2px solid #444;
    cursor: pointer;
    position: relative;
    background-size: cover;
    background-position: center;
    background-color: #000;
    image-rendering: pixelated;
    z-index: 1;
}

.tile-btn:hover {
    z-index: 100;
}

.tile-btn.active {
    border-color: #fff;
    box-shadow: 0 0 5px var(--accent);
}

.tile-char {
    position: absolute;
    bottom: 0;
    right: 0;
    background: rgba(0,0,0,0.7);
    color: white;
    font-size: 10px;
    padding: 1px 3px;
}

.tile-tooltip {
    position: fixed;
    background: #333;
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    z-index: 1000;
    font-size: 10px;
    pointer-events: none;
    display: none;
    color: white;
}

.cell {
    position: relative;
    overflow: visible;
    contain: layout style;
    will-change: contents;
}

.cell-layer {
    position: absolute;
    inset: 0;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    image-rendering: pixelated;
    pointer-events: none;
    contain: layout style paint;
}

.cell-fg-layer.custom-texture {
    inset: auto;
    z-index: 100;
}

.cell-bg-layer {
    filter: brightness(0.35);
    opacity: 0.85;
}

.cell-fg-layer {
    z-index: 2;
    transition: transform 0.1s;
}

.offset-marker {
    position: absolute;
    width: 4px; height: 4px;
    background: #D69D36;
    border-radius: 50%;
    left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    z-index: 10;
    display: none;
    pointer-events: none;
}
.cell.has-offset .offset-marker { display: block; }
.cell.selected-for-offset { outline: 2px solid #D69D36; z-index: 10; }

#main {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    padding: 20px;
    align-items: stretch;
    justify-content: flex-start;
    background-color: #111;
    min-height: 0;
}

#canvas-container {
    position: relative;
    overflow: hidden;
    width: 100%;
    height: 100%;
    flex: 1 1 auto;
    border: 1px solid #444;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    min-height: 0;
    background: #0f0f0f;
    touch-action: none;
    transition: background 0.2s, border 0.2s;
}

#canvas-container.drag-over {
    background: rgba(14, 99, 156, 0.15);
    border: 2px dashed var(--accent);
}

.drop-hint {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--accent);
    font-size: 24px;
    font-weight: bold;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 1000;
    text-shadow: 0 0 10px rgba(0,0,0,0.8);
}

#canvas-container.drag-over .drop-hint {
    opacity: 1;
}

#center-arrow {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 0;
    height: 0;
    transform: translate(-50%, -50%);
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
    border-top: 18px solid rgba(255, 255, 255, 0.252);
    pointer-events: none;
}

#grid-wrapper {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) translate(var(--pan-x, 0px), var(--pan-y, 0px));
    will-change: transform;
}

#grid {
    display: grid;
    gap: 1px;
    background-color: var(--grid-line);
    width: max-content;
    border: 1px solid rgba(255,255,255,0.25);
    box-shadow: 0 0 10px rgba(0,0,0,0.6);
    position: relative;
}

.grid-resize-handle {
    position: absolute;
    background: rgba(14, 99, 156, 0.3);
    transition: background 0.2s;
}

.grid-resize-handle:hover {
    background: rgba(14, 99, 156, 0.6);
}

.grid-resize-right {
    right: -5px;
    top: 0;
    width: 10px;
    height: 100%;
    cursor: ew-resize;
}

.grid-resize-left {
    left: -5px;
    top: 0;
    width: 10px;
    height: 100%;
    cursor: ew-resize;
}

.grid-resize-bottom {
    bottom: -5px;
    left: 0;
    width: 100%;
    height: 10px;
    cursor: ns-resize;
}

.grid-resize-top {
    top: -5px;
    left: 0;
    width: 100%;
    height: 10px;
    cursor: ns-resize;
}

.grid-resize-corner {
    right: -5px;
    bottom: -5px;
    width: 15px;
    height: 15px;
    cursor: nwse-resize;
    background: rgba(14, 99, 156, 0.5);
}

.grid-resize-corner:hover {
    background: rgba(14, 99, 156, 0.8);
}

.grid-resize-corner-tl {
    left: -5px;
    top: -5px;
    width: 15px;
    height: 15px;
    cursor: nwse-resize;
    background: rgba(14, 99, 156, 0.5);
}

.grid-resize-corner-tl:hover {
    background: rgba(14, 99, 156, 0.8);
}

.grid-resize-corner-tr {
    right: -5px;
    top: -5px;
    width: 15px;
    height: 15px;
    cursor: nesw-resize;
    background: rgba(14, 99, 156, 0.5);
}

.grid-resize-corner-tr:hover {
    background: rgba(14, 99, 156, 0.8);
}

.grid-resize-corner-bl {
    left: -5px;
    bottom: -5px;
    width: 15px;
    height: 15px;
    cursor: nesw-resize;
    background: rgba(14, 99, 156, 0.5);
}

.grid-resize-corner-bl:hover {
    background: rgba(14, 99, 156, 0.8);
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    background-color: #000;
    cursor: crosshair;
    background-size: cover;
    image-rendering: pixelated;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255,255,255,0.5);
    font-size: 12px;
    font-family: monospace;
}

.cell:hover { opacity: 0.8; outline: 1px solid white; z-index: 1; }

#grid.precision-mode-active .cell:hover {
    outline-color: #FFFF99;
}

.cell.selected-cell:hover {
    outline: none;
}

#instructions {
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 1);
    opacity: 0.18;
    font-size: 9px;
    white-space: nowrap;
    pointer-events: auto;
    transition: opacity 0.2s ease;
    text-align: center;
    user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    cursor: default;
}

#instructions:hover {
    opacity: 0.9;
}

.right-output {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: #2d2d2d;
    border: 1px solid #444;
    padding: 10px;
    box-sizing: border-box;
}

textarea {
    width: 100%;
    height: 100%;
    background: #1e1e1e;
    color: #9cdcfe;
    border: none;
    font-family: 'Consolas', monospace;
    resize: none;
    box-sizing: border-box;
}

.control-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 20px;
}

.grid-left {
    flex: 1;
}

.grid-left h3 {
    margin-top: 0;
    margin-bottom: 10px;
}

.grid-right {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.custom-id-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.custom-id-full {
    width: 100%;
    box-sizing: border-box;
}

.layer-inputs {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 12px;
}

.layer-input-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.layer-input-row label {
    margin: 0;
    flex: 0 0 auto;
}

.layer-input-row input {
    flex: 1;
    padding: 5px;
}

.grid-size-row {
    display: flex;
    gap: 10px;
    align-items: center;
}

.grid-size-row label {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 6px;
}

.layer-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    margin-top: 8px;
}

.layer-grid-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.layer-grid-item label {
    margin: 0;
    font-size: 0.9em;
}

.layer-grid-item img {
    width: 32px;
    height: 32px;
    image-rendering: pixelated;
}

.layer-grid-item input[type="number"] {
    width: 100%;
    text-align: center;
}

@media (max-width: 900px) {
    .control-row {
        flex-direction: column;
        gap: 0;
        align-items: stretch;
    }

    .grid-left {
        margin-bottom: 15px;
    }

    .grid-right {
        flex: 1;
    }

    .custom-id-group input[type="text"] {
        width: 100%;
    }
}

#mobile-save-button {
    display: none;
    position: fixed;
    top: 10px;
    right: 110px;
    z-index: 25;
    padding: 8px 12px;
    background-color: transparent;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 14px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    width: 40px;
    height: 40px;
    background-image: url('Images/save.png');
    image-rendering: pixelated;
    pointer-events: auto;
}

#mobile-save-button:hover {
    opacity: 0.9;
}

#mobile-load-button {
    display: none;
    position: fixed;
    top: 10px;
    right: 160px;
    z-index: 25;
    padding: 8px 12px;
    background-color: transparent;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 14px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    width: 40px;
    height: 40px;
    background-image: url('Images/load.png');
    image-rendering: pixelated;
    pointer-events: auto;
}

#mobile-load-button:hover {
    opacity: 0.9;
}

@media (max-width: 900px) {
    #mobile-save-button {
        display: block;
    }
    #mobile-load-button {
        display: block;
    }
    #mobile-flip-button {
        display: block;
    }
    #sidebar.mobile-open #mobile-load-button,
    #sidebar.mobile-open #mobile-save-button,
    #sidebar.mobile-open #mobile-flip-button {
        pointer-events: none;
        display: none;
    }
}

.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background-color: #333;
    color: white;
    padding: 12px 20px;
    border-radius: 4px;
    font-size: 14px;
    z-index: 30;
    animation: slideUp 0.3s ease-out, slideDown 0.3s ease-out 2.7s;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@keyframes slideDown {
    from {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
    to {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
}

#mobile-flip-button {
    display: none;
    position: fixed;
    top: 10px;
    right: 60px;
    z-index: 25;
    padding: 8px 12px;
    background-color: transparent;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 14px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    width: 40px;
    height: 40px;
    background-image: url('Images/flip.png');
    image-rendering: pixelated;
    pointer-events: auto;
}

#mobile-flip-button:hover {
    opacity: 0.9;
}

#reload-grid-button {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 25;
    width: 20px;
    height: 20px;
    background-color: transparent;
    color: white;
    border: none;
    cursor: pointer;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    background-image: url('Images/reload.png');
    image-rendering: pixelated;
    pointer-events: auto;
}

#reload-grid-button:hover {
    opacity: 0.9;
}

@media (max-width: 900px) {
    #reload-grid-button {
        display: none;
    }
}

#mobile-sidebar-toggle {
    display: none;
    position: fixed;
    top: 10px;
    right: 10px;
    z-index: 25;
    padding: 8px 12px;
    background-color: transparent;
    color: transparent;
    border: none;
    cursor: pointer;
    font-size: 14px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    width: 40px;
    height: 40px;
    background-image: url('Images/selector.png');
    image-rendering: pixelated;
}

#mobile-zoom-in, #mobile-zoom-out {
    display: none;
    position: fixed;
    bottom: 70px;
    left: 10px;
    z-index: 25;
    padding: 8px 12px;
    background-color: transparent;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 14px;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    width: 40px;
    height: 40px;
    image-rendering: pixelated;
}

#mobile-zoom-in {
    background-image: url('Images/zoomin.png');
    left: 55px;
}

#mobile-zoom-out {
    background-image: url('Images/zoomout.png');
    left: 10px;
}

#mobile-zoom-in:hover, #mobile-zoom-out:hover {
    opacity: 0.9;
}

#mobile-zoom-in.hidden, #mobile-zoom-out.hidden {
    display: none !important;
}


#sidebar.mobile-open {
    position: fixed;
    left: 0;
    top: 0;
    width: 100% !important;
    max-width: 100% !important;
    height: 100dvh !important;
    flex: none !important;
    border-right: none;
    border-bottom: none;
    z-index: 24;
    padding-top: 70px;



}

#sidebar-close {
    display: none !important;
}

#mobile-toolbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: none;
    gap: 8px;
    padding: 10px;
    background: rgba(20,20,20,0.95);
    border-top: 1px solid #333;
    z-index: 20;
}

#mobile-toolbar button {
    flex: 1;
    font-size: 14px;
    padding: 12px 8px;
}

@media (max-width: 900px) {
    body {
        flex-direction: column;
        height: 100dvh;
    }
    #sidebar, #rightbar {
        display: none;
    }
    #sidebar.mobile-open {
        display: flex;
    }
    #rightbar.mobile-open {
        display: flex;
        position: fixed;
        right: 0;
        top: 0;
        width: 100% !important;
        max-width: 100% !important;
        height: 100dvh !important;
        flex: none !important;
        border-left: none;
        border-bottom: none;
        z-index: 24;
        padding-top: 70px;

        background: rgba(37,37,38,0.98);
        overflow-y: auto;
        padding-left: 12px;
        padding-right: 12px;
        box-sizing: border-box;
    }
    #sidebar.mobile-open #mobile-load-button,
    #sidebar.mobile-open #mobile-save-button,
    #sidebar.mobile-open #mobile-flip-button {
        display: none;
    }
    #mobile-flip-button {
        display: block;
    }
    #mobile-sidebar-toggle {
        display: block;
    }
    #mobile-zoom-in, #mobile-zoom-out {
        display: block;
    }
    #main {
        padding: 10px;
        height: 100%;
    }
    #canvas-container {
        height: calc(100vh - 80px);
    }
    #mobile-toolbar {
        display: flex;
        pointer-events: auto;
    }
    #grid {
        max-width: 100vw;
        max-height: 100%;
    }
    body {
        pointer-events: none;
    }
    #sidebar.mobile-open,
    #rightbar.mobile-open,
    #main,
    #canvas-container,
    #mobile-toolbar,
    #mobile-flip-button,
    #mobile-sidebar-toggle,
    #mobile-zoom-in,
    #mobile-zoom-out,
    #mobile-save-button,
    #mobile-load-button {
        pointer-events: auto !important;
    }
}

button.active-tool { background-color: #D69D36; color: #000; font-weight: bold; }

.offset-properties {
    padding: 12px;
    background: #1a1a1a;
    border: 1px solid #444;
    border-radius: 4px;
}

.offset-properties label {
    display: block;
    margin-bottom: 4px;
    font-size: 0.85em;
    color: #aaa;
}

.offset-properties .sel-coords {
    font-size: 0.8em;
    color: #888;
    margin-bottom: 10px;
    display: block;
}

.offset-properties .slider-row {
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.offset-properties .slider-row label {
    min-width: 60px;
    margin: 0;
}

.offset-properties input[type="range"] {
    flex: 1;
}

@media (max-width: 900px) {
    .control-row {
        flex-direction: row !important;
        gap: 10px !important;
        align-items: flex-start !important;
    }
    .grid-left {
        flex: 1;
        margin-bottom: 0 !important;
    }
    #rightbar button:nth-of-type(2),
    #rightbar button:nth-of-type(3) {
        display: none;
    }
    #copy-output-btn {
        display: block !important;
    }
    .mobile-only {
        display: block !important;
    }
}
