:root {
    --bg-main: #0a0a0a;
    --sidebar-bg: #161616;
    --card-bg: #1e1e1e;
    --accent: #0078d4;
    --accent-glow: rgba(0, 120, 212, 0.3);
    --border: #2d2d2d;
    --text-primary: #ffffff;
    --text-secondary: #a0a0a0;
    --success: #2ea043;
    --warn: #f5a623;
    --error: #ff453a;
    --measure-glow: #00ff9d;
}

* { box-sizing: border-box; }

body, html {
    margin: 0; padding: 0; height: 100%; width: 100%;
    font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
    background: var(--bg-main); color: var(--text-primary); overflow: hidden;
}

#app-container { display: flex; height: 100vh; width: 100vw; }

#sidebar {
    width: 330px; background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    box-shadow: 10px 0 30px rgba(0,0,0,0.5);
    z-index: 20;
}

.brand { padding: 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 15px; }
.logo-container { width: 42px; height: 42px; display: flex; align-items: center; justify-content: center; background: rgba(255,255,255,0.05); border-radius: 8px; border: 1px solid var(--border); padding: 4px; flex-shrink: 0; }
.logo-img { max-width: 100%; max-height: 100%; object-fit: contain; }
.scroll-area { flex: 1; overflow-y: auto; padding: 10px; }

.panel { background: var(--card-bg); border-radius: 8px; border: 1px solid var(--border); margin-bottom: 12px; overflow: hidden; }
.panel-header { padding: 12px 16px; background: rgba(255,255,255,0.03); border-bottom: 1px solid var(--border); font-size: 11px; font-weight: 700; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 1px; display: flex; justify-content: space-between; align-items: center; }
.panel-content { padding: 14px; }

.btn {
    width: 100%; padding: 11px; border-radius: 6px;
    border: 1px solid var(--border); background: #262626;
    color: white; cursor: pointer; font-size: 12.5px; font-weight: 500;
    transition: all 0.15s ease;
    display: flex; align-items: center; justify-content: center; gap: 7px;
    margin-bottom: 8px;
}
.btn:hover { background: #323232; border-color: #444; transform: translateY(-1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--accent); border: none; }
.btn-success { background: var(--success); border: none; }
.btn-row { display: flex; gap: 6px; }
.btn-row .btn { margin-bottom: 0; }
.btn.active { background: var(--measure-glow); color: #000; border: none; font-weight: 700; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }

.field-label { font-size: 11px; color: var(--text-secondary); display: block; margin-bottom: 4px; }
.text-input, .select-input {
    width: 100%; background: #262626; border: 1px solid var(--border); color: white;
    padding: 7px 8px; border-radius: 4px; box-sizing: border-box; font-size: 12.5px;
}
.field-block { margin-bottom: 10px; }

.switch-container { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; font-size: 12.5px; }
.meta-item { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 12px; color: var(--text-secondary); gap: 10px; }
.meta-item span:first-child { flex-shrink: 0; }
.meta-val { color: var(--text-primary); font-weight: 600; text-align: right; overflow-wrap: break-word; min-width: 0; }
.empty-hint { text-align: center; color: var(--text-secondary); font-size: 12px; padding: 10px; }

/* layers */
.layer-toolbar { display: flex; gap: 6px; margin-bottom: 8px; }
.mini-btn {
    background: #262626; border: 1px solid var(--border); color: var(--text-secondary);
    border-radius: 4px; font-size: 11px; padding: 4px 8px; cursor: pointer;
}
.mini-btn:hover { background: #323232; color: white; }
.layer-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; border-radius: 6px; background: rgba(0,0,0,0.2); margin-bottom: 6px; font-size: 12px; gap: 8px; }
.layer-row-main { display: flex; align-items: center; gap: 8px; cursor: pointer; min-width: 0; }
.layer-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 120px; }
.layer-row-meta { display: flex; align-items: center; gap: 8px; color: var(--text-secondary); font-size: 10.5px; flex-shrink: 0; }
.swatch { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.kind-badge { font-size: 9px; text-transform: uppercase; letter-spacing: 0.5px; padding: 1px 5px; border-radius: 3px; background: #333; color: #ccc; flex-shrink: 0; }
.kind-badge.kind-notch { background: #4a2b00; color: #ffb74d; }
.kind-badge.kind-drill { background: #3a0d0d; color: #ff8a80; }
.kind-badge.kind-grainline { background: #0d3a2a; color: #69f0ae; }
.kind-badge.kind-internal, .kind-badge.kind-fold { background: #2a2a3a; color: #b39ddb; }
.kind-badge.kind-annotation { background: #1a2a3a; color: #81d4fa; }

/* format info */
.format-badge { display: inline-block; font-size: 10px; font-weight: 700; letter-spacing: 0.5px; padding: 3px 8px; border-radius: 4px; margin-bottom: 10px; }
.badge-full { background: #0d3320; color: #4ade80; }
.badge-partial { background: #3a2d0d; color: var(--warn); }
.badge-experimental { background: #3a0d1a; color: #ff6b9d; }
.warnings-list { margin-bottom: 8px; }
.warning-item { display: flex; align-items: flex-start; gap: 6px; font-size: 11px; color: var(--warn); background: rgba(245,166,35,0.08); border-left: 2px solid var(--warn); padding: 6px 8px; border-radius: 3px; margin-bottom: 5px; line-height: 1.4; }
.warning-item .icon-inline { margin: 2px 0 0; flex-shrink: 0; }
.inspector-block { margin-top: 8px; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; }
.inspector-block summary { cursor: pointer; font-size: 11px; color: var(--text-secondary); }
.mono-row { font-family: 'JetBrains Mono', monospace; font-size: 10.5px; color: #ccc; padding: 2px 0; border-bottom: 1px solid rgba(255,255,255,0.04); word-break: break-all; }
.hex-pre { font-family: 'JetBrains Mono', monospace; font-size: 10px; color: #9aa0a6; max-height: 260px; overflow: auto; white-space: pre; margin: 6px 0 0; }

/* viewport */
#viewport { flex: 1; position: relative; display: flex; flex-direction: column; min-width: 0; }
#toolbar-top { height: 48px; background: var(--sidebar-bg); border-bottom: 1px solid var(--border); display: flex; align-items: center; padding: 0 18px; justify-content: space-between; font-size: 12.5px; gap: 16px; }
#toolbar-top .toolbar-right { display: flex; gap: 18px; align-items: center; color: var(--text-secondary); flex-shrink: 0; }
#current-filename { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
#sidebarToggleBtn { display: none; }
.icon-btn { background: transparent; border: 1px solid var(--border); color: var(--text-secondary); border-radius: 5px; width: 28px; height: 28px; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.icon-btn:hover { color: white; border-color: #444; }
.lang-toggle { display: flex; border: 1px solid var(--border); border-radius: 5px; overflow: hidden; }
.lang-btn { background: transparent; border: none; color: var(--text-secondary); font-size: 10.5px; font-weight: 700; padding: 5px 8px; cursor: pointer; letter-spacing: 0.5px; }
.lang-btn:hover { color: white; }
.lang-btn.active { background: var(--accent); color: white; }
.lang-btn + .lang-btn { border-left: 1px solid var(--border); }

#canvas-wrapper {
    flex: 1; position: relative; display: grid;
    grid-template-columns: 22px 1fr; grid-template-rows: 22px 1fr;
    background: #000; min-height: 0;
}
#canvas-wrapper.rulers-hidden { grid-template-columns: 0 1fr; grid-template-rows: 0 1fr; }
#ruler-corner { grid-column: 1; grid-row: 1; background: var(--sidebar-bg); border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); }
#ruler-h { grid-column: 2; grid-row: 1; width: 100%; height: 100%; display: block; }
#ruler-v { grid-column: 1; grid-row: 2; width: 100%; height: 100%; display: block; }
#canvas-holder { grid-column: 2; grid-row: 2; position: relative; cursor: grab; overflow: hidden; touch-action: none; }
canvas { display: block; width: 100%; height: 100%; touch-action: none; }

#hud-coordinates {
    position: absolute; bottom: 20px; left: 20px;
    background: rgba(15,15,15,0.85); border: 1px solid var(--border);
    padding: 10px 16px; border-radius: 8px; backdrop-filter: blur(10px);
    font-family: 'JetBrains Mono', monospace; font-size: 12.5px;
    color: #00ff9d; box-shadow: 0 8px 32px rgba(0,0,0,0.4); pointer-events: none;
}

#cross-h, #cross-v { position: absolute; background: rgba(0, 120, 212, 0.4); pointer-events: none; display: none; z-index: 5; }
#cross-h { height: 1px; width: 100%; left: 0; }
#cross-v { width: 1px; height: 100%; top: 0; }

/* toasts */
#toast-container { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast {
    background: #1e1e1e; border: 1px solid var(--border); border-left: 3px solid var(--accent);
    color: white; padding: 12px 16px; border-radius: 6px; font-size: 12.5px;
    max-width: min(340px, calc(100vw - 40px));
    overflow-wrap: break-word;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5); opacity: 0; transform: translateX(20px); transition: all 0.25s ease;
}
.toast.show { opacity: 1; transform: translateX(0); }
.toast-warn { border-left-color: var(--warn); }
.toast-error { border-left-color: var(--error); }
.toast-success { border-left-color: var(--success); }
.toast-info { border-left-color: var(--accent); }

/* progress overlay */
#progress-overlay {
    position: fixed; inset: 0; background: rgba(5,5,5,0.75); z-index: 300;
    display: none; align-items: center; justify-content: center; flex-direction: column; gap: 14px;
    backdrop-filter: blur(3px);
}
.progress-track { width: 320px; height: 8px; background: #262626; border-radius: 4px; overflow: hidden; border: 1px solid var(--border); }
.progress-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--measure-glow)); transition: width 0.15s ease; }
#progress-text { font-family: 'JetBrains Mono', monospace; color: var(--text-secondary); font-size: 12px; }

/* drag & drop overlay */
#dropOverlay {
    position: fixed; inset: 0; z-index: 250; display: none; align-items: center; justify-content: center;
    background: rgba(0,120,212,0.12); backdrop-filter: blur(2px);
}
#dropOverlay .drop-box {
    border: 2px dashed var(--accent); border-radius: 16px; padding: 60px 80px;
    color: white; font-size: 18px; font-weight: 600; background: rgba(10,10,10,0.6); text-align: center;
}
#dropOverlay .drop-box small { display: block; margin-top: 8px; color: var(--text-secondary); font-weight: 400; font-size: 12.5px; }

/* shortcuts modal */
#shortcuts-help {
    position: fixed; inset: 0; z-index: 400; display: none; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.6);
}
#shortcuts-help.open { display: flex; }
.shortcuts-panel { background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px; padding: 22px 26px; width: 340px; box-shadow: 0 20px 60px rgba(0,0,0,0.6); }
.shortcuts-panel h3 { margin: 0 0 14px; font-size: 14px; display: flex; justify-content: space-between; align-items: center; }
.shortcut-row { display: flex; justify-content: space-between; font-size: 12.5px; padding: 6px 0; border-bottom: 1px solid rgba(255,255,255,0.05); color: var(--text-secondary); }
.kbd { background: #262626; border: 1px solid #3a3a3a; border-bottom-width: 2px; border-radius: 4px; padding: 1px 7px; font-family: monospace; color: white; font-size: 11px; }

.icon { width: 15px; height: 15px; flex-shrink: 0; }
.icon-inline { width: 13px; height: 13px; margin-right: 6px; opacity: 0.75; vertical-align: -2px; flex-shrink: 0; }
.panel-header { display: flex; align-items: center; gap: 0; }
.panel-header-title { display: flex; align-items: center; }
.suspect-piece { color: var(--warn); }
.suspect-piece .meta-val { color: var(--warn); }
.piece-row { cursor: pointer; border-radius: 4px; padding: 2px 4px; margin: 0 -4px 8px; transition: background-color 0.1s; }
.piece-row:hover { background-color: var(--accent-glow); }
.piece-row .meta-item { margin-bottom: 2px; }
.piece-dims { font-size: 10px; color: var(--text-secondary); opacity: 0.75; padding-left: 1px; }

/* terminal-style empty state, shown in the viewport until a file loads */
#empty-state {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    padding: 40px; pointer-events: none; z-index: 4;
}
.empty-state-inner {
    font-family: 'JetBrains Mono', monospace; font-size: 13px; line-height: 2;
    color: var(--measure-glow); max-width: 640px; width: 100%;
    text-shadow: 0 0 12px rgba(0,255,157,0.25);
}
.terminal-line { white-space: pre-wrap; word-break: break-word; }
.terminal-brand { color: var(--text-primary); font-weight: 700; font-size: 15px; margin-bottom: 10px; display: flex; align-items: center; }
.terminal-brand .icon-inline { width: 16px; height: 16px; opacity: 1; color: var(--accent); }
.terminal-dim { color: var(--text-secondary); font-weight: 400; }
.terminal-status { color: var(--accent); opacity: 0.9; margin-bottom: 6px; }
.terminal-cursor { display: inline-block; margin-left: 4px; animation: term-blink 1s step-end infinite; }
@keyframes term-blink { 50% { opacity: 0; } }

/* guide content (shared by the guide modal sections) */
.guide-content { font-size: 13px; line-height: 1.7; color: var(--text-secondary); margin-bottom: 18px; }
.guide-content h3 { color: var(--text-primary); font-size: 15px; margin: 0 0 10px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.guide-content h4 { color: var(--accent); font-size: 13px; margin: 14px 0 4px; }
.guide-content p { margin: 0 0 10px; }
.guide-content b { color: var(--text-primary); }
.guide-content code { background: #262626; padding: 1px 5px; border-radius: 3px; font-size: 12px; }
.guide-content ul, .guide-content ol { margin: 0 0 4px; padding-left: 20px; }
.guide-content li { margin-bottom: 6px; }

#guide-modal {
    position: fixed; inset: 0; z-index: 400; display: none; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.65); padding: 30px;
}
#guide-modal.open { display: flex; }
.guide-panel {
    background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px;
    width: 100%; max-width: 720px; max-height: 100%; display: flex; flex-direction: column;
    box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
.guide-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.guide-header h2 { margin: 0; font-size: 16px; display: flex; align-items: center; }
.guide-scroll { padding: 20px 22px; overflow-y: auto; }

/* command palette */
#cmdk-overlay {
    position: fixed; inset: 0; z-index: 410; display: none; align-items: flex-start; justify-content: center;
    background: rgba(0,0,0,0.5); padding-top: 12vh;
}
#cmdk-overlay.open { display: flex; }
.cmdk-panel { background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px; width: 100%; max-width: 520px; box-shadow: 0 20px 60px rgba(0,0,0,0.6); overflow: hidden; }
.cmdk-input-row { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.cmdk-input-row .icon-inline { margin: 0; width: 16px; height: 16px; }
#cmdkInput { flex: 1; background: transparent; border: none; outline: none; color: white; font-size: 14px; }
.cmdk-list { max-height: 320px; overflow-y: auto; padding: 6px; }
.cmdk-item { display: flex; justify-content: space-between; align-items: center; padding: 9px 12px; border-radius: 6px; font-size: 13px; cursor: pointer; color: var(--text-secondary); }
.cmdk-item .cmdk-item-icon { display: flex; align-items: center; gap: 10px; color: var(--text-primary); }
.cmdk-item.active { background: var(--accent); color: white; }
.cmdk-item.active .cmdk-item-icon { color: white; }
.cmdk-empty { padding: 16px; text-align: center; color: var(--text-secondary); font-size: 12.5px; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #444; }

/* ============ Responsive: tablet/phone (sidebar becomes an off-canvas
   drawer instead of a fixed 330px flex column - at anything under ~900px
   the classic side-by-side layout leaves too little room for the canvas
   to be usable, and a permanently-visible sidebar is pointless on a
   touch device where it's opened on demand anyway). Everything here is
   scoped inside the media query, so desktop layout above the breakpoint
   is completely unaffected. ============ */
#sidebarBackdrop { display: none; }

@media (max-width: 900px) {
    #sidebarToggleBtn { display: flex; }

    #sidebar {
        --drawer-width: min(330px, 85vw);
        position: fixed; top: 0; bottom: 0; width: var(--drawer-width);
        left: calc(-1 * var(--drawer-width));
        transition: left 0.25s ease;
    }
    #sidebar.open { left: 0; }

    #sidebarBackdrop {
        display: block; position: fixed; inset: 0; z-index: 15;
        background: rgba(0,0,0,0.5); opacity: 0; pointer-events: none;
        transition: opacity 0.2s ease;
    }
    #sidebarBackdrop.open { opacity: 1; pointer-events: auto; }

    /* Secondary read-only info, not worth the width on a cramped toolbar -
       the same numbers are always visible in the Viewport Settings panel. */
    .toolbar-info { display: none; }
    #toolbar-top { padding: 0 10px; gap: 8px; }
    #toolbar-top .toolbar-right { gap: 10px; }

    /* Bigger tap targets - the desktop sizes below (28px icon buttons,
       10px swatches, etc.) are too small to reliably hit with a finger. */
    .icon-btn { width: 40px; height: 40px; }
    .lang-btn { padding: 9px 10px; }
    .mini-btn { padding: 8px 11px; }
    .swatch { width: 14px; height: 14px; }
    input[type="checkbox"] { width: 18px; height: 18px; }
    .solo-btn { min-height: 30px; }

    /* Modals: fixed desktop pixel widths don't fit a phone screen. */
    .shortcuts-panel { width: calc(100vw - 40px); max-width: 340px; max-height: calc(100vh - 40px); overflow-y: auto; }
    #guide-modal { padding: 14px; }
    .guide-panel { max-height: 100%; }
    .guide-header { padding: 14px 16px; }
    .guide-scroll { padding: 16px; }
    #cmdk-overlay { padding-top: 8vh; padding-left: 12px; padding-right: 12px; }
}

@media (max-width: 420px) {
    /* Below this, even the language toggle competes with the hamburger,
       filename, and the 3 icon buttons for space. */
    #toolbar-top { gap: 6px; }
    .lang-toggle { display: none; }
}
