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

:root {
    --bg: #1e1e2e;
    --surface: #282840;
    --surface2: #313150;
    --text: #cdd6f4;
    --text-dim: #585b70;
    --green: #a6e3a1;
    --red: #f38ba8;
    --blue: #89b4fa;
    --yellow: #f9e2af;
    --accent: #cba6f7;
    --ghost: rgba(205, 214, 244, 0.25);
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

.screen {
    display: none;
    padding: 2rem;
    max-width: 960px;
    margin: 0 auto;
}

.screen.active {
    display: block;
}

/* ===== Modal ===== */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--surface);
    border-radius: 16px;
    padding: 2rem;
    width: 90%;
    max-width: 450px;
}

.modal-content h2 {
    margin-bottom: 1.5rem;
    color: var(--accent);
}

.form-group {
    margin-bottom: 1.2rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.4rem;
    font-size: 0.9rem;
    color: var(--text-dim);
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.7rem;
    border-radius: 8px;
    border: 1px solid var(--surface2);
    background: var(--bg);
    color: var(--text);
    font-size: 0.95rem;
}

.form-hint {
    font-size: 0.8rem;
    color: var(--text-dim);
    margin-top: 0.3rem;
}

.modal-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
    margin-top: 1.5rem;
}

/* ===== Main Screen ===== */
.main-header {
    text-align: center;
    padding: 2.5rem 0 1.5rem;
    position: relative;
}

.main-header h1 {
    font-size: 2.8rem;
    color: var(--accent);
    letter-spacing: -1px;
}

.subtitle {
    color: var(--text-dim);
    margin-top: 0.5rem;
    font-size: 1rem;
    line-height: 1.5;
}

/* Auth Bar */
.auth-bar {
    position: absolute;
    top: 2.5rem;
    right: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.auth-state.hidden { display: none; }

.google-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--surface);
    border: 1px solid var(--surface2);
    color: var(--text);
    padding: 0.4rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.85rem;
}

.google-btn:hover { border-color: var(--accent); }

.auth-user-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.user-photo {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
}

.user-name {
    font-size: 0.85rem;
    color: var(--text);
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.btn.small {
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem;
}

#auth-user {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.settings-btn {
    position: absolute;
    top: 2.5rem;
    right: 0;
    background: var(--surface);
    border: 1px solid var(--surface2);
    color: var(--text-dim);
    padding: 0.5rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: border-color 0.15s;
}

.settings-btn:hover {
    border-color: var(--accent);
    color: var(--text);
}

/* Input Area */
.input-area {
    margin-top: 1.5rem;
}

.input-area textarea {
    width: 100%;
    padding: 1rem;
    border-radius: 12px;
    border: 2px solid var(--surface2);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    line-height: 1.6;
    resize: vertical;
    font-family: inherit;
    transition: border-color 0.15s;
}

.input-area textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.input-area textarea::placeholder {
    color: var(--text-dim);
}

.btn {
    padding: 0.6rem 1.2rem;
    border-radius: 8px;
    border: 1px solid var(--surface2);
    background: var(--surface2);
    color: var(--text);
    cursor: pointer;
    font-size: 0.95rem;
    transition: background 0.15s, opacity 0.15s;
}

.btn:hover {
    opacity: 0.85;
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn.primary {
    background: var(--accent);
    color: var(--bg);
    border-color: var(--accent);
    font-weight: 600;
}

.btn.large {
    width: 100%;
    margin-top: 0.75rem;
    padding: 0.9rem;
    font-size: 1.05rem;
}

/* Example Chips */
.examples-section {
    margin-top: 2rem;
}

.examples-section h3 {
    font-size: 0.95rem;
    color: var(--text-dim);
    margin-bottom: 0.75rem;
}

.example-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.chip {
    background: var(--surface);
    border: 1px solid var(--surface2);
    color: var(--blue);
    padding: 0.4rem 0.9rem;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: border-color 0.15s, background 0.15s;
}

.chip:hover {
    border-color: var(--blue);
    background: var(--surface2);
}

/* History */
.history-section {
    margin-top: 2rem;
}

.history-section.hidden {
    display: none;
}

.history-section h3 {
    font-size: 0.95rem;
    color: var(--text-dim);
    margin-bottom: 0.75rem;
}

.history-list-scroll {
    max-height: 400px;
    overflow-y: auto;
}

.history-item {
    background: var(--surface);
    border: 1px solid var(--surface2);
    border-radius: 10px;
    padding: 0.8rem 1rem;
    margin-bottom: 0.5rem;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: border-color 0.15s;
}

.history-item:hover {
    border-color: var(--accent);
}

.history-item-main {
    flex: 1;
    min-width: 0;
}

.history-item .desc {
    font-size: 0.9rem;
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-stats {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.3rem;
}

.history-stats span {
    font-size: 0.75rem;
    color: var(--text-dim);
    background: var(--bg);
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
}

.history-item-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-shrink: 0;
    margin-left: 1rem;
}

.history-item .meta {
    color: var(--text-dim);
    font-size: 0.8rem;
    white-space: nowrap;
}

.history-delete-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    line-height: 1;
    transition: all 0.15s;
    opacity: 0;
}

.history-item:hover .history-delete-btn {
    opacity: 1;
}

.history-delete-btn:hover {
    color: var(--red);
    background: rgba(243, 139, 168, 0.15);
}

.load-more {
    width: 100%;
    margin-top: 0.5rem;
    text-align: center;
}

.load-more.hidden { display: none; }

/* ===== Loading Screen ===== */
.loading-card {
    text-align: center;
    padding: 4rem 2rem;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--surface2);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 1.5rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

#loading-msg {
    color: var(--text-dim);
    font-size: 1.05rem;
}

/* ===== Sub Header ===== */
.sub-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--surface2);
    margin-bottom: 1rem;
}

.back-btn {
    background: var(--surface);
    border: 1px solid var(--surface2);
    color: var(--text);
    padding: 0.5rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background 0.15s;
}

.back-btn:hover {
    background: var(--surface2);
}

.typing-stats {
    display: flex;
    gap: 1.5rem;
    margin-left: auto;
    font-size: 0.9rem;
    color: var(--text-dim);
    font-family: 'Courier New', monospace;
}

/* Task Description */
.task-description {
    background: var(--surface);
    border-radius: 10px;
    padding: 1rem 1.2rem;
    margin-bottom: 1rem;
}

.task-description h3 {
    color: var(--yellow);
    font-size: 1rem;
    line-height: 1.5;
}

/* Algorithm Section */
.algorithm-section {
    background: var(--surface);
    border: 1px solid var(--surface2);
    border-radius: 10px;
    margin-bottom: 1rem;
    overflow: hidden;
}

.algorithm-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.6rem 1rem;
    background: var(--surface2);
}

.algorithm-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.algorithm-toggle-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 0.2rem 0.5rem;
}

.algorithm-toggle-btn:hover {
    color: var(--text);
}

.algorithm-body {
    padding: 1rem 1.2rem;
    line-height: 1.8;
    font-size: 0.92rem;
}

.algorithm-body.collapsed {
    display: none;
}

.algorithm-body .step {
    display: flex;
    gap: 0.7rem;
    margin-bottom: 0.5rem;
    align-items: flex-start;
}

.algorithm-body .step-num {
    background: var(--accent);
    color: var(--bg);
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    flex-shrink: 0;
    margin-top: 2px;
}

.algorithm-body .step-text {
    flex: 1;
}

.algorithm-body .step-text code {
    background: #1a1a2e;
    color: var(--blue);
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.85rem;
}

.algorithm-body .concept {
    margin-top: 0.8rem;
    padding: 0.7rem 0.9rem;
    background: rgba(137, 180, 250, 0.08);
    border-left: 3px solid var(--blue);
    border-radius: 0 6px 6px 0;
    font-size: 0.85rem;
    color: var(--blue);
    line-height: 1.6;
}

/* Typing Section Label */
.typing-section-label {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
    position: relative;
}

.typing-section-label > span {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--green);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.hint-btn {
    background: none;
    border: 1px solid var(--surface2);
    color: var(--text-dim);
    padding: 0.2rem 0.6rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.75rem;
    transition: background 0.15s;
}

.hint-btn:hover {
    background: var(--surface2);
    color: var(--blue);
}

.hint-panel {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 0.3rem;
    background: #1a1a2e;
    border: 1px solid var(--surface2);
    border-radius: 8px;
    z-index: 10;
    overflow: hidden;
}

.hint-panel.hidden {
    display: none;
}

.hint-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0.8rem;
    background: var(--surface2);
    font-size: 0.8rem;
    color: var(--text-dim);
}

.hint-copy-btn {
    background: var(--surface);
    border: 1px solid var(--text-dim);
    color: var(--text);
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.75rem;
    transition: background 0.15s;
}

.hint-copy-btn:hover {
    background: var(--accent);
    color: var(--bg);
}

.hint-code {
    padding: 0.8rem;
    font-size: 0.85rem;
    color: var(--text);
    overflow-x: auto;
    font-family: 'Courier New', Consolas, monospace;
    line-height: 1.6;
    white-space: pre;
}

/* C syntax highlight colors */
.hl-keyword { color: var(--accent); font-weight: 600; }
.hl-string { color: var(--green); }
.hl-comment { color: var(--text-dim); font-style: italic; }
.hl-preproc { color: var(--yellow); }
.hl-number { color: #fab387; }

/* Hidden input for IME suppression */
.typing-capture {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
    padding: 0;
    border: none;
    pointer-events: none;
}

/* ===== Editor Area ===== */
.editor-area {
    background: #1a1a2e;
    border: 2px solid var(--surface2);
    border-radius: 12px;
    display: flex;
    overflow: hidden;
    min-height: 280px;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 15px;
    line-height: 1.7;
    position: relative;
    transition: border-color 0.2s;
}

.editor-area:focus-within,
.editor-area.active {
    border-color: var(--accent);
}

.line-numbers {
    background: var(--surface);
    padding: 1rem 0.8rem;
    text-align: right;
    color: var(--text-dim);
    user-select: none;
    min-width: 3rem;
    font-size: 13px;
    line-height: 1.7;
}

.code-container {
    flex: 1;
    padding: 1rem;
    position: relative;
    overflow-x: auto;
    cursor: text;
}

.ghost-text {
    color: var(--ghost);
    white-space: pre;
    position: absolute;
    top: 1rem;
    left: 1rem;
    pointer-events: none;
}

.ghost-comment {
    color: rgba(88, 91, 112, 0.5);
    font-style: italic;
}

.user-text {
    white-space: pre;
    position: relative;
    z-index: 1;
    min-height: 100%;
}

.user-text .correct {
    color: var(--green);
}

.user-text .wrong {
    color: var(--red);
    background: rgba(243, 139, 168, 0.15);
    border-radius: 2px;
}

.user-text .cursor-char {
    border-left: 2px solid var(--accent);
    animation: blink 1s infinite;
    margin-left: -1px;
    padding-left: 1px;
}

@keyframes blink {
    0%, 50% { border-color: var(--accent); }
    51%, 100% { border-color: transparent; }
}

.typing-footer {
    text-align: center;
    margin-top: 0.6rem;
}

.key-hint {
    font-size: 0.8rem;
    color: var(--text-dim);
}

/* ===== Result Screen ===== */
.result-card {
    background: var(--surface);
    border-radius: 16px;
    padding: 2rem;
    max-width: 700px;
    margin: 2rem auto;
}

.result-card h2 {
    font-size: 2rem;
    color: var(--green);
    text-align: center;
    margin-bottom: 1.5rem;
}

.result-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.result-item {
    background: var(--surface2);
    border-radius: 10px;
    padding: 0.8rem;
    text-align: center;
}

.result-label {
    display: block;
    color: var(--text-dim);
    font-size: 0.8rem;
    margin-bottom: 0.2rem;
}

.result-value {
    display: block;
    font-size: 1.4rem;
    font-weight: 700;
    font-family: 'Courier New', monospace;
}

/* ===== Compile Section ===== */
.compile-section {
    margin-bottom: 1.5rem;
}

.compile-section h3 {
    color: var(--yellow);
    margin-bottom: 0.75rem;
    font-size: 1rem;
}

.compile-tabs {
    display: flex;
    gap: 0;
    margin-bottom: 0;
}

.tab-btn {
    flex: 1;
    padding: 0.5rem;
    background: var(--bg);
    border: 1px solid var(--surface2);
    color: var(--text-dim);
    cursor: pointer;
    font-size: 0.85rem;
    transition: background 0.15s;
}

.tab-btn:first-child {
    border-radius: 8px 0 0 0;
}

.tab-btn:last-child {
    border-radius: 0 8px 0 0;
}

.tab-btn.active {
    background: var(--surface2);
    color: var(--text);
    border-bottom-color: var(--surface2);
}

.compile-code {
    background: #1a1a2e;
    border: 1px solid var(--surface2);
    border-top: none;
    border-radius: 0 0 8px 8px;
    padding: 1rem;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.6;
    overflow-x: auto;
    white-space: pre;
    max-height: 300px;
    overflow-y: auto;
    color: var(--text);
}

.compile-input-row {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.stdin-input {
    flex: 1;
    padding: 0.6rem 0.8rem;
    border-radius: 8px;
    border: 1px solid var(--surface2);
    background: var(--bg);
    color: var(--text);
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
}

.compile-output {
    margin-top: 0.75rem;
    border: 1px solid var(--surface2);
    border-radius: 8px;
    overflow: hidden;
}

.compile-output.hidden {
    display: none;
}

.output-header {
    background: var(--surface2);
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
}

.output-header .success {
    color: var(--green);
}

.output-header .error {
    color: var(--red);
}

#compile-result {
    background: #0d0d1a;
    padding: 0.8rem;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.5;
    white-space: pre-wrap;
    max-height: 200px;
    overflow-y: auto;
    color: var(--text);
}

.result-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .screen {
        padding: 1rem;
    }

    .main-header h1 {
        font-size: 2rem;
    }

    .typing-stats {
        gap: 0.8rem;
        font-size: 0.8rem;
    }

    .editor-area {
        font-size: 13px;
    }

    .result-card {
        padding: 1.2rem;
    }

    .settings-btn {
        position: static;
        margin-top: 0.5rem;
    }
}
