/* ===== CSS Variables & Reset ===== */
:root {
    --bg-primary: #0f0f14;
    --bg-secondary: #1a1a24;
    --bg-tertiary: #252532;
    --bg-hover: #2d2d3d;
    --text-primary: #f0f0f5;
    --text-secondary: #a0a0b0;
    --text-muted: #606070;
    --accent-primary: #00d4ff;
    --accent-secondary: #7b61ff;
    --accent-success: #00ff88;
    --accent-warning: #ffaa00;
    --accent-danger: #ff4466;
    --rapid-color: #ffaa00;
    --linear-color: #00ff88;
    --tool-color: #ff4466;
    --grid-color: rgba(100, 100, 120, 0.15);
    --border-color: rgba(255, 255, 255, 0.08);
    --shadow-color: rgba(0, 0, 0, 0.4);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --transition: all 0.2s ease;
}

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

body {
    font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow: hidden;
}

/* ===== App Container ===== */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ===== Header ===== */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--accent-primary);
}

.logo h1 {
    font-size: 1.25rem;
    font-weight: 600;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.status-indicator {
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
    background: rgba(0, 255, 136, 0.15);
    color: var(--accent-success);
    border: 1px solid rgba(0, 255, 136, 0.3);
}

.status-indicator.running {
    background: rgba(0, 212, 255, 0.15);
    color: var(--accent-primary);
    border-color: rgba(0, 212, 255, 0.3);
    animation: pulse 1.5s infinite;
}

.status-indicator.paused {
    background: rgba(255, 170, 0, 0.15);
    color: var(--accent-warning);
    border-color: rgba(255, 170, 0, 0.3);
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.6;
    }
}

/* ===== Main Content ===== */
.main-content {
    display: grid;
    grid-template-columns: 280px 1fr 280px;
    flex: 1;
    overflow: hidden;
}

/* ===== Panels ===== */
.panel {
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.info-panel {
    border-right: none;
    border-left: 1px solid var(--border-color);
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.panel-header h2 {
    font-size: 0.875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.panel-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

/* ===== Compact Buttons for Panel ===== */
.panel-actions .btn {
    padding: 6px 8px;
    font-size: 0.7rem;
}

.panel-actions .btn svg {
    width: 12px;
    height: 12px;
}

/* ===== G-code Panel ===== */
.gcode-panel textarea {
    flex: 1;
    padding: 16px;
    background: var(--bg-tertiary);
    border: none;
    color: var(--text-primary);
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 0.8rem;
    line-height: 1.6;
    resize: none;
    outline: none;
}

.gcode-panel textarea::placeholder {
    color: var(--text-muted);
}

/* ===== Interactive G-code Editor ===== */
.gcode-editor {
    flex: 1;
    background: var(--bg-tertiary);
    overflow-y: auto;
    font-family: 'JetBrains Mono', 'Fira Code', monospace;
    font-size: 0.8rem;
    line-height: 1.6;
}

.editor-lines {
    display: flex;
    flex-direction: column;
}

.editor-line {
    display: flex;
    align-items: stretch;
    cursor: pointer;
    transition: background 0.15s ease;
    border-left: 3px solid transparent;
}

.editor-line:hover {
    background: var(--bg-hover);
    border-left-color: var(--accent-primary);
}

.editor-line.selected {
    background: rgba(0, 212, 255, 0.1);
    border-left-color: var(--accent-primary);
}

.line-number {
    min-width: 40px;
    padding: 2px 8px;
    text-align: right;
    color: var(--text-muted);
    background: var(--bg-secondary);
    user-select: none;
    font-size: 0.7rem;
}

.line-content {
    flex: 1;
    padding: 2px 12px;
    color: var(--text-primary);
    white-space: pre;
}

.line-content .gcode-cmd {
    color: var(--accent-primary);
    font-weight: 600;
}

.line-content .gcode-param {
    color: var(--accent-success);
}

.line-content .gcode-comment {
    color: var(--text-muted);
    font-style: italic;
}

.line-edit-btn {
    padding: 2px 8px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s ease;
}

.editor-line:hover .line-edit-btn {
    opacity: 1;
}

.line-edit-btn:hover {
    color: var(--accent-primary);
}

/* ===== Line Edit Modal ===== */
.line-edit-modal {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    max-width: 400px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    z-index: 1001;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    color: white;
    font-weight: 600;
}

.modal-close {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

.modal-body {
    padding: 16px;
}

.original-line {
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.8rem;
    color: var(--accent-primary);
    margin-bottom: 12px;
    overflow-x: auto;
}

.modal-body input {
    width: 100%;
    padding: 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.85rem;
    outline: none;
    margin-bottom: 12px;
}

.modal-body input:focus {
    border-color: var(--accent-primary);
}

.modal-actions {
    display: flex;
    gap: 8px;
}

.modal-actions .btn {
    flex: 1;
}

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: white;
    box-shadow: 0 4px 15px rgba(0, 212, 255, 0.3);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 212, 255, 0.4);
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--bg-hover);
    border-color: var(--accent-primary);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none !important;
}

.btn-success {
    background: linear-gradient(135deg, #00ff88, #00cc6a);
    color: #0f0f14;
    font-weight: 600;
    box-shadow: 0 4px 15px rgba(0, 255, 136, 0.3);
}

.btn-success:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 255, 136, 0.4);
}

/* ===== Filename Bar ===== */
.filename-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
}

.filename-bar label {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.filename-bar input {
    flex: 1;
    padding: 6px 10px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.8rem;
    font-family: 'JetBrains Mono', monospace;
    outline: none;
    transition: var(--transition);
}

.filename-bar input:focus {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.2);
}

/* ===== Canvas Section ===== */
.canvas-section {
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
}

.canvas-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

.control-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

.control-group label {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.control-group input[type="range"] {
    width: 120px;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-tertiary);
    border-radius: 3px;
    outline: none;
}

.control-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: var(--accent-primary);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 212, 255, 0.4);
}

#speedValue {
    font-size: 0.75rem;
    color: var(--accent-primary);
    min-width: 35px;
}

.canvas-wrapper {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    overflow: hidden;
}

#simulatorCanvas {
    background: var(--bg-tertiary);
    border-radius: var(--radius-md);
    box-shadow:
        0 0 0 1px var(--border-color),
        0 10px 40px var(--shadow-color),
        inset 0 0 100px rgba(0, 212, 255, 0.02);
}

.canvas-overlay {
    position: absolute;
    bottom: 30px;
    left: 30px;
    padding: 8px 14px;
    background: rgba(15, 15, 20, 0.85);
    backdrop-filter: blur(10px);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
}

.grid-info span {
    font-size: 0.75rem;
    font-family: 'JetBrains Mono', monospace;
    color: var(--text-secondary);
}

/* ===== Progress Bar ===== */
.progress-container {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
}

.progress-bar {
    flex: 1;
    height: 6px;
    background: var(--bg-tertiary);
    border-radius: 3px;
    overflow: hidden;
    position: relative;
}

.progress-bar::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: var(--progress, 0%);
    background: linear-gradient(90deg, var(--accent-primary), var(--accent-success));
    border-radius: 3px;
    transition: width 0.1s ease;
}

.progress-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
    min-width: 40px;
    text-align: right;
}

/* ===== Info Panel ===== */
.info-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.info-section {
    margin-bottom: 24px;
}

.info-section h3 {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: 12px;
}

.info-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.info-item {
    background: var(--bg-tertiary);
    padding: 12px;
    border-radius: var(--radius-sm);
    text-align: center;
}

.info-item label {
    display: block;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.info-item span {
    font-size: 1.25rem;
    font-weight: 600;
    font-family: 'JetBrains Mono', monospace;
    color: var(--accent-primary);
}

.info-item small {
    font-size: 0.65rem;
    color: var(--text-muted);
    margin-left: 2px;
}

/* ===== Stats List ===== */
.stats-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.stat-value {
    font-size: 0.85rem;
    font-weight: 600;
    font-family: 'JetBrains Mono', monospace;
    color: var(--text-primary);
}

/* ===== Bounding Box Info ===== */
.bbox-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.bbox-item {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.bbox-item span:last-child {
    font-family: 'JetBrains Mono', monospace;
    color: var(--text-primary);
}

/* ===== Legend ===== */
.legend {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

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

.legend-color {
    width: 24px;
    height: 4px;
    border-radius: 2px;
}

.legend-color.rapid {
    background: var(--rapid-color);
    opacity: 0.7;
}

.legend-color.linear {
    background: var(--linear-color);
}

.legend-color.tool {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--tool-color);
    box-shadow: 0 0 10px var(--tool-color);
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ===== Responsive ===== */
@media (max-width: 1200px) {
    .main-content {
        grid-template-columns: 240px 1fr 240px;
    }
}

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

    .gcode-panel {
        max-height: 200px;
    }

    .info-panel {
        max-height: 250px;
        border-left: none;
        border-top: 1px solid var(--border-color);
    }
}

/* ===== Chat Panel ===== */
.chat-panel {
    position: fixed;
    bottom: 0;
    right: 20px;
    width: 380px;
    max-height: 500px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-bottom: none;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -10px 40px var(--shadow-color);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
}

.chat-panel.collapsed {
    transform: translateY(calc(100% - 48px));
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    cursor: pointer;
}

.chat-title {
    display: flex;
    align-items: center;
    gap: 8px;
    color: white;
    font-weight: 600;
    font-size: 0.85rem;
}

.chat-toggle {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    padding: 6px;
    border-radius: 50%;
    cursor: pointer;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.chat-toggle:hover {
    background: rgba(255, 255, 255, 0.3);
}

.chat-panel.collapsed .chat-toggle svg {
    transform: rotate(180deg);
}

.chat-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: 350px;
}

.chat-message {
    display: flex;
    flex-direction: column;
}

.chat-message.user {
    align-items: flex-end;
}

.chat-message.assistant {
    align-items: flex-start;
}

.message-content {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: var(--radius-md);
    font-size: 0.8rem;
    line-height: 1.5;
}

.chat-message.user .message-content {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-message.assistant .message-content {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message-content ul {
    margin: 8px 0 0 16px;
    padding: 0;
}

.message-content li {
    margin-bottom: 4px;
}

.message-content code,
.message-content pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.75rem;
}

.message-content pre {
    padding: 10px;
    margin: 8px 0;
    overflow-x: auto;
    white-space: pre-wrap;
}

.chat-input-area {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
}

.chat-input-area input {
    flex: 1;
    padding: 10px 14px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.8rem;
    outline: none;
    transition: var(--transition);
}

.chat-input-area input:focus {
    border-color: var(--accent-primary);
}

.chat-input-area .btn {
    padding: 10px 14px;
}

.chat-message.loading .message-content::after {
    content: '...';
    animation: dots 1.5s infinite;
}

@keyframes dots {

    0%,
    20% {
        content: '.';
    }

    40% {
        content: '..';
    }

    60%,
    100% {
        content: '...';
    }
}