/* ========== Global Variables ========== */
:root {
    --primary: #6C5CE7;
    --primary-light: #A29BFE;
    --secondary: #00CEC9;
    --accent: #FDCB6E;
    --danger: #FF7675;
    --success: #00B894;
    --bg: #F8F9FE;
    --card: #FFFFFF;
    --text: #2D3436;
    --text-light: #636E72;
    --shadow: 0 4px 20px rgba(108, 92, 231, 0.12);
    --shadow-hover: 0 8px 30px rgba(108, 92, 231, 0.22);
    --radius: 16px;
    --radius-sm: 10px;
    --touch-min: 44px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
    background: var(--bg); color: var(--text);
    min-height: 100vh; overflow-x: hidden;
}
body::before {
    content: ""; position: fixed; top: -50%; left: -50%;
    width: 200%; height: 200%; z-index: -1;
    background: radial-gradient(ellipse at 20% 50%, rgba(108,92,231,0.06) 0%, transparent 50%),
                radial-gradient(ellipse at 80% 20%, rgba(0,206,201,0.06) 0%, transparent 50%),
                radial-gradient(ellipse at 50% 80%, rgba(253,203,110,0.06) 0%, transparent 50%);
}

/* ========== Buttons ========== */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    padding: 12px 28px; border: none; border-radius: var(--radius-sm);
    font-size: 16px; font-weight: 600; cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    min-height: var(--touch-min); font-family: inherit;
}
.btn:active { transform: scale(0.96); }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: #fff; box-shadow: 0 4px 15px rgba(108,92,231,0.35); }
.btn-primary:hover { box-shadow: 0 6px 25px rgba(108,92,231,0.5); transform: translateY(-2px); }
.btn-secondary { background: linear-gradient(135deg, var(--secondary), #55EFC4); color: #fff; box-shadow: 0 4px 15px rgba(0,206,201,0.35); }
.btn-accent { background: linear-gradient(135deg, var(--accent), #F9CA24); color: var(--text); }
.btn-danger { background: linear-gradient(135deg, var(--danger), #FAB1A0); color: #fff; }
.btn-outline { background: transparent; border: 2px solid var(--primary); color: var(--primary); }
.btn-outline:hover { background: var(--primary); color: #fff; }
.btn-lg { padding: 16px 36px; font-size: 18px; border-radius: var(--radius); }
.btn-sm { padding: 8px 18px; font-size: 14px; }

.card {
    background: var(--card); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 24px; transition: all 0.3s ease;
}
.card:hover { box-shadow: var(--shadow-hover); }

.input-field {
    width: 100%; padding: 14px 18px; border: 2px solid #E8E8F0;
    border-radius: var(--radius-sm); font-size: 16px; font-family: inherit;
    transition: border-color 0.3s; outline: none; background: #FAFAFE;
}
.input-field:focus { border-color: var(--primary); background: #fff; box-shadow: 0 0 0 4px rgba(108,92,231,0.1); }

.badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge-purple { background: #F0EDFF; color: var(--primary); }
.badge-green { background: #E0FFF5; color: var(--success); }

/* ========== Homepage ========== */
.hero { text-align: center; padding: 50px 20px 30px; }
.hero-title {
    font-size: 44px; font-weight: 800;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
    margin-bottom: 8px;
}
.hero-subtitle { font-size: 18px; color: var(--text-light); }

.user-select-section { max-width: 800px; margin: 0 auto; padding: 0 16px; }
.user-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 14px; margin: 20px 0; }
.user-card { text-align: center; padding: 20px 12px; cursor: pointer; border: 3px solid transparent; }
.user-card:hover { border-color: var(--primary-light); transform: translateY(-4px); }
.user-card.active { border-color: var(--primary); background: linear-gradient(135deg, #F0EDFF, #E8F4FD); }
.user-avatar {
    width: 64px; height: 64px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 32px; margin: 0 auto 10px;
    background: linear-gradient(135deg, var(--primary-light), var(--secondary)); color: #fff;
}
.user-name { font-size: 15px; font-weight: 600; margin-bottom: 4px; }
.user-stats { font-size: 12px; color: var(--text-light); }
.new-user-card { border: 3px dashed #D0D0E0; background: transparent; box-shadow: none; }
.new-user-card:hover { border-color: var(--primary); background: #F8F6FF; }
.new-user-icon { width: 64px; height: 64px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 28px; margin: 0 auto 10px; background: #F0F0F8; color: var(--primary); }

/* Modal */
.modal-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4); backdrop-filter: blur(4px);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000; opacity: 0; visibility: hidden; transition: all 0.3s;
}
.modal-overlay.active { opacity: 1; visibility: visible; }
.modal {
    background: var(--card); border-radius: var(--radius); padding: 32px;
    width: 90%; max-width: 440px; box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    transform: translateY(20px); transition: transform 0.3s; max-height: 90vh; overflow-y: auto;
}
.modal-overlay.active .modal { transform: translateY(0); }
.modal-title { font-size: 22px; font-weight: 700; margin-bottom: 20px; text-align: center; color: var(--primary); }

.avatar-picker { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 14px 0; }
.avatar-option {
    width: 52px; height: 52px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 26px; cursor: pointer; border: 3px solid transparent;
    transition: all 0.2s; background: #F0F0F8; min-width: var(--touch-min); min-height: var(--touch-min);
}
.avatar-option:hover { transform: scale(1.1); border-color: var(--primary-light); }
.avatar-option.selected { border-color: var(--primary); background: #F0EDFF; transform: scale(1.15); }
.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; color: var(--text-light); }

/* ========== Game Header ========== */
.game-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; background: var(--card);
    box-shadow: 0 2px 10px rgba(0,0,0,0.06); position: sticky; top: 0; z-index: 100;
}
.game-header-left { display: flex; align-items: center; gap: 12px; }
.game-logo { font-size: 20px; font-weight: 800; background: linear-gradient(135deg, var(--primary), var(--secondary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
.level-info-badge { background: #F0EDFF; color: var(--primary); padding: 5px 14px; border-radius: 20px; font-weight: 600; font-size: 13px; }
.game-header-right { display: flex; align-items: center; gap: 8px; }
.timer-display { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--primary); background: #F0EDFF; padding: 5px 14px; border-radius: var(--radius-sm); }

/* ========== Game Layout ========== */
.game-layout {
    display: grid; grid-template-columns: 1fr 380px; gap: 20px;
    padding: 20px; max-width: 1400px; margin: 0 auto;
}
.maze-section { display: flex; flex-direction: column; gap: 14px; }
.maze-container { display: flex; align-items: center; justify-content: center; }
#mazeCanvas { border-radius: var(--radius); box-shadow: var(--shadow); background: #fff; max-width: 100%; }

.level-description {
    padding: 12px 16px; background: linear-gradient(135deg, #F0EDFF, #E8F8F5);
    border-radius: var(--radius-sm); font-size: 14px; line-height: 1.6;
    border-left: 4px solid var(--primary); cursor: pointer;
}
.level-description.collapsed { max-height: 40px; overflow: hidden; position: relative; }
.level-description.collapsed::after { content: "... \u70B9\u51FB\u5C55\u5F00"; position: absolute; right: 12px; top: 10px; color: var(--primary); font-size: 12px; }

/* Status bar for collected items */
.game-status-bar {
    display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
    padding: 8px 12px; background: #FAFAFE; border-radius: var(--radius-sm); font-size: 13px;
}
.status-item { display: flex; align-items: center; gap: 4px; }

/* ========== Instruction Panel ========== */
.instruction-panel { display: flex; flex-direction: column; gap: 14px; }
.instruction-editor { flex: 1; display: flex; flex-direction: column; }
.editor-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.editor-title { font-size: 17px; font-weight: 700; color: var(--primary); }
.tier-badge { font-size: 12px; padding: 4px 10px; border-radius: 12px; background: var(--accent); color: var(--text); font-weight: 600; }

/* Instruction Assembler */
.assembler {
    display: flex; flex-direction: column; gap: 8px;
    background: #FAFAFE; border-radius: var(--radius-sm); padding: 12px;
    border: 1px solid #E8E8F0; margin-bottom: 10px;
}
.assembler-row {
    display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
.asm-label {
    font-weight: 700; font-size: 14px; color: var(--primary);
    min-width: 44px; text-align: center; padding: 6px 0;
}
.step-btn {
    width: 40px; height: 40px; border: 2px solid #E0E0F0; border-radius: 8px;
    background: #fff; font-size: 16px; font-weight: 700; cursor: pointer;
    transition: all 0.15s; display: flex; align-items: center; justify-content: center;
    min-width: var(--touch-min); min-height: var(--touch-min); font-family: inherit;
}
.step-btn:hover { border-color: var(--primary); color: var(--primary); }
.step-btn.selected { background: var(--primary); color: #fff; border-color: var(--primary); }
.step-btn.expr-btn { width: auto; padding: 0 10px; font-size: 14px; }
.asm-insert {
    padding: 6px 14px; border: none; border-radius: 8px;
    background: var(--primary); color: #fff; font-size: 13px; font-weight: 600;
    cursor: pointer; min-height: var(--touch-min); font-family: inherit;
}
.asm-insert:active { transform: scale(0.95); }

#instructionInput {
    min-height: 140px; padding: 14px; border: 2px solid #E8E8F0;
    border-radius: var(--radius-sm); font-size: 15px;
    font-family: "Consolas", "Microsoft YaHei", monospace;
    line-height: 1.8; resize: vertical; outline: none; background: #FAFAFE;
    transition: border-color 0.3s;
}
#instructionInput:focus { border-color: var(--primary); box-shadow: 0 0 0 4px rgba(108,92,231,0.1); }
#instructionInput::placeholder { color: #B0B0C0; }

.action-btns { display: flex; gap: 10px; margin-top: 10px; }
.action-btns .btn { flex: 1; }

/* Parsed display */
.parsed-display {
    max-height: 140px; overflow-y: auto; padding: 10px; background: #F8F9FE;
    border-radius: var(--radius-sm); font-size: 13px; line-height: 1.8;
    font-family: "Consolas", "Microsoft YaHei", monospace; border: 1px solid #E8E8F0;
}
.parsed-line { padding: 2px 0; transition: background 0.3s; }
.parsed-line.active { background: #FFEAA7; border-radius: 4px; padding: 2px 6px; }

/* Segment path display */
.segment-info {
    display: flex; align-items: center; gap: 8px; padding: 8px 12px;
    background: #E8F8F5; border-radius: var(--radius-sm); font-size: 13px; color: var(--secondary);
}

/* ========== Level Select ========== */
.page-section { max-width: 1200px; margin: 0 auto; padding: 20px 16px; }
.section-title { font-size: 26px; font-weight: 800; text-align: center; margin-bottom: 6px; }
.section-subtitle { text-align: center; color: var(--text-light); margin-bottom: 24px; font-size: 15px; }
.nav-bar { display: flex; justify-content: center; gap: 10px; margin: 20px 0; }
.levels-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.level-card { padding: 18px; cursor: pointer; border: 2px solid transparent; text-align: center; }
.level-card:hover { border-color: var(--primary-light); transform: translateY(-4px); }
.level-card.locked { opacity: 0.5; cursor: not-allowed; filter: grayscale(0.6); }
.level-number { width: 44px; height: 44px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 20px; font-weight: 800; margin: 0 auto 10px; background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: #fff; }
.level-card.cleared .level-number { background: linear-gradient(135deg, var(--success), #55EFC4); }
.level-name { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.level-diff { display: flex; justify-content: center; gap: 2px; margin-top: 6px; }
.star { font-size: 14px; color: #DDD; }
.star.filled { color: var(--accent); }
.lock-icon { position: absolute; top: 10px; right: 10px; font-size: 18px; color: var(--text-light); }

/* ========== Result Modal ========== */
.result-modal .modal { text-align: center; max-width: 480px; }
.result-stars { font-size: 44px; margin: 12px 0; letter-spacing: 6px; }
.result-title { font-size: 26px; font-weight: 800; margin-bottom: 8px; }
.result-title.success { color: var(--success); }
.result-title.fail { color: var(--danger); }
.result-stats { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin: 20px 0; }
.stat-item { background: #F8F9FE; border-radius: var(--radius-sm); padding: 10px; }
.stat-value { font-size: 22px; font-weight: 800; color: var(--primary); }
.stat-label { font-size: 11px; color: var(--text-light); margin-top: 4px; }
.optimal-compare { background: linear-gradient(135deg, #F0EDFF, #E8F8F5); border-radius: var(--radius-sm); padding: 14px; margin: 14px 0; text-align: left; }
.optimal-title { font-weight: 700; color: var(--primary); margin-bottom: 6px; font-size: 14px; }
.optimal-path { font-family: "Consolas", monospace; font-size: 12px; line-height: 1.5; }
.result-actions { display: flex; gap: 8px; margin-top: 16px; }
.result-actions .btn { flex: 1; }

/* ========== Segment Choice Modal ========== */
.segment-modal .modal { text-align: center; max-width: 400px; }
.segment-modal .modal p { margin: 16px 0; font-size: 15px; color: var(--text-light); }

/* ========== 24-Point & Math Gate Modals ========== */
.twentyfour-modal .modal, .mathgate-modal .modal { max-width: 420px; text-align: center; }
.tf-numbers { display: flex; justify-content: center; gap: 14px; margin: 18px 0; }
.tf-number { width: 60px; height: 60px; border-radius: var(--radius-sm); display: flex; align-items: center; justify-content: center; font-size: 26px; font-weight: 800; background: linear-gradient(135deg, var(--accent), #F9CA24); color: var(--text); box-shadow: 0 4px 12px rgba(253,203,110,0.4); }
.mathgate-question { font-size: 36px; font-weight: 800; color: var(--primary); margin: 20px 0; }

/* ========== Leaderboard ========== */
.leaderboard-container { max-width: 800px; margin: 0 auto; padding: 20px 16px; }
.leaderboard-table { width: 100%; border-collapse: separate; border-spacing: 0 6px; }
.leaderboard-table th { padding: 10px 14px; text-align: left; font-size: 12px; color: var(--text-light); font-weight: 600; text-transform: uppercase; }
.leaderboard-table td { padding: 12px 14px; background: var(--card); font-size: 14px; }
.leaderboard-table tr td:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
.leaderboard-table tr td:last-child { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.rank-1 td { background: linear-gradient(135deg, #FFF8E0, #FFFCED); }
.rank-2 td { background: linear-gradient(135deg, #F0F0F8, #F8F8FC); }
.rank-3 td { background: linear-gradient(135deg, #FFF0EB, #FFF8F5); }
.rank-number { font-weight: 800; font-size: 16px; width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; }
.rank-1 .rank-number { background: var(--accent); color: #fff; }
.rank-2 .rank-number { background: #C0C0C0; color: #fff; }
.rank-3 .rank-number { background: #CD7F32; color: #fff; }

/* ========== Animations ========== */
@keyframes fadeInUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
@keyframes bounceIn { 0% { transform: scale(0.5); opacity: 0; } 60% { transform: scale(1.08); } 100% { transform: scale(1); opacity: 1; } }
@keyframes collectPop { 0% { transform: scale(1); } 50% { transform: scale(1.6); } 100% { transform: scale(0); opacity: 0; } }
.animate-fade-in { animation: fadeInUp 0.4s ease forwards; }
.animate-shake { animation: shake 0.4s ease; }
.animate-bounce { animation: bounceIn 0.5s ease; }

/* ========== Tablet ========== */
@media (max-width: 1024px) {
    .game-layout { grid-template-columns: 1fr 340px; gap: 16px; padding: 16px; }
}

/* ========== Mobile Portrait ========== */
@media (max-width: 640px) {
    .hero { padding: 30px 16px 16px; }
    .hero-title { font-size: 30px; }
    .hero-subtitle { font-size: 15px; }
    .user-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
    .user-avatar { width: 52px; height: 52px; font-size: 26px; }
    .user-name { font-size: 14px; }

    .game-header { padding: 10px 12px; flex-wrap: wrap; gap: 6px; }
    .game-logo { font-size: 16px; }
    .level-info-badge { font-size: 11px; padding: 4px 10px; }
    .timer-display { font-size: 15px; padding: 4px 10px; }
    .desktop-only { display: none !important; }

    .game-layout {
        display: flex; flex-direction: column; gap: 12px;
        padding: 12px; min-height: auto;
    }
    .maze-container { width: 100%; }
    #mazeCanvas { width: 100% !important; height: auto !important; }

    .level-description { font-size: 13px; padding: 10px 14px; }

    .instruction-panel .card { padding: 14px; }
    .assembler { padding: 10px; gap: 6px; }
    .step-btn { width: 36px; height: 36px; font-size: 14px; min-width: 36px; min-height: 36px; }
    .asm-label { font-size: 13px; min-width: 38px; }
    #instructionInput { min-height: 100px; font-size: 14px; padding: 12px; }
    .action-btns .btn { padding: 12px 8px; font-size: 15px; }

    .levels-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .level-card { padding: 14px 10px; }
    .level-name { font-size: 14px; }

    .modal { padding: 24px 18px; margin: 0 8px; width: calc(100% - 16px); }
    .modal-overlay { align-items: flex-end; }
    .modal-overlay.active .modal { transform: translateY(0); border-radius: var(--radius) var(--radius) 0 0; }

    .result-stats { grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
    .stat-value { font-size: 18px; }
    .result-stars { font-size: 36px; }
    .result-title { font-size: 22px; }
    .result-actions { flex-direction: column; }
    .result-actions .btn { width: 100%; }

    .leaderboard-table th { padding: 8px 10px; font-size: 11px; }
    .leaderboard-table td { padding: 10px; font-size: 13px; }

    .section-title { font-size: 22px; }
    .section-subtitle { font-size: 13px; }
}
