/* style.css - Premium Modern GUI */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
    --bg-base: #09090b;
    --bg-panel: #18181b;
    --bg-elevated: #27272a;
    --border: #3f3f46;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --success: #10b981;
    --success-hover: #059669;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --warning: #f59e0b;
    --text-main: #f4f4f5;
    --text-muted: #a1a1aa;
    --radius-lg: 12px;
    --radius-md: 8px;
    --radius-sm: 6px;
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.5), 0 2px 4px -1px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.6), 0 4px 6px -2px rgba(0, 0, 0, 0.4);
    --neon-glow: 0 0 10px rgba(59, 130, 246, 0.5);

    /* === Block Colors (Path Stack 積木) === */
    --block-bg: #252526;                /* 功能積木背景 */
    --block-setup: #aaa;                /* SETUP 設定 */
    --block-tool: #007acc;              /* TOOL 換刀 / MCODE */
    --block-cycle-start: #ff9800;       /* G71/G72_START 粗車循環 */
    --block-cycle-g73: #FFC107;         /* G73_START 仿形循環 */
    --block-cycle-g75: #00ffff;         /* G75_START 端面溝槽循環 */
    --block-cycle-end: #FFC107;         /* CYCLE_END 循環結束 */
    --block-finish: #10b981;            /* G70 精車 */
    --block-drill: #a8e6cf;             /* 鑽孔 / 中心鑽 / PCD */
    --block-mill: #00ffcc;              /* 多邊形 / 自訂輪廓 / 型腔 */
    --block-radial-slot: #007bff;       /* 外徑直槽 / 鍵槽 / 切平 */
    --block-radial-profile: #17a2b8;    /* 外徑輪廓 / 外徑陣列孔 */
    --block-engrave: #FFD700;           /* 文字雕刻 */
    --block-parting: #e67e22;           /* 切斷 */
    --block-tap: #ffd3b6;              /* 攻牙 */
    --block-face-tap: #ff9800;          /* 端面攻牙 */
    --block-thread: #ff00ff;            /* G76/G34 螺紋 */
    --block-multi-thread: #ff9800;      /* 多頭螺紋 */
    --block-pulley: #00bcd4;            /* 皮帶輪槽 */
    --block-insert-indicator: #00ffcc;  /* 插入點指示線 */
    --block-insert-bg: #1e2d3d;         /* 插入點背景 */
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Inter', -apple-system, sans-serif;
    background: var(--bg-base);
    color: var(--text-main);
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* --- Scrollbar Customization --- */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--bg-elevated);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #52525b;
}

/* --- Header --- */
header {
    height: 60px;
    background: rgba(24, 24, 27, 0.85);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 20px;
    justify-content: space-between;
    font-weight: 600;
    flex-shrink: 0;
    z-index: 100;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

header div:first-child {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    background: linear-gradient(90deg, #60a5fa, #c084fc);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

@keyframes headerGradientPan {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

header.header-executing {
    background-image: linear-gradient(90deg, transparent 0%, rgba(74, 222, 128, 0.6) 50%, transparent 100%) !important;
    background-size: 200% 100% !important;
    animation: headerGradientPan 2s infinite linear !important;
}

select {
    font-family: 'Inter', sans-serif;
    outline: none;
    transition: all 0.2s;
}

select:focus {
    border-color: var(--accent) !important;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.3);
}

/* --- Layout --- */
.main-container {
    display: flex;
    flex: 1;
    overflow: hidden;
    width: 100%;
}

/* --- Left Column: Editor --- */
.col-left {
    width: 360px;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    background: var(--bg-panel);
    z-index: 10;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.3);
}

.editor-header,
.panel-header {
    background: rgba(39, 39, 42, 0.6);
    padding: 6px 16px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.editor-wrapper {
    position: relative;
    flex: 1;
    overflow: hidden;
    background: #000;
}

textarea {
    width: 100%;
    height: 100%;
    background: transparent;
    color: #38bdf8;
    border: none;
    padding: 16px;
    font-family: 'JetBrains Mono', 'Consolas', monospace;
    font-size: 14px;
    line-height: 22px;
    outline: none;
    resize: none;
    z-index: 2;
    position: relative;
    white-space: pre;
    overflow-x: auto;
}

.highlight-bar {
    position: absolute;
    left: 0;
    width: 100%;
    height: 22px;
    /* Matches line-height */
    background: linear-gradient(90deg, rgba(59, 130, 246, 0.2), transparent);
    border-left: 3px solid var(--accent);
    z-index: 1;
    pointer-events: none;
    display: none;
    transition: top 0.1s ease-out;
}

/* Alarm List */
.error-panel {
    height: 120px;
    border-top: 1px solid var(--border);
    background: var(--bg-panel);
    display: flex;
    flex-direction: column;
}

.error-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.error-item {
    padding: 12px;
    margin-bottom: 8px;
    background: rgba(239, 68, 68, 0.1);
    border-radius: var(--radius-sm);
    border-left: 4px solid var(--danger);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
    color: #fca5a5;
}

.error-item:hover {
    background: rgba(239, 68, 68, 0.2);
    transform: translateX(2px);
}

/* --- Center Column: Canvas & Controls --- */
.col-center {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    background: radial-gradient(circle at center, #1f1f22 0%, #000 100%);
    min-width: 0;
}

canvas,
#view3D {
    flex: 1;
    width: 100%;
    height: 100%;
    display: block;
    cursor: crosshair;
    touch-action: none;
}

#view3D {
    background: transparent;
}

/* Floating Dashboard */
.dashboard {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(9, 9, 11, 0.7);
    padding: 12px;
    border-radius: var(--radius-md);
    border: 1px solid rgba(255, 255, 255, 0.1);
    pointer-events: none;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: var(--shadow-lg);
    min-width: 180px;
}

.dash-row {
    color: #38bdf8;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    margin-bottom: 6px;
    display: flex;
    justify-content: space-between;
}

.dash-row span:first-child {
    color: var(--text-muted);
}

.dash-row span[id="valRpm"] {
    font-weight: bold;
    color: #4ade80;
    text-shadow: 0 0 8px rgba(74, 222, 128, 0.4);
}

#measurePanel {
    background: rgba(0, 0, 0, 0.3);
    border-radius: var(--radius-sm);
    padding: 10px;
}

/* Controls Area */
.controls-area {
    background: rgba(24, 24, 27, 0.95);
    backdrop-filter: blur(10px);
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    max-height: 280px;
    z-index: 20;
}

.btn-row {
    display: flex;
    gap: 12px;
}

/* Buttons */
button {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    color: white;
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    font-family: 'Inter', sans-serif;
    background: var(--bg-elevated);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

button:hover {
    background: #3f3f46;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

button:active {
    transform: translateY(1px);
    box-shadow: none;
}

.btn-run {
    background: linear-gradient(135deg, var(--success), #047857);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.btn-run:hover {
    background: linear-gradient(135deg, var(--success-hover), #065f46);
}

.btn-stop {
    background: linear-gradient(135deg, var(--danger), #be123c);
    box-shadow: 0 4px 12px rgba(244, 63, 94, 0.3);
}

.btn-stop:hover {
    background: linear-gradient(135deg, var(--danger-hover), #9f1239);
}

/* Parameter Form Sets */
.param-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #0f0f12;
    padding: 12px;
    border-radius: var(--radius-md);
    border: 1px solid #27272a;
}

.param-row {
    display: flex;
    gap: 15px;
    align-items: center;
    justify-content: space-between;
    font-size: 13px;
    color: var(--text-muted);
    width: 100%;
}

.param-row span {
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

input[type="number"],
input[type="text"],
select {
    background: #1f1f22;
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
    transition: border-color 0.2s, box-shadow 0.2s;
    outline: none;
}

input[type="number"]:focus,
input[type="text"]:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* 隱藏所有的數值輸入框上下調整箭頭 (Spinners) */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    appearance: none;
    margin: 0;
}
input[type="number"] {
    -moz-appearance: textfield;
    appearance: textfield;
}

input[type="range"] {
    flex: 1;
    height: 6px;
    background: var(--border);
    border-radius: 3px;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
}

/* --- Right Column: Info --- */
.col-right {
    width: 280px;
    min-width: 280px;
    border-left: 1px solid var(--border);
    background: var(--bg-panel);
    display: flex;
    flex-direction: column;
    z-index: 10;
    box-shadow: -2px 0 10px rgba(0, 0, 0, 0.3);
    transition: margin-right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.col-right.collapsed-right {
    margin-right: -280px;
}



.feature-list {
    list-style: none;
    padding: 8px 15px;
    margin: 0;
}

.feature-list li {
    padding: 6px 0;
    font-size: 13px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
}

.feature-list li b {
    color: var(--text-main);
}

.program-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 10px;
}

.program-item {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    margin-bottom: 8px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
}

.program-item:hover {
    background: #3f3f46;
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.prog-id {
    font-weight: 700;
    color: #60a5fa;
    font-size: 15px;
    margin-bottom: 0;
    margin-right: 10px;
    font-family: 'JetBrains Mono', monospace;
}

.prog-desc {
    font-size: 12px;
    color: var(--text-muted);
}

/* Command List */
.cmd-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    color: var(--text-muted);
}

.cmd-item:hover {
    background: rgba(255, 255, 255, 0.03);
}

.cmd-item b {
    color: #fbbf24;
    font-size: 14px;
    font-family: 'JetBrains Mono', monospace;
    background: rgba(251, 191, 36, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
}

/* --- Modals & Overlays --- */
.designer-overlay,
.tool-manager-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.2s ease-out;
}

.tool-manager-overlay {
    display: none;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.designer-container {
    width: 96%;
    height: 94%;
    background: var(--bg-base);
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

.designer-header {
    height: 60px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 24px;
    font-weight: 600;
    font-size: 16px;
}

.header-btns button {
    padding: 8px 16px;
    font-size: 13px;
    margin-left: 12px;
    border-radius: var(--radius-sm);
}

.designer-body {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* Three Column Modal Layout */
.d-col-preview {
    flex: 2;
    position: relative;
    background: #000;
    border-right: 1px solid var(--border);
}

.d-col-stack {
    flex: 1;
    background: var(--bg-base);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.d-col-tools {
    flex: 1.2;
    background: var(--bg-panel);
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow-y: auto;
}

.panel-label {
    background: var(--bg-elevated);
    color: var(--text-main);
    padding: 8px 16px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    border-bottom: 1px solid var(--border);
}

/* Stack List */
.stack-list {
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.stack-item {
    background: #1f1f22;
    border-radius: var(--radius-md);
    padding: 12px 15px;
    display: flex;
    align-items: center;
    gap: 15px;
    border-left: 4px solid var(--border);
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.stack-item:hover {
    background: #27272a;
    transform: translateX(2px);
}

.stack-item.start-node {
    border-left-color: var(--success);
}

.si-icon {
    width: 36px;
    height: 36px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.si-info {
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.5;
}

.si-info b {
    color: var(--text-main);
    font-size: 14px;
}

/* Tool Buttons Grid */
.tool-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    padding: 15px 0;
}

.t-btn {
    position: relative;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 20px 10px;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.t-btn span:first-child {
    font-size: 28px !important;
}

.t-btn span:last-child {
    font-weight: 500;
    font-size: 14px;
    line-height: 1.3;
    text-align: center;
}

.t-btn:hover {
    background: var(--bg-panel);
    border-color: var(--accent);
    transform: translateY(-4px);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3), var(--neon-glow);
}

.t-btn:active {
    transform: translateY(0);
}

/* 🚧 未實裝 (WIP) 懸浮標籤 */
.badge-wip {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #f59e0b;
    /* 亮橘色警告感 */
    color: #000;
    font-size: 10px;
    font-weight: 800;
    padding: 2px 6px;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
    z-index: 2;
    pointer-events: none;
    /* 讓點擊穿透 */
    letter-spacing: 0.5px;
}

/* Direction Matrix Grid */
.dir-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}

.d-btn {
    padding: 20px 0;
    background: #27272a;
    border: 1px solid var(--border);
    color: var(--text-main);
    cursor: pointer;
    font-size: 16px;
    border-radius: var(--radius-sm);
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.d-btn span {
    font-size: 12px;
    color: var(--text-muted);
}

.d-btn:hover {
    background: rgba(59, 130, 246, 0.15);
    border-color: var(--accent);
}

.d-btn:hover span {
    color: #60a5fa;
}

/* Input Rows inside Modals */
.input-group {
    background: #131315;
    padding: 16px;
    border-radius: var(--radius-md);
    border: 1px solid #27272a;
    margin-bottom: 15px;
}

.input-row {
    margin-bottom: 15px;
}

.input-row label {
    display: block;
    color: var(--text-muted);
    margin-bottom: 6px;
    font-size: 13px;
    font-weight: 500;
}

.input-row input,
.input-row select {
    width: 100%;
    padding: 10px;
    background: #1f1f22;
    border: 1px solid var(--border);
    color: white;
    border-radius: var(--radius-sm);
    font-size: 14px;
}

.input-row input:focus,
.input-row select:focus {
    border-color: var(--accent);
}

/* Tool Manager */
.tool-manager-modal {
    background: var(--bg-base);
    width: 900px;
    max-width: 95%;
    max-height: 90vh;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.8);
}

.tm-header {
    background: var(--bg-panel);
    padding: 16px 24px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 16px;
}

.tm-body {
    display: flex;
    flex: 1;
    min-height: 500px;
    overflow: hidden;
}

.tm-list {
    width: 35%;
    border-right: 1px solid var(--border);
    overflow-y: auto;
    background: var(--bg-panel);
    padding: 10px;
}

.tm-item {
    padding: 15px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: pointer;
    transition: all 0.2s;
    border-radius: var(--radius-sm);
    margin-bottom: 5px;
    background: rgba(255, 255, 255, 0.02);
}

.tm-item:hover {
    background: var(--bg-elevated);
    transform: translateX(2px);
}

.tm-item.active {
    background: rgba(59, 130, 246, 0.15);
    border-left: 4px solid var(--accent);
}

.tm-form {
    width: 65%;
    padding: 25px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    background: var(--bg-base);
    overflow-y: auto;
}

.tm-form label {
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 500;
}

.tm-form input,
.tm-form select {
    padding: 10px;
    background: #1f1f22;
    color: white;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.tm-custom-area {
    display: flex;
    gap: 20px;
    margin-top: 15px;
    background: #131315;
    padding: 15px;
    border-radius: var(--radius-md);
    border: 1px dashed #3f3f46;
}

.tm-custom-inputs {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.tm-custom-diagram {
    flex: 1;
    background: #1f1f22;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
}

/* =========================================
💥 系統升級：全端響應式架構 (RWD) v3.0 - 自動適應 (平板 > 電腦 > 手機)
========================================= */

/* 💻 1. 針對小型電腦與大平板 (Laptop / Large Tablet, 1025px ~ 1280px) */
@media (max-width: 1280px) {
    .col-left {
        width: 300px;
    }

    .col-right {
        width: 240px;
    }

    .controls-area {
        padding: 12px 15px;
    }
}

/* 📱 2. 針對橫向平板 (Tablet Landscape, <= 1024px) 
   完美優化橫向平板，分為三欄，完美分配空間 */
@media (max-width: 1024px) and (orientation: landscape) {
    .main-container {
        display: flex !important;
        flex-direction: row !important;
        height: calc(100vh - 60px) !important;
        /* 扣除 header 高度 */
        overflow: hidden !important;
    }

    /* 左側編輯器 */
    .col-left {
        width: 32% !important;
        max-width: 320px !important;
        min-width: 260px !important;
        border-right: 1px solid var(--border);
    }

    /* 中間模擬器 */
    .col-center {
        width: 43% !important;
        flex: 1 !important;
    }

    /* 右側操作面板 */
    .col-right {
        width: 25% !important;
        max-width: 260px !important;
        min-width: 220px !important;
    }

    /* 優化控制列，避免按鈕擠壓 */
    .controls-area {
        padding: 10px !important;
        gap: 8px !important;
        max-height: 250px !important;
        overflow-y: auto;
    }

    .btn-row {
        gap: 8px !important;
    }

    .btn-row button {
        padding: 8px 10px !important;
        font-size: 13px !important;
    }

    /* 參數列自適應換行 */
    .param-row {
        flex-wrap: wrap;
        gap: 6px;
    }
}

/* 📱 3. 針對直向平板 (Tablet Portrait, <= 1024px) */
@media (max-width: 1024px) and (orientation: portrait) {
    body {
        height: auto !important;
        /* 允許整體滾動 */
        overflow-y: auto !important;
    }

    .main-container {
        display: flex !important;
        flex-direction: column !important;
        height: auto !important;
        overflow: visible !important;
    }

    /* 直向佈局順序：模擬器(最上) -> 編輯器(中) -> 說明與命令(下) */
    .col-center {
        width: 100% !important;
        height: 55vh !important;
        min-height: 450px !important;
        order: 1;
    }

    .col-left {
        width: 100% !important;
        height: 500px !important;
        max-width: 100% !important;
        border-right: none;
        border-bottom: 1px solid var(--border);
        order: 2;
        box-shadow: none;
    }

    .col-right {
        width: 100% !important;
        height: auto !important;
        min-height: 400px;
        max-width: 100% !important;
        border-left: none;
        order: 3;
        box-shadow: none;
    }

    /* 右側分為上下兩塊，直向時也給予適當高度 */
    .right-top,
    .right-bottom {
        flex: none;
        height: auto;
    }

    .controls-area {
        max-height: none !important;
        padding: 15px !important;
    }

    .btn-row {
        flex-wrap: wrap;
    }

    .btn-row button {
        flex: 1 1 calc(50% - 12px);
        /* 一排兩個按鈕 */
    }
}

/* 📱 4. 針對手機 (Mobile, <= 768px) */
@media (max-width: 768px) {
    header {
        height: auto !important;
        padding: 10px !important;
        flex-wrap: wrap;
        gap: 10px;
    }

    header > div:nth-child(2) {
        flex-wrap: wrap !important;
        justify-content: flex-start;
        gap: 5px !important;
    }

    /* 隱藏/縮小 header 文字以節省空間 */
    header div:first-child span {
        display: none;
    }

    header div:nth-child(2),
    header div:nth-child(2) span,
    header select {
        font-size: 11px !important;
    }

    /* 手機版標題列排列調整：將切換按鈕縮小並往後放 */
    #btnToggleView {
        padding: 5px 10px !important; /* 縮小按鈕內距 */
        order: 2; /* 放在語言選單後面 */
    }
    #btnToggleCS, #btnCSViewMode {
        order: 3;
    }
    
    .lang-ctrl-group {
        order: 1; /* 手機版語言與控制器選單優先 (最左側/最前面) */
        width: 100%; /* 讓它自己佔據一行可以排版更好看 */
        justify-content: flex-end; /* 如果要放上面，可以推到邊邊 */
    }

    .param-row {
        flex-wrap: wrap;
    }
    
    /* 讓速度拉桿這整組獨立跳下一行佔據全寬 (加入 !important 以覆蓋 HTML 行內樣式) */
    .param-row > div {
        flex: 1 1 100% !important;
        margin-top: 5px !important;
    }

    #statusBadge {
        display: none;
        /* 手機隱藏狀態徽章 */
    }

    /* 手機直立佈局：絕對保留 40vh 給主畫面 */
    .col-center {
        height: auto !important;
        min-height: 0 !important;
    }

    .col-center > div:first-child {
        height: 40vh !important;
        flex: none !important;
        border-bottom: 2px solid var(--accent);
    }

    /* 資訊框改為底部橫式並隱藏非必要參數 */
    .dashboard {
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        min-width: 0 !important;
        width: 100% !important;
        display: flex !important;
        flex-direction: row;
        justify-content: space-around;
        padding: 6px !important;
        background: rgba(9, 9, 11, 0.85) !important;
        border-radius: 0 !important;
        border: none !important;
        box-shadow: 0 -4px 10px rgba(0,0,0,0.5) !important;
        backdrop-filter: blur(8px);
    }

    .dashboard .dash-row {
        margin: 0 !important;
        font-size: 13px !important;
    }

    .dashboard > .dash-row:nth-child(4),
    .dashboard > .dash-row:nth-child(5),
    .dashboard > .dash-row:nth-child(6),
    .dashboard > .dash-row:nth-child(7) {
        display: none !important; /* 隱藏 F、RPM、MODE 標籤 */
    }

    .col-left {
        height: 400px !important;
    }

    .col-right {
        height: auto !important;
    }

    /* 把控制面板改為 flex 讓按鈕可以直接流動 */
    .controls-area {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: wrap !important;
        gap: 6px !important;
        padding: 10px !important;
    }

    .btn-row {
        display: contents !important; /* 忽略 .btn-row 容器，將按鈕平鋪給 controls-area 排版 */
    }

    .btn-row button {
        flex: 1 1 calc(33.333% - 6px) !important;
        /* 手機版：一行三顆，高度壓縮 */
        font-size: 13px !important;
        padding: 6px 4px !important;
        min-height: 38px;
    }
    
    /* 啟動按鍵獨立一行，長度滿版 */
    #btnRun {
        flex: 1 1 100% !important;
    }
    
    /* 防止參數區被擠壓，強迫換行獨立滿版 */
    .param-group {
        flex: 1 1 100% !important;
        margin-top: 5px !important;
    }

    .param-row span {
        font-size: 11px;
    }

    .param-row input[type="number"],
    .param-row input[type="text"] {
        width: 45px !important;
        padding: 4px !important;
    }
}