:root {
  --bg: #0e0f12;
  --bg-deep: #07080b;
  --surface: rgba(255, 255, 255, 0.045);
  --well: #17181d;
  --key: #5a5b62;
  --key-fn: #3a3b42;
  --key-down: #6d6e76;
  --ink: #f6f1e8;
  --muted: #b7b1a6;
  --faint: #8d877c;
  --gold: #e6c27a;
  --gold-ink: #2a2112;
  --jade: #8fd6ae;
  --jade-deep: #1d3d2e;
  --danger: #ff6b6b;
  --chip: #24262e;
  --line: rgba(255, 255, 255, 0.08);
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.22) inset, 0 -1px 0 rgba(0, 0, 0, 0.45) inset, 0 1px 2px rgba(0, 0, 0, 0.35);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
  font-family: "PingFang HK", "Noto Sans HK", "Heiti TC", "Microsoft JhengHei", "Source Han Sans TC", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100dvh;
  max-height: 100dvh;
  background: var(--bg-deep);
  color: var(--ink);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  position: fixed;
  inset: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: stretch;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button,
textarea {
  font: inherit;
  color: inherit;
}

button,
.key,
textarea {
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  overscroll-behavior: none;
}

textarea {
  cursor: text;
}

#app {
  width: min(560px, 100%);
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background:
    radial-gradient(120% 50% at 50% -10%, rgba(230, 194, 122, 0.14), transparent 55%),
    linear-gradient(180deg, #14161c 0%, var(--bg) 28%);
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
  position: relative;
}

.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + var(--safe-t)) 16px 0;
}

.eyebrow {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold);
}

h1 {
  margin: 2px 0 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

h2 {
  margin: 0;
  font-size: 28px;
}

.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  padding-top: 4px;
}

.icon-btn,
.ghost,
.text-btn,
.primary {
  border-radius: 999px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--gold);
  font-weight: 700;
}

.icon-btn[aria-pressed="false"] {
  color: var(--faint);
}

.ghost,
.text-btn,
.primary {
  min-height: 40px;
  padding: 0 14px;
}

.ghost,
.text-btn {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink);
}

.primary {
  background: var(--gold);
  color: var(--gold-ink);
  font-weight: 700;
}

.modes {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin: 12px 16px 0;
  padding: 4px;
  background: rgba(0, 0, 0, 0.28);
  border-radius: 14px;
  border: 1px solid var(--line);
}

.mode-btn {
  min-height: 40px;
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
  position: relative;
}

.mode-btn[aria-selected="true"] {
  background: #2a2c34;
  color: var(--ink);
  box-shadow: var(--shadow);
}

.badge {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  margin-left: 4px;
  border-radius: 999px;
  background: var(--danger);
  color: white;
  font-size: 11px;
  font-weight: 700;
  vertical-align: 1px;
}

.blurb,
.offline-flag,
.key-legend {
  margin: 8px 16px 0;
  color: var(--faint);
  font-size: 12px;
  line-height: 1.45;
}

.offline-flag {
  color: var(--gold);
}

#stage {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin-top: 8px;
}

.panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0 16px;
}

[hidden] {
  display: none !important;
}

.composer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.composer-bar label,
#char-count {
  font-size: 12px;
  color: var(--muted);
}

#clear-text {
  margin-left: auto;
  min-height: 32px;
  padding: 0 10px;
  font-size: 12px;
}

#composer {
  flex: 1;
  min-height: 48px;
  width: 100%;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.03);
  padding: 12px 14px;
  line-height: 1.65;
  font-size: 18px;
  caret-color: var(--gold);
  -webkit-user-select: text;
  user-select: text;
}

#composer:focus {
  outline: 2px solid rgba(230, 194, 122, 0.55);
  outline-offset: 1px;
}

.buffer {
  display: flex;
  gap: 6px;
  min-height: 36px;
  align-items: center;
  overflow-x: auto;
  padding: 8px 0 2px;
}

.pill {
  flex: 0 0 auto;
  min-width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #2c2418;
  color: var(--gold);
  font-size: 16px;
}

.buffer-empty {
  color: var(--faint);
  font-size: 12px;
}

.idle {
  margin: auto 0;
  padding: 12px 4px 18px;
}

.idle h2 {
  margin-top: 4px;
}

.idle p {
  color: var(--muted);
  line-height: 1.5;
}

.best {
  color: var(--gold) !important;
  font-variant-numeric: tabular-nums;
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.hud span {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  min-height: 36px;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

#hud-combo {
  color: var(--gold);
  font-weight: 700;
}

#hud-combo.pop {
  animation: pop 0.28s ease;
}

.quiz-meta {
  margin-top: 8px;
  min-height: 18px;
  font-size: 12px;
  color: var(--muted);
}

#quiz-play,
#level-play {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}

#target-wrap,
#lv-wrap {
  flex: 0 0 auto;
  text-align: center;
  padding: 4px 0 2px;
}

#target-char,
.big-char {
  font-size: clamp(40px, 12vw, 64px);
  line-height: 1;
  font-weight: 600;
  text-align: center;
}

.jyut-line {
  margin-top: 4px;
  color: var(--gold);
  letter-spacing: 0.08em;
  text-align: center;
}

#target-jyut {
  margin-top: 4px;
  color: var(--gold);
  letter-spacing: 0.08em;
}

.slots {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  padding: 6px 0 12px;
}

.slot {
  width: 32px;
  height: 36px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #121318;
  border: 1px dashed rgba(255, 255, 255, 0.16);
  color: var(--faint);
  font-size: 18px;
}

.slot.done {
  border-style: solid;
  border-color: transparent;
  background: var(--jade-deep);
  color: var(--jade);
}

.slot.next {
  border-style: solid;
  border-color: var(--gold);
  color: var(--gold);
}

.queue-row {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.queue-chip {
  flex: 0 0 auto;
  min-width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--chip);
  border: 1px solid var(--line);
  color: var(--ink);
}

.queue-chip.active {
  border-color: var(--gold);
  color: var(--gold);
}

.shake {
  animation: shake 0.38s ease;
}

.breakdown {
  flex-shrink: 0;
  margin: 0 16px;
  min-height: 0;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--line);
}

.breakdown-empty,
.bd-meta {
  margin: 0;
  color: var(--faint);
  font-size: 12px;
}

.bd-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}

.bd-char {
  font-size: 18px;
  font-weight: 650;
}

.bd-jyut {
  color: var(--gold);
  font-size: 13px;
}

.bd-strokes {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.bd-stroke {
  font-size: 12px;
  color: var(--faint);
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.03);
}

.bd-stroke.on {
  color: var(--gold-ink);
  background: var(--gold);
}

.bd-stroke.wild {
  color: var(--ink);
  background: #3d4a66;
}

.candidates {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 8px 16px;
  min-height: 74px;
  align-items: center;
  scrollbar-width: thin;
  touch-action: pan-x;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  cursor: grab;
}

.candidates.is-dragging {
  cursor: grabbing;
}

.candidates .candidate {
  touch-action: pan-x;
  -webkit-user-select: none;
  user-select: none;
}

.candidates::-webkit-scrollbar,
.buffer::-webkit-scrollbar,
.queue-row::-webkit-scrollbar {
  display: none;
}

.candidate,
.cand-empty {
  flex: 0 0 auto;
}

.candidate {
  min-width: 64px;
  height: 62px;
  border-radius: 12px;
  background: var(--chip);
  border: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.candidate .c-char {
  font-size: 24px;
  line-height: 1;
}

.candidate .c-jyut {
  font-size: 11px;
  color: var(--muted);
}

.candidate.active {
  border-color: var(--gold);
  background: #3a3122;
}

.candidate.active .c-jyut {
  color: var(--gold);
}

.cand-empty {
  color: var(--faint);
  font-size: 13px;
}

.keyboard {
  flex-shrink: 0;
  background: #121318;
  border-top: 1px solid var(--line);
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.key {
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.punct {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  padding: 8px 8px 0;
}

.punct button {
  min-height: 36px;
  border-radius: 8px;
  background: #2a2b31;
  color: var(--ink);
  box-shadow: var(--shadow);
}

.punct button:active,
.key:active,
.key.is-down {
  transform: translateY(1px) scale(0.97);
  filter: brightness(1.12);
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 8px;
}

.key {
  height: 64px;
  border-radius: 10px;
  background: var(--key);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  position: relative;
}

.key-fn {
  background: var(--key-fn);
}

.key-glyph {
  font-size: 22px;
  line-height: 1;
  display: grid;
  place-items: center;
  height: 22px;
}

.key-name {
  font-size: 13px;
  font-weight: 650;
}

.key-sub {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.72);
  letter-spacing: 0.04em;
}

.key.is-ok {
  background: #2f6b4a;
}

.key.is-bad {
  background: #8a3030;
}

.key.flash {
  animation: hintpulse 0.7s ease;
  box-shadow: 0 0 0 2px var(--gold), var(--shadow);
}

.key-legend {
  margin: 0 10px 8px;
  text-align: center;
}

#toast {
  display: none;
  margin: 8px 16px 0;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(20, 18, 14, 0.94);
  border: 1px solid rgba(230, 194, 122, 0.35);
  color: var(--ink);
  font-size: 13px;
  text-align: center;
  pointer-events: none;
}

#toast.show {
  display: block;
}

.modal {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.62);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 6;
}

.modal-card {
  width: min(420px, 100%);
  background: #1a1b21;
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 20px 18px 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.4);
}

.modal-card h2 {
  margin: 4px 0 12px;
}

#modal-stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  margin: 0 0 16px;
}

#modal-stats dt {
  color: var(--muted);
}

#modal-stats dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-actions button {
  width: 100%;
  border-radius: 12px;
}

.key:focus-visible,
.candidate:focus-visible,
.mode-btn:focus-visible,
.primary:focus-visible,
.ghost:focus-visible,
.text-btn:focus-visible,
.punct button:focus-visible,
.queue-chip:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.noscript {
  position: fixed;
  inset: auto 16px 16px 16px;
  background: #1a1b21;
  padding: 12px;
  border-radius: 12px;
}

@media (min-height: 780px) {
  .key {
    height: 74px;
  }
}

@media (min-width: 640px) {
  body {
    align-items: center;
    background:
      radial-gradient(60% 40% at 50% 0%, rgba(230, 194, 122, 0.08), transparent 60%),
      var(--bg-deep);
  }

  #app {
    height: min(960px, calc(100dvh - 24px));
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 28px;
    overflow: hidden;
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
  }
}

@media (max-height: 740px) {
  .blurb,
  .key-legend {
    display: none;
  }

  .key {
    height: 54px;
  }

  .breakdown {
    padding: 6px 8px;
  }

  .candidates {
    min-height: 62px;
    padding-top: 6px;
    padding-bottom: 6px;
  }

  #target-char,
  .big-char {
    font-size: 64px;
  }
}

@media (max-width: 360px) {
  h1 {
    font-size: 18px;
  }

  .mode-btn {
    font-size: 13px;
  }

  .key {
    height: 58px;
  }

  .key-sub {
    display: none;
  }
}

#app.mode-blitz .punct,
#app.mode-review .punct,
#app.mode-level .punct {
  display: none;
}

.view-login {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  height: 100%;
  overflow: auto;
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
}

.view-login h1 {
  margin-bottom: 4px;
}

.view-login label {
  font-size: 12px;
  color: var(--muted);
}

#login-name,
#login-pin {
  width: 100%;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #121318;
  color: var(--ink);
  padding: 0 14px;
  font-size: 18px;
}

#login-pin {
  letter-spacing: 0.4em;
  text-align: center;
}

.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.pin-key,
.roster-btn {
  min-height: 48px;
  border-radius: 12px;
  background: var(--chip);
  border: 1px solid var(--line);
  font-size: 18px;
}

.login-roster {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.roster-btn {
  flex: 0 0 auto;
  padding: 0 14px;
  font-size: 15px;
}

.stay-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  color: var(--ink);
  font-size: 15px;
}

.login-error {
  min-height: 1.2em;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.guide-fieldset {
  border: 1px solid var(--line);
  border-radius: 12px;
  margin: 0 0 12px;
  padding: 8px;
}

.guide-fieldset legend {
  padding: 0 6px;
  color: var(--gold);
}

.guide-option {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 52px;
  padding: 8px;
  border-radius: 10px;
}

.guide-option span {
  color: var(--faint);
  font-size: 12px;
  padding-left: 24px;
}

#level-play {
  position: relative;
}

.eye-btn {
  position: absolute;
  top: 0;
  right: 0;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 20px;
}

#view-game {
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

#app.is-locked .key-stroke {
  opacity: 0.45;
}

@keyframes shake {
  0%,
  100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(4px); }
}

@keyframes hintpulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

.profile-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 16px 0;
}

.profile-slot {
  position: relative;
  flex: 1;
  min-width: 0;
}

.profile-switch {
  width: 100%;
  min-height: 40px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  text-align: left;
}

#profile-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 650;
}

.profile-pop {
  position: absolute;
  z-index: 7;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  border-radius: 12px;
  background: #1c1d24;
  border: 1px solid var(--line);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
}

.profile-pop button,
.profile-row {
  min-height: 40px;
  border-radius: 8px;
  text-align: left;
  padding: 0 10px;
}

.profile-pop button.active,
.profile-row.active {
  background: #3a3122;
  color: var(--gold);
}

.level-actions,
.drawer-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.level-actions button,
.drawer-bar button {
  min-height: 40px;
}

.level-hud span {
  font-size: 12px;
}

.level-card {
  width: min(520px, 100%);
  max-height: min(86dvh, 760px);
  display: flex;
  flex-direction: column;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  overflow: auto;
  min-height: 0;
  padding: 4px 2px 8px;
  -webkit-overflow-scrolling: touch;
}

.level-band {
  grid-column: 1 / -1;
  margin-top: 8px;
  color: var(--gold);
  font-size: 12px;
}

.level-cell {
  min-height: 52px;
  border-radius: 10px;
  background: var(--chip);
  border: 1px solid var(--line);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.level-cell.locked {
  color: var(--faint);
}

.level-cell.done {
  border-color: rgba(143, 214, 174, 0.45);
}

.level-cell.current {
  outline: 2px solid var(--gold);
  outline-offset: 1px;
}

.mini-stars {
  color: var(--gold);
  font-size: 9px;
  letter-spacing: -0.04em;
  line-height: 1;
}

.star-row {
  margin: 0 0 8px;
  font-size: 28px;
  letter-spacing: 0.08em;
  min-height: 36px;
}

.star-row .star {
  display: inline-block;
  animation: pop 0.35s ease both;
}

.star-row .star:nth-child(2) { animation-delay: 0.08s; }
.star-row .star:nth-child(3) { animation-delay: 0.16s; }

.star-row .star.off {
  opacity: 0.28;
}

.missed {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0 0 12px;
  max-height: 140px;
  overflow: auto;
  color: var(--muted);
  font-size: 13px;
}

.profile-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.profile-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.profile-row span {
  flex: 1;
  text-align: left;
}

.avatar-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 8px 0 12px;
}

.avatar-picker button {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--chip);
  border: 1px solid var(--line);
  font-size: 20px;
}

.avatar-picker button[aria-pressed="true"] {
  border-color: var(--gold);
  background: #3a3122;
}

#profile-form label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

#profile-name-input {
  width: 100%;
  min-height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #121318;
  color: var(--ink);
  padding: 0 12px;
  font-size: 16px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 12px;
  min-height: 0;
}

.badge-chip {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 999px;
  background: #3a3122;
  color: var(--gold);
}

#profile-close {
  width: 100%;
  margin-top: 8px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
