/* ===========================
   CSS 變數與基礎設定
   =========================== */
:root {
    /* 主色調 */
    --primary: #6366f1;
    --primary-light: #818cf8;
    --primary-dark: #4f46e5;

    /* 類別顏色 */
    --class-a: #f43f5e;
    --class-a-light: #fb7185;
    --class-b: #06b6d4;
    --class-b-light: #22d3ee;

    /* 決策樹區域顏色 */
    --region-a: rgba(244, 63, 94, 0.2);
    --region-b: rgba(6, 182, 212, 0.2);
    --split-line: rgba(255, 255, 255, 0.4);

    /* 背景與表面 */
    --bg-gradient-start: #0f172a;
    --bg-gradient-end: #1e293b;
    --surface: rgba(30, 41, 59, 0.8);
    --surface-light: rgba(51, 65, 85, 0.6);
    --glass: rgba(255, 255, 255, 0.05);
    --glass-border: rgba(255, 255, 255, 0.1);

    /* 文字 */
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;

    /* 其他 */
    --success: #22c55e;
    --warning: #f59e0b;
    --radius: 16px;
    --radius-sm: 8px;
    --shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    /* 樹狀圖連接線 */
    --connector-color: #94a3b8;
}

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

body {
    font-family: 'Noto Sans TC', -apple-system, BlinkMacSystemFont, sans-serif;
    background: linear-gradient(135deg, var(--bg-gradient-start), var(--bg-gradient-end));
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.6;
}

/* ===========================
   容器佈局
   =========================== */
.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ===========================
   頁首
   =========================== */
.header {
    text-align: center;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: var(--glass);
    border-radius: var(--radius);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
}

.header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    background: linear-gradient(135deg, #10b981, #3b82f6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1.1rem;
}

/* ===========================
   主要內容區
   =========================== */
.main-content {
    display: grid;
    grid-template-columns: 1fr 380px;
    gap: 2rem;
}

/* ===========================
   視覺化區域
   =========================== */
.visualization-section {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* ===========================
   畫布工具列
   =========================== */
.canvas-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--glass-border);
    padding: 0.75rem 1.25rem;
    flex-wrap: wrap;
}

.toolbar-left,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.toolbar-label {
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-weight: 500;
    white-space: nowrap;
}

/* 畫筆按鈕 */
.brush-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0.85rem;
    border: 2px solid transparent;
    border-radius: 50px;
    background: var(--glass);
    color: var(--text-secondary);
    font-size: 0.82rem;
    font-family: inherit;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
}

.brush-btn:hover {
    transform: translateY(-1px);
}

.brush-btn.active.brush-a {
    border-color: var(--class-a);
    background: rgba(244, 63, 94, 0.15);
    color: var(--class-a-light);
    box-shadow: 0 0 12px rgba(244, 63, 94, 0.25);
}

.brush-btn.active.brush-b {
    border-color: var(--class-b);
    background: rgba(6, 182, 212, 0.15);
    color: var(--class-b-light);
    box-shadow: 0 0 12px rgba(6, 182, 212, 0.25);
}

.brush-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.brush-dot.dot-a {
    background: var(--class-a);
}

.brush-dot.dot-b {
    background: var(--class-b);
}

/* 模板下拉選單 */
.template-select {
    appearance: none;
    background-color: var(--surface-light);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-sm);
    padding: 0.5rem 2rem 0.5rem 0.75rem;
    font-size: 0.82rem;
    color: var(--text-primary);
    cursor: pointer;
    font-family: inherit;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.5rem center;
    transition: var(--transition);
}

.template-select:focus {
    outline: none;
    border-color: var(--primary);
}

.template-select option {
    background: #1e293b;
    color: var(--text-primary);
}

/* 小型圖標按鈕 */
.btn-icon {
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-sm);
    background: var(--glass);
    color: var(--text-secondary);
    font-size: 1rem;
    cursor: pointer;
    transition: var(--transition);
}

.btn-icon:hover {
    background: rgba(244, 63, 94, 0.15);
    border-color: var(--class-a);
    transform: translateY(-1px);
}

/* ===========================
   Canvas 容器
   =========================== */
.canvas-container {
    position: relative;
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--glass-border);
    padding: 1.25rem;
    box-shadow: var(--shadow);
}

#treeCanvas {
    display: block;
    width: 100%;
    height: 100%;
    min-height: 420px;
    background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
    border-radius: var(--radius-sm);
    cursor: crosshair;
}

.canvas-hint {
    position: absolute;
    bottom: 1.75rem;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 23, 42, 0.8);
    padding: 0.4rem 1rem;
    border-radius: 50px;
    font-size: 0.8rem;
    color: var(--text-secondary);
    pointer-events: none;
    text-align: center;
    white-space: nowrap;
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(5px);
    transition: opacity 0.5s;
}

.canvas-hint.hidden {
    opacity: 0;
}

/* ===========================
   樹狀圖顯示區域
   =========================== */
.tree-container-wrapper {
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--glass-border);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    overflow: auto;
    min-height: 200px;
}

.tree-container-wrapper h3 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
    text-align: center;
    position: sticky;
    left: 0;
}

.tree-hint {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 400;
}

.tree-structure {
    padding: 0.5rem;
    min-width: 500px;
}

/* ===========================
   橫向樹節點佈局
   =========================== */
.node {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.node-children {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

/* 節點卡片基底 */
.tf-nc {
    position: relative;
    padding: 0.5rem 0.85rem;
    margin: 8px 18px 8px 0;
    text-align: center;
    background: var(--glass);
    border: 1px solid var(--glass-border);
    border-radius: 6px;
    min-width: 95px;
    transition: var(--transition);
    z-index: 10;
    cursor: pointer;
    user-select: none;
}

.tf-nc:hover {
    transform: scale(1.06);
    z-index: 20;
}

/* 內部節點 (Split Node) */
.node-internal {
    background: linear-gradient(135deg, var(--surface), var(--bg-gradient-end));
    border-color: var(--primary-light);
    color: var(--text-primary);
    font-weight: 500;
}

.node-internal:hover {
    box-shadow: 0 0 18px rgba(99, 102, 241, 0.4);
    border-color: var(--primary);
}

.node-internal .condition {
    font-family: 'Courier New', monospace;
    color: var(--primary-light);
    font-weight: bold;
    font-size: 0.85rem;
}

/* 高亮中的節點（懸停觸發） */
.tf-nc.highlight {
    box-shadow: 0 0 20px rgba(250, 204, 21, 0.5);
    border-color: #facc15;
}

/* 葉節點 (Leaf) */
.node-leaf {
    border-width: 2px;
}

.node-leaf.leaf-a {
    background: rgba(244, 63, 94, 0.15);
    border-color: var(--class-a);
    color: var(--class-a-light);
}

.node-leaf.leaf-a:hover {
    box-shadow: 0 0 18px rgba(244, 63, 94, 0.4);
}

.node-leaf.leaf-b {
    background: rgba(6, 182, 212, 0.15);
    border-color: var(--class-b);
    color: var(--class-b-light);
}

.node-leaf.leaf-b:hover {
    box-shadow: 0 0 18px rgba(6, 182, 212, 0.4);
}

.node-stats {
    font-size: 0.7rem;
    margin-top: 0.25rem;
    color: var(--text-muted);
}

/* ===========================
   樹連接線 (橫向)
   =========================== */
.node-internal::after {
    content: '';
    position: absolute;
    top: 50%;
    right: -18px;
    width: 18px;
    height: 1px;
    background-color: var(--connector-color);
}

.node-children > .node {
    position: relative;
    padding-left: 18px;
}

.node-children > .node::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    width: 18px;
    height: 1px;
    background-color: var(--connector-color);
}

.node-children > .node::after {
    content: '';
    position: absolute;
    left: 0;
    width: 1px;
    background-color: var(--connector-color);
}

.node-children > .node::after {
    top: 0;
    height: 100%;
}

.node-children > .node:first-child::after {
    top: 50%;
    height: 50%;
}

.node-children > .node:last-child::after {
    top: 0;
    height: 50%;
}

.node-children > .node:only-child::after {
    display: block;
    top: 50%;
    height: 0;
    width: 0;
}

/* ===========================
   右側控制面板
   =========================== */
.control-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* 卡片通用樣式 */
.card {
    background: var(--surface);
    padding: 1.25rem;
    border-radius: var(--radius);
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
}

.card h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text-primary);
}

.card p {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

/* 圖例 */
.legend-items {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.82rem;
    color: var(--text-secondary);
}

.dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}

.dot.class-a {
    background: var(--class-a);
    box-shadow: 0 0 8px var(--class-a);
}

.dot.class-b {
    background: var(--class-b);
    box-shadow: 0 0 8px var(--class-b);
}

.dot.dot-split {
    background: rgba(255, 255, 255, 0.6);
    width: 20px;
    border-radius: 2px;
    height: 3px;
    border: 1px dashed rgba(255, 255, 255, 0.6);
}

/* 控制面板 */
.control-panel {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.slider-container {
    width: 100%;
}

.slider-container label {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.label-text {
    font-weight: 500;
    color: var(--text-secondary);
}

.k-value {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    font-size: 1.15rem;
    font-weight: 700;
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

/* 滑桿 */
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 7px;
    background: var(--surface-light);
    border-radius: 4px;
    outline: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 22px;
    height: 22px;
    background: linear-gradient(135deg, var(--primary-light), var(--primary));
    border-radius: 50%;
    cursor: grab;
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.5);
    transition: var(--transition);
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.1);
}

.slider-labels {
    display: flex;
    justify-content: space-between;
    margin-top: 0.25rem;
    font-size: 0.72rem;
    color: var(--text-muted);
}

/* 下拉選單 */
.select-container {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.custom-select {
    appearance: none;
    background-color: var(--surface-light);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-sm);
    padding: 0.7rem;
    font-size: 0.88rem;
    color: var(--text-primary);
    width: 100%;
    cursor: pointer;
    font-family: inherit;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    background-size: 1rem;
    transition: var(--transition);
}

.custom-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.custom-select option {
    background: #1e293b;
    color: var(--text-primary);
}

/* 按鈕群組 */
.button-group {
    display: flex;
    gap: 0.75rem;
}

.btn-reset,
.btn-random {
    flex: 1;
    padding: 0.7rem 1rem;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
    font-family: inherit;
}

.btn-reset {
    background: var(--surface-light);
    color: var(--text-primary);
    border: 1px solid var(--glass-border);
}

.btn-reset:hover {
    background: var(--glass-border);
    transform: translateY(-2px);
}

.btn-random {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    box-shadow: 0 4px 15px rgba(99, 102, 241, 0.3);
}

.btn-random:hover {
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5);
    transform: translateY(-2px);
}

/* ===========================
   逐步演練面板
   =========================== */
.step-desc {
    font-size: 0.82rem !important;
    margin-bottom: 0.75rem !important;
}

.step-controls {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.btn-step {
    flex: 1;
    padding: 0.55rem;
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-sm);
    background: var(--glass);
    color: var(--text-secondary);
    font-size: 1.1rem;
    cursor: pointer;
    transition: var(--transition);
}

.btn-step:hover {
    background: var(--surface-light);
    transform: translateY(-1px);
}

.btn-step-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    border-color: transparent;
    box-shadow: 0 3px 10px rgba(99, 102, 241, 0.3);
}

.btn-step-primary:hover {
    box-shadow: 0 5px 15px rgba(99, 102, 241, 0.5);
}

.btn-step.active-autoplay {
    background: linear-gradient(135deg, var(--warning), #d97706);
    border-color: transparent;
    color: white;
    box-shadow: 0 3px 10px rgba(245, 158, 11, 0.3);
}

.step-status {
    text-align: center;
    font-size: 0.82rem;
    color: var(--text-muted);
    font-family: 'Courier New', monospace;
}

/* ===========================
   結果面板
   =========================== */
.result-details {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.6rem;
}

.detail-item {
    background: var(--glass);
    padding: 0.6rem;
    border-radius: var(--radius-sm);
    text-align: center;
}

.detail-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 0.2rem;
}

.detail-value {
    font-size: 1rem;
    font-weight: 600;
}

/* ===========================
   數學解析卡片
   =========================== */
.math-card {
    border-color: rgba(99, 102, 241, 0.2);
}

.math-content {
    font-size: 0.85rem;
    color: var(--text-secondary);
    line-height: 1.7;
}

.math-placeholder {
    text-align: center;
    padding: 1rem 0;
    color: var(--text-muted);
    font-size: 0.82rem;
}

.math-formula {
    background: var(--glass);
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    font-family: 'Courier New', monospace;
    color: var(--primary-light);
    margin: 0.5rem 0;
    text-align: center;
    font-size: 0.92rem;
    letter-spacing: 0.5px;
}

.math-result {
    text-align: center;
    font-weight: 600;
    font-size: 1.1rem;
    margin-top: 0.5rem;
}

.math-label {
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

/* ===========================
   下方教學說明區域
   =========================== */
.info-section {
    margin-top: 2rem;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.25rem;
}

.info-card p,
.info-card li {
    font-size: 0.82rem;
    color: var(--text-secondary);
    line-height: 1.55;
}

.info-list {
    list-style: none;
    padding: 0;
}

.info-list li {
    margin-bottom: 0.4rem;
    padding-left: 1rem;
    position: relative;
}

.info-list li::before {
    content: "•";
    position: absolute;
    left: 0;
    color: var(--primary-light);
}

/* 公式區塊 */
.formula-block {
    margin: 0.6rem 0;
}

.formula {
    background: var(--glass);
    padding: 0.65rem;
    border-radius: var(--radius-sm);
    text-align: center;
    font-family: 'Courier New', monospace;
    font-size: 0.92rem;
    color: var(--primary-light);
    letter-spacing: 0.5px;
}

/* ===========================
   頁尾
   =========================== */
.footer {
    text-align: center;
    padding: 1.5rem;
    margin-top: 2rem;
    color: var(--text-muted);
    font-size: 0.875rem;
    border-top: 1px solid var(--glass-border);
}

.back-home {
    display: inline-block;
    margin-top: 1rem;
    padding: 0.6rem 1.5rem;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    text-decoration: none;
    border-radius: 50px;
    font-weight: 500;
    font-size: 0.9rem;
    transition: var(--transition);
    box-shadow: 0 4px 15px rgba(99, 102, 241, 0.3);
}

.back-home:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 25px rgba(99, 102, 241, 0.5);
}

/* ===========================
   響應式設計
   =========================== */
@media (max-width: 1200px) {
    .info-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 1024px) {
    .main-content {
        grid-template-columns: 1fr;
    }

    .control-section {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
    }

    .result-panel,
    .math-card {
        grid-column: span 2;
    }
}

@media (max-width: 768px) {
    .container {
        padding: 1rem;
    }

    .header h1 {
        font-size: 1.75rem;
    }

    .control-section {
        display: flex;
        flex-direction: column;
    }

    .result-panel,
    .math-card {
        grid-column: span 1;
    }

    .info-grid {
        grid-template-columns: 1fr;
    }

    .canvas-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .toolbar-left,
    .toolbar-right {
        justify-content: center;
        flex-wrap: wrap;
    }
}

@media (max-width: 480px) {
    .header h1 {
        font-size: 1.5rem;
    }

    .canvas-container {
        padding: 0.75rem;
    }

    #treeCanvas {
        min-height: 280px;
    }

    .button-group {
        flex-direction: column;
    }

    .step-controls {
        flex-wrap: wrap;
    }
}