:root {
    --bg: #f3f6f5;
    --panel: #ffffff;
    --sidebar: #f8faf9;
    --border: #dce4e0;
    --ink: #17231e;
    --muted: #5f7169;
    --emerald: #166549;
    --emerald-hover: #11513a;
    --emerald-soft: #e6f4ec;
    --blue: #1d5d9b;
    --blue-soft: #e8f2fc;
    --danger: #b43422;
    --danger-soft: #fff1ee;
    --warn: #9a5b00;
    --warn-soft: #fff7e6;
    --code-bg: #171d20;
    --code-gutter: #12171a;
    --code-ink: #e8eff2;
    --code-muted: #70828b;
    font: 14px/1.55 "Hiragino Sans", "Yu Gothic UI", "Yu Gothic", Meiryo, system-ui, sans-serif;
    color: var(--ink);
    background: var(--bg);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
.shell {
    height: 100vh;
    height: 100svh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Header */
.topbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    z-index: 20;
}
.brand-group { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-badge {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: linear-gradient(135deg, #166549, #1f7f5d);
    color: #fff;
    font-weight: 800;
    font-size: 13px;
    letter-spacing: -0.02em;
    flex: none;
}
.brand strong { display: block; font-size: 15.5px; line-height: 1.2; }
.brand small { display: block; font-size: 11px; color: var(--muted); }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.mode-pill {
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--muted);
    white-space: nowrap;
}
.mode-pill.is-lti { background: var(--emerald-soft); border-color: #b7dec8; color: var(--emerald); }

/* Buttons */
.btn, .icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    min-height: 34px;
    padding: 5px 12px;
    border-radius: 7px;
    border: 1px solid #c5d2cb;
    background: var(--panel);
    color: var(--ink);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color 0.13s ease, border-color 0.13s ease;
}
.btn:hover, .icon-btn:hover { background: #eff4f1; border-color: #a9bbb1; }
.btn.primary { background: var(--emerald); border-color: var(--emerald); color: #fff; }
.btn.primary:hover { background: var(--emerald-hover); border-color: var(--emerald-hover); }
.btn.accent { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.accent:hover { background: #164b7e; border-color: #164b7e; }
.btn.tiny { min-height: 26px; padding: 2px 9px; font-size: 12px; border-radius: 6px; }
.btn:disabled { opacity: 0.48; cursor: wait; }
.mobile-only { display: none; }

/* Workspace */
.workspace {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: 256px minmax(0, 1fr);
}
.sidebar {
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.sidebar-head {
    padding: 10px;
    border-bottom: 1px solid var(--border);
}
.sidebar-head input {
    width: 100%;
    padding: 6px 10px;
    border: 1px solid #c5d2cb;
    border-radius: 7px;
    font: inherit;
    font-size: 13px;
    background: var(--panel);
}
.lesson-list {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 8px 8px 18px;
}
.chapter-title {
    margin: 12px 6px 4px;
    font-size: 11px;
    font-weight: 700;
    color: var(--muted);
    letter-spacing: 0.04em;
}
.chapter-title:first-child { margin-top: 2px; }
.lesson-item {
    width: 100%;
    text-align: left;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 6px 9px;
    margin-bottom: 2px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--ink);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}
.lesson-item:hover { background: #e8f0ec; }
.lesson-item.active {
    background: var(--emerald-soft);
    color: #0e4a34;
    font-weight: 700;
    box-shadow: inset 3px 0 0 var(--emerald);
}
.lesson-tag {
    flex: none;
    font-size: 10.5px;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 999px;
    background: var(--blue-soft);
    color: var(--blue);
}

/* Main Editor + Console */
.main-panes {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(300px, 0.9fr);
}
.editor-pane, .console-pane {
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}
.editor-pane { border-right: 1px solid var(--border); background: var(--code-bg); }
.console-pane { background: var(--panel); }

.pane-bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: 40px;
    padding: 0 10px;
    background: #eef3f0;
    border-bottom: 1px solid var(--border);
}
.file-tabs {
    display: flex;
    align-items: center;
    gap: 3px;
    overflow-x: auto;
    min-width: 0;
}
.file-tab {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border: 1px solid transparent;
    border-bottom: 0;
    border-radius: 7px 7px 0 0;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}
.file-tab:hover { color: var(--ink); background: rgba(255, 255, 255, 0.55); }
.file-tab.active {
    background: var(--code-bg);
    color: #fff;
    border-color: #2b363c;
}
.file-tab-del {
    border: 0;
    background: transparent;
    color: inherit;
    opacity: 0.65;
    cursor: pointer;
    padding: 0 2px;
    font-size: 13px;
    line-height: 1;
}
.file-tab-del:hover { opacity: 1; color: #ff8e7a; }
.file-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.save-indicator { font-size: 11.5px; color: var(--muted); white-space: nowrap; }

.editor-wrap {
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-columns: 48px minmax(0, 1fr);
    background: var(--code-bg);
    overflow: hidden;
}
.line-numbers {
    margin: 0;
    padding: 14px 8px 14px 4px;
    background: var(--code-gutter);
    color: var(--code-muted);
    font: 13.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    text-align: right;
    user-select: none;
    overflow: hidden;
    border-right: 1px solid #242e33;
}
.code-editor {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 14px 14px;
    border: 0;
    outline: none;
    resize: none;
    background: var(--code-bg);
    color: var(--code-ink);
    font: 13.5px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    tab-size: 4;
    overflow: auto;
}
.editor-footer {
    flex: 0 0 auto;
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 5px 12px;
    background: #12171a;
    border-top: 1px solid #242e33;
    color: #8fa3ad;
    font-size: 11.5px;
}

/* Console */
.console-title { display: flex; align-items: center; gap: 8px; }
.run-status {
    font-size: 11.5px;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 999px;
    background: #e3eae6;
    color: var(--muted);
}
.run-status.ok { background: var(--emerald-soft); color: var(--emerald); }
.run-status.error { background: var(--danger-soft); color: var(--danger); }
.run-status.warn { background: var(--warn-soft); color: var(--warn); }
.run-status.running { background: var(--blue-soft); color: var(--blue); }

.output-area {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    padding: 14px;
    background: #fbfdfc;
}
.output-block {
    margin: 0 0 10px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    color: var(--ink);
}
.output-block.stderr {
    padding: 10px 12px;
    border-radius: 8px;
    background: var(--danger-soft);
    border: 1px solid #f0c4bc;
    color: #7d2013;
}

.io-panel {
    flex: 0 0 auto;
    border-top: 1px solid var(--border);
    background: var(--sidebar);
}
.io-panel summary {
    padding: 9px 12px;
    font-size: 12.5px;
    font-weight: 700;
    color: var(--muted);
    cursor: pointer;
    user-select: none;
}
.io-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 0 12px 12px;
}
.io-grid label { display: block; font-size: 12px; font-weight: 700; color: var(--muted); }
.io-grid textarea {
    width: 100%;
    margin-top: 4px;
    padding: 6px 8px;
    border: 1px solid #c5d2cb;
    border-radius: 6px;
    font: 12.5px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    resize: vertical;
    background: var(--panel);
}

@media (max-width: 980px) {
    .mobile-only { display: inline-flex; }
    .workspace { grid-template-columns: minmax(0, 1fr); position: relative; }
    .sidebar {
        position: fixed;
        inset: 54px auto 0 0;
        width: 270px;
        z-index: 40;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.16);
        transform: translateX(-105%);
        transition: transform 0.18s ease;
    }
    .sidebar.open { transform: translateX(0); }
    .main-panes { grid-template-columns: minmax(0, 1fr); grid-template-rows: 1.2fr 0.9fr; }
    .editor-pane { border-right: 0; border-bottom: 1px solid var(--border); }
    .io-grid { grid-template-columns: 1fr; }
}
