:root {
    --bg: #121212;
    --panel: #1e1e1e;
    --text: #e0e0e0;
    --accent: #00bcd4;
    --border: #333;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    user-select: none;
    touch-action: none;
}

/* --- STAGE --- */
#viewport {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    background-image: radial-gradient(#333 1px, transparent 1px);
    background-size: 20px 20px;
    z-index: 1;
}

#world {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    will-change: transform;
}

/* LAYERS */
#layer-base {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
    z-index: 1;
}

#layer-fog {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
}

#layer-grid {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 3;
    pointer-events: none;
}

#layer-draw {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 4;
    pointer-events: none;
    /* Input handled by viewport wrapper */
}

/* --- UI --- */
#ui-layer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 9999;
}

.top-bar {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    background: rgba(30, 30, 30, 0.95);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
    pointer-events: auto;
}

.mode-switch {
    display: flex;
    background: #111;
    border-radius: 8px;
    padding: 4px;
    border: 1px solid #444;
}

.tab-btn {
    background: transparent;
    border: none;
    color: #888;
    padding: 8px 16px;
    font-weight: bold;
    font-size: 0.9rem;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s;
}

.tab-btn.active {
    background: var(--accent);
    color: #000;
}

.icon-btn {
    font-size: 20px;
    color: #aeaeae;
    border: none;
    cursor: pointer;
    background: none;
    transition: all 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 5px;
    /* Increase hit area */
}

/* Allow labels to act as buttons */
label.icon-btn {
    margin: 0;
}

.icon-btn:hover {
    color: #fff;
    transform: scale(1.1);
}

.toolbar {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 15px;
    background: rgba(20, 20, 20, 0.8);
    padding: 15px 25px;
    border-radius: 40px;
    backdrop-filter: blur(10px);
    border: 1px solid #333;
    pointer-events: auto;
    align-items: center;
}

.toolbar.hidden {
    transform: translate(-50%, 150%);
}

.tool-btn {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: rgba(30, 30, 30, 0.9);
    border: 1px solid #444;
    color: #eee;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Export button hover */
#btn-export-png:hover {
    border-color: #00e5ff;
    color: #00e5ff;
}

.tool-btn.active {
    transform: scale(1.1);
    color: #fff;
    border-color: var(--accent);
}

.tool-btn.active#tool-pan {
    background: #ffb74d;
    border-color: #ffb74d;
    color: #000;
}

.tool-btn.active#tool-reveal {
    background: #66bb6a;
    border-color: #66bb6a;
    color: #000;
}

.tool-btn.active#tool-hide {
    background: #ef5350;
    border-color: #ef5350;
    color: #000;
}

/* SUB TOOLBAR */
.sub-toolbar {
    position: absolute;
    bottom: 90px;
    /* Above main toolbar */
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    background: rgba(20, 20, 20, 0.85);
    padding: 8px 15px;
    border-radius: 20px;
    backdrop-filter: blur(10px);
    border: 1px solid #444;
    pointer-events: auto;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 9999;
}

.sub-toolbar.visible {
    opacity: 1;
    pointer-events: auto;
}

.color-swatch {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform 0.1s;
}

.color-swatch:hover {
    transform: scale(1.1);
}

.color-swatch.active {
    border-color: #fff;
    transform: scale(1.1);
    box-shadow: 0 0 5px rgba(255, 255, 255, 0.5);
}

#brush-cursor {
    position: fixed;
    top: 0;
    left: 0;
    width: 20px;
    height: 20px;
    border: 1px solid rgba(255, 255, 255, 0.8);
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    pointer-events: none;
    transform: translate(-50%, -50%);
    z-index: 10000;
    display: none;
    box-shadow: 0 0 2px #000;
}

.align-hud {
    position: absolute;
    bottom: 30px;
    width: 100%;
    text-align: center;
    color: var(--accent);
    font-weight: bold;
    font-size: 1.1rem;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
    opacity: 0;
    transition: opacity 0.3s;
}

.align-hud.visible {
    opacity: 1;
}

/* SETTINGS MENU */
#settings-menu {
    position: absolute;
    top: 0;
    right: 0;
    width: 320px;
    height: 100%;
    background-color: #1e1e1e;
    border-left: 1px solid var(--border);
    transform: translateX(100%);
    transition: transform 0.3s;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    box-shadow: -5px 0 30px rgba(0, 0, 0, 0.5);
    z-index: 10001;
    /* Ensure it stays on top */
}

#settings-menu.open {
    transform: translateX(0);
}

.menu-header {
    padding: 15px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #252525;
}

.menu-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #1e1e1e;
}

.control-group {
    margin-bottom: 25px;
    border-bottom: 1px solid #333;
    padding-bottom: 15px;
}

label {
    display: block;
    color: #aaa;
    margin-bottom: 8px;
    font-size: 0.9rem;
}

.row {
    display: flex;
    gap: 10px;
    align-items: center;
}

select,
input[type="range"],
input[type="number"] {
    padding: 8px;
    background: #333;
    border: 1px solid #555;
    color: white;
    border-radius: 6px;
    height: 40px;
    /* Consistent height for alignment */
}

.action-btn {
    width: 100%;
    padding: 12px;
    margin-bottom: 10px;
    background: #333;
    color: white;
    border: 1px solid #555;
    border-radius: 6px;
    cursor: pointer;
    text-align: center;
}

.file-input-wrapper {
    position: relative;
    overflow: hidden;
}

.file-input-wrapper input {
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
}

#overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    pointer-events: auto;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

#overlay.visible {
    opacity: 1;
    pointer-events: auto;
}