:root {
  --bg: #f2f3f5;
  --fg: #1a1a2e;
  --tile-empty: #ffffff;
  --tile-border: #d3d6da;
  --tile-gray: #787c7e;
  --tile-green: #6aaa64;
  --tile-yellow: #c9b458;
  --tile-text: #1a1a2e;
  --tile-text-light: #ffffff;
  --accent: #5b5bd6;
  --toast-bg: #1a1a2e;
  --toast-fg: #f2f3f5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121213;
    --fg: #f2f3f5;
    --tile-empty: #121213;
    --tile-border: #3a3a3c;
    --tile-gray: #3a3a3c;
    --tile-green: #538d4e;
    --tile-yellow: #b59f3b;
    --tile-text: #f2f3f5;
    --accent: #8b8bf5;
    --toast-bg: #f2f3f5;
    --toast-fg: #121213;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

header {
  width: 100%;
  max-width: 480px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 8px;
  text-align: center;
}

h1 { font-size: 1.3rem; letter-spacing: 2px; }

.hint {
  margin-top: 10px;
  font-size: 1rem;
  color: var(--fg);
  background: var(--tile-empty);
  border: 1px solid var(--tile-border);
  border-radius: 10px;
  padding: 8px 12px;
}

.toolbar {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.toolbar button {
  flex: 1;
  min-height: 40px;
  font-size: 0.9rem;
  border: 2px solid var(--tile-border);
  border-radius: 10px;
  background: var(--tile-empty);
  color: var(--fg);
  cursor: pointer;
}

.toolbar button:active { border-color: var(--accent); }

.share-btn {
  border: none;
  background: transparent;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 2px 4px;
  vertical-align: middle;
}

.assist-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.assist-row button {
  min-height: 36px;
  padding: 2px 16px;
  font-size: 0.88rem;
  border: 2px dashed var(--tile-border);
  border-radius: 10px;
  background: var(--tile-empty);
  color: var(--fg);
  cursor: pointer;
}

.assist-row button:active { border-color: var(--accent); }

.difficulty {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.diff-label {
  font-size: 0.85rem;
  opacity: 0.75;
  flex-shrink: 0;
}

.diff-btns {
  display: flex;
  flex: 1;
}

.diff-btns button {
  flex: 1;
  min-height: 36px;
  font-size: 0.88rem;
  border: 2px solid var(--tile-border);
  background: var(--tile-empty);
  color: var(--fg);
  cursor: pointer;
}

.diff-btns button + button { border-left: none; }
.diff-btns button:first-child { border-radius: 10px 0 0 10px; }
.diff-btns button:last-child { border-radius: 0 10px 10px 0; }

.diff-btns button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.diff-btns button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

main {
  width: 100%;
  max-width: 480px;
  padding: 8px 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(5px, 1.6vw, 8px);
  width: min(88vw, 400px);
  margin-top: 12px;
}

.cell {
  aspect-ratio: 1 / 1.05;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--tile-border);
  border-radius: 8px;
  background: var(--tile-empty);
  font-size: clamp(24px, 8vw, 36px);
  font-weight: 700;
  line-height: 1;
  user-select: none;
  transition: background 0.2s ease;
}

.cell .sub {
  margin-top: 3px;
  font-size: clamp(10px, 3.4vw, 14px);
  font-weight: 400;
  line-height: 1;
  opacity: 0.92;
  min-height: 1em;
}

.cell.empty .sub { opacity: 0; }

.hint-chars {
  width: min(92vw, 420px);
  margin-top: 12px;
}

.hint-chars-title {
  font-size: 0.8rem;
  opacity: 0.75;
  margin-bottom: 6px;
  text-align: center;
}

.hint-chars-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-height: 150px;
  overflow-y: auto;
}

.hint-chars-list button {
  min-height: 38px;
  min-width: 44px;
  padding: 2px 8px;
  font-size: 1.05rem;
  border: 2px solid var(--tile-border);
  border-radius: 8px;
  background: var(--tile-empty);
  color: var(--fg);
  cursor: pointer;
  line-height: 1;
}

.hint-chars-list button .stk {
  font-size: 0.65rem;
  opacity: 0.6;
  margin-left: 2px;
}

.hint-chars-list button.impossible {
  opacity: 0.3;
}

.hint-chars-list button:active { border-color: var(--accent); }

.cell.hint-pos {
  color: var(--tile-gray);
  border-style: dashed;
  font-size: clamp(18px, 6vw, 26px);
  opacity: 0.6;
}

.idiom-list {
  width: min(92vw, 420px);
  margin-top: 10px;
}

.idiom-list-body {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-height: 160px;
  overflow-y: auto;
}

.idiom-list-body button {
  min-height: 36px;
  padding: 2px 10px;
  font-size: 0.95rem;
  border: 2px solid var(--tile-border);
  border-radius: 8px;
  background: var(--tile-empty);
  color: var(--fg);
  cursor: pointer;
}

.idiom-list-body button:active { border-color: var(--accent); }

.cell.g { background: var(--tile-green); color: var(--tile-text-light); border-color: var(--tile-green); }
.cell.y { background: var(--tile-yellow); color: var(--tile-text); border-color: var(--tile-yellow); }
.cell.n { background: var(--tile-gray); color: var(--tile-text-light); border-color: var(--tile-gray); }

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 10px;
  margin: 14px 0 4px;
  font-size: 0.78rem;
  color: var(--fg);
  opacity: 0.85;
}

.chip { white-space: nowrap; }

.input-area {
  position: sticky;
  bottom: 0;
  width: 100%;
  max-width: 480px;
  background: var(--bg);
  z-index: 5;
}

.input-area .idiom-list {
  width: 100%;
  margin: 0;
  padding: 8px 16px 0;
  border-top: 1px solid var(--tile-border);
  background: var(--bg);
}

.input-area .idiom-list-body {
  max-height: 132px;
}

.input-row {
  display: flex;
  gap: 8px;
  width: 100%;
  margin-top: 10px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--tile-border);
}

#guess-input {
  flex: 1;
  min-height: 48px;
  font-size: 1.1rem;
  text-align: center;
  letter-spacing: 6px;
  border: 2px solid var(--tile-border);
  border-radius: 10px;
  background: var(--tile-empty);
  color: var(--fg);
  outline: none;
  padding: 0 8px;
}

#guess-input:focus { border-color: var(--accent); }

#submit-btn {
  min-width: 56px;
  min-height: 48px;
  font-size: 1rem;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #ffffff;
  cursor: pointer;
}

#submit-btn:disabled { opacity: 0.4; }

.toast {
  position: fixed;
  left: 50%;
  top: 12vh;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 0.95rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 10;
  max-width: 80vw;
  text-align: center;
}

.toast.show { opacity: 1; }

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
  z-index: 20;
  padding: 16px;
}

.modal-box {
  position: relative;
  width: min(90vw, 420px);
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--tile-border);
  border-radius: 16px;
  padding: 22px 18px 18px;
  text-align: center;
  animation: modal-in 0.18s ease-out;
  max-height: 85vh;
  overflow-y: auto;
}

@keyframes modal-in {
  from { transform: scale(0.94); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-close {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--fg);
  font-size: 1rem;
  cursor: pointer;
  opacity: 0.6;
}

.modal-close:active { opacity: 1; }

.modal-title {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 6px;
}

.modal-answer {
  font-size: clamp(2rem, 12vw, 2.6rem);
  font-weight: 700;
  letter-spacing: 6px;
  margin: 8px 0;
}

.modal-meta {
  font-size: 0.95rem;
  opacity: 0.85;
  margin-bottom: 10px;
}

.modal-exp {
  font-size: 0.92rem;
  line-height: 1.7;
  opacity: 0.9;
}

.modal-detail {
  font-size: 0.8rem;
  opacity: 0.7;
  margin-top: 10px;
}

footer {
  width: 100%;
  max-width: 480px;
  padding: 10px 16px calc(16px + env(safe-area-inset-bottom));
  text-align: center;
  font-size: 0.7rem;
  opacity: 0.6;
}

#version {
  display: block;
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}
