:root {
    --bg: #f3f6f9;
    --panel: #ffffff;
    --line: #dfe7ef;
    --text: #17324d;
    --muted: #6b7c8f;
    --blue: #2f80d1;
    --blue-strong: #1967d2;
    --green: #22a06b;
    --red: #d93025;
    --amber: #b7791f;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    color: var(--text);
    background: var(--bg);
    font-family: "Microsoft JhengHei", "Segoe UI", Arial, sans-serif;
}

.topbar {
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 28px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 20;
}
.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text);
    font-size: 20px;
    font-weight: 800;
    text-decoration: none;
}
.brand-mark { font-size: 24px; }
.topnav {
    display: flex;
    align-items: center;
    gap: 16px;
}
.topnav a {
    color: var(--text);
    text-decoration: none;
    font-weight: 700;
}
.danger-link, .link-danger {
    color: var(--red) !important;
    background: transparent;
    border: 0;
    cursor: pointer;
    font-weight: 700;
}
.user-chip {
    color: var(--blue-strong);
    background: #e8f0fe;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 13px;
}
.page {
    width: min(1100px, calc(100% - 32px));
    margin: 28px auto 60px;
}
.flash-wrap {
    width: min(1100px, calc(100% - 32px));
    margin: 16px auto 0;
}
.flash, .notice {
    background: #fff8e1;
    color: var(--amber);
    border: 1px solid #f5d78e;
    border-radius: 8px;
    padding: 12px 14px;
}
.hero {
    min-height: calc(100vh - 150px);
    display: grid;
    place-items: center;
    text-align: center;
}
.hero h1 {
    font-size: 44px;
    margin: 0 0 12px;
}
.hero p {
    max-width: 680px;
    color: var(--muted);
    font-size: 18px;
    line-height: 1.7;
}
.hero-actions { margin-top: 24px; }
.panel, .auth-card, .problem-group {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 8px 20px rgba(23, 50, 77, 0.04);
    padding: 26px;
    margin-bottom: 20px;
}
.auth-card {
    max-width: 460px;
    margin: 56px auto;
}
h1, h2, h3 { margin-top: 0; }
.muted, .meta, .panel p {
    color: var(--muted);
}
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 38px;
    padding: 8px 16px;
    border-radius: 7px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    text-decoration: none;
    font-weight: 800;
    cursor: pointer;
}
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.secondary { background: #eef4fb; border-color: #c8d8e8; }
.btn.blue { background: #4f97d7; border-color: #4f97d7; color: #fff; }
.btn.google { width: 100%; margin: 14px 0; background: #fff; }
.btn.ghost { background: #fff; }
.btn.mini { min-height: 28px; padding: 4px 10px; font-size: 12px; }
.stack-form {
    display: grid;
    gap: 14px;
}
.wide-form { gap: 18px; }
label {
    display: grid;
    gap: 6px;
    font-weight: 700;
}
input, select, textarea {
    width: 100%;
    border: 1px solid #cbd6e2;
    border-radius: 7px;
    padding: 10px 12px;
    font: inherit;
    background: #fff;
}
textarea { resize: vertical; }
.filters, .inline-form, .form-row {
    display: flex;
    gap: 14px;
    align-items: end;
    flex-wrap: wrap;
}
.filters label, .form-row label { min-width: 210px; flex: 1; }
.form-row.compact label { max-width: 170px; }
.section-title {
    border-top: 1px solid var(--line);
    padding-top: 18px;
}
.section-title h2 {
    margin: 0 0 6px;
    border-left: 4px solid var(--blue);
    padding-left: 10px;
    font-size: 18px;
}
.section-title p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}
.test-case-grid {
    display: grid;
    gap: 12px;
}
.test-case-card {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfdff;
    padding: 14px;
}
.test-case-card h3 {
    margin: 0 0 10px;
    color: var(--blue);
    font-size: 15px;
}
.file-picker {
    display: inline-grid;
    min-width: 280px;
}
.csv-example {
    display: grid;
    gap: 6px;
    margin-top: 14px;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
    color: var(--muted);
}
.csv-example code {
    color: var(--text);
    background: #eef4fb;
    border-radius: 5px;
    padding: 6px 8px;
}
.import-summary {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin: 18px 0;
}
.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}
.problem-group h2 {
    border-left: 5px solid var(--blue);
    padding-left: 12px;
}
.problem-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
    border: 1px solid var(--line);
    border-radius: 9px;
    padding: 16px 20px;
    margin-top: 12px;
}
.problem-row h3 { margin: 4px 0 8px; }
.score {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 13px;
    font-weight: 800;
}
.score.good { color: #065f46; background: #d8f3e7; border: 1px solid #9fe0c2; }
.score.draft { color: #7a4b00; background: #fff3cd; border: 1px solid #f4d483; }
.score.none { color: #5f6c7b; background: #f3f6f9; border: 1px solid #d8e0e9; }
.empty { text-align: center; padding: 50px; color: var(--muted); }
.two-col {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 20px;
}
.admin-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
}
.admin-card {
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 18px;
    text-decoration: none;
    color: var(--text);
    display: grid;
    gap: 8px;
}
.admin-card span { color: var(--muted); line-height: 1.5; }
table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    overflow: hidden;
    border-radius: 8px;
}
th, td {
    text-align: left;
    padding: 13px 14px;
    border-bottom: 1px solid var(--line);
}
th { background: #eef4fb; color: #244563; }

.editor-page {
    height: calc(100vh - 64px);
    overflow: hidden;
}
.editor-shell {
    height: 100%;
    display: grid;
    grid-template-rows: auto 1fr;
    background: #fff;
}
.editor-toolbar {
    min-height: 58px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--line);
    flex-wrap: wrap;
}
.toolbar-status {
    color: var(--muted);
    font-size: 13px;
    margin-left: auto;
}
.editor-main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 430px;
    min-height: 0;
}
.workspace-pane {
    position: relative;
    min-width: 0;
    border-right: 1px solid var(--line);
    display: grid;
    grid-template-rows: 42px 1fr;
}
.tabs {
    display: flex;
    align-items: stretch;
    border-bottom: 1px solid var(--line);
    background: #f7f9fb;
}
.tab {
    width: 96px;
    border: 0;
    border-right: 1px solid var(--line);
    background: transparent;
    font: inherit;
    cursor: pointer;
}
.tab.active {
    background: #fff;
    font-weight: 800;
}
.run-button {
    margin-left: auto;
    width: 54px;
    border: 0;
    border-left: 1px solid var(--line);
    background: #fff;
    color: #1683f2;
    font-size: 24px;
    cursor: pointer;
}
#blocklyDiv {
    width: 100%;
    height: 100%;
}
#codeDisplay {
    display: none;
    position: absolute;
    top: 42px;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    padding: 20px;
    background: #101820;
    color: #d8f3e7;
    overflow: auto;
}
.problem-side {
    min-height: 0;
    overflow-y: auto;
    padding: 24px;
}
.problem-side h1 {
    text-align: center;
}
.problem-side h2 {
    font-size: 16px;
    margin: 22px 0 10px;
    border-bottom: 2px solid #4f97d7;
    padding-bottom: 8px;
}
.desc, .hint, .example {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 13px;
    line-height: 1.7;
}
.hint { background: #fff8e1; color: #7a4b00; }
.example table {
    border-collapse: collapse;
}
.example th, .example td {
    border: 1px solid #9aaabc;
}
.console-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
#consoleText {
    min-height: 170px;
    background: #171a1f;
    color: #8ff0a4;
    padding: 14px;
    border-radius: 8px;
    overflow: auto;
}

@media (max-width: 900px) {
    .topbar { height: auto; align-items: flex-start; flex-direction: column; padding: 14px; }
    .topnav { flex-wrap: wrap; }
    .two-col, .editor-main { grid-template-columns: 1fr; }
    .editor-page { height: auto; overflow: auto; }
    .workspace-pane { height: 70vh; }
    .problem-side { height: auto; }
}

.admin-body {
    min-height: 100vh;
    background: #f2f5f8;
}
.admin-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 250px;
    background: #2b4053;
    color: #e8f1fa;
    padding: 24px 16px;
    overflow-y: auto;
}
.admin-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #fff;
    text-decoration: none;
    font-size: 21px;
    padding-bottom: 18px;
    border-bottom: 1px solid rgba(255,255,255,.15);
}
.admin-user {
    color: #c6d4e2;
    padding: 16px 4px 22px;
    line-height: 1.6;
}
.admin-user small {
    color: #95b2ca;
}
.admin-menu {
    display: grid;
    gap: 7px;
}
.admin-menu a {
    color: #d8e5f0;
    text-decoration: none;
    border-radius: 8px;
    padding: 13px 14px;
    font-weight: 700;
}
.admin-menu a:hover,
.admin-menu a.active {
    background: #2375df;
    color: #fff;
}
.admin-menu a.logout {
    color: #ff8f7c;
}
.admin-sep {
    height: 1px;
    background: rgba(255,255,255,.16);
    margin: 12px 0;
}
.admin-main {
    margin-left: 250px;
    padding: 34px 34px 80px;
}
.flash-wrap.in-admin {
    width: auto;
    margin: 0 0 18px;
}
.admin-title-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 18px;
}
.filter-card {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
    background: #f8fafc;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 18px;
    margin-top: 18px;
}
.filter-card input,
.filter-card select {
    width: auto;
    min-width: 150px;
}
.filter-card input[name="q"] {
    min-width: 280px;
    flex: 1;
}
.stats-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 20px;
    margin-bottom: 20px;
}
.stat-card {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
    text-align: center;
    padding: 18px;
}
.stat-card span {
    display: block;
    color: var(--muted);
    margin-bottom: 6px;
}
.stat-card strong {
    color: var(--blue-strong);
    font-size: 28px;
}
.stat-card strong.red { color: #e11d48; }
.stat-card strong.green { color: #15803d; }
.role-pill {
    display: inline-flex;
    border-radius: 999px;
    padding: 5px 11px;
    font-size: 13px;
    font-weight: 800;
}
.role-student { background: #dbeafe; color: #1d4ed8; }
.role-teacher { background: #dcfce7; color: #15803d; }
.role-school_admin { background: #fef3c7; color: #92400e; }
.role-super_admin { background: #fee2e2; color: #b91c1c; }
.table-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}
.seat {
    background: #f1f5f9;
    border-radius: 5px;
    padding: 4px 8px;
    font-weight: 800;
}
.ok-score { color: #15803d; font-weight: 900; }
.bad-score { color: #dc2626; font-weight: 900; }
.report-box {
    background: #111827;
    color: #d1fae5;
    padding: 16px;
    border-radius: 9px;
    white-space: pre-wrap;
    overflow: auto;
}
.xml-view {
    min-height: 260px;
    font-family: Consolas, monospace;
}
.student-blockly-view {
    width: 100%;
    height: 560px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    overflow: hidden;
}
.blockly-load-error {
    margin-top: 12px;
}
.raw-xml-details {
    margin-top: 20px;
}
.raw-xml-details summary {
    cursor: pointer;
    color: var(--muted);
    font-weight: 800;
    margin-bottom: 10px;
}
.import-preview-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 16px;
    margin-bottom: 18px;
}
.preview-box {
    border: 1px solid var(--line);
    background: #f8fafc;
    border-radius: 9px;
    padding: 14px;
    font-weight: 800;
}
.import-example {
    overflow-x: auto;
    max-height: 420px;
}
.check-line {
    display: flex;
    align-items: center;
    gap: 8px;
}
.check-line input {
    width: auto;
}

@media (max-width: 900px) {
    .admin-sidebar {
        position: static;
        width: auto;
    }
    .admin-main {
        margin-left: 0;
        padding: 18px;
    }
    .stats-row {
        grid-template-columns: 1fr;
    }
    .import-preview-grid {
        grid-template-columns: 1fr;
    }
}
