:root {
  --bg: #f8fbff;
  --surface: #f1f7ff;
  --surface-strong: #dcebff;
  --panel: #ffffff;
  --text: #102033;
  --muted: #607086;
  --border: #d5e3f5;
  --teal: #2563eb;
  --teal-dark: #1d4ed8;
  --coral: #c93235;
  --amber: #c78312;
  --green: #25875a;
  --shadow: 0 18px 42px rgba(20, 55, 98, 0.1);
  --shadow-strong: 0 24px 70px rgba(20, 55, 98, 0.16);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  background:
    linear-gradient(180deg, rgba(220, 235, 255, 0.72), rgba(248, 251, 255, 0) 340px),
    var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Heiti TC", Arial, sans-serif;
  letter-spacing: 0;
}

body:not(.is-authenticated) .app-shell {
  display: none;
}

body.is-authenticated .login-screen {
  display: none;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.22);
  outline-offset: 2px;
}

.app-shell {
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(220, 235, 255, 0.82), rgba(255, 255, 255, 0) 280px),
    linear-gradient(90deg, rgba(37, 99, 235, 0.035), rgba(255, 255, 255, 0) 24%),
    var(--bg);
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.1) 0 32%, rgba(255, 255, 255, 0) 32%),
    linear-gradient(180deg, rgba(220, 235, 255, 0.88), rgba(255, 255, 255, 0) 360px),
    #fff;
}

.login-panel {
  width: min(440px, 100%);
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.98);
  box-shadow: var(--shadow-strong);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.login-brand h1,
.login-brand p,
.login-message {
  margin: 0;
}

.login-brand h1 {
  font-size: 22px;
  line-height: 1.2;
  color: var(--text);
}

.login-brand p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.login-tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  margin-bottom: 22px;
}

.login-tab {
  min-height: 46px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fbff;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.9);
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

.login-tab:hover {
  transform: translateY(-1px);
  border-color: rgba(37, 99, 235, 0.38);
  box-shadow: 0 10px 20px rgba(20, 55, 98, 0.08);
}

.login-tab.is-active {
  background: #fff;
  border-color: rgba(37, 99, 235, 0.48);
  color: var(--teal-dark);
  box-shadow: 0 10px 22px rgba(20, 55, 98, 0.12);
}

.login-form,
.guest-login-pane {
  display: grid;
  gap: 16px;
}

.login-form[hidden],
.guest-login-pane[hidden] {
  display: none;
}

.login-field {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.login-field input {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 12px;
  color: var(--text);
  background: #fff;
}

.login-submit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  border-radius: var(--radius);
  background: linear-gradient(180deg, #3b82f6, var(--teal-dark));
  color: #fff;
  cursor: pointer;
  font-size: 15px;
  font-weight: 900;
  box-shadow: 0 12px 24px rgba(29, 78, 216, 0.2);
}

.login-submit svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.login-message {
  min-height: 20px;
  margin-top: 12px;
  color: var(--coral);
  font-size: 13px;
  font-weight: 800;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 18px 26px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: 0 8px 24px rgba(20, 55, 98, 0.05);
}

.topbar::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--teal-dark), var(--teal), #a9c7ff);
  pointer-events: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 220px;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, var(--teal-dark), var(--teal));
  color: #fff;
  font-weight: 800;
  font-size: 22px;
  box-shadow: 0 10px 18px rgba(29, 78, 216, 0.18);
}

.brand h1,
.brand p,
.section-heading h2,
.practice-header h2,
.practice-header p {
  margin: 0;
}

.brand h1 {
  font-size: 20px;
  line-height: 1.15;
}

.brand p {
  color: var(--muted);
  font-size: 13px;
  margin-top: 3px;
}

.session-strip {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.user-strip {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  min-width: 180px;
}

.user-strip > span {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 14px;
  font-weight: 900;
}

.segmented {
  display: inline-flex;
  align-items: center;
  padding: 3px;
  gap: 2px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: var(--radius);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
}

.segment {
  min-height: 34px;
  padding: 0 13px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
}

.segment.is-active {
  background: linear-gradient(180deg, #3b82f6, var(--teal-dark));
  color: #fff;
  box-shadow: 0 6px 14px rgba(29, 78, 216, 0.16);
}

.mini-field {
  display: grid;
  grid-template-columns: auto 74px;
  align-items: center;
  gap: 8px;
  height: 42px;
  padding-left: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.mini-field input {
  width: 74px;
  height: 40px;
  border: 0;
  border-left: 1px solid var(--border);
  padding: 0 10px;
  color: var(--text);
  background: transparent;
  font-weight: 800;
}

.login-field input:focus,
.preset-field select:focus,
.source-box:focus,
.typing-input:focus,
.url-field input:focus,
.mini-field:focus-within {
  border-color: rgba(37, 99, 235, 0.55);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.08);
}

.command,
.wide-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.86);
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

.command svg,
.wide-button svg,
.file-drop svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
  flex: 0 0 auto;
}

.command:hover,
.wide-button:hover,
.file-drop:hover {
  transform: translateY(-1px);
  border-color: rgba(37, 99, 235, 0.42);
  box-shadow: 0 10px 20px rgba(20, 55, 98, 0.08);
}

.command:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  transform: none;
}

.command.primary,
.wide-button.accent {
  background: linear-gradient(180deg, #3b82f6, var(--teal-dark));
  color: #fff;
  border-color: var(--teal-dark);
  box-shadow: 0 12px 22px rgba(29, 78, 216, 0.18);
}

.command.ghost {
  color: var(--teal-dark);
  background: #f8fbff;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(420px, 1fr) minmax(250px, 300px);
  gap: 18px;
  padding: 18px 26px 26px;
  align-items: start;
}

.workspace > * {
  min-width: 0;
}

.panel,
.practice-panel {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.panel {
  overflow: hidden;
}

.panel-section {
  padding: 18px;
  border-bottom: 1px solid var(--border);
}

.panel-section:last-child {
  border-bottom: 0;
}

.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.section-heading h2 {
  font-size: 16px;
  line-height: 1.2;
  color: var(--teal-dark);
}

.section-heading span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #eef6ff;
  margin-bottom: 12px;
}

.preset-box {
  display: grid;
  gap: 10px;
  padding: 12px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background:
    linear-gradient(180deg, rgba(220, 235, 255, 0.72), rgba(255, 255, 255, 0.72)),
    var(--surface);
}

.preset-box .muted-line {
  margin-top: 0;
}

.preset-field {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.preset-field select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 12px;
  background: #fff;
  color: var(--text);
  font-weight: 800;
}

.wide-button.compact {
  margin-top: 0;
}

.tab {
  min-height: 34px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  font-weight: 800;
}

.tab.is-active {
  background: #fff;
  color: var(--teal-dark);
  box-shadow: 0 1px 8px rgba(20, 55, 98, 0.12);
}

.tab-pane {
  display: none;
}

.tab-pane.is-active {
  display: block;
}

.source-box,
.typing-input {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
}

.source-box {
  min-height: 170px;
  padding: 12px;
  line-height: 1.7;
  font-size: 15px;
}

.wide-button {
  width: 100%;
  margin-top: 10px;
}

.file-drop {
  display: grid;
  place-items: center;
  gap: 8px;
  min-height: 144px;
  border: 1px dashed rgba(37, 99, 235, 0.44);
  border-radius: var(--radius);
  background: #f0f7ff;
  color: var(--teal-dark);
  cursor: pointer;
  text-align: center;
  font-weight: 800;
}

.file-drop input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.file-drop svg {
  width: 30px;
  height: 30px;
}

.muted-line {
  min-height: 20px;
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.url-field {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.url-field input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 12px;
  color: var(--text);
}

.range-row {
  display: grid;
  grid-template-columns: 74px 1fr 48px;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.range-row output {
  text-align: right;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  accent-color: var(--teal);
}

.practice-panel {
  min-height: 0;
  height: calc(100vh - 190px);
  display: grid;
  grid-template-rows: auto minmax(160px, 1fr) auto;
  overflow: hidden;
}

.practice-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  background:
    linear-gradient(180deg, #fff, #f8fbff);
}

.practice-header h2 {
  font-size: 18px;
  line-height: 1.3;
  color: var(--teal-dark);
}

.practice-header p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.legend-dot {
  width: 9px;
  height: 9px;
  border-radius: 99px;
  display: inline-block;
}

.legend-dot.correct {
  background: var(--green);
}

.legend-dot.wrong {
  background: var(--coral);
}

.legend-dot.missing {
  background: var(--amber);
}

.passage {
  padding: 22px 24px;
  overflow: auto;
  line-height: 2;
  font-size: 24px;
  background:
    linear-gradient(90deg, rgba(37, 99, 235, 0.055) 0, rgba(37, 99, 235, 0) 42px),
    #fff;
  overflow-wrap: anywhere;
  word-break: break-all;
  scrollbar-color: rgba(37, 99, 235, 0.42) #eaf3ff;
}

.char {
  display: inline;
  border-radius: 4px;
  padding: 1px 1px 2px;
  transition: background 120ms ease, color 120ms ease, box-shadow 120ms ease;
}

.char.correct {
  color: #1d6f4a;
  background: rgba(79, 157, 102, 0.14);
}

.char.wrong {
  color: #9f2e25;
  background: rgba(217, 87, 74, 0.18);
  box-shadow: inset 0 -2px 0 rgba(217, 87, 74, 0.55);
}

.char.missing {
  color: #8a5d0a;
  background: rgba(215, 155, 36, 0.18);
  box-shadow: inset 0 -2px 0 rgba(215, 155, 36, 0.62);
}

.char.pending {
  color: #263235;
}

.char.current {
  background: rgba(37, 99, 235, 0.13);
  box-shadow: inset 0 -2px 0 var(--teal);
}

.extra-char {
  display: inline-block;
  margin: 0 1px;
  padding: 0 4px;
  min-width: 16px;
  border-radius: 4px;
  background: rgba(217, 87, 74, 0.14);
  color: #a83229;
  font-size: 0.72em;
  line-height: 1.45;
  text-decoration: line-through;
  vertical-align: 10%;
}

.input-wrap {
  display: grid;
  gap: 8px;
  padding: 16px 20px 20px;
  border-top: 1px solid var(--border);
  background:
    linear-gradient(180deg, #f8fbff, var(--surface));
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.typing-input {
  min-height: 124px;
  max-height: 30vh;
  padding: 14px;
  line-height: 1.8;
  font-size: 22px;
}

.typing-input:disabled {
  background: #f3f8ff;
  color: #6b7c92;
}

.stat-hero,
.stat-block {
  padding: 18px;
  border-bottom: 1px solid var(--border);
}

.stat-hero {
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.12), rgba(255, 255, 255, 0.78)),
    var(--surface-strong);
}

.stat-hero span,
.stat-title span,
.score-grid span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.stat-hero strong {
  display: block;
  margin: 8px 0 12px;
  font-size: 42px;
  line-height: 1;
  color: var(--teal-dark);
  font-variant-numeric: tabular-nums;
}

.stat-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.stat-title strong {
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

.bar {
  height: 10px;
  border-radius: 99px;
  background: #dbeafe;
  overflow: hidden;
}

.bar i {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: var(--teal);
  transition: width 180ms ease;
}

#timeBar {
  background: var(--amber);
}

#accuracyBar {
  background: var(--green);
}

#progressBar {
  background: var(--teal);
}

#speedBar {
  background: linear-gradient(90deg, var(--teal-dark), var(--teal), var(--amber));
}

.score-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

.score-grid div {
  min-height: 86px;
  padding: 16px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.score-grid div:nth-child(2n) {
  border-right: 0;
}

.score-grid div:nth-last-child(-n + 2) {
  border-bottom: 0;
}

.score-grid strong {
  display: block;
  margin-top: 7px;
  font-size: 26px;
  color: var(--teal-dark);
  font-variant-numeric: tabular-nums;
}

.result-modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.56);
  backdrop-filter: blur(8px);
  z-index: 30;
}

.result-modal[hidden] {
  display: none;
}

.result-dialog {
  width: min(980px, 100%);
  max-height: calc(100vh - 48px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  border: 1px solid rgba(213, 227, 245, 0.9);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow-strong);
  overflow: hidden;
}

.result-dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #fff, #f8fbff);
}

.result-dialog-header h2,
.result-dialog-header p {
  margin: 0;
}

.result-dialog-header h2 {
  color: var(--teal-dark);
  font-size: 20px;
  line-height: 1.25;
}

.result-dialog-header p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.icon-command {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--teal-dark);
  cursor: pointer;
}

.icon-command svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.result-canvas-wrap {
  min-height: 0;
  padding: 18px;
  overflow: auto;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.08), rgba(255, 255, 255, 0) 45%),
    #f8fbff;
}

.result-canvas-wrap canvas {
  display: block;
  width: min(100%, 860px);
  height: auto;
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow);
}

.result-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  background: #fff;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 16px);
  min-width: 240px;
  max-width: min(520px, calc(100vw - 32px));
  padding: 12px 16px;
  border-radius: var(--radius);
  background: #0f172a;
  color: #fff;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  text-align: center;
  font-size: 14px;
  font-weight: 800;
  z-index: 20;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 1180px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .session-strip {
    justify-content: flex-start;
  }

  .user-strip {
    justify-content: flex-start;
  }

  .workspace {
    grid-template-columns: minmax(260px, 330px) minmax(420px, 1fr);
  }

  .stats-panel {
    grid-column: 1 / -1;
  }

  .score-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .score-grid div,
  .score-grid div:nth-child(2n),
  .score-grid div:nth-last-child(-n + 2) {
    border-right: 1px solid var(--border);
    border-bottom: 0;
  }

  .score-grid div:last-child {
    border-right: 0;
  }
}

@media (max-width: 820px) {
  .topbar,
  .workspace {
    padding-left: 14px;
    padding-right: 14px;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .practice-panel {
    height: auto;
    min-height: auto;
    grid-template-rows: auto minmax(220px, 45vh) auto;
  }

  .practice-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .legend {
    justify-content: flex-start;
  }

  .session-strip {
    width: 100%;
  }

  .user-strip {
    width: 100%;
  }

  .segmented {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  .command {
    flex: 1 1 120px;
  }

  .mini-field {
    width: 100%;
    grid-template-columns: 1fr 86px;
  }

  .passage {
    max-height: 45vh;
    padding: 18px;
  }

  .score-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .score-grid div,
  .score-grid div:nth-child(2n),
  .score-grid div:nth-last-child(-n + 2) {
    border-bottom: 1px solid var(--border);
  }

  .score-grid div:nth-child(2n) {
    border-right: 0;
  }

  .score-grid div:nth-last-child(-n + 2) {
    border-bottom: 0;
  }

  .result-modal {
    padding: 12px;
  }

  .result-dialog {
    max-height: calc(100vh - 24px);
  }

  .result-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .result-actions .command {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
