/* 亮色為預設；暗色由 html[data-theme="dark"] 覆寫。
   沒有明確選擇時（data-theme="system"）跟隨系統設定。 */
:root {
    --bg: #f2f4f7;
    --surface: #ffffff;
    --surface-2: #f7f8fa;
    --border: #d8dce3;
    --text: #1c2128;
    --text-muted: #626b78;
    --primary: #2f6feb;
    --primary-hover: #245bc4;
    --primary-text: #ffffff;
    --danger: #c8412f;
    --shadow: 0 1px 2px rgba(16, 22, 32, 0.06), 0 4px 12px rgba(16, 22, 32, 0.05);
    --radius: 8px;
    --sidebar-width: 300px;
    color-scheme: light;
}

html[data-theme="dark"] {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #232830;
    --border: #333a45;
    --text: #e6e9ee;
    --text-muted: #98a1b0;
    --primary: #4c8dff;
    --primary-hover: #3d78e0;
    --primary-text: #0d1117;
    --danger: #f06a5a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
    html[data-theme="system"] {
        --bg: #14171c;
        --surface: #1c2027;
        --surface-2: #232830;
        --border: #333a45;
        --text: #e6e9ee;
        --text-muted: #98a1b0;
        --primary: #4c8dff;
        --primary-hover: #3d78e0;
        --primary-text: #0d1117;
        --danger: #f06a5a;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
        color-scheme: dark;
    }
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei",
        "PingFang TC", "Noto Sans TC", Roboto, Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
}

.app {
    display: flex;
    min-height: 100vh;
}

/* ---------------- 側邊欄 ---------------- */

.sidebar {
    width: var(--sidebar-width);
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    height: 100vh;
    position: sticky;
    top: 0;
    overflow: hidden;
    transition: width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.2s ease,
                border-color 0.28s ease;
}

body.sidebar-collapsed .sidebar {
    width: 0;
    opacity: 0;
    border-right-color: transparent;
    pointer-events: none;
}

/* 收合時內容不要被壓扁，維持原寬度往左滑出去 */
.sidebar-head,
.sidebar-body,
.sidebar-foot {
    min-width: var(--sidebar-width);
}

.sidebar-head {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid var(--border);
}

.sidebar-head select {
    flex: 1;
}

.sidebar-body {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
}

.sidebar-foot {
    padding: 12px;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
}

.group {
    border-bottom: 1px solid var(--border);
    display: grid;
    grid-template-rows: auto 1fr;
    transition: grid-template-rows 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.group:has(.group-head[aria-expanded="false"]) {
    grid-template-rows: auto 0fr;
}

.group:last-child {
    border-bottom: none;
}

.group-head {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 11px 12px;
    background: none;
    border: none;
    color: var(--text);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    text-align: start;
}

.group-head:hover {
    background: var(--surface-2);
}

.group-head span:nth-child(2) {
    flex: 1;
}

/* SVG 圖示：線稿統一由這裡控制，顏色吃 currentColor 所以亮暗色自動跟著換 */
.icon-sprite {
    position: absolute;
    width: 0;
    height: 0;
    overflow: hidden;
}

.icon {
    width: 18px;
    height: 18px;
    display: block;
    flex: 0 0 auto;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.group-icon {
    display: flex;
    color: var(--text-muted);
}

.chevron {
    display: flex;
    color: var(--text-muted);
    transition: transform 0.15s ease;
}

.group-head[aria-expanded="false"] .chevron {
    transform: rotate(-90deg);
}

.group-head:hover .group-icon,
.group-head:hover .chevron {
    color: var(--text);
}

.group-body {
    padding: 2px 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: hidden;
    min-height: 0;
    /* 只有 0fr 還收不乾淨，垂直 padding 撐著會留一條，所以一起收 */
    transition: padding-top 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                padding-bottom 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.group:has(.group-head[aria-expanded="false"]) .group-body {
    padding-top: 0;
    padding-bottom: 0;
}

/* ---------------- 表單元件 ---------------- */

.field {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.field-label {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    font-size: 12px;
    color: var(--text-muted);
}

.field.readout {
    flex-direction: row;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.value {
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

.mono {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 11px;
    max-width: 16ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.inline {
    display: flex;
    gap: 6px;
}

.inline input {
    flex: 1;
    min-width: 0;
}

.hint {
    margin: 0;
    font-size: 11px;
    color: var(--text-muted);
}

.checkbox {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    cursor: pointer;
}

input[type=text],
input[type=number],
input[type=file],
select {
    width: 100%;
    padding: 7px 8px;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    font-size: 13px;
}

input[type=text]:focus,
input[type=number]:focus,
select:focus {
    outline: 2px solid var(--primary);
    outline-offset: -1px;
}

input:disabled,
select:disabled,
button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

input[type=range] {
    width: 100%;
    accent-color: var(--primary);
}

input[type=file]::file-selector-button {
    margin-inline-end: 8px;
    padding: 4px 10px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 5px;
    cursor: pointer;
    font: inherit;
    font-size: 12px;
}

button {
    padding: 7px 12px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease,
                color 0.15s ease, transform 0.08s ease;
}

button:active:not(:disabled) {
    transform: scale(0.97);
}

button:hover:not(:disabled) {
    border-color: var(--primary);
}

button.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--primary-text);
    font-weight: 600;
}

button.primary:hover:not(:disabled) {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
}

button.block {
    width: 100%;
}

button.small {
    padding: 3px 8px;
    font-size: 12px;
}

.icon-button {
    width: 34px;
    height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex: 0 0 auto;
}

.row {
    display: flex;
    gap: 8px;
}

.row.two>* {
    flex: 1;
}

.row.three>* {
    flex: 1;
}

.tool-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}

.tool-grid.compact {
    grid-template-columns: repeat(6, 1fr);
}

.tool-button {
    aspect-ratio: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.tool-button .icon {
    width: 20px;
    height: 20px;
}

/* 只有圖示、沒有文字的按鈕，讓圖示置中 */
.row>button>.icon,
.tool-grid>button>.icon {
    margin: 0 auto;
}

.icon-button .icon {
    width: 19px;
    height: 19px;
}

.tool-button.active {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--primary-text);
}

.canvas-title {
    animation: fade-in 0.2s ease;
}

@keyframes fade-in {
    from {
        opacity: 0;
        transform: translateY(-3px);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

/* 使用者要求減少動態效果時全部關掉 */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        transition-duration: 0.001ms !important;
    }
}

/* ---------------- 工作區 ---------------- */

.workspace {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 20px;
}

.sidebar-toggle {
    align-self: flex-start;
    display: none;
}

/* 只有側邊欄收起來時才需要這顆 */
body.sidebar-collapsed .sidebar-toggle {
    display: inline-flex;
}

.canvas-container {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    overflow: auto;
}

.canvas-title {
    display: none;
    color: var(--primary);
    font-size: 13px;
    font-weight: 600;
}

.canvas-frame {
    position: relative;
    width: fit-content;
    max-width: 100%;
    /* 棋盤格底色，白色畫布放上去才看得出邊界 */
    background-image:
        linear-gradient(45deg, var(--border) 25%, transparent 25%),
        linear-gradient(-45deg, var(--border) 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, var(--border) 75%),
        linear-gradient(-45deg, transparent 75%, var(--border) 75%);
    background-size: 16px 16px;
    background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

.canvas-container canvas {
    display: block;
    border: 1px solid var(--border);
    background: #ffffff;
    max-width: 100%;
    height: auto;
    image-rendering: pixelated;
}

.canvas-container.crop-mode canvas {
    border: 2px dashed var(--primary);
    cursor: crosshair;
    touch-action: none;
}

/* 裁切時把手會擋到選取框的角落，先收起來 */
.canvas-container.crop-mode .resize-handle {
    display: none;
}

canvas.text-placement-mode {
    border: 2px dashed var(--primary) !important;
    cursor: text !important;
}

.resize-handle {
    position: absolute;
    right: -7px;
    bottom: -7px;
    width: 16px;
    height: 16px;
    border-right: 3px solid var(--primary);
    border-bottom: 3px solid var(--primary);
    border-bottom-right-radius: 3px;
    cursor: nwse-resize;
    opacity: 0.6;
    touch-action: none;
}

.resize-handle:hover,
.resize-handle.dragging {
    opacity: 1;
}

.canvas-container:has(.resize-handle.dragging) {
    user-select: none;
}

.status-bar {
    display: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: 13px;
    color: var(--text-muted);
}

.status-bar b {
    color: var(--text);
    margin-inline-end: 6px;
}

.log-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.log-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
}

.log-container {
    height: 200px;
    overflow-y: auto;
    padding: 8px 12px;
    background: var(--surface-2);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12px;
    word-break: break-word;
}

.log-container .log-line {
    padding: 1px 0;
}

.log-container .time,
.log-container .action {
    display: inline-block;
    white-space: nowrap;
    margin-inline-end: 0.5em;
    color: var(--text-muted);
}

/* ---------------- 條件顯示 ---------------- */

/* 進階設定只在開發者模式出現 */
.dev-only {
    display: none;
}

body.dev-mode .dev-only {
    display: flex;
}

body.dev-mode .row.dev-only {
    display: flex;
}

body.dev-mode .log-panel.dev-only {
    display: block;
}

/* 繪圖工具的細項只在相關工具啟用時出現 */
.text-tools,
.crop-tools {
    display: none;
    flex-direction: column;
    gap: 10px;
}

body.tool-text .text-tools,
body.crop-active .crop-tools {
    display: flex;
}

#shape-fill-field,
#fill-tolerance-field {
    display: none;
}

body.tool-rect #shape-fill-field,
body.tool-ellipse #shape-fill-field,
body.tool-fill #fill-tolerance-field {
    display: flex;
}

/* 沒有槽位資訊時隱藏槽位選擇 */
#slotgroup {
    display: none;
}

/* ---------------- 響應式 ---------------- */

@media (max-width: 860px) {
    .app {
        flex-direction: column;
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 20;
        width: var(--sidebar-width);
        opacity: 1;
        transform: translateX(0);
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: var(--shadow);
    }

    /* 窄畫面用滑出/滑入，不要把寬度縮成 0，動畫比較自然 */
    body.sidebar-collapsed .sidebar {
        width: var(--sidebar-width);
        opacity: 1;
        transform: translateX(-100%);
        border-right-color: var(--border);
    }

    .workspace {
        padding: 12px;
    }
}
