:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f3f5f8;
  --border: #e0e4ea;
  --text: #1e2733;
  --muted: #6b7684;
  --primary: #3f7bfd;
  --primary-2: #2f63e0;
  --green: #1f9d5a;
  --red: #e5484d;
  --amber: #ef8c00;
  --flame: #ff7a1a;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16,24,40,.06), 0 1px 2px rgba(16,24,40,.04);
  --shadow-lg: 0 6px 20px rgba(16,24,40,.10);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, "PingFang TC", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 22px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 800; font-size: 18px; letter-spacing: .2px; }
.login-btn { margin-left: auto; background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 9px; padding: 7px 18px; font-size: 14px; font-weight: 600; cursor: pointer; }
.login-btn:hover { background: var(--border); }

/* Tabs */
.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  background: none; border: none; color: var(--muted); padding: 13px 16px;
  font-size: 15px; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 700; }
.badge { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 0 8px; font-size: 12px; color: var(--muted); }

main { max-width: 1120px; margin: 0 auto; padding: 22px; }
.panel { display: none; }
.panel.active { display: block; }

/* ---- HOME / gamification ---- */
.home-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.hello { font-size: 20px; font-weight: 800; }
.streak { display: inline-flex; align-items: center; gap: 6px; background: #fff3e9; border: 1px solid #ffd9bd; color: var(--flame); font-weight: 800; padding: 6px 12px; border-radius: 999px; font-size: 15px; }
.streak .num { font-size: 17px; }
.home-grid { display: grid; grid-template-columns: 300px 1fr; gap: 18px; }
@media (max-width: 820px) { .home-grid { grid-template-columns: 1fr; } }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.card h3 { margin: 0 0 14px; font-size: 15px; }

/* XP ring */
.ring-wrap { display: flex; flex-direction: column; align-items: center; text-align: center; }
.ring { position: relative; width: 150px; height: 150px; }
.ring svg { transform: rotate(-90deg); }
.ring .track { fill: none; stroke: var(--surface-2); stroke-width: 12; }
.ring .fill { fill: none; stroke: var(--primary); stroke-width: 12; stroke-linecap: round; transition: stroke-dashoffset .6s ease; }
.ring .center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring .lv-num { font-size: 34px; font-weight: 800; line-height: 1; }
.ring .lv-lbl { font-size: 12px; color: var(--muted); }
.tier-badge { margin-top: 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 5px 14px; font-size: 13px; font-weight: 700; color: var(--primary); }
.xp-line { margin-top: 8px; font-size: 12px; color: var(--muted); }

/* Today tasks */
.tasks { display: flex; flex-direction: column; gap: 10px; }
.task-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 11px; background: var(--surface); }
.task-row.done { background: #edf9f1; border-color: #bfe6cd; }
.task-check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--border); flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 14px; color: #fff; }
.task-row.done .task-check { background: var(--green); border-color: var(--green); }
.task-body { flex: 1; }
.task-title { font-weight: 700; font-size: 15px; }
.task-sub { font-size: 12.5px; color: var(--muted); }
.task-xp { font-size: 12px; font-weight: 700; color: var(--amber); }
.task-go { background: var(--primary); color: #fff; border: none; border-radius: 8px; padding: 7px 14px; font-size: 13px; font-weight: 700; cursor: pointer; }
.task-go:hover { background: var(--primary-2); }
.daily-bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: 4px 0 16px; }
.daily-bar > span { display: block; height: 100%; background: var(--green); transition: width .5s ease; }
.daily-done { text-align: center; background: #edf9f1; border: 1px solid #bfe6cd; color: var(--green); font-weight: 800; padding: 12px; border-radius: 11px; }

.goal-card .goal-row { display: flex; justify-content: space-between; font-size: 14px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.goal-card .goal-row:last-child { border-bottom: none; }
.goal-card .goal-row b { color: var(--text); }
.link-btn { background: none; border: none; color: var(--primary); cursor: pointer; font-size: 13px; padding: 0; }

/* ---- shared inputs / layout ---- */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }
.field-label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.input, select.input {
  width: 100%; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 11px;
  padding: 12px; font-size: 15px; font-family: inherit; resize: vertical;
}
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(63,123,253,.12); }
.essay { min-height: 260px; line-height: 1.7; }
.row { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.spacer { flex: 1; }
.wordcount { color: var(--muted); font-size: 13px; }
.btn { border: none; border-radius: 10px; padding: 10px 18px; font-size: 15px; cursor: pointer; font-weight: 700; }
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover { background: var(--primary-2); }
.btn.ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--border); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.result { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-height: 300px; }
.result.empty { display: flex; align-items: center; justify-content: center; }
.placeholder { text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.scores { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-bottom: 16px; }
.score-card { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 6px; text-align: center; }
.score-card .lbl { font-size: 11px; color: var(--muted); }
.score-card .val { font-size: 22px; font-weight: 800; }
.score-card.overall { background: linear-gradient(135deg, var(--primary-2), var(--primary)); border: none; }
.score-card.overall .lbl, .score-card.overall .val { color: white; }

.result h4 { margin: 18px 0 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.summary { background: var(--surface-2); border-left: 3px solid var(--primary); padding: 10px 14px; border-radius: 8px; }
.correction { border-bottom: 1px solid var(--border); padding: 10px 0; }
.correction .type-tag, .err-item .type-tag { display: inline-block; font-size: 11px; background: #fff5e6; color: var(--amber); border: 1px solid #ffe2b3; border-radius: 6px; padding: 1px 8px; margin-bottom: 4px; }
.del { color: var(--red); text-decoration: line-through; }
.ins { color: var(--green); }
.correction .explain { font-size: 13px; color: var(--muted); margin-top: 4px; }
.upgraded { background: var(--surface-2); border-radius: 8px; padding: 14px; line-height: 1.8; }
.alt { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.alt .plain { color: var(--muted); } .alt .up { color: var(--green); }
.alt .note { font-size: 12px; color: var(--muted); }

.err-list { margin-top: 16px; display: flex; flex-direction: column; gap: 8px; }
.err-item { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); padding: 12px 14px; display: flex; gap: 12px; align-items: flex-start; }
.err-item .body { flex: 1; }
.err-item .fix { font-size: 15px; }
.err-item .note { font-size: 13px; color: var(--muted); }
.err-item .count { font-size: 12px; color: var(--amber); }
.panel-head h2 { margin: 0 0 4px; }
.empty-state { text-align: center; color: var(--muted); padding: 40px; }

.drill-box, .typing-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; max-width: 760px; }
.timer { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--primary); }
.timer.warn { color: var(--amber); } .timer.danger { color: var(--red); }
.drill-topic { background: var(--surface-2); padding: 14px; border-radius: 8px; margin: 12px 0; line-height: 1.7; }
.typing-target { background: var(--surface-2); padding: 16px; border-radius: 8px; font-size: 17px; line-height: 1.9; letter-spacing: .3px; }
.typing-target .ok { color: var(--green); } .typing-target .bad { color: var(--red); background: rgba(229,72,77,.14); } .typing-target .cur { background: var(--primary); color: white; border-radius: 2px; }
.typing-stats b { color: var(--primary); font-size: 18px; }

/* Modal + toast */
.modal { position: fixed; inset: 0; background: rgba(30,39,51,.45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal.hidden { display: none; }
.modal-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 26px; width: 100%; max-width: 460px; }
.modal-card h3 { margin: 0 0 6px; font-size: 20px; }
.modal-card .sub { color: var(--muted); font-size: 14px; margin: 0 0 16px; }
.ob-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: #fff; padding: 12px 20px; border-radius: 10px; z-index: 60; box-shadow: var(--shadow-lg); font-weight: 600; }
.toast.hidden { display: none; }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { text-align: center; color: var(--muted); padding: 60px 20px; }
.levelup { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 70; pointer-events: none; }
.levelup.hidden { display: none; }
.levelup .pop { background: var(--surface); border-radius: 18px; box-shadow: var(--shadow-lg); padding: 30px 44px; text-align: center; animation: pop .4s ease; }
.levelup .pop .big { font-size: 40px; font-weight: 900; color: var(--primary); }
@keyframes pop { from { transform: scale(.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* utility + auth + admin */
.hidden { display: none !important; }
.auth { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; z-index: 80; padding: 18px; }
.auth-card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-lg); padding: 30px; width: 100%; max-width: 400px; }
.auth-brand { font-size: 22px; font-weight: 800; text-align: center; }
.auth-sub { color: var(--muted); font-size: 14px; text-align: center; margin: 8px 0 18px; }
.auth-err { color: var(--red); font-size: 13px; min-height: 18px; margin-top: 8px; text-align: center; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table th, .admin-table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.admin-table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.admin-table tbody tr:hover { background: var(--surface-2); }

/* ---- result readability ---- */
.result h4 { margin: 22px 0 10px; padding-top: 16px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.legend { text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--muted); font-size: 12px; }
.chg { background: #fff3bf; border-radius: 3px; padding: 0 2px; }
.upgraded { line-height: 1.85; }
.upgraded p { margin: 0 0 12px; }
.upgraded p:last-child { margin-bottom: 0; }
.correction { padding: 12px 0; }

/* ---- writing timer bar ---- */
.timer-bar { display: flex; align-items: center; gap: 10px; margin: 12px 0 2px; padding: 8px 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; }
.tb-label { font-size: 13px; color: var(--muted); }
.write-timer { margin-left: auto; font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--primary); }
.write-timer.warn { color: var(--amber); } .write-timer.danger { color: var(--red); }
.tb-btn { padding: 6px 14px; font-size: 13px; }

/* ---- drill focus selector ---- */
.focus-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.focus-btn { background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); border-radius: 999px; padding: 7px 16px; font-size: 14px; font-weight: 600; cursor: pointer; }
.focus-btn:hover { background: var(--border); }
.focus-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.tasktype-row { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.tasktype-btn { border: none; background: transparent; color: var(--muted); padding: 7px 16px; border-radius: 8px; font-size: 14px; font-weight: 700; cursor: pointer; }
.tasktype-btn.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }
.tasktype-btn:disabled { opacity: .5; cursor: not-allowed; }

/* Task 1 圖表 */
.task-figure { margin: 4px 0 12px; padding: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow-x: auto; }
.task-figure svg { display: block; max-width: 100%; }
.fig-title { text-align: center; font-size: 14px; font-weight: 600; color: var(--text); margin-bottom: 10px; }
.t1table { width: 100%; border-collapse: collapse; font-size: 14px; }
.t1table th, .t1table td { border: 1px solid var(--border); padding: 8px 12px; text-align: center; }
.t1table thead th { background: var(--surface-2); font-weight: 700; }
.t1table th.rh { text-align: left; background: var(--surface-2); font-weight: 600; }

@media (max-width: 600px) {
  .topbar { padding: 10px 14px; gap: 10px; }
  .brand { font-size: 16px; }
  .tabs { padding: 0 6px; }
  .tab { padding: 11px 10px; font-size: 14px; }
  main { padding: 14px; }
  .card, .drill-box, .typing-box, .result { padding: 14px; }
  .scores { gap: 5px; }
  .score-card { padding: 8px 3px; }
  .score-card .val { font-size: 18px; }
  .score-card .lbl { font-size: 10px; }
  .hello { font-size: 17px; }
  .ring { width: 130px; height: 130px; }
  .modal-card { padding: 20px; }
  .ob-grid { grid-template-columns: 1fr; }
  .timer-bar { flex-wrap: wrap; }
  .tasktype-row, .tasktype-btn { width: auto; }
  .focus-btn { padding: 6px 12px; font-size: 13px; }
  .admin-table { font-size: 12px; }
}

.drill-formula { background: #eef4ff; border: 1px solid #cfe0ff; color: #2f4d80; border-radius: 10px; padding: 10px 14px; font-size: 13.5px; line-height: 1.6; margin-bottom: 12px; }
.drill-formula:empty { display: none; }

.stat-row { display: flex; gap: 10px; }
.stat { flex: 1; background: var(--surface-2); border-radius: 10px; padding: 12px 4px; text-align: center; }
.stat-v { font-size: 22px; font-weight: 800; }
.stat-l { font-size: 12px; color: var(--muted); }
@media (max-width: 600px) { .stat-v { font-size: 18px; } }
.hist-head { display: flex; flex-direction: column; gap: 3px; }
.hist-body { margin-top: 12px; border-top: 1px solid var(--border); padding-top: 6px; }

/* ---- error review card ---- */
.quiz-wrong { font-size: 20px; font-weight: 700; color: var(--red); background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin: 6px 0 8px; text-align: center; }
.quiz-answer { background: #edf9f1; border: 1px solid #bfe6cd; border-radius: 10px; padding: 12px 14px; margin: 10px 0; font-size: 16px; }
