:root {
  --paper: #ffffff;
  --ink: #0b0b0b;
  --muted: #666666;
  --guide: #d0d0d0;
  --grid: #c8c8c8;
  --line: #dddddd;
  --page-pad: clamp(18px, 4vw, 56px);
  --max-width: 1280px;
}

* {
  box-sizing: border-box;
}

html {
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: #fff;
}

button,
a {
  font: inherit;
}

button {
  color: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: flex-end;
  width: min(var(--max-width), 100%);
  margin: 0 auto;
  padding: 24px var(--page-pad) 18px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid #eeeeee;
  backdrop-filter: blur(8px);
}

.view-switch {
  display: flex;
  align-items: center;
  gap: 11px;
  font-size: 18px;
  letter-spacing: 0.06em;
}

.view-switch__button {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 5px 0;
  cursor: pointer;
  color: #999;
  font-weight: 600;
}

.view-switch__button.is-active {
  color: #111;
}

.view-switch__divider {
  color: #c4c4c4;
}

.gallery {
  width: min(var(--max-width), 100%);
  margin: 0 auto;
  padding: clamp(34px, 6vw, 84px) var(--page-pad) 100px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(18px, 2.4vw, 34px);
}

.gallery-piece {
  position: relative;
  aspect-ratio: 1 / 1;
  min-width: 0;
  background: #fff;
  border: 1px solid var(--line);
  overflow: hidden;
}

.gallery-piece__button {
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
  background: #fff;
  cursor: pointer;
}

.gallery-piece__button:focus-visible,
.brush-piece:focus-visible,
.detail__close:focus-visible,
.resources__close:focus-visible,
.replay:focus-visible,
.view-switch__button:focus-visible {
  outline: 2px solid #111;
  outline-offset: 4px;
}

.gallery-piece__image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: grayscale(1);
}

.gallery-piece__image.is-missing {
  opacity: 0;
}

.practice-frame {
  position: relative;
  width: 100%;
  height: 100%;
  background: #fff;
  overflow: hidden;
}

.practice-frame::before,
.practice-frame::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

/* Square border + centre cross */
.practice-frame::before {
  background:
    linear-gradient(to right, transparent calc(50% - .5px), var(--grid) calc(50% - .5px), var(--grid) calc(50% + .5px), transparent calc(50% + .5px)),
    linear-gradient(to bottom, transparent calc(50% - .5px), var(--grid) calc(50% - .5px), var(--grid) calc(50% + .5px), transparent calc(50% + .5px));
}

/* Diagonals for a restrained 米字格 */
.practice-frame::after {
  background:
    linear-gradient(45deg, transparent calc(50% - .5px), var(--grid) calc(50% - .5px), var(--grid) calc(50% + .5px), transparent calc(50% + .5px)),
    linear-gradient(-45deg, transparent calc(50% - .5px), var(--grid) calc(50% - .5px), var(--grid) calc(50% + .5px), transparent calc(50% + .5px));
  opacity: .78;
}

.practice-frame > svg {
  position: relative;
  z-index: 2;
  display: block;
}

.gallery-piece[data-mode="strokes"] {
  cursor: default;
}

.gallery-piece[data-mode="strokes"] .gallery-piece__button {
  cursor: default;
  pointer-events: none;
}

.brush-section {
  width: min(900px, calc(100% - (var(--page-pad) * 2)));
  margin: 0 auto;
  padding: 120px 0 150px;
  border-top: 1px solid #ededed;
}

.brush-piece {
  display: block;
  width: min(620px, 100%);
  margin: 0 auto;
  padding: 0;
  border: 0;
  background: #fff;
  cursor: pointer;
}

.brush-piece img {
  display: block;
  width: 100%;
  max-height: 520px;
  object-fit: contain;
  filter: grayscale(1);
}

.detail,
.resources {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  background: #fff;
  overflow-y: auto;
}

.detail.is-open,
.resources.is-open {
  display: block;
}

.detail__close,
.resources__close {
  position: fixed;
  top: max(22px, env(safe-area-inset-top));
  right: max(24px, env(safe-area-inset-right));
  z-index: 110;
  width: 52px;
  height: 52px;
  border: 0;
  background: #fff;
  font-size: 44px;
  line-height: 1;
  font-weight: 300;
  cursor: pointer;
}

.detail__inner {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 30px;
  padding: 86px 24px 70px;
}

.practice-frame--large {
  width: min(68vw, 620px);
  height: min(68vw, 620px);
  max-width: calc(100vw - 48px);
  max-height: calc(100vw - 48px);
  border: 1px solid var(--line);
}

.detail__text {
  text-align: center;
  max-width: 720px;
}

.detail__pinyin {
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(30px, 4vw, 46px);
  line-height: 1.15;
  font-weight: 400;
}

.detail__meaning {
  margin-top: 10px;
  font-size: clamp(20px, 2.4vw, 28px);
  line-height: 1.4;
  color: #444;
}

.replay {
  border: 1px solid #cfcfcf;
  background: #fff;
  padding: 13px 20px;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: .06em;
  cursor: pointer;
}

.resources__inner {
  width: min(900px, calc(100% - 48px));
  margin: 0 auto;
  padding: 120px 0 90px;
}

.resources h1 {
  margin: 0 0 60px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(42px, 7vw, 78px);
  line-height: 1;
  font-weight: 400;
  letter-spacing: -0.03em;
}

.resources__list {
  display: grid;
  border-top: 1px solid #111;
}

.resources__list a {
  display: flex;
  align-items: center;
  min-height: 82px;
  border-bottom: 1px solid #d7d7d7;
  color: #111;
  text-decoration: none;
  font-size: clamp(21px, 3vw, 30px);
}

.resources__list a:hover {
  text-decoration: underline;
  text-underline-offset: 6px;
}

body.overlay-open {
  overflow: hidden;
}

@media (max-width: 980px) {
  .gallery {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  .topbar {
    padding-top: 18px;
    padding-bottom: 14px;
  }

  .view-switch {
    font-size: 17px;
  }

  .gallery {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    padding-top: 24px;
  }

  .practice-frame--large {
    width: min(88vw, 560px);
    height: min(88vw, 560px);
    max-width: calc(100vw - 32px);
    max-height: calc(100vw - 32px);
  }

  .detail__inner {
    gap: 24px;
    padding-inline: 16px;
  }

  .detail__close,
  .resources__close {
    right: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    scroll-behavior: auto !important;
  }
}
