:root {
    --bg: #0f172a;
    --panel: #111827;
    --panel-2: #1f2937;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --accent: #22c55e;
    --danger: #ef4444;
    --border: #374151;
    --editor-font-size: 0.95rem;
    --layout-bottom-row-size: 320px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    overflow: auto;
    display: flex;
    flex-direction: column;
    font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.topbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid var(--border);
    padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px;
    background: #020617;
}

.topbar-main {
    min-width: 0;
}

.topbar-title-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.mobile-files-menu-btn {
    display: none;
}

.install-btn {
    margin-top: 8px;
}

.mobile-author {
    display: none;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    color: var(--muted);
    font-size: 0.85rem;
    text-align: center;
}

.topbar h1 {
    margin: 0;
    font-size: 1.05rem;
}

.topbar p {
    margin: 2px 0 0;
    color: var(--muted);
    font-size: 0.82rem;
}

.topbar-author {
    margin-top: 0;
    color: var(--muted);
    font-size: 0.82rem;
    white-space: nowrap;
    text-align: right;
}

.layout {
    flex: 1;
    min-height: 0;
    overflow: auto;
    display: grid;
    grid-template-columns: minmax(240px, 320px) minmax(0, 1fr) minmax(320px, 0.9fr);
    grid-template-rows: minmax(240px, 1fr) minmax(180px, var(--layout-bottom-row-size));
    gap: 12px;
    padding: 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}

body.bottom-panels-hidden .layout {
    grid-template-rows: minmax(0, 1fr) 0;
}

.intro-strip {
    position: relative;
    margin: 6px 12px 0;
    padding: 7px 30px 7px 10px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #0b1220;
}

.intro-strip p {
    margin: 0;
    color: var(--text);
    font-size: 0.86rem;
}

.intro-cta {
    margin-top: 4px !important;
    color: var(--muted) !important;
}

.intro-cta a {
    color: #86efac;
}

.intro-close {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--muted);
    padding: 0;
    min-height: 0;
    font-weight: 700;
    line-height: 1;
}

.intro-strip.is-hidden {
    display: none;
}

.ad-slot {
    margin: 10px 12px 0;
    border: 1px dashed var(--border);
    border-radius: 10px;
    background: #0b1220;
    padding: 8px;
    min-height: 102px;
}

.ad-slot .adsbygoogle,
.docs-ad-wrap .adsbygoogle {
    display: block;
    width: 100%;
    min-height: 90px;
}

.ad-hidden {
    display: none !important;
}

.panel {
    min-height: 0;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--panel);
    overflow: hidden;
}

.panel-collapsible-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.panel-toggle-btn {
    display: none;
}

.panel-mini-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
}

.panel-accordion-btn {
    display: none;
}

.panel-accordion-label {
    font-size: 0.95rem;
    font-weight: 600;
}

.panel-accordion-chevron {
    display: inline-flex;
    line-height: 1;
    transition: transform 0.2s ease;
}

.editor-panel {
    grid-column: 2 / 4;
    grid-row: 1;
    display: flex;
    flex-direction: column;
}

.terminal-panel {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    flex-direction: column;
}

.docs-panel {
    grid-column: 3;
    grid-row: 2;
    display: flex;
    flex-direction: column;
}

body.bottom-panels-hidden .terminal-panel,
body.bottom-panels-hidden .docs-panel {
    display: none;
}

.files-panel {
    grid-column: 1;
    grid-row: 1 / span 2;
    display: flex;
    flex-direction: column;
}

.bottom-row-resizer {
    grid-column: 2 / 4;
    grid-row: 2;
    align-self: start;
    justify-self: stretch;
    height: 8px;
    margin-top: -4px;
    z-index: 20;
    cursor: row-resize;
    background: transparent;
}

.bottom-row-resizer::before {
    content: '';
    display: block;
    width: 42px;
    height: 4px;
    border-radius: 999px;
    margin: 2px auto;
    background: rgba(156, 163, 175, 0.55);
}

body.bottom-panels-hidden .bottom-row-resizer {
    display: none;
}

.restore-bottom-panels-btn {
    position: fixed;
    right: 14px;
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    z-index: 1100;
    padding: 6px 10px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}

.restore-bottom-panels-btn[hidden] {
    display: none !important;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    background: #0b1220;
}

.panel-header h2 {
    margin: 0;
    font-size: 1rem;
}

.editor-header-main {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 10px;
}

.editor-file-meta {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

.editor-file-meta h2 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.editor-file-status {
    color: var(--muted);
    font-size: 0.8rem;
    white-space: nowrap;
}

.editor-file-status.is-dirty {
    color: #facc15;
}

.editor-file-status.is-error {
    color: #fca5a5;
}

.editor-file-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

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

.editor-panel .panel-header .btn-label {
    display: none;
}

.editor-panel .panel-header button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    padding: 6px 8px;
}

.editor-panel .panel-header button .btn-icon+.btn-label {
    margin-left: 0;
}

.expand-editor-btn {
    display: inline-flex;
}

.expand-editor-btn[hidden] {
    display: none !important;
}

body.editor-expanded {
    overflow: hidden;
}

body.editor-expanded .topbar,
body.editor-expanded .intro-strip,
body.editor-expanded .ad-slot,
body.editor-expanded .files-panel,
body.editor-expanded .terminal-panel,
body.editor-expanded .docs-panel,
body.editor-expanded .mobile-author {
    display: none;
}

body.editor-expanded .layout {
    display: block;
    padding: 0;
    margin: 0;
    height: 100dvh;
}

body.editor-expanded .editor-panel {
    position: fixed;
    z-index: 999;
    inset: 0;
    border-radius: 0;
    border: 0;
}

body.editor-expanded .editor-panel .panel-header {
    border-bottom: 1px solid var(--border);
}

.btn-icon {
    font-size: 0.95rem;
    line-height: 1;
}

.btn-label {
    font-size: 0.88rem;
}

button .btn-icon+.btn-label {
    margin-left: 6px;
}

button {
    border: 1px solid #16a34a;
    background: var(--accent);
    color: #052e16;
    font-weight: 600;
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
}

button.secondary {
    background: transparent;
    color: var(--text);
    border-color: var(--border);
}

button:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

#editor {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: var(--gutter-width);
    width: auto;
    min-height: 0;
    height: 100%;
    resize: none;
    overflow: auto;
    border: 0;
    outline: none;
    background: transparent;
    color: transparent;
    caret-color: var(--text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: var(--editor-font-size);
    line-height: 1.5;
    padding: 12px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    tab-size: 4;
}

body.editor-no-wrap #editor {
    white-space: pre;
    overflow-wrap: normal;
    word-break: normal;
}

#editor::selection {
    background: rgba(59, 130, 246, 0.35);
    color: transparent;
}

.editor-shell {
    --gutter-width: 56px;
    position: relative;
    flex: 1;
    min-height: 0;
    background: var(--panel-2);
}

.line-numbers {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: var(--gutter-width);
    overflow: hidden;
    margin: 0;
    padding: 12px 8px 12px 0;
    border-right: 1px solid rgba(148, 163, 184, 0.2);
    background: rgba(2, 6, 23, 0.35);
    text-align: right;
    color: rgba(148, 163, 184, 0.8);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: var(--editor-font-size);
    line-height: 1.5;
    white-space: pre;
    pointer-events: none;
    user-select: none;
}

#editorHighlight {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: var(--gutter-width);
    margin: 0;
    overflow: auto;
    pointer-events: none;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: var(--editor-font-size);
    line-height: 1.5;
    padding: 12px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    color: var(--text);
    tab-size: 4;
}

body.editor-no-wrap #editorHighlight {
    white-space: pre;
    overflow-wrap: normal;
    word-break: normal;
}

.tok-keyword {
    color: #60a5fa;
    font-weight: 600;
}

.tok-type {
    color: #c084fc;
    font-weight: 600;
}

.tok-fn {
    color: #34d399;
}

.tok-bool {
    color: #f59e0b;
    font-weight: 600;
}

.tok-number {
    color: #f472b6;
}

.tok-string {
    color: #facc15;
}

.tok-comment {
    color: #94a3b8;
    font-style: italic;
}

.tok-variable {
    color: #e2e8f0;
}

.tok-op {
    color: #7dd3fc;
}

.tok-guide {
    color: rgba(148, 163, 184, 0.82);
}

.terminal-output {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 12px;
    background: #030712;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.93rem;
    white-space: pre-wrap;
}

.terminal-input-line {
    display: flex;
    align-items: center;
    gap: 6px;
}

.terminal-inline-input {
    flex: 1;
    min-width: 0;
    border: 0;
    outline: none;
    padding: 0;
    margin: 0;
    background: transparent;
    color: var(--text);
    font: inherit;
}

.terminal-inline-input:disabled {
    color: var(--text);
    opacity: 1;
}

.docs-content {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 12px;
    color: var(--muted);
}

.files-manager {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 10px 12px;
    background: #0b1220;
}

.files-panel-body {
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.files-toggle-btn {
    display: none;
}

.files-sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.6);
    z-index: 1000;
}

.app-modal {
    position: fixed;
    inset: 0;
    z-index: 1300;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.app-modal[hidden] {
    display: none !important;
}

.app-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(2, 6, 23, 0.7);
}

.app-modal-dialog {
    position: relative;
    width: min(100%, 460px);
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #0b1220;
    padding: 14px;
}

.app-modal-title {
    margin: 0 0 8px;
    font-size: 1rem;
}

.app-modal-message {
    margin: 0;
    color: var(--text);
    white-space: pre-wrap;
}

.app-modal-input {
    width: 100%;
    margin-top: 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--panel);
    color: var(--text);
    font: inherit;
    font-size: 0.9rem;
    padding: 8px 10px;
    outline: none;
}

.app-modal-input:focus {
    border-color: #86efac;
}

.app-modal-actions {
    margin-top: 12px;
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.app-modal-confirm-danger {
    border-color: #dc2626;
    background: #ef4444;
    color: #fff;
}

.files-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
}

.files-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: none;
    overflow: auto;
}

.file-item {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: transparent;
    color: var(--text);
    text-align: left;
    font: inherit;
    font-size: 0.88rem;
    padding: 6px 8px;
}

.file-item.is-active {
    border-color: #16a34a;
    background: rgba(34, 197, 94, 0.16);
}

.file-item-meta {
    display: block;
    margin-top: 2px;
    color: var(--muted);
    font-size: 0.78rem;
}

.docs-ad-wrap {
    border-top: 1px solid var(--border);
    background: #0b1220;
    padding: 10px 12px;
}

.docs-content a {
    color: #86efac;
}

.docs-live-title {
    margin: 0 0 8px;
    font-size: 0.92rem;
    color: var(--muted);
}

.docs-live-command {
    margin: 0 0 8px;
    font-size: 1rem;
    color: var(--text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.docs-live-desc {
    margin: 0 0 10px;
    color: var(--text);
}

.docs-live-signature {
    margin: 0;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #0b1220;
    color: #cbd5e1;
    white-space: pre-wrap;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.9rem;
}

.docs-live-footer {
    margin-top: 10px;
    color: var(--muted);
    font-size: 0.88rem;
}

@media (max-width: 980px) {
    .topbar {
        flex-direction: column;
        align-items: stretch;
        gap: 6px;
        padding: calc(8px + env(safe-area-inset-top, 0px)) 10px 8px;
    }

    .topbar-author {
        display: none;
    }

    .layout {
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: minmax(320px, 1fr) auto auto;
        gap: 10px;
        padding: 8px;
    }

    body.bottom-panels-hidden .layout {
        grid-template-rows: minmax(320px, 1fr);
    }

    .intro-strip,
    .ad-slot {
        margin: 8px 8px 0;
        border-radius: 8px;
    }

    .intro-strip {
        padding: 8px 32px 8px 10px;
    }

    .intro-strip p {
        font-size: 0.88rem;
    }

    .editor-panel,
    .terminal-panel,
    .docs-panel {
        grid-column: 1;
    }

    .files-panel {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(86vw, 340px);
        z-index: 1001;
        border-radius: 0 10px 10px 0;
        transform: translateX(-105%);
        transition: transform 0.2s ease;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    }

    body.files-sidebar-open .files-panel {
        transform: translateX(0);
    }

    body.files-sidebar-open {
        overflow: hidden;
    }

    .editor-panel {
        grid-row: 1;
    }

    .terminal-panel {
        grid-row: 2;
    }

    .docs-panel {
        grid-row: 3;
        display: flex;
        min-height: 0;
    }

    .files-actions {
        display: flex;
    }

    .files-toggle-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .mobile-files-menu-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .panel-header {
        align-items: center;
        flex-direction: row;
        gap: 8px;
        padding: 10px;
    }

    .editor-panel .panel-header {
        align-items: flex-start;
        flex-direction: column;
        gap: 8px;
    }

    .panel-toggle-btn {
        display: inline-flex;
    }

    .panel-mini-toggle,
    .bottom-row-resizer {
        display: none !important;
    }

    .restore-bottom-panels-btn {
        right: 10px;
        bottom: calc(10px + env(safe-area-inset-bottom, 0px));
        padding: 8px 10px;
    }

    .panel-title-static {
        display: none;
    }

    .panel-accordion-btn {
        width: 100%;
        display: inline-flex;
        align-items: center;
        justify-content: space-between;
        border: 1px solid var(--border);
        background: rgba(15, 23, 42, 0.6);
        color: var(--text);
        padding: 10px 12px;
    }

    .panel-collapsible-body {
        overflow: hidden;
        max-height: 1000px;
        opacity: 1;
        transition: max-height 0.24s ease, opacity 0.2s ease;
    }

    .terminal-panel .panel-collapsible-body {
        max-height: min(40dvh, 360px);
        overflow: auto;
    }

    .docs-panel .panel-collapsible-body {
        max-height: min(32dvh, 280px);
        overflow: auto;
    }

    .terminal-panel.is-collapsed .panel-collapsible-body,
    .docs-panel.is-collapsed .panel-collapsible-body {
        max-height: 0 !important;
        opacity: 0;
    }

    .terminal-panel.is-collapsed .panel-accordion-chevron,
    .docs-panel.is-collapsed .panel-accordion-chevron {
        transform: rotate(-90deg);
    }

    .editor-header-main {
        width: 100%;
        justify-content: space-between;
    }

    .editor-file-actions {
        flex-wrap: nowrap;
    }

    .actions {
        width: 100%;
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 2px;
        -webkit-overflow-scrolling: touch;
    }

    .actions button {
        flex: 0 0 auto;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 40px;
        padding: 8px;
    }

    .actions .btn-label,
    .install-btn .btn-label,
    .editor-file-actions .btn-label {
        display: none;
    }

    .panel-toggle-btn .btn-label {
        display: inline;
    }

    button .btn-icon+.btn-label {
        margin-left: 0;
    }

    .mobile-author {
        display: block;
    }

}

@media (max-width: 640px) {
    .topbar h1 {
        font-size: 1rem;
    }

    .topbar p {
        font-size: 0.8rem;
    }

    button {
        padding: 8px 10px;
        min-height: 38px;
    }

    .install-btn {
        width: 100%;
    }

    .editor-shell {
        --gutter-width: 46px;
    }

    #editor,
    #editorHighlight,
    .line-numbers,
    .terminal-output,
    .docs-content {
        font-size: 0.88rem;
    }

    .docs-ad-wrap {
        display: none;
    }

    .files-actions button {
        min-width: 38px;
        padding: 8px;
    }

    .files-toggle-btn .btn-label {
        display: inline;

        .app-modal-dialog {
            width: min(100%, 520px);
        }
    }

    .mobile-files-menu-btn .btn-label {
        display: inline;
    }
}