:root {
  color-scheme: light;
  --ink: #173330;
  --muted: #53655e;
  --paper: #f6f7f2;
  --line: #dce3dc;
  --accent: #bb5136;
  --accent-dark: #973c27;
  --app-height: 100dvh;
  --app-top: 0px;
  --page-gutter: max(16px, env(safe-area-inset-left));
  --page-gutter-end: max(16px, env(safe-area-inset-right));
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { overflow: hidden; }
body { margin: 0; overflow: hidden; background: #e9eeea; color: var(--ink); overscroll-behavior: none; }
button, input { font: inherit; }
button { cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible { outline: 3px solid #e8a280; outline-offset: 2px; }
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }

.app-shell {
  position: fixed;
  top: var(--app-top);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  width: min(100%, 480px);
  height: var(--app-height);
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 0 35px rgba(13, 40, 33, .08);
}
.app-header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: calc(52px + env(safe-area-inset-top));
  padding: calc(env(safe-area-inset-top) + 4px) var(--page-gutter-end) 8px var(--page-gutter);
  background: var(--ink);
  color: #fff;
}
.back-button { flex: 0 0 44px; width: 44px; min-height: 44px; padding: 0; border: 0; background: transparent; color: #fff; font-size: 34px; line-height: 1; }
.brand-mark { flex: 0 0 36px; height: 36px; display: grid; place-items: center; border: 1.5px solid #deb78e; border-radius: 9px; color: #efd6b3; font-family: "Songti SC", "SimSun", serif; font-size: 23px; }
.header-title { min-width: 0; flex: 1; display: flex; align-items: baseline; gap: 8px; }
.header-title h1 { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-family: "Songti SC", "SimSun", serif; font-size: 22px; line-height: 1.2; }
.header-progress { flex: 0 0 auto; color: #c6d8cd; font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
.language-button { flex: 0 0 auto; min-width: 44px; min-height: 44px; padding: 7px 9px; border: 1px solid #638076; border-radius: 8px; background: transparent; color: #fff; font-size: 14px; font-weight: 750; }

.home-view {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 22px var(--page-gutter-end) calc(20px + env(safe-area-inset-bottom)) var(--page-gutter);
}
.home-intro h2 { font-family: "Songti SC", "SimSun", serif; font-size: 28px; line-height: 1.25; }
.theme-list { display: grid; gap: 12px; margin-top: 18px; }
.theme-card { width: 100%; min-width: 0; padding: 16px 16px 14px; border: 1px solid var(--line); border-left: 5px solid var(--accent); border-radius: 13px; background: #fff; color: var(--ink); text-align: left; box-shadow: 0 5px 17px rgba(16, 45, 40, .045); }
.theme-card[data-theme="market"] { border-left-color: #5d8770; }
.theme-card[data-theme="streets"] { border-left-color: #6a8291; }
.theme-card[data-theme="celebrations"] { border-left-color: #9a698c; }
.theme-card[data-theme="cooking"] { border-left-color: #a77a3d; }
.theme-card[data-theme="practice"] { border-left-color: #3d6f8f; }
@media (hover: hover) {
  .theme-card:hover { border-top-color: #b9cbc0; border-right-color: #b9cbc0; border-bottom-color: #b9cbc0; }
}
.theme-card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.theme-card-title { flex: 1; min-width: 0; font-family: "Songti SC", "SimSun", serif; font-size: 22px; font-weight: 750; line-height: 1.25; }
.theme-card-status { flex: 0 0 auto; color: #426b54; font-size: 14px; font-weight: 800; white-space: nowrap; }
.theme-card-description { display: block; margin-top: 6px; color: var(--muted); font-size: 15px; line-height: 1.35; }
.theme-card-bottom { display: flex; align-items: center; gap: 11px; margin-top: 12px; }
.theme-card-progress { flex: 1; height: 5px; overflow: hidden; border-radius: 999px; background: #e4ebe4; }
.theme-card-progress span { display: block; height: 100%; background: var(--accent); }
.theme-card[data-theme="market"] .theme-card-progress span { background: #5d8770; }
.theme-card[data-theme="streets"] .theme-card-progress span { background: #6a8291; }
.theme-card[data-theme="celebrations"] .theme-card-progress span { background: #9a698c; }
.theme-card[data-theme="cooking"] .theme-card-progress span { background: #a77a3d; }
.theme-card[data-theme="practice"] .theme-card-progress span { background: #3d6f8f; }
.theme-card-count { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.storage-note { margin-top: 20px; color: #76847c; font-size: 13px; line-height: 1.45; text-align: center; }

.game {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}
.active-clue {
  flex: none;
  min-width: 0;
  margin: 12px var(--page-gutter-end) 0 var(--page-gutter);
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: white;
}
.clue-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
.clue-label { min-width: 0; display: inline-block; padding: 4px 8px; border-radius: 6px; background: #faece6; color: var(--accent-dark); font-size: 13px; font-weight: 800; }
.clue-nav { flex: none; display: flex; gap: 4px; }
.clue-nav button { width: 44px; height: 44px; padding: 0; border: 1px solid var(--line); border-radius: 9px; background: #f7f9f6; color: var(--ink); font-size: 28px; line-height: 1; }
.active-clue-text { padding: 8px 0 2px; font-size: 18px; line-height: 1.35; font-weight: 700; overflow-wrap: anywhere; }
.answer-status { min-height: 22px; margin: 2px 0 8px; color: var(--muted); font-size: 15px; line-height: 1.35; font-weight: 750; }
.answer-status[data-result="correct"] { color: #176f4c; }
.answer-status[data-result="incorrect"] { color: #a13f2b; }
.entry-row { min-width: 0; }
.entry-row label { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.entry-row input {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: 48px;
  margin: 0;
  padding: 0 12px;
  border: 1.5px solid #bdcbc0;
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-size: 17px;
  line-height: 1.2;
  -webkit-appearance: none;
  appearance: none;
  -webkit-user-select: text;
  user-select: text;
}
.entry-row input::placeholder { color: #83918a; }

.play-scroll {
  container-type: size;
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px var(--page-gutter-end) 12px var(--page-gutter);
}
.crossword-board {
  width: min(100%, calc(100cqh * var(--cols) / var(--rows)));
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: var(--cols) / var(--rows);
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: 2px;
  overflow: hidden;
  border: 2px solid #d2ded4;
  border-radius: 8px;
  background: #d2ded4;
  box-shadow: 0 8px 19px rgba(20, 54, 43, .1);
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border: 0;
  padding: 2px 0 0;
  background: #fff;
  color: var(--ink);
  font-family: "Songti SC", "SimSun", Georgia, serif;
  font-size: clamp(18px, 6.4vw, 30px);
  font-weight: 700;
  line-height: 1;
  -webkit-touch-callout: none;
}
.crossword-board[data-script="latin"] .cell { font-family: -apple-system, BlinkMacSystemFont, sans-serif; font-size: clamp(16px, 5.2vw, 28px); letter-spacing: 0; }
.cell.in-word { background: #f8eee4; }
.cell.active { z-index: 1; background: #f2c4a7; box-shadow: inset 0 0 0 2px var(--accent); }
.cell.solved { color: #176f4c; }
.cell.revealed { color: #9b552f; }
.cell.block { background: #1d3b37; pointer-events: none; }
.cell-number { position: absolute; top: 2px; left: 3px; color: #52665f; font-family: -apple-system, BlinkMacSystemFont, sans-serif; font-size: 10px; font-weight: 800; line-height: 1; }
.cell-value { max-width: 100%; overflow: hidden; }

.game-actions {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px var(--page-gutter-end) calc(8px + env(safe-area-inset-bottom)) var(--page-gutter);
  border-top: 1px solid var(--line);
  background: var(--paper);
}
html.keyboard-open .game-actions { display: none; }
.hint-button {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  border: 1px solid var(--accent-dark);
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 800;
}
@media (hover: hover) {
  .hint-button:hover { background: var(--accent-dark); }
}
.reset-button {
  width: 100%;
  min-height: 44px;
  padding: 6px 14px;
  border: 0;
  background: transparent;
  color: #67776e;
  font-size: 14px;
  text-decoration: underline;
}

.toast { position: fixed; bottom: calc(16px + env(safe-area-inset-bottom)); left: 50%; z-index: 20; transform: translateX(-50%); width: max-content; max-width: calc(100vw - 32px); padding: 11px 16px; border-radius: 10px; background: var(--ink); color: #fff; box-shadow: 0 10px 28px rgba(0, 0, 0, .19); text-align: center; font-size: 15px; }
body:has(#gameView:not([hidden])) .toast { bottom: calc(118px + env(safe-area-inset-bottom)); }
html.keyboard-open .toast { bottom: calc(16px + env(safe-area-inset-bottom)); }
.completion { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: max(18px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom)); }
.completion-backdrop { position: absolute; inset: 0; background: rgba(11, 32, 29, .66); }
.completion-dialog { position: relative; width: min(100%, 420px); padding: 30px 24px; border-radius: 17px; background: #fff; text-align: center; box-shadow: 0 30px 70px rgba(0, 0, 0, .22); }
.completion-seal { width: 61px; height: 61px; display: grid; place-items: center; margin: 0 auto 17px; border-radius: 50%; background: var(--ink); color: #f0d7b3; font-family: "Songti SC", serif; font-size: 32px; }
.completion-dialog h2 { margin-bottom: 9px; font-family: "Songti SC", "SimSun", serif; font-size: 28px; }
.completion-dialog p { color: var(--muted); font-size: 16px; line-height: 1.55; }
.complete-button { min-height: 48px; margin-top: 21px; padding: 8px 19px; border: 0; border-radius: 9px; background: var(--accent); color: #fff; font-size: 16px; font-weight: 800; }
.completion-actions { display: grid; justify-items: center; }

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