:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-strong: #eef2f7;
  --text: #1d2733;
  --muted: #667085;
  --line: #d9e0ea;
  --initial: #b42318;
  --final: #175cd3;
  --accent: #147d73;
  --accent-soft: #d9f3ef;
  --warn: #b54708;
  --bad: #b42318;
  --shadow: 0 18px 45px rgba(29, 39, 51, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Microsoft YaHei", sans-serif;
}

button,
select {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0 34px;
  display: grid;
  grid-template-columns: minmax(360px, 0.9fr) minmax(520px, 1.1fr);
  gap: 22px;
  align-items: start;
}

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

.rotate-hint {
  display: none;
}

.practice,
.keyboard-section {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.practice {
  padding: 22px;
}

.keyboard-section {
  padding: 22px 18px 18px;
}

.topbar {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
}

.topbar > div {
  min-width: 0;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: 0;
}

h2 {
  font-size: 20px;
  line-height: 1.2;
  letter-spacing: 0;
}

.topbar p,
.section-title p,
.message,
.legend,
label {
  color: var(--muted);
}

.topbar p,
.section-title p {
  margin-top: 7px;
  font-size: 14px;
}

.ghost-button {
  flex: 0 0 auto;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.ghost-button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 22px;
}

label {
  display: grid;
  gap: 7px;
  font-size: 13px;
  min-width: 0;
}

select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  padding: 0 11px;
}

.target-panel {
  margin-top: 18px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.queue {
  min-width: 0;
  min-height: 54px;
  padding: 10px 12px;
  display: flex;
  gap: 7px;
  align-items: center;
  overflow: hidden;
  background: var(--surface-strong);
}

.queue-item {
  width: 34px;
  height: 34px;
  display: inline-grid;
  place-items: center;
  border-radius: 6px;
  border: 1px solid transparent;
  background: var(--surface);
  color: var(--muted);
  font-size: 18px;
}

.queue-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 700;
}

.target-main {
  min-height: 220px;
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 20px;
  align-items: center;
  padding: 24px;
}

.character {
  width: 136px;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #182230;
  color: #fff;
  font-size: 76px;
  font-weight: 800;
  line-height: 1;
}

.target-info {
  min-width: 0;
}

.pinyin {
  font-size: 38px;
  line-height: 1.1;
  font-weight: 800;
  letter-spacing: 0;
}

.typed {
  min-height: 56px;
  margin-top: 18px;
  display: flex;
  gap: 10px;
}

.typed span {
  width: 48px;
  height: 52px;
  display: grid;
  place-items: center;
  border: 2px solid var(--line);
  border-radius: 7px;
  font-size: 24px;
  font-weight: 800;
  text-transform: uppercase;
  background: var(--surface);
}

.typed span.filled {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.typed span.wrong {
  border-color: var(--bad);
  background: #fee4e2;
  color: var(--bad);
}

.message {
  min-height: 42px;
  padding: 11px 14px;
  border-top: 1px solid var(--line);
  font-size: 14px;
}

.message.error {
  color: var(--bad);
}

.message.success {
  color: var(--accent);
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
}

.stats div {
  min-height: 74px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.stats span {
  font-size: 25px;
  line-height: 1;
  font-weight: 800;
}

.stats small {
  color: var(--muted);
  font-size: 12px;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
  margin-bottom: 16px;
}

.keyboard {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.key-row {
  display: flex;
  gap: 8px;
  min-width: 0;
}

.key-row:nth-child(2) {
  padding-left: 28px;
}

.key-row:nth-child(3) {
  padding-left: 70px;
}

.key {
  position: relative;
  width: 58px;
  min-width: 0;
  min-height: 84px;
  padding: 8px 7px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 5px;
  text-align: left;
  transition:
    transform 120ms ease,
    border-color 120ms ease,
    background 120ms ease;
}

.key:active {
  transform: translateY(1px);
}

.key:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.key.next {
  border-color: var(--accent);
  background: var(--accent-soft);
  transform: translateY(-2px);
}

.key.hit {
  border-color: var(--accent);
  background: #ccfbef;
}

.key.miss {
  border-color: var(--bad);
  background: #fee4e2;
}

.letter {
  font-size: 18px;
  line-height: 1;
  font-weight: 900;
  text-transform: uppercase;
}

.maps {
  display: grid;
  gap: 3px;
  align-content: end;
  min-width: 0;
}

.map-line {
  min-height: 16px;
  font-size: 12px;
  line-height: 1.15;
  white-space: normal;
  overflow-wrap: anywhere;
}

.map-line.initial {
  color: var(--initial);
  font-weight: 700;
}

.map-line.final {
  color: var(--final);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 16px;
  font-size: 13px;
}

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

.dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  display: inline-block;
}

.initial-dot {
  background: var(--initial);
}

.final-dot {
  background: var(--final);
}

.next-dot {
  background: var(--accent);
}

@media (max-width: 980px) {
  .app {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) {
  .app {
    width: 100%;
    padding: 0 0 14px;
    gap: 8px;
  }

  .practice,
  .keyboard-section {
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    box-shadow: none;
    padding: 14px 12px;
  }

  .topbar {
    display: grid;
    align-items: start;
    gap: 10px;
  }

  h1 {
    font-size: 22px;
  }

  .topbar p,
  .section-title p {
    font-size: 13px;
  }

  .ghost-button {
    width: 100%;
    min-height: 36px;
    padding: 0 12px;
    white-space: nowrap;
  }

  .toolbar,
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .toolbar {
    margin-top: 14px;
    gap: 10px;
  }

  select {
    min-height: 36px;
    min-width: 0;
  }

  .target-panel {
    margin-top: 12px;
  }

  .queue {
    min-height: 44px;
    padding: 7px 8px;
    gap: 6px;
  }

  .queue-item {
    width: 30px;
    height: 30px;
    font-size: 17px;
  }

  .target-main {
    grid-template-columns: 108px 1fr;
    min-height: 136px;
    padding: 14px 16px;
    gap: 14px;
  }

  .character {
    width: 104px;
    font-size: 58px;
  }

  .pinyin {
    font-size: 31px;
  }

  .typed {
    min-height: 44px;
    margin-top: 14px;
    gap: 8px;
  }

  .typed span {
    width: 42px;
    height: 44px;
    font-size: 21px;
  }

  .message {
    min-height: 36px;
    padding: 9px 12px;
    font-size: 13px;
  }

  .stats {
    order: 3;
    gap: 8px;
    margin-top: 10px;
  }

  .stats div {
    min-height: 58px;
  }

  .stats span {
    font-size: 22px;
  }

  .keyboard-section {
    order: 2;
    padding-top: 12px;
  }

  .section-title {
    display: grid;
    gap: 4px;
    margin-bottom: 10px;
  }

  h2 {
    font-size: 18px;
  }

  .keyboard {
    gap: 6px;
  }

  .key-row {
    display: flex;
    gap: 3px;
  }

  .key-row:nth-child(1) {
    padding-left: 0;
  }

  .key-row:nth-child(2) {
    padding-left: calc((100% - 27px) / 20);
  }

  .key-row:nth-child(3) {
    padding-left: calc((100% - 27px) / 10 * 1.5);
    padding-right: 0;
  }

  .key {
    width: calc((100% - 27px) / 10);
    min-height: 50px;
    padding: 4px 3px;
    gap: 2px;
  }

  .letter {
    font-size: 16px;
  }

  .maps {
    gap: 2px;
  }

  .map-line {
    min-height: 11px;
    font-size: 8.5px;
    line-height: 1.05;
    word-break: break-all;
  }

  .legend {
    margin-top: 10px;
    gap: 12px;
    font-size: 12px;
  }
}

@media (max-width: 560px) and (orientation: portrait) {
  .rotate-hint {
    display: block;
    margin: 0;
    padding: 8px 12px;
    background: var(--accent-soft);
    color: var(--accent);
    font-size: 13px;
    font-weight: 700;
  }
}

@media (max-width: 920px) and (orientation: landscape) {
  body {
    overflow: hidden;
  }

  .rotate-hint {
    display: none;
  }

  .app {
    width: 100vw;
    height: 100dvh;
    padding: 6px 8px;
    display: grid;
    grid-template-columns: 268px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas:
      "practice keyboard"
      "stats keyboard";
    gap: 7px;
    overflow: hidden;
  }

  .practice {
    grid-area: practice;
    min-height: 0;
    overflow: hidden;
    padding: 8px;
    display: flex;
    flex-direction: column;
  }

  .keyboard-section {
    grid-area: keyboard;
    min-height: 0;
    overflow: hidden;
    padding: 8px;
  }

  .stats {
    grid-area: stats;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
    margin-top: 0;
  }

  .stats div {
    min-height: 45px;
  }

  .stats span {
    font-size: 18px;
  }

  .stats small {
    font-size: 10px;
  }

  .topbar {
    display: flex;
    gap: 8px;
    align-items: start;
  }

  h1 {
    font-size: 17px;
  }

  .topbar p {
    display: none;
  }

  .ghost-button {
    width: auto;
    min-height: 30px;
    padding: 0 9px;
    font-size: 12px;
  }

  .toolbar {
    margin-top: 7px;
    gap: 6px;
  }

  label {
    gap: 4px;
    font-size: 11px;
  }

  select {
    min-height: 30px;
    padding: 0 7px;
    font-size: 12px;
  }

  .target-panel {
    margin-top: 7px;
    flex: 1;
    min-height: 0;
  }

  .queue {
    min-height: 34px;
    padding: 5px 6px;
    gap: 4px;
  }

  .queue-item {
    width: 24px;
    height: 24px;
    font-size: 15px;
  }

  .target-main {
    grid-template-columns: 78px 1fr;
    min-height: 94px;
    padding: 9px;
    gap: 10px;
  }

  .character {
    width: 76px;
    font-size: 44px;
  }

  .pinyin {
    font-size: 26px;
  }

  .typed {
    min-height: 34px;
    margin-top: 8px;
    gap: 6px;
  }

  .typed span {
    width: 34px;
    height: 34px;
    font-size: 18px;
  }

  .message {
    min-height: 30px;
    padding: 7px 9px;
    font-size: 12px;
  }

  .section-title {
    display: flex;
    align-items: center;
    margin-bottom: 7px;
  }

  h2 {
    font-size: 17px;
  }

  .section-title p,
  .legend {
    font-size: 11px;
  }

  .keyboard {
    gap: 5px;
  }

  .key-row {
    display: flex;
    gap: 4px;
  }

  .key-row:nth-child(2) {
    padding-left: calc((100% - 36px) / 20);
  }

  .key-row:nth-child(3) {
    padding-left: calc((100% - 36px) / 10 * 1.5);
  }

  .key {
    width: calc((100% - 36px) / 10);
    min-height: 67px;
    padding: 6px 4px;
  }

  .letter {
    font-size: 16px;
  }

  .map-line {
    min-height: 12px;
    font-size: 9px;
    line-height: 1.05;
  }

  .legend {
    margin-top: 7px;
    gap: 10px;
  }
}
