/* ============ TOKENS — LIGHT ============ */
:root,
[data-theme="light"] {
    --bg-app: #f5f5f5;
    --bg-panel: #ffffff;
    --bg-elevated: #fafafa;
    --bg-code: #282c34;
    --bg-terminal: #1e1e1e;

    --text-primary: #1a1a1a;
    --text-secondary: #555555;
    --text-muted: #888888;
    --text-on-code: #abb2bf;
    --text-on-terminal: #4af626;

    --border: #dddddd;
    --border-soft: #eeeeee;

    --accent: #006837;
    --accent-hover: #00552e;
    --accent-light: #e6f1ea;

    --info: #1976d2;
    --warn: #f57c00;
    --danger: #d32f2f;
    --danger-bg: #ffcdd2;
    --success: #2e7d32;
    --success-bg: #c8e6c9;
    --purple: #6a1b9a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 2px 6px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.18);

    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 10px;

    --font-body: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    --font-code: 'Consolas', 'Monaco', 'Courier New', monospace;
}

/* ============ TOKENS — DARK ============ */
[data-theme="dark"] {
    --bg-app: #0e1116;
    --bg-panel: #181c22;
    --bg-elevated: #232932;
    --bg-code: #1e2227;
    --bg-terminal: #0a0a0a;

    --text-primary: #e6e6e6;
    --text-secondary: #b8b8b8;
    --text-muted: #7a7a7a;
    --text-on-code: #c5c8cc;

    --border: #2a3038;
    --border-soft: #1f242b;

    --accent: #2dbe6b;
    --accent-hover: #4ddc8a;
    --accent-light: rgba(45, 190, 107, 0.18);

    --info: #4faaf9;
    --warn: #ffa84a;
    --danger: #ff7373;
    --danger-bg: rgba(255, 115, 115, 0.18);
    --success: #4caf50;
    --success-bg: rgba(76, 175, 80, 0.22);
    --purple: #c894ff;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 2px 6px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.65);
}

/* ============ RESET ============ */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
    font-family: var(--font-body);
    background: var(--bg-app);
    color: var(--text-primary);
    display: grid;
    grid-template-rows: auto 1fr;
    min-height: 100vh;
}

/* ============ HEADER ============ */
.app-header {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 16px;
    padding: 10px 16px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}
.app-header__brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.app-header__actions { display: flex; gap: 8px; align-items: center; }
.brand-mark {
    display: inline-grid; place-items: center;
    width: 34px; height: 34px;
    background: var(--accent); color: #ffffff;
    font-weight: 800; border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-title { font-size: 1.05rem; font-weight: 700; color: var(--accent); }
.brand-sub { font-size: 0.72rem; color: var(--text-muted); }

/* ============ TOOLBAR ============ */
.toolbar {
    display: flex; flex-wrap: wrap;
    gap: 6px;
    align-items: center; justify-content: center;
}
.toolbar__sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
.toolbar__label { font-size: 0.85em; color: var(--text-secondary); margin-left: 4px; }
.toolbar__hint { font-size: 0.85em; color: var(--text-muted); min-width: 28px; text-align: right; }
#speed-control { width: 100px; cursor: pointer; accent-color: var(--accent); }
#speed-control:disabled { cursor: not-allowed; opacity: 0.5; }

/* ============ BUTTONS ============ */
.btn {
    padding: 7px 14px;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text-primary);
    cursor: pointer;
    transition: background 0.15s, transform 0.05s, border-color 0.15s;
}
.btn:hover:not(:disabled) { background: var(--border-soft); }
[data-theme="dark"] .btn:hover:not(:disabled) { background: #2c333d; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn--primary { background: var(--accent); color: #ffffff; }
.btn--primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn--icon { padding: 6px 9px; font-size: 1rem; }
.btn--xs { padding: 3px 9px; font-size: 0.78rem; }
.btn--sm { padding: 5px 11px; font-size: 0.85rem; }
.btn--ghost { background: transparent; border-color: var(--border); }
.btn--ghost:hover:not(:disabled) { background: var(--bg-elevated); }
#btn-play.playing { background: var(--warn); color: #ffffff; }

/* ============ LAYOUT ============ */
.layout {
    display: grid;
    grid-template-columns: 200px minmax(0, 1.1fr) minmax(0, 1.6fr);
    gap: 12px;
    padding: 12px;
    min-height: 0;
    overflow: hidden;
}
.panel {
    display: flex; flex-direction: column;
    background: var(--bg-panel);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    min-height: 0;
    overflow: hidden;
}
.panel__head {
    display: flex; align-items: center; justify-content: space-between;
    gap: 8px;
    padding: 9px 12px;
    border-bottom: 1px solid var(--border-soft);
    background: var(--bg-elevated);
}
.panel__title { font-size: 0.95rem; font-weight: 600; color: var(--text-primary); }
.panel__actions { display: flex; gap: 6px; }
.panel__body { flex: 1; min-height: 0; overflow: auto; padding: 10px; }
.panel-stack {
    display: grid;
    grid-auto-rows: minmax(140px, 1fr);
    gap: 12px;
    min-height: 0;
}
.panel-stack > .panel { min-height: 140px; }

/* ============ FILES PANEL ============ */
.file-list { list-style: none; display: flex; flex-direction: column; gap: 2px; }
.file-list__empty { color: var(--text-muted); font-size: 0.85em; font-style: italic; padding: 8px 4px; }
.file-item {
    display: flex; align-items: center; gap: 6px;
    padding: 6px 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.88rem;
    color: var(--text-primary);
}
.file-item:hover { background: var(--bg-elevated); }
.file-item--active { background: var(--accent-light); color: var(--accent); font-weight: 600; }
.file-item__remove { margin-left: auto; opacity: 0; cursor: pointer; color: var(--danger); }
.file-item:hover .file-item__remove { opacity: 1; }

/* ============ EDITOR ============ */
.code-container {
    flex: 1; min-height: 0; position: relative;
    background: var(--bg-code);
    font-family: var(--font-code);
    font-size: 14px;
    overflow: auto;
}
#code-editor, #code-execution-view {
    width: 100%; height: 100%;
    padding: 12px;
    border: none; outline: none;
    background: transparent;
    resize: none;
    color: var(--text-on-code);
    font-family: inherit;
    font-size: inherit;
    tab-size: 4;
    line-height: 1.5;
}

/* ============ AST ============ */
.ast-container {
    font-family: var(--font-code);
    font-size: 13px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-sm);
    padding: 10px;
}
.ast-node { margin-left: 20px; margin-top: 5px; }
.ast-node-header {
    display: flex; align-items: center;
    padding: 4px 8px;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.15s;
    color: var(--text-primary);
}
.ast-node-header:hover { background: var(--accent-light); }
.ast-node-header.executing { background: var(--success-bg); border-left: 3px solid var(--success); padding-left: 5px; }
.ast-toggle { display: inline-block; width: 16px; height: 16px; text-align: center; line-height: 16px; margin-right: 5px; user-select: none; font-weight: bold; color: var(--text-secondary); }
.ast-type { font-weight: bold; color: var(--info); }
.ast-details { color: var(--text-secondary); margin-left: 5px; font-size: 0.9em; }
.ast-property { color: var(--success); }
.ast-value { color: var(--danger); }
.ast-children { margin-left: 20px; border-left: 1px dashed var(--border); padding-left: 5px; }
.ast-collapsed > .ast-children { display: none; }
.ast-root {
    font-weight: bold; font-size: 14px; color: var(--purple);
    margin-bottom: 10px; padding: 8px;
    background: rgba(106, 27, 154, 0.10);
    border-radius: var(--radius-sm);
    border-left: 4px solid var(--purple);
}

/* ============ SYNTAX HIGHLIGHTING (One Dark palette) ============ */
.syntax-keyword     { color: #c678dd; font-weight: bold; }
.syntax-type        { color: #e5c07b; font-weight: bold; }
.syntax-function    { color: #61afef; }
.syntax-string      { color: #98c379; }
.syntax-number      { color: #d19a66; }
.syntax-comment     { color: #5c6370; font-style: italic; }
.syntax-operator    { color: #56b6c2; }
.syntax-preprocessor{ color: #e06c75; }

/* ============ LINE HIGHLIGHTING IN EXECUTION VIEW ============ */
.code-line {
    padding-left: 10px;
    margin-left: 5px;
    min-height: 20px;
    white-space: pre;
    border-left: 4px solid transparent;
}
.line-just-executed { background: rgba(244, 67, 54, 0.18); border-left: 4px solid var(--danger); }
.line-next-execute  { background: rgba(76, 175, 80, 0.22); border-left: 4px solid var(--success); }

/* ============ MEMORY ============ */
.memory-container { padding: 0; }
.memory-item {
    background: var(--bg-elevated);
    border: 1px solid var(--border-soft);
    border-left: 4px solid var(--info);
    padding: 10px;
    margin-bottom: 10px;
    border-radius: var(--radius-sm);
    font-family: var(--font-code);
    box-shadow: var(--shadow-sm);
    color: var(--text-primary);
}
.region-badge { display: inline-block; padding: 2px 6px; border-radius: 3px; font-size: 0.8em; font-weight: bold; margin-left: 10px; color: white; }
.bg-stack { background: var(--success); }
.bg-heap { background: var(--warn); }
.bg-rodata { background: var(--purple); }
.array-container {
    display: flex; flex-wrap: nowrap; overflow-x: auto;
    margin-top: 5px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}
.array-cell {
    flex: 0 0 auto; min-width: 50px;
    padding: 8px;
    text-align: center;
    border-right: 1px solid var(--border);
    background: var(--bg-elevated);
}
.array-cell:last-child { border-right: none; }
.array-index {
    font-size: 0.8em; color: var(--text-secondary);
    margin-bottom: 4px; display: block;
    background: var(--border-soft);
    padding: 2px;
    border-radius: 2px;
}
.uninitialized-memory {
    color: var(--danger); font-size: 0.9em; font-style: italic;
    background: var(--danger-bg);
    padding: 2px 4px; border-radius: 2px;
}
.null-terminator {
    color: var(--purple); font-weight: bold;
    background: rgba(106, 27, 154, 0.15);
    padding: 2px 4px; border-radius: 2px;
}
.struct-fields-container { margin-top: 6px; padding-left: 10px; border-left: 2px dashed var(--border); }

/* ============ WATCH ============ */
.watch-container { padding: 4px; }
.watch-item {
    background: var(--bg-elevated);
    border-left: 4px solid var(--info);
    padding: 8px;
    margin-bottom: 8px;
    border-radius: var(--radius-sm);
    font-family: var(--font-code);
    display: flex; justify-content: space-between; align-items: center;
    color: var(--text-primary);
}
.watch-item-name { font-weight: bold; color: var(--info); }
.watch-item-value { color: var(--success); }
.watch-item-remove { cursor: pointer; color: var(--danger); font-weight: bold; padding: 0 8px; }
.watch-item-remove:hover { background: var(--danger-bg); border-radius: 3px; }

/* ============ TERMINAL ============ */
.terminal-output {
    background: var(--bg-terminal);
    color: var(--text-on-terminal);
    padding: 10px;
    font-family: var(--font-code);
    font-size: 13px;
    border-radius: var(--radius-sm);
    min-height: 80px;
}

/* ============ EMPTY ============ */
.empty-msg { color: var(--text-muted); font-style: italic; }

/* ============ MODAL ============ */
.modal {
    border: none;
    padding: 0;
    background: transparent;
    max-width: 920px;
    width: 90vw;
    color: var(--text-primary);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.5); backdrop-filter: blur(2px); }
.modal[open] { display: flex; flex-direction: column; box-shadow: var(--shadow-lg); border-radius: var(--radius-lg); overflow: hidden; }
.modal__head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 18px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
}
.modal__title { font-size: 1.1rem; color: var(--text-primary); }
.modal__body {
    background: var(--bg-panel);
    padding: 16px;
    max-height: 70vh;
    overflow: auto;
}
.example-group { margin-bottom: 18px; }
.example-group:last-child { margin-bottom: 0; }
.example-group__title {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
    margin-bottom: 8px;
}
.example-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 8px;
}
.example-card {
    display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
    padding: 10px 12px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.05s;
    font-family: inherit;
    text-align: left;
}
.example-card:hover { background: var(--accent-light); border-color: var(--accent); }
.example-card:active { transform: translateY(1px); }
.example-card strong { font-size: 0.92rem; }
.example-card span { font-size: 0.75rem; color: var(--text-muted); }
.modal--manual { max-width: 1100px; width: 94vw; }
.modal__body--manual { padding: 0; height: 80vh; max-height: 80vh; }
#manual-frame {
    width: 100%; height: 100%;
    border: none; display: block;
    background: #ffffff;
}


/* ============ RESPONSIVE ============ */
@media (max-width: 1100px) {
    .layout { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1.6fr); }
    .panel--files { display: none; }
}
@media (max-width: 768px) {
    body { display: flex; flex-direction: column; }
    .app-header { display: grid; grid-template-columns: 1fr auto; row-gap: 8px; }
    .toolbar { grid-column: 1 / -1; justify-content: flex-start; }
    .layout {
        grid-template-columns: 1fr;
        grid-auto-rows: minmax(280px, auto);
        overflow: auto;
    }
    .panel-stack { display: flex; flex-direction: column; gap: 12px; }
}

/* ============ MISC ============ */
[hidden] { display: none !important; }
