/**
 * canvas-editor.css — shared base styles for all drawing applications.
 *
 * Load this before any app-specific stylesheet.
 * App-specific files only need to contain rules that differ from these.
 *
 * Covers: reset, body, canvas, sidebar, controls, status bar, tool indicators,
 * modals, settings form, primary button, utility classes, mobile breakpoints.
 */

/* ===== RESET ===== */

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

body {
    font-family: 'Courier New', monospace;
    background: #1f2937;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
}

/* ===== CANVAS ===== */

#mainCanvas {
    width: 100%;
    height: 100%;
    display: block;
    background: #1f2937;
    cursor: crosshair;
    touch-action: none;

    /* Crisp pixel rendering (overridden on high-DPI displays below) */
    image-rendering: -moz-crisp-edges;
    image-rendering: -webkit-crisp-edges;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* On high-DPI displays let the browser smooth the upscaled canvas */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 2dppx) {
    #mainCanvas {
        image-rendering: auto;
    }
}

/* ===== SIDEBAR ===== */

.canvas-sidebar {
    position: fixed;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    padding: 12px;
    border-radius: 16px 0 0 16px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-right: none;
    z-index: 10;

    /* Never overflow the viewport */
    max-height: 100dvh;
    overflow: visible;
}

/* Scroll the button list, not the sidebar wrapper (keeps tab outside clip) */
.canvas-sidebar .controls {
    overflow-y: auto;
    max-height: calc(100dvh - 24px);
    scrollbar-width: none;
}

.canvas-sidebar .controls::-webkit-scrollbar {
    display: none;
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* ===== CONTROL BUTTONS ===== */

.control-btn {
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
}

.control-btn:hover {
    background: rgba(255, 255, 255, 0.25);
    border-color: rgba(255, 255, 255, 0.5);
    transform: scale(1.05);
}

.control-btn:active {
    transform: scale(0.95);
}

.control-btn:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}


/* ===== SAVE INDICATOR ===== */

#saveIndicator {
    position: fixed;
    bottom: 12px;
    right: 72px;
    background: rgba(0, 0, 0, 0.75);
    color: #fff;
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 12px;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 6px;
    z-index: 9999;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease;
}

#saveIndicator.visible {
    opacity: 1;
}

/* ===== STATUS BAR ===== */

.status-display {
    position: absolute;
    bottom: 12px;
    left: 12px;
    z-index: 10;
    overflow: visible;
}

.status-panel {
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 12px 20px 12px 16px;
    border-radius: 12px;
    font-size: 14px;
}

/* ===== TOOL INDICATORS ===== */

.tool-indicator {
    padding: 4px 8px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
}

.tool-brush {
    background: rgba(59, 130, 246, 0.3);
    color: #93c5fd;
}

.tool-eraser {
    background: rgba(239, 68, 68, 0.3);
    color: #fca5a5;
}

/* ===== MODALS ===== */

/*
 * We use native <dialog> + showModal() so the modal enters the browser's top
 * layer and stays visible even when the parent page is in fullscreen mode.
 * display is set to flex inline by show() to override the UA's display:block.
 */
dialog.modal-overlay {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    margin: 0;
    padding: 20px;
    border: none;
    background: rgba(0, 0, 0, 0.6);
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

/* Use ::backdrop for the blur so it doesn't interfere with dialog rendering */
dialog.modal-overlay::backdrop {
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
}

.modal-content {
    background: white;
    border-radius: 12px;
    box-shadow:
        0 20px 25px -5px rgba(0, 0, 0, 0.1),
        0 10px 10px -5px rgba(0, 0, 0, 0.04);
    width: 100%;
    max-width: 400px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 20px 0 20px;
    margin-bottom: 16px;
    flex-shrink: 0;
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
    color: #1f2937;
    margin: 0;
}

.close-btn {
    background: none;
    border: none;
    font-size: 24px;
    color: #6b7280;
    cursor: pointer;
    padding: 4px;
    line-height: 1;
}

.close-btn:hover {
    color: #374151;
}

.modal-body {
    padding: 0 20px 16px 20px;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-body .setting-group {
    margin-bottom: 12px;
}

.modal-body .setting-group label {
    margin-bottom: 4px;
}

.modal-footer {
    flex-shrink: 0;
    padding: 16px 20px 20px 20px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* ===== SETTINGS FORM ===== */

.setting-group {
    margin-bottom: 20px;
}

.setting-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: #374151;
    margin-bottom: 8px;
}

.setting-group input[type="range"] {
    width: 100%;
    height: 6px;
    background: #e5e7eb;
    border-radius: 3px;
    outline: none;
    appearance: none;
}

.setting-group input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 20px;
    height: 20px;
    background: #f97316;
    border-radius: 50%;
    border: 2px solid white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    cursor: pointer;
}

.setting-group input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: #f97316;
    border-radius: 50%;
    border: 2px solid white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    cursor: pointer;
}

.setting-group input[type="color"] {
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    padding: 0;
}

.setting-group input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

.setting-group input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 6px;
}

.setting-group select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    background: white;
    font-size: 14px;
    color: #374151;
}

.setting-group--inline {
    display: flex;
    align-items: center;
    gap: 10px;
}

.setting-group--inline label {
    margin-bottom: 0;
    font-weight: 400;
}

.setting-group--inline input[type="checkbox"] {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    cursor: pointer;
    accent-color: #f97316;
}

/* ===== BUTTONS ===== */

.btn-primary {
    background: #f97316;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
}

.btn-primary:hover {
    background: #ea580c;
}

/* ===== UTILITY CLASSES ===== */

.flex {
    display: flex;
}

.items-center {
    align-items: center;
}

.space-x-4 > * + * {
    margin-left: 1rem;
}

/* ===== RESPONSIVE: NARROW SCREENS ===== */

@media (max-width: 768px) {
    .canvas-sidebar {
        padding: 8px;
        border-radius: 12px 0 0 12px;
    }

    .control-btn {
        width: 40px;
        height: 40px;
        font-size: 14px;
    }

    .modal-content {
        margin: 10px;
        max-width: none;
    }
}

/* ===== RESPONSIVE: SHORT SCREENS ===== */

/* ~600px height: tighten sidebar so all buttons stay visible */
@media (max-height: 620px) {
    .canvas-sidebar {
        padding: 8px;
    }

    .controls {
        gap: 5px;
    }

    .control-btn {
        width: 38px;
        height: 38px;
        font-size: 14px;
        border-radius: 10px;
    }
}

/* ~480px height: shrink further */
@media (max-height: 490px) {
    .canvas-sidebar {
        padding: 6px;
    }

    .controls {
        gap: 3px;
    }

    .control-btn {
        width: 32px;
        height: 32px;
        font-size: 12px;
        border-radius: 8px;
    }
}

/* ===== STATUS INFO BUTTON ===== */

.status-info-btn {
    background: none;
    border: 1px solid #475569;
    border-radius: 50%;
    color: #64748b;
    font-size: 11px;
    font-style: normal;
    font-weight: 600;
    cursor: pointer;
    padding: 0;
    width: 18px;
    height: 18px;
    margin-left: 20px;
    line-height: 1;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s, border-color 0.15s;
    font-family: inherit;
}

.status-info-btn:hover {
    color: #e2e8f0;
    border-color: #94a3b8;
}

/* ===== SIDEBAR COLLAPSE ===== */

.canvas-sidebar {
    transition: transform 0.2s ease, opacity 0.15s ease;
}

.canvas-sidebar.sidebar-collapsed {
    transform: translateY(-50%) translateX(100%);
    opacity: 0;
    pointer-events: none;
}

/* Tab on the left edge of the sidebar — visible on hover */
.sidebar-edge-tab {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, background 0.15s, color 0.15s;
}

.canvas-sidebar:hover .sidebar-edge-tab {
    opacity: 1;
    pointer-events: auto;
}

/* Tab on the left edge of the sidebar */
.sidebar-edge-tab {
    position: absolute;
    left: -13px;
    top: 50%;
    transform: translateY(-50%);
    width: 13px;
    height: 40px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-right: none;
    border-radius: 8px 0 0 8px;
    cursor: pointer;
    font-size: 8px;
    color: rgba(255, 255, 255, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
    z-index: 11;
}

.sidebar-edge-tab:hover {
    background: rgba(255, 255, 255, 0.22);
    color: rgba(255, 255, 255, 0.9);
}

/* Dot that restores the sidebar */
.sidebar-collapse-dot {
    position: fixed;
    right: 12px;
    bottom: 12px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.35);
    border: 1px solid rgba(255, 255, 255, 0.5);
    cursor: pointer;
    z-index: 11;
    display: none;
    transition: background 0.15s, transform 0.15s;
}

.sidebar-collapse-dot.visible {
    display: block;
}

.sidebar-collapse-dot:hover {
    background: rgba(255, 255, 255, 0.65);
    transform: scale(1.4);
}

/* ===== STATUS BAR COLLAPSE ===== */

.status-panel {
    transition: opacity 0.15s ease, transform 0.2s ease;
    transform-origin: bottom left;
}

.status-display.status-collapsed .status-panel {
    opacity: 0;
    pointer-events: none;
    transform: translateY(8px);
}

/* Small dot / restore tab */
.status-collapse-dot {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.35);
    border: 1px solid rgba(255, 255, 255, 0.5);
    cursor: pointer;
    transition: background 0.15s, transform 0.15s;
    display: none;
}

.status-display.status-collapsed .status-collapse-dot {
    display: block;
}

.status-collapse-dot:hover {
    background: rgba(255, 255, 255, 0.65);
    transform: scale(1.3);
}

/* Tab on the top edge of the status bar — visible on hover */
.status-edge-tab {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, background 0.15s, color 0.15s;
}

.status-panel:hover .status-edge-tab {
    opacity: 1;
    pointer-events: auto;
}

/* Tab on the top edge of the status bar */
.status-edge-tab {
    position: absolute;
    top: -13px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 13px;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    cursor: pointer;
    font-size: 8px;
    color: rgba(255, 255, 255, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
}

.status-edge-tab:hover {
    background: rgba(30, 30, 30, 0.95);
    color: rgba(255, 255, 255, 0.85);
}

.status-display.status-collapsed .status-edge-tab {
    display: none;
}
