:root {
    --ink: #16181d;
    --ink-2: #4a4f59;
    --line: #e3e5ea;
    --soft: #f4f5f7;
    --panel: #ffffff;
    --stage: #eceef2;
    --accent: #e8364f;
    --accent-ink: #ffffff;
    --fill: #d9dce3;
    --radius: 10px;
    color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--stage);
    color: var(--ink);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, input, select { font: inherit; color: inherit; }

/* ---------- top bar ---------- */

.top {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 0 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    position: relative;
    z-index: 20;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: var(--ink);
    font-weight: 700;
    font-size: 17px;
}

.brand-mark {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: var(--ink);
    color: #fff;
    display: grid;
    place-items: center;
    font-size: 13px;
    letter-spacing: .5px;
}

.top-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn {
    height: 36px;
    padding: 0 14px;
    border: 1px solid var(--line);
    background: var(--panel);
    border-radius: 8px;
    cursor: pointer;
    white-space: nowrap;
}

.btn:hover:not(:disabled) { background: var(--soft); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.icon { width: 36px; padding: 0; font-size: 18px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { background: #d42a43; }

/* ---------- aspect ratio popover ---------- */

.ratio-wrap { position: relative; }

.ratio-pop {
    position: absolute;
    top: 44px;
    left: 50%;
    transform: translateX(-50%);
    width: 420px;
    max-width: calc(100vw - 32px);
    max-height: 70vh;
    overflow: auto;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 18px 40px rgba(16, 20, 30, .18);
    padding: 14px;
}

.pop-title { font-weight: 600; margin-bottom: 10px; color: var(--ink-2); }

.ratio-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.ratio-item {
    border: 1px solid transparent;
    background: none;
    border-radius: 8px;
    padding: 10px 6px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    text-align: center;
    color: var(--ink-2);
}

.ratio-item:hover { background: var(--soft); }
.ratio-item.active { background: #fde8eb; border-color: #f6b3bd; color: var(--ink); }

.ratio-shape {
    border: 2px solid #9aa0ab;
    border-radius: 3px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ---------- layout ---------- */

.layout {
    display: flex;
    height: calc(100% - 60px);
}

.stage {
    flex: 1;
    min-width: 0;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px;
    overflow: hidden;
}

.canvas-wrap {
    position: relative;
    line-height: 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .08);
}

/* A checkerboard behind the canvas, so a transparent export is visibly transparent. */
#preview {
    display: block;
    background-color: #fff;
    background-image:
        linear-gradient(45deg, #e6e8ec 25%, transparent 25%),
        linear-gradient(-45deg, #e6e8ec 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #e6e8ec 75%),
        linear-gradient(-45deg, transparent 75%, #e6e8ec 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

.stage-info {
    margin-top: 12px;
    color: var(--ink-2);
    font-size: 13px;
    min-height: 18px;
}

.empty {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    line-height: 1.45;
}

.empty[hidden] { display: none; }

.empty-card {
    background: rgba(255, 255, 255, .94);
    border-radius: 14px;
    padding: 26px 30px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
    max-width: 340px;
}

.empty-title { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.empty-line { color: var(--ink-2); margin-bottom: 4px; }
.empty-card .btn { margin-top: 12px; }
.empty-note { margin-top: 12px; font-size: 13px; color: var(--ink-2); }

kbd {
    display: inline-block;
    padding: 1px 7px;
    border: 1px solid var(--line);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: var(--soft);
    font: 13px/1.5 ui-monospace, Consolas, monospace;
}

.drop-hint {
    position: absolute;
    inset: 12px;
    border: 3px dashed var(--accent);
    border-radius: 16px;
    background: rgba(232, 54, 79, .08);
    display: none;
    place-items: center;
    font-size: 20px;
    font-weight: 700;
    color: var(--accent);
    pointer-events: none;
}

.stage.dragging .drop-hint { display: grid; }

/* ---------- side panel ---------- */

.panel {
    width: 360px;
    flex-shrink: 0;
    background: var(--panel);
    border-left: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.tabs {
    display: flex;
    gap: 4px;
    padding: 10px;
    border-bottom: 1px solid var(--line);
}

.tab {
    flex: 1;
    height: 36px;
    border: 0;
    border-radius: 8px;
    background: none;
    cursor: pointer;
    color: var(--ink-2);
    font-weight: 500;
}

.tab:hover { background: var(--soft); }
.tab.active { background: var(--soft); color: var(--ink); font-weight: 600; }

.tab-body {
    flex: 1;
    overflow-y: auto;
    padding: 6px 16px 40px;
}

.group {
    padding: 14px 0;
    border-bottom: 1px solid var(--line);
}

.group:last-child { border-bottom: 0; }

h3 {
    margin: 0 0 10px;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .6px;
    color: var(--ink-2);
}

details > summary { list-style: none; cursor: pointer; }
details > summary::-webkit-details-marker { display: none; }
details > summary h3 { margin: 0; display: inline-flex; gap: 8px; align-items: center; }
details > summary h3::after { content: "+"; font-size: 15px; }
details[open] > summary h3::after { content: "\2212"; }
details[open] > summary { margin-bottom: 10px; }

.head-row { display: flex; justify-content: space-between; align-items: center; }

.row + .row { margin-top: 8px; }

/* Slider: a bar that fills, with the label and value painted on it. A native
   range input sits on top at zero opacity so dragging and keys still work. */
.slider {
    position: relative;
    height: 36px;
    border-radius: 8px;
    background: var(--soft);
    overflow: hidden;
    user-select: none;
}

.slider .fill {
    position: absolute;
    inset: 0 auto 0 0;
    background: var(--fill);
    pointer-events: none;
}

.slider .label,
.slider .val {
    position: absolute;
    top: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    pointer-events: none;
    font-size: 14px;
}

.slider .label { left: 12px; color: var(--ink); }
.slider .val { right: 12px; font-variant-numeric: tabular-nums; color: var(--ink-2); }

.slider input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: ew-resize;
}

.slider:focus-within { outline: 2px solid #f6b3bd; outline-offset: 1px; }

.color-row {
    margin-top: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 36px;
    padding: 0 6px 0 12px;
    border-radius: 8px;
    background: var(--soft);
    cursor: pointer;
    font-size: 14px;
}

input[type=color] {
    width: 44px;
    height: 26px;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: none;
    cursor: pointer;
}

/* ---------- frames ---------- */

.frame-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.frame-item {
    border: 2px solid transparent;
    background: var(--soft);
    border-radius: 8px;
    padding: 6px 4px 6px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    color: var(--ink-2);
    line-height: 1.2;
}

.frame-item canvas { width: 64px; height: 44px; display: block; }
.frame-item:hover { border-color: var(--line); }
.frame-item.active { border-color: var(--accent); color: var(--ink); }

/* ---------- position ---------- */

.pos-layout { display: flex; gap: 12px; align-items: center; }
.pos-sliders { flex: 1; }

.pos-grid {
    display: grid;
    grid-template-columns: repeat(3, 22px);
    gap: 5px;
}

.pos-cell {
    width: 22px;
    height: 22px;
    border: 0;
    border-radius: 5px;
    background: var(--fill);
    cursor: pointer;
    padding: 0;
}

.pos-cell:hover { background: #b9bec8; }
.pos-cell.active { background: var(--accent); }

/* ---------- swatches ---------- */

.swatches, .overlay-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 8px;
}

.overlay-grid { grid-template-columns: repeat(4, 1fr); margin-bottom: 10px; }

.swatch {
    aspect-ratio: 1;
    border-radius: 8px;
    border: 2px solid transparent;
    cursor: pointer;
    padding: 0;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08);
}

.swatch:hover { transform: scale(1.06); }
.swatch.active { border-color: var(--ink); box-shadow: 0 0 0 2px #fff inset; }

.overlay-item {
    aspect-ratio: 3 / 2;
    border-radius: 8px;
    border: 2px solid transparent;
    background: var(--soft);
    cursor: pointer;
    padding: 0;
    overflow: hidden;
    font-size: 13px;
    color: var(--ink-2);
}

.overlay-item canvas { width: 100%; height: 100%; display: block; }
.overlay-item.active { border-color: var(--accent); }

.custom-bg { display: flex; gap: 8px; }

.swatch-btn {
    flex: 1;
    height: 44px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 12px;
    border: 2px solid transparent;
    border-radius: 8px;
    background: var(--soft);
    cursor: pointer;
    font-size: 14px;
    position: relative;
}

.swatch-btn input[type=color] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.swatch-btn.active { border-color: var(--ink); }

.dot {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .15);
    flex-shrink: 0;
}

.dot.checker {
    background:
        conic-gradient(#cfd3da 25%, #fff 0 50%, #cfd3da 0 75%, #fff 0) 0 0 / 10px 10px;
}

/* ---------- export tab ---------- */

.field {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 10px;
    font-size: 14px;
}

.field select, .field input {
    height: 36px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0 10px;
    background: var(--panel);
    min-width: 170px;
}

.export-size { color: var(--ink-2); font-size: 13px; }
.note { margin: 0 0 10px; color: var(--ink-2); font-size: 14px; }

/* ---------- toast ---------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translate(-50%, 20px);
    background: var(--ink);
    color: #fff;
    padding: 10px 18px;
    border-radius: 10px;
    opacity: 0;
    transition: opacity .2s, transform .2s;
    pointer-events: none;
    z-index: 50;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- narrow screens ---------- */

@media (max-width: 860px) {
    html, body { height: auto; }
    .layout { flex-direction: column; height: auto; }
    .stage { min-height: 56vh; padding: 16px; }
    .panel { width: 100%; border-left: 0; border-top: 1px solid var(--line); }
    .tab-body { overflow: visible; }
    .top { height: auto; flex-wrap: wrap; padding: 10px 16px; }
    .top-actions { flex-wrap: wrap; }
    #open-btn { display: none; }
}

/* ---------- code mode ---------- */

.seg {
    display: flex;
    background: var(--soft);
    border-radius: 10px;
    padding: 4px;
    gap: 4px;
}

.seg button {
    flex: 1;
    height: 34px;
    border: 0;
    border-radius: 7px;
    background: none;
    cursor: pointer;
    color: var(--ink-2);
    font-weight: 500;
}

.seg button.active {
    background: var(--panel);
    color: var(--ink);
    font-weight: 600;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
}

#code-input {
    display: block;
    width: 100%;
    min-height: 150px;
    resize: vertical;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfbfc;
    font: 13px/1.5 ui-monospace, "Cascadia Mono", Consolas, monospace;
    tab-size: 4;
    white-space: pre;
    overflow: auto;
    margin-bottom: 8px;
}

#code-input:focus { outline: 2px solid #f6b3bd; outline-offset: 1px; }

.code-note { font-size: 13px; margin-bottom: 12px; }
.code-note.is-pasted { color: #0f7a4a; font-weight: 600; }

.check-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 10px;
    font-size: 14px;
    cursor: pointer;
}

.check-row input { width: 18px; height: 18px; accent-color: var(--accent); }

/* ---------- tilt pad ---------- */

.tilt-layout { display: flex; gap: 16px; align-items: center; }

.tilt-pad {
    position: relative;
    width: 120px;
    height: 120px;
    flex-shrink: 0;
    border-radius: 12px;
    background: var(--soft);
    border: 1px solid var(--line);
    cursor: grab;
    touch-action: none;
}

.tilt-pad:active { cursor: grabbing; }
.tilt-pad:focus-visible { outline: 2px solid #f6b3bd; outline-offset: 2px; }

.tilt-cross::before,
.tilt-cross::after {
    content: "";
    position: absolute;
    background: var(--fill);
}

.tilt-cross::before { left: 50%; top: 10px; bottom: 10px; width: 1px; }
.tilt-cross::after { top: 50%; left: 10px; right: 10px; height: 1px; }

.tilt-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 26px;
    height: 26px;
    margin: -13px 0 0 -13px;
    border-radius: 50%;
    background: var(--ink);
    box-shadow: 0 2px 6px rgba(0, 0, 0, .3);
    pointer-events: none;
}

.tilt-side { flex: 1; min-width: 0; }
.tilt-readout { font-weight: 600; margin-bottom: 4px; font-size: 14px; }
.tilt-side .note { font-size: 13px; margin-bottom: 8px; }
