:root {
    --primary: #3B82F6;
    --primary-hover: #2563EB;
    --accent: #10B981;
    --bg-dark: #0F172A;
    --bg-light: #F8FAFC;
    --panel-dark: rgba(30, 41, 59, 0.8);
    --panel-light: rgba(255, 255, 255, 0.9);
    --text-dark: #F8FAFC;
    --text-light: #0F172A;
    --border-dark: rgba(255, 255, 255, 0.1);
    --border-light: rgba(0, 0, 0, 0.1);

    /* Active Theme Variables */
    --bg: var(--bg-dark);
    --panel: var(--panel-dark);
    --text: var(--text-dark);
    --text-muted: #94A3B8;
    --border: var(--border-dark);

    --cell-size: 14px;
    --font-mono: 'Fira Code', 'Courier New', Courier, monospace;
}

body:not(.dark-mode) {
    --bg: var(--bg-light);
    --panel: var(--panel-light);
    --text: var(--text-light);
    --text-muted: #64748B;
    --border: var(--border-light);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', system-ui, sans-serif;
    background-color: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    transition: background-color 0.3s, color 0.3s;
}

.app-container {
    display: flex;
    height: 100%;
}

/* Sidebar */
.toolbar {
    width: 250px;
    background: var(--panel);
    border-right: 1px solid var(--border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
    z-index: 10;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 2rem;
    color: var(--primary);
}

.brand h1 {
    font-size: 1.25rem;
    font-weight: 600;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.tools-section {
    margin-bottom: 2rem;
}

.bottom-section {
    margin-top: auto;
}

.tools-section h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.tool-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.tool-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 8px;
    padding: 0.75rem 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    transition: all 0.2s;
}

.tool-btn span {
    font-size: 0.75rem;
    font-weight: 500;
}

.tool-btn:hover {
    background: rgba(59, 130, 246, 0.1);
    border-color: rgba(59, 130, 246, 0.3);
}

.tool-btn.active {
    background: rgba(59, 130, 246, 0.2);
    border-color: var(--primary);
    color: var(--primary);
}

.action-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.action-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 6px;
    padding: 0.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
}

.action-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.05);
}

.action-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.action-btn.primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.action-btn.primary:hover {
    background: var(--primary-hover);
}

.action-btn.danger {
    color: #EF4444;
    border-color: rgba(239, 68, 68, 0.3);
}

.action-btn.danger:hover {
    background: rgba(239, 68, 68, 0.1);
}

/* Theme Toggle */
.theme-toggle {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.theme-label {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 20px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .4s;
}

input:checked+.slider {
    background-color: var(--primary);
}

input:focus+.slider {
    box-shadow: 0 0 1px var(--primary);
}

input:checked+.slider:before {
    transform: translateX(20px);
}

.slider.round {
    border-radius: 20px;
}

.slider.round:before {
    border-radius: 50%;
}

/* Main Area */
.canvas-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: repeating-linear-gradient(to right,
            var(--border) 0,
            var(--border) 1px,
            transparent 1px,
            transparent var(--cell-size)),
        repeating-linear-gradient(to bottom,
            var(--border) 0,
            var(--border) 1px,
            transparent 1px,
            transparent var(--cell-size));
    background-position: 0 0;
    overflow: hidden;
    position: relative;
}

.canvas-wrapper {
    flex: 1;
    overflow: auto;
    position: relative;
    cursor: crosshair;
}

.canvas-wrapper.tool-text {
    cursor: text;
}

.canvas-wrapper.tool-erase {
    cursor: cell;
}

.ascii-grid {
    position: absolute;
    top: 0;
    left: 0;
    font-family: var(--font-mono);
    font-size: var(--cell-size);
    /* Size of mono font matches grid roughly */
    line-height: var(--cell-size);
    white-space: pre;
    color: var(--text);
    pointer-events: none;
    /* Let the wrapper handle drag events */
    padding: 0;
    margin: 0;
    user-select: none;
}

.hidden-input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    pointer-events: none;
}

/* Blinking cursor for text mode */
.cursor {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    background: rgba(59, 130, 246, 0.4);
    border-bottom: 2px solid var(--primary);
    pointer-events: none;
    animation: blink 1s step-end infinite;
    display: none;
}

@keyframes blink {
    50% {
        opacity: 0;
    }
}

/* Status Bar */
.status-bar {
    background: var(--panel);
    border-top: 1px solid var(--border);
    padding: 0.5rem 1rem;
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-muted);
}