:root {
  --bg: #faf4e8;
  --paper: #fff8ea;
  --paper-light: #fffaf0;
  --gold: #b77a2e;
  --gold-dark: #875d3b;
  --line: #c99b57;
  --line-soft: #e3cfaa;
  --ink: #3d2a1b;
  --muted: #756351;
  --blue: #829daf;
  --blue-soft: #e5edf1;
  --red: #a64b38;
  --green: #6e8667;
  --shadow: 0 9px 18px rgba(77, 57, 31, .12);
}

* { box-sizing: border-box; }

html { min-height: 100%; scroll-behavior: smooth; }

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, system-ui, sans-serif;
}

body::before {
  content: "";
  position: fixed;
  z-index: -1;
  inset: 64px 0 0;
  background: var(--bg) url("./assets/mountain-parchment-background.png") center top / 100% auto no-repeat;
  pointer-events: none;
}

button, input { font: inherit; }

button { color: inherit; }

button:focus-visible, input:focus-visible {
  outline: 3px solid rgba(130, 157, 175, .48);
  outline-offset: 3px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.sitebar {
  position: sticky;
  z-index: 20;
  top: 0;
  height: 64px;
  border-bottom: 1px solid rgba(183, 122, 46, .3);
  background: rgba(250, 244, 232, .94);
  box-shadow: 0 3px 12px rgba(61, 42, 27, .06);
  backdrop-filter: blur(14px);
}

.sitebar-inner {
  width: min(1020px, calc(100% - 32px));
  height: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: grid;
  gap: 4px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  text-align: left;
}

.brand-name {
  font-family: Georgia, "Noto Serif Thai", serif;
  font-size: 22px;
  font-weight: 700;
  line-height: 1;
}

.site-status {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--gold-dark);
  font: 700 13px/1 Georgia, "Noto Serif Thai", serif;
}

.status-divider {
  width: 1px;
  height: 18px;
  background: rgba(183, 122, 46, .35);
}

.page {
  width: min(1020px, calc(100% - 32px));
  margin: 0 auto;
  padding: 52px 0 72px;
}

.view { display: none; }
.view.active { display: block; animation: view-in .25s ease both; }

@keyframes view-in {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: translateY(0); }
}

.mode-grid, .bottom-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 20px;
}

.mode-grid { row-gap: 6px; }

.mode-card {
  position: relative;
  display: block;
  width: 100%;
  height: 204px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  filter: drop-shadow(0 5px 7px rgba(91, 69, 39, .08));
  transition: transform .16s ease, filter .16s ease;
}

.mode-card::before {
  content: "";
  position: absolute;
  inset: 0;
  background: url("./assets/scroll-frame-warm-matte.png") center / 100% 100% no-repeat;
}

.mode-card:hover {
  transform: translateY(-3px);
  filter: drop-shadow(0 10px 10px rgba(91, 69, 39, .14));
}

.mode-content {
  position: absolute;
  inset: 11% 7% 11% 12%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
}

.mode-art {
  display: block;
  flex: 0 0 118px;
  width: 118px;
  height: 118px;
  object-fit: contain;
  filter: drop-shadow(0 4px 4px rgba(74, 55, 31, .11));
}

.mode-title {
  font-family: Georgia, "Noto Serif Thai", serif;
  font-size: 19px;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
}

.mode-flashcards .mode-content { transform: translate(-14px, -9px); }
.mode-listening .mode-content { transform: translate(-18px, -9px); }
.mode-vocabulary .mode-content { transform: translate(-12px, -8px); }
.mode-compound .mode-content { transform: translate(-18px, -8px); }

.bottom-grid {
  margin-top: 8px;
  gap: 20px;
}

.level-panel, .home-panel {
  width: 100%;
  height: 268px;
  overflow: hidden;
}

.level-panel {
  padding: 40px 34px 18px;
  background: url("./assets/level-manuscript-frame.png") center / 100% 100% no-repeat;
}

.level-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
}

.level {
  display: inline-flex;
  align-items: baseline;
  color: #a97429;
  font: 700 34px/1 Cambria, "Times New Roman", serif;
  font-variant-numeric: lining-nums;
}

.rank {
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
}

.xp {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  font: 700 14px/1 Cambria, "Times New Roman", serif;
  font-variant-numeric: lining-nums;
}

.progress {
  height: 8px;
  margin: 15px 0 13px;
  overflow: hidden;
  border-radius: 999px;
  background: #eee4d4;
}

.progress span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: #ba7f2d;
  transition: width .28s ease;
}

.divider {
  height: 1px;
  margin-bottom: 12px;
  background: #e4d2b3;
}

.buddy {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 10px;
  row-gap: 2px;
}

.portrait {
  grid-column: 1;
  grid-row: 1 / 3;
  width: 56px;
  height: 56px;
  padding: 3px;
  border: 1px solid #d5b576;
  border-radius: 50%;
  background: #f8edda;
  object-fit: contain;
}

.buddy-name {
  grid-column: 2;
  grid-row: 1;
  font-size: 17px;
  line-height: 1.2;
}

.buddy-meta {
  grid-column: 2 / 4;
  grid-row: 2;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
}

.change {
  grid-column: 3;
  grid-row: 1;
  min-width: 68px;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid #cba565;
  border-radius: 8px;
  background: #fff9ee;
  color: #7b5627;
  font-weight: 700;
  cursor: pointer;
}

.home-panel {
  position: relative;
  overflow: hidden;
  background: var(--paper);
}

.companion-scene-slot {
  position: absolute;
  z-index: 1;
  inset: 12px 13px 14px;
  overflow: hidden;
  border-radius: 8px;
  background: var(--paper);
}

.companion-scene-slot img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 55%;
  transition: opacity .18s ease;
}

.room-frame {
  position: absolute;
  z-index: 2;
  inset: 5px;
  width: calc(100% - 10px);
  height: calc(100% - 10px);
  object-fit: fill;
  pointer-events: none;
  filter: drop-shadow(0 5px 5px rgba(61, 42, 27, .12));
}

.learning-view {
  max-width: 860px;
  margin: 0 auto;
}

.view-head {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
  padding-bottom: 17px;
  border-bottom: 1px solid rgba(183, 122, 46, .28);
}

.view-head h1, .dialog-head h2 {
  margin: 0;
  font-family: Georgia, "Noto Serif Thai", serif;
  font-size: 28px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 12px;
}

.back-button, .icon-button, .dialog-close {
  display: grid;
  place-items: center;
  border: 1px solid var(--line-soft);
  border-radius: 50%;
  background: var(--paper-light);
  color: var(--gold-dark);
  cursor: pointer;
}

.back-button { width: 44px; height: 44px; font-size: 22px; }
.view-count { color: var(--gold-dark); font-weight: 700; }

.study-layout, .listening-panel {
  width: min(620px, 100%);
  margin: 0 auto;
}

.study-card {
  position: relative;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 10px;
  width: 100%;
  min-height: 360px;
  padding: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(90deg, rgba(201, 155, 87, .06), transparent 14%, transparent 86%, rgba(201, 155, 87, .06)), var(--paper-light);
  box-shadow: var(--shadow);
  cursor: pointer;
}

.study-card::before {
  content: "";
  position: absolute;
  inset: 12px;
  border: 1px solid rgba(201, 155, 87, .48);
  pointer-events: none;
}

.hanzi {
  color: var(--red);
  font: 700 78px/1 "SimSun", "Noto Serif SC", serif;
}

.pinyin { color: var(--blue); font-size: 21px; }
.translation { font-size: 24px; font-weight: 700; }
.translation.concealed { visibility: hidden; }

.study-actions {
  display: grid;
  grid-template-columns: 46px minmax(150px, 1fr) 46px;
  gap: 12px;
  margin-top: 16px;
}

.icon-button { width: 46px; height: 46px; font-size: 22px; }

.primary-action {
  min-height: 46px;
  border: 1px solid #9c6b2b;
  border-radius: 8px;
  background: #a97429;
  color: #fffaf0;
  font-weight: 700;
  cursor: pointer;
}

.listening-panel {
  min-height: 410px;
  padding: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-light);
  box-shadow: var(--shadow);
  text-align: center;
}

.sound-button {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 0 20px;
  border: 1px solid #718b9c;
  border-radius: 8px;
  background: var(--blue-soft);
  color: #425e70;
  font-weight: 700;
  cursor: pointer;
}

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

.choice {
  min-height: 58px;
  padding: 10px 14px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #fffaf0;
  cursor: pointer;
}

.choice:hover { border-color: var(--gold); }
.choice.correct { border-color: var(--green); background: #eef3e9; color: #496245; }
.choice.wrong { border-color: var(--red); background: #f8e7e1; color: #863d32; }
.feedback { min-height: 24px; margin: 22px 0 0; color: var(--muted); }

.search-wrap { display: block; margin-bottom: 18px; }

.search-wrap input {
  width: 100%;
  min-height: 48px;
  padding: 0 15px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: rgba(255, 250, 240, .94);
  color: var(--ink);
}

.vocab-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.vocab-item {
  display: grid;
  grid-template-columns: 68px 1fr auto;
  align-items: center;
  gap: 12px;
  min-height: 88px;
  padding: 14px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: rgba(255, 250, 240, .94);
}

.vocab-zh { color: var(--red); font: 700 31px/1 "SimSun", serif; text-align: center; }
.vocab-py { color: #637f91; font-size: 14px; }
.vocab-th { display: block; margin-top: 3px; font-weight: 700; }

.audio-mini {
  width: 38px;
  height: 38px;
  border: 1px solid #9bb0bd;
  border-radius: 50%;
  background: var(--blue-soft);
  color: #425e70;
  cursor: pointer;
}

.character-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  margin-bottom: 18px;
}

.character-tab {
  min-width: 50px;
  min-height: 44px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: var(--paper-light);
  color: var(--red);
  font: 700 22px/1 "SimSun", serif;
  cursor: pointer;
}

.character-tab.active { border-color: var(--gold); background: #f5e8ce; color: var(--gold-dark); }

.compound-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.compound-item {
  padding: 18px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: rgba(255, 250, 240, .94);
}

.compound-formula { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.compound-formula strong { color: var(--red); font: 700 28px/1 "SimSun", serif; }
.compound-result { margin-top: 12px; font-size: 20px; font-weight: 700; }
.compound-result span { margin-left: 8px; color: var(--blue); font-size: 14px; font-weight: 500; }
.compound-meaning { margin-top: 5px; color: var(--muted); }

.buddy-dialog {
  width: min(620px, calc(100% - 28px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-light);
  color: var(--ink);
  box-shadow: 0 22px 70px rgba(61, 42, 27, .28);
}

.buddy-dialog::backdrop { background: rgba(52, 39, 24, .34); backdrop-filter: blur(3px); }
.buddy-dialog form { padding: 22px; }
.dialog-head { display: flex; align-items: start; justify-content: space-between; margin-bottom: 18px; }
.dialog-head h2 { font-size: 24px; }
.dialog-close { width: 38px; height: 38px; font-size: 24px; }

.buddy-choices { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

.buddy-choice {
  display: grid;
  grid-template-columns: 62px 1fr;
  align-items: center;
  gap: 10px;
  min-height: 82px;
  padding: 10px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #fffaf0;
  cursor: pointer;
  text-align: left;
}

.buddy-choice.selected { border-color: var(--gold); background: #f7ebd2; }
.buddy-choice img { width: 62px; height: 62px; object-fit: contain; }
.buddy-choice strong { display: block; }
.buddy-choice small { color: var(--muted); }

.empty-state {
  grid-column: 1 / -1;
  padding: 42px 20px;
  border: 1px dashed var(--line-soft);
  color: var(--muted);
  text-align: center;
}

@media (max-width: 720px) {
  body::before { inset: 58px 0 0; background-size: auto 100%; }
  .sitebar { height: 58px; }
  .sitebar-inner { width: calc(100% - 24px); }
  .brand-name { font-size: 21px; }
  .brand-sub { font-size: 8px; }
  .page { width: min(520px, calc(100% - 24px)); padding: 24px 0 40px; }
  .mode-grid, .bottom-grid, .vocab-list, .compound-list { grid-template-columns: 1fr; }
  .mode-grid { gap: 12px; }
  .mode-card { height: auto; aspect-ratio: 2.7 / 1; }
  .mode-content { inset: 10% 7% 10% 12%; gap: 16px; }
  .mode-art { flex-basis: 94px; width: 94px; height: 94px; }
  .mode-title { font-size: 18px; }
  .bottom-grid { margin-top: 18px; gap: 14px; }
  .level-panel, .home-panel { height: auto; aspect-ratio: 1.65 / 1; }
  .level-panel { padding: 29px 30px 21px; }
  .home-panel { min-height: 225px; }
  .view-head { grid-template-columns: 42px 1fr; }
  .view-count { grid-column: 2; }
  .study-card { min-height: 310px; padding: 34px 24px; }
  .hanzi { font-size: 66px; }
  .listening-panel { padding: 28px 18px; }
  .choice-grid { grid-template-columns: 1fr; }
  .buddy-choices { grid-template-columns: 1fr; }
}

@media (max-width: 390px) {
  .site-status { gap: 7px; font-size: 12px; }
  .level-panel { padding: 25px 25px 18px; }
  .level { font-size: 30px; }
  .buddy-meta { font-size: 10px; }
}

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