:root {
  --bg: #f0ebe0;
  --paper: #fffdfa;
  --paper-warm: #faf6ee;
  --line: #d9c9ad;
  --line-strong: #c4ad8a;
  --ink: #2f2a24;
  --accent: #b4462b;
  --accent-soft: #f4e0d6;
  --example: #8a7f6e;
  --score-high: #3d7a52;
  --score-mid: #b07a28;
  --score-low: #b4462b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Noto Serif SC", "Source Han Serif SC", "KaiTi", serif;
  background: linear-gradient(165deg, #fbf7ef 0%, var(--bg) 45%, #e8dfd0 100%);
  color: var(--ink);
  min-height: 100vh;
}

.app-shell {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 14px calc(20px + env(safe-area-inset-bottom));
  display: grid;
  gap: 12px;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 4px 2px;
}

.top-bar h1 {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.08em;
}

.top-bar p {
  margin: 4px 0 0;
  color: #6a6357;
  font-size: 0.88rem;
}

.badge-btn {
  flex-shrink: 0;
  white-space: nowrap;
}

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 10px 28px rgba(78, 58, 28, 0.07);
}

.toolbar-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 14px;
  align-items: end;
}

.field-block label {
  display: block;
  font-size: 0.88rem;
  margin-bottom: 6px;
  color: #5c5549;
}

.field-block textarea {
  width: 100%;
  min-height: 88px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 1.05rem;
  line-height: 1.55;
  background: #fff;
}

.field-narrow {
  min-width: 140px;
  display: grid;
  gap: 8px;
}

.field-narrow input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 1rem;
  text-align: center;
  background: #fff;
}

.toolbar-hint {
  margin: 10px 0 0;
  font-size: 0.84rem;
}

.main-layout {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(320px, 420px);
  gap: 14px;
  align-items: start;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.panel-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.copybook-panel {
  background: linear-gradient(180deg, #fffdf9 0%, var(--paper-warm) 100%);
}

.copybook-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-x: auto;
}

.copybook-header,
.copybook-row {
  display: grid;
  gap: 6px;
  min-width: min-content;
}

.copybook-header {
  margin-bottom: 2px;
}

.copybook-header span {
  font-size: 0.72rem;
  color: #8a8174;
  text-align: center;
  padding: 2px 0;
}

.copybook-cell {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  aspect-ratio: 1;
  min-width: 52px;
  min-height: 52px;
  padding: 0;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
}

.copybook-cell::before {
  content: "";
  position: absolute;
  inset: 4px;
  border: 1px solid #ebe2d2;
  border-radius: 4px;
  pointer-events: none;
}

.copybook-cell::after {
  content: "";
  position: absolute;
  inset: 4px;
  background: linear-gradient(
      to bottom right,
      transparent calc(50% - 0.5px),
      #efe6d8 calc(50% - 0.5px),
      #efe6d8 calc(50% + 0.5px),
      transparent calc(50% + 0.5px)
    ),
    linear-gradient(
      to bottom left,
      transparent calc(50% - 0.5px),
      #efe6d8 calc(50% - 0.5px),
      #efe6d8 calc(50% + 0.5px),
      transparent calc(50% + 0.5px)
    );
  pointer-events: none;
  opacity: 0.85;
}

.copybook-cell.example {
  cursor: default;
  background: #f7f1e6;
  border-color: var(--line-strong);
}

.copybook-cell.example .cell-char {
  color: var(--ink);
  font-size: 1.65rem;
  font-weight: 600;
  z-index: 1;
}

.copybook-cell.practice .cell-char.ghost {
  color: #d4cabb;
  font-size: 1.35rem;
  z-index: 0;
  pointer-events: none;
}

.cell-ink-canvas {
  position: absolute;
  inset: 4px;
  width: calc(100% - 8px);
  height: calc(100% - 8px);
  z-index: 1;
  pointer-events: none;
  border-radius: 4px;
  display: block;
}

.copybook-cell.practice:hover {
  border-color: #c9a87a;
  box-shadow: 0 4px 12px rgba(120, 88, 40, 0.12);
}

.copybook-cell.practice.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft), 0 6px 16px rgba(180, 70, 43, 0.18);
  transform: translateY(-1px);
}

.copybook-cell.practice.draft {
  background: #fff9f2;
}

.copybook-cell.practice.scored {
  background: #fffaf5;
}

.cell-score {
  position: absolute;
  inset: auto 4px 4px auto;
  z-index: 2;
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1;
  padding: 3px 6px;
  border-radius: 6px;
  background: rgba(47, 42, 36, 0.88);
  color: #fff;
}

.copybook-cell.score-high .cell-score {
  background: var(--score-high);
}

.copybook-cell.score-mid .cell-score {
  background: var(--score-mid);
}

.copybook-cell.score-low .cell-score {
  background: var(--score-low);
}

.write-panel .panel-head h2 {
  font-size: 1.1rem;
}

#write-cell-meta {
  margin: 4px 0 0;
  font-size: 0.82rem;
}

.grid-wrapper {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  overflow: hidden;
  touch-action: none;
  position: relative;
  background: #fffdf8;
}

#grid-canvas {
  width: 100%;
  height: auto;
  display: block;
}

#overlay-target {
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#overlay-target.active {
  opacity: 0.42;
}

#overlay-target canvas {
  width: 100%;
  height: 100%;
  display: block;
}

#demo-stroke-host {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 253, 248, 0.94);
  pointer-events: none;
}

.write-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.write-actions #submit-btn {
  grid-column: 1 / -1;
}

button {
  appearance: none;
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  padding: 11px 12px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  font-family: inherit;
}

button.secondary {
  background: var(--accent-soft);
  color: #5b3025;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.feedback-inline {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
  font-size: 0.9rem;
}

.muted {
  color: #756e61;
  margin: 0;
}

.error {
  color: #9b2f16;
}

.score-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.tag {
  font-size: 0.78rem;
  color: #6a6358;
  background: #f3ece0;
  border-radius: 999px;
  padding: 3px 10px;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin: 8px 0;
  color: #4c463d;
  font-size: 0.86rem;
}

.metrics span {
  background: #faf5ec;
  border-radius: 8px;
  padding: 6px 8px;
  text-align: center;
}

.feedback-inline ul {
  margin: 8px 0 0;
  padding-left: 18px;
}

#standard-target {
  position: fixed;
  left: -9999px;
  top: -9999px;
  width: 360px;
  height: 360px;
  opacity: 0;
  pointer-events: none;
}

.debug-details summary {
  cursor: pointer;
  font-weight: 600;
  margin-bottom: 8px;
}

.debug-image-wrap {
  margin: 10px 0 12px;
  padding: 10px;
  border: 1px solid #e3d4bb;
  border-radius: 10px;
  background: #fffdf8;
}

.debug-image-label {
  margin: 0 0 8px;
  font-size: 13px;
  color: #7d756a;
}

#debug-request-image {
  display: block;
  width: 100%;
  max-width: 860px;
  height: auto;
  border-radius: 8px;
  border: 1px solid #e8ddca;
}

#debug-log {
  margin: 8px 0 0;
  padding: 10px;
  border: 1px solid #e3d4bb;
  border-radius: 10px;
  background: #fffaf0;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 360px;
  overflow: auto;
}

@media (max-width: 900px) {
  .main-layout {
    grid-template-columns: 1fr;
  }

  .copybook-panel {
    order: 1;
  }

  .write-panel {
    order: 0;
  }
}

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

  .copybook-cell {
    min-width: 46px;
    min-height: 46px;
  }
}

/* 评分鼓励弹层 */
.score-cheer-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.28s ease;
}

.score-cheer-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.score-cheer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(24, 20, 16, 0.55);
  backdrop-filter: blur(4px);
}

.score-cheer-dialog {
  position: relative;
  width: min(92vw, 360px);
  padding: 18px 16px 16px;
  border-radius: 20px;
  background: linear-gradient(180deg, #fffef9 0%, #faf3e6 100%);
  border: 2px solid #e8d5b8;
  box-shadow: 0 24px 60px rgba(40, 28, 12, 0.35);
  text-align: center;
  transform: scale(0.72) translateY(24px);
  opacity: 0;
  transition:
    transform 0.45s cubic-bezier(0.34, 1.45, 0.64, 1),
    opacity 0.28s ease;
  overflow: hidden;
}

.score-cheer-overlay.visible .score-cheer-dialog {
  transform: scale(1) translateY(0);
  opacity: 1;
}

.score-cheer-overlay.cheer-good .score-cheer-dialog {
  border-color: #e8c56a;
  box-shadow:
    0 0 0 1px rgba(255, 220, 120, 0.5),
    0 24px 60px rgba(120, 80, 20, 0.28);
}

.score-cheer-overlay.cheer-comeon .score-cheer-dialog {
  border-color: #b8c4e8;
  box-shadow:
    0 0 0 1px rgba(160, 180, 255, 0.4),
    0 24px 50px rgba(30, 40, 90, 0.25);
}

.score-cheer-glow {
  position: absolute;
  inset: -30%;
  pointer-events: none;
  opacity: 0;
  animation: cheer-glow-pulse 2s ease-in-out infinite;
}

.score-cheer-overlay.visible .score-cheer-glow {
  opacity: 1;
}

.cheer-good .score-cheer-glow {
  background: radial-gradient(circle, rgba(255, 214, 90, 0.45) 0%, transparent 62%);
}

.cheer-comeon .score-cheer-glow {
  background: radial-gradient(circle, rgba(120, 160, 255, 0.35) 0%, transparent 65%);
}

.score-cheer-particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.cheer-particle {
  position: absolute;
  top: -12px;
  opacity: 0;
  animation: cheer-particle-fall var(--fall-dur, 2.4s) var(--fall-delay, 0s) ease-in forwards;
}

.cheer-particle.confetti {
  width: var(--size, 8px);
  height: calc(var(--size, 8px) * 0.55);
  border-radius: 2px;
}

.cheer-particle.sparkle {
  width: var(--size, 6px);
  height: var(--size, 6px);
  border-radius: 50%;
  box-shadow: 0 0 8px currentColor;
}

.score-cheer-image-wrap {
  position: relative;
  z-index: 1;
  margin: 0 auto 10px;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  animation: cheer-image-pop 0.55s cubic-bezier(0.34, 1.4, 0.64, 1) 0.12s both;
}

.score-cheer-image {
  display: block;
  width: 100%;
  max-width: 300px;
  height: auto;
  margin: 0 auto;
}

.score-cheer-caption {
  position: relative;
  z-index: 1;
  margin: 0 0 12px;
  font-size: 1.05rem;
  font-weight: 700;
  color: #3d362c;
}

.cheer-good .score-cheer-caption {
  color: #8a5a12;
}

.cheer-comeon .score-cheer-caption {
  color: #3a4a7a;
}

.score-cheer-close {
  position: absolute;
  top: 8px;
  right: 10px;
  z-index: 3;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  color: #6a6358;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.score-cheer-ok {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 220px;
}

@keyframes cheer-glow-pulse {
  0%,
  100% {
    transform: scale(0.95);
    opacity: 0.75;
  }
  50% {
    transform: scale(1.05);
    opacity: 1;
  }
}

@keyframes cheer-image-pop {
  0% {
    transform: scale(0.85);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes cheer-particle-fall {
  0% {
    transform: translate3d(0, 0, 0) rotate(0deg);
    opacity: 0;
  }
  8% {
    opacity: 1;
  }
  100% {
    transform: translate3d(var(--drift, 0px), 420px, 0) rotate(var(--spin, 180deg));
    opacity: 0;
  }
}
