:root {
    color-scheme: light;
    --ink: #172033;
    --muted: #697386;
    --line: #e1e5eb;
    --surface: #ffffff;
    --canvas-bg: #f2f4f7;
    --accent: #14b8a6;
    --accent-dark: #0f766e;
    --panel-hover: #f3f5f7;
    --workspace: #eef1f4;
    --workspace-glow: #ffffff;
    --shadow-color: 23 32 51;
    font-family: Inter, "Avenir Next", "Segoe UI", sans-serif;
}

:root.dark {
    color-scheme: dark;
    --ink: #e7edf5;
    --muted: #98a5b7;
    --line: #303947;
    --surface: #17202d;
    --canvas-bg: #0d141e;
    --accent: #2dd4bf;
    --accent-dark: #5eead4;
    --panel-hover: #232e3d;
    --workspace: #0d141e;
    --workspace-glow: #182332;
    --shadow-color: 0 0 0;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    overflow: hidden;
    background: var(--canvas-bg);
    color: var(--ink);
    text-rendering: optimizeLegibility;
}

button, input, textarea { font: inherit; }
button { color: inherit; }
button, label { -webkit-tap-highlight-color: transparent; }

button:focus-visible, input:focus-visible, textarea:focus-visible {
    outline: 3px solid rgb(20 184 166 / 30%);
    outline-offset: 2px;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.board-app {
    display: grid;
    grid-template-rows: 68px minmax(0, 1fr);
    height: 100dvh;
    min-height: 480px;
}

.board-header {
    position: relative;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding: 0 20px;
    border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    box-shadow: 0 1px 10px rgb(var(--shadow-color) / 8%);
    backdrop-filter: blur(16px);
}

.board-brand, .header-actions { display: flex; align-items: center; }
.board-brand { min-width: 0; gap: 12px; }
.header-actions { gap: 7px; }

.board-home {
    display: grid;
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
    place-items: center;
    border-radius: 11px;
    background: #2dd4bf;
    color: #062f2d;
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-size: 14px;
    font-weight: 900;
    text-decoration: none;
    transition: transform 160ms ease, background 160ms ease;
}
.board-home:hover { transform: rotate(-5deg); background: #5eead4; }

.board-title {
    display: block;
    width: min(38vw, 340px);
    padding: 2px 5px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--ink);
    font-size: 15px;
    font-weight: 750;
    outline: 0;
}
.board-title:hover, .board-title:focus { background: var(--panel-hover); }
.board-brand p { margin: 2px 5px 0; color: var(--muted); font-size: 11px; }
.save-status::before { content: ""; display: inline-block; width: 6px; height: 6px; margin-right: 6px; border-radius: 50%; background: var(--accent); vertical-align: 1px; }
.save-status.is-saving::before { background: #f59e0b; }

.icon-action, .text-action, .primary-action {
    height: 38px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--surface);
    cursor: pointer;
    font-size: 13px;
    font-weight: 700;
    transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}
.icon-action { display: grid; width: 38px; place-items: center; padding: 0; }
.icon-action svg, .primary-action svg { width: 18px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.icon-action:hover:not(:disabled), .text-action:hover { border-color: #8793a3; background: var(--panel-hover); }
.icon-action:disabled { opacity: .35; cursor: default; }
.text-action { padding: 0 15px; }
.primary-action { display: flex; align-items: center; gap: 7px; padding: 0 15px; border-color: #0f766e; background: #0f766e; color: white; }
.primary-action:hover { background: #115e59; transform: translateY(-1px); }
.primary-action svg { width: 14px; }
.theme-toggle .sun-icon { display: none; }
.dark .theme-toggle .moon-icon { display: none; }
.dark .theme-toggle .sun-icon { display: block; }

.menu-wrap { position: relative; }
.export-menu {
    position: absolute;
    top: calc(100% + 9px);
    right: 0;
    width: 220px;
    padding: 7px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface);
    box-shadow: 0 18px 45px rgb(var(--shadow-color) / 30%);
}
.export-menu[hidden] { display: none; }
.export-menu button { display: block; width: 100%; padding: 10px 11px; border: 0; border-radius: 9px; background: transparent; text-align: left; cursor: pointer; }
.export-menu button:hover { background: var(--panel-hover); }
.export-menu strong, .export-menu span { display: block; }
.export-menu strong { font-size: 13px; }
.export-menu span { margin-top: 3px; color: var(--muted); font-size: 11px; }

.board-workspace {
    display: grid;
    grid-template-columns: 78px minmax(0, 1fr) 230px;
    min-height: 0;
}

.tool-rail {
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 14px 10px;
    overflow-y: auto;
    border-right: 1px solid var(--line);
    background: var(--surface);
}

.tool-button {
    display: grid;
    width: 56px;
    min-height: 52px;
    padding: 7px 4px 5px;
    place-items: center;
    gap: 2px;
    border: 0;
    border-radius: 11px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 10px;
    font-weight: 700;
    transition: color 140ms ease, background 140ms ease;
}
.tool-button svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.tool-button:hover { background: var(--panel-hover); color: var(--ink); }
.tool-button.is-active { background: #dff7f3; color: #0b6b63; }
.dark .tool-button.is-active { background: #123d3a; color: #5eead4; }
.tool-separator { width: 34px; height: 1px; margin: 5px 0; background: var(--line); }

.canvas-area {
    position: relative;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    background-color: var(--workspace);
    background-image: radial-gradient(circle at 50% 30%, var(--workspace-glow) 0, transparent 64%);
}

.canvas-scroll { width: 100%; height: 100%; overflow: auto; }

.canvas-stage {
    display: grid;
    width: max-content;
    min-width: 100%;
    min-height: 100%;
    padding: 28px;
    place-items: center;
}

.canvas-frame {
    position: relative;
    flex: none;
    width: 800px;
    aspect-ratio: 8 / 5;
    overflow: hidden;
    border: 1px solid #d8dde4;
    border-radius: 4px;
    background: white;
    box-shadow: 0 12px 35px rgb(var(--shadow-color) / 22%), 0 2px 7px rgb(var(--shadow-color) / 14%);
}

#boardCanvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
    touch-action: none;
    user-select: none;
}
#boardCanvas[data-tool="eraser"] { cursor: cell; }
#boardCanvas[data-tool="text"] { cursor: text; }
#boardCanvas[data-tool="select"] { cursor: default; }
#boardCanvas[data-tool="hand"] { cursor: grab; }
#boardCanvas[data-tool="hand"].is-panning { cursor: grabbing; }

.text-editor {
    position: absolute;
    z-index: 3;
    min-width: 120px;
    max-width: min(420px, 70%);
    min-height: 1.35em;
    padding: 2px 4px;
    overflow: hidden;
    border: 1px dashed var(--accent-dark);
    background: rgb(255 255 255 / 88%);
    color: var(--editor-color, #172033);
    font-family: Inter, "Avenir Next", "Segoe UI", sans-serif;
    font-size: var(--editor-size, 24px);
    line-height: 1.25;
    resize: none;
    outline: none;
}
.text-editor[hidden] { display: none; }

.canvas-hint {
    position: absolute;
    z-index: 5;
    bottom: 12px;
    left: 50%;
    width: max-content;
    padding: 6px 10px;
    border: 1px solid rgb(210 215 223 / 70%);
    border-radius: 999px;
    background: rgb(255 255 255 / 80%);
    color: #7b8493;
    font-size: 11px;
    transform: translateX(-50%);
    pointer-events: none;
    backdrop-filter: blur(8px);
}

.zoom-controls {
    position: absolute;
    z-index: 6;
    bottom: 16px;
    right: 16px;
    display: flex;
    width: max-content;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    box-shadow: 0 6px 20px rgb(var(--shadow-color) / 16%);
    backdrop-filter: blur(12px);
}
.zoom-controls button { display: grid; height: 36px; min-width: 36px; padding: 0 10px; place-items: center; border: 0; border-right: 1px solid var(--line); background: transparent; cursor: pointer; font-size: 18px; font-weight: 700; }
.zoom-controls button:last-child { border-right: 0; }
.zoom-controls button:hover:not(:disabled) { background: var(--panel-hover); }
.zoom-controls button:disabled { opacity: .35; cursor: default; }
.zoom-controls .zoom-level { min-width: 61px; color: var(--muted); font-size: 11px; }

.property-panel {
    z-index: 10;
    overflow-y: auto;
    border-left: 1px solid var(--line);
    background: var(--surface);
}
.panel-section { padding: 19px 18px; border-bottom: 1px solid var(--line); }
.panel-section h2, .range-heading label { margin: 0 0 12px; color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.section-note { display: block; margin-top: 4px; font-size: 8px; font-weight: 650; letter-spacing: 0; text-transform: none; }
.color-row { display: flex; flex-wrap: wrap; gap: 8px; }
.color-swatch, .custom-color {
    position: relative;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 2px solid white;
    border-radius: 50%;
    background: var(--swatch);
    box-shadow: 0 0 0 1px #d4d9e0;
    cursor: pointer;
}
.color-swatch.is-active { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--accent); }
.custom-color { display: grid; place-items: center; overflow: hidden; border-style: dashed; background: conic-gradient(#f43f5e, #f59e0b, #22c55e, #3b82f6, #a855f7, #f43f5e); color: white; font-size: 18px; font-weight: 700; text-shadow: 0 1px 3px #000; }
.custom-color input { position: absolute; width: 50px; height: 50px; opacity: 0; cursor: pointer; }

.range-heading { display: flex; align-items: center; justify-content: space-between; }
.range-heading label { margin: 0; }
.range-heading output { min-width: 31px; padding: 3px 6px; border-radius: 6px; background: #f1f3f5; color: #5c6677; font-family: ui-monospace, monospace; font-size: 11px; text-align: center; }
.dark .range-heading output { background: #263140; color: #b5c0cf; }
.range-input { width: 100%; height: 4px; border-radius: 999px; accent-color: var(--accent-dark); cursor: pointer; }

.background-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.background-choice { display: grid; gap: 5px; padding: 7px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); color: var(--muted); font-size: 10px; font-weight: 700; text-align: left; cursor: pointer; }
.background-choice.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px rgb(20 184 166 / 12%); color: var(--accent-dark); }
.bg-preview { position: relative; display: block; height: 31px; overflow: hidden; border: 1px solid #e0e4e9; border-radius: 5px; background-color: #fff; }
.bg-preview.dots { background-image: radial-gradient(#aab2bf 1px, transparent 1px); background-size: 8px 8px; }
.bg-preview.grid { background-image: linear-gradient(#d7dce3 1px, transparent 1px), linear-gradient(90deg, #d7dce3 1px, transparent 1px); background-size: 9px 9px; }
.bg-preview.color { display: grid; place-items: center; background: linear-gradient(135deg, #fff3d6 0 50%, #dcecff 50%); }
.bg-preview.color input { width: 24px; height: 24px; padding: 0; border: 0; border-radius: 4px; background: transparent; cursor: pointer; }

.shape-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.shape-choice { display: grid; min-width: 0; padding: 8px 4px 6px; place-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); color: var(--muted); cursor: pointer; font-size: 10px; font-weight: 700; }
.shape-choice svg { width: 35px; height: 27px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.7; }
.shape-glyph { display: grid; width: 35px; height: 27px; place-items: center; font-family: "Segoe UI Symbol", sans-serif; font-size: 24px; font-weight: 500; line-height: 1; }
.shape-glyph-text { font-size: 12px; font-weight: 900; letter-spacing: -.03em; }
.shape-choice:hover { background: var(--panel-hover); color: var(--ink); }
.shape-choice.is-active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--accent-dark); box-shadow: 0 0 0 2px rgb(20 184 166 / 10%); }

.panel-footer { border-bottom: 0; }
.danger-action { width: 100%; padding: 9px; border: 1px solid #fecaca; border-radius: 9px; background: var(--surface); color: #b91c1c; font-size: 12px; font-weight: 750; cursor: pointer; }
.dark .danger-action { border-color: #68343b; color: #fca5a5; }
.danger-action:hover { background: #fef2f2; }
.dark .danger-action:hover { background: #362329; }
.panel-footer p { margin: 12px 2px 0; color: var(--muted); font-size: 10px; line-height: 1.5; }

.toast {
    position: fixed;
    z-index: 50;
    right: 22px;
    bottom: 20px;
    padding: 10px 14px;
    border-radius: 10px;
    background: #172033;
    color: white;
    box-shadow: 0 10px 30px rgb(23 32 51 / 22%);
    font-size: 12px;
    font-weight: 700;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 160ms ease, transform 160ms ease;
    pointer-events: none;
}
.toast.is-visible { opacity: 1; transform: translateY(0); }

@media (max-width: 900px) {
    .board-workspace { grid-template-columns: 68px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
    .property-panel { grid-column: 1 / -1; display: flex; min-height: 116px; overflow-x: auto; overflow-y: hidden; border-top: 1px solid var(--line); border-left: 0; }
    .panel-section { min-width: 190px; border-right: 1px solid var(--line); border-bottom: 0; }
    .panel-footer { min-width: 150px; }
    .canvas-stage { padding: 18px; }
}

@media (max-width: 620px) {
    .board-app { grid-template-rows: 60px minmax(0, 1fr); }
    .board-header { padding: 0 10px; gap: 8px; }
    .board-home { width: 34px; height: 34px; }
    .board-title { width: 30vw; font-size: 13px; }
    .board-brand p { display: none; }
    .header-actions { gap: 4px; }
    .icon-action, .text-action { display: none; }
    #undoButton, #redoButton { display: grid; width: 34px; height: 34px; }
    .text-action { height: 34px; padding: 0 10px; }
    .primary-action { height: 34px; padding: 0 11px; }
    .board-workspace { grid-template-columns: 1fr; grid-template-rows: 58px minmax(0, 1fr) 108px; }
    .tool-rail { flex-direction: row; align-items: center; gap: 2px; padding: 5px 8px; overflow-x: auto; overflow-y: hidden; border-right: 0; border-bottom: 1px solid var(--line); }
    .tool-button { flex: 0 0 48px; width: 48px; min-height: 46px; padding: 5px 2px; }
    .tool-button svg { width: 19px; height: 19px; }
    .tool-separator { flex: 0 0 1px; width: 1px; height: 30px; margin: 0 3px; }
    .canvas-stage { padding: 10px; }
    .canvas-hint { display: none; }
    .property-panel { grid-column: 1; min-height: 108px; }
    .panel-section { min-width: 170px; padding: 14px; }
    .background-grid { display: flex; }
    .background-choice { width: 64px; flex: 0 0 64px; }
    .object-library-section { min-width: max-content; }
    .shape-grid { display: flex; width: max-content; }
    .shape-choice { width: 70px; flex: 0 0 70px; }
    .zoom-controls { right: 10px; bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition-duration: .01ms !important; }
}
