:root {
  --bg: #f3efe6;
  --ink: #1d1a16;
  --muted: #6b645b;
  --card: #fffdf8;
  --line: #e4dccf;
  --accent: #1f7a6c;
  --accent-ink: #f6fffd;
  --warn: #b42318;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 10% -10%, rgba(31, 122, 108, 0.12), transparent 50%),
    var(--bg);
}

button, input { font: inherit; }
button { cursor: pointer; }
.is-hidden { display: none !important; }

.page { min-height: 100%; }
#home { width: min(1120px, calc(100% - 32px)); margin: 0 auto; padding: 28px 0 48px; }

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 22px;
}
.brand { display: flex; gap: 12px; align-items: center; }
.mark {
  width: 48px; height: 48px; display: block;
  border-radius: 10px; object-fit: cover;
}
.brand h1 { margin: 0; letter-spacing: 0.18em; font-size: 28px; font-weight: 650; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.upload {
  background: var(--ink); color: var(--card);
  padding: 10px 16px; display: inline-block;
}
.upload input { display: none; }

.error { min-height: 1.2em; color: var(--warn); margin: 0 0 12px; }

.diffs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 22px; }
.diff {
  border: 1px solid var(--line); background: var(--card);
  color: var(--ink); padding: 10px 14px; min-width: 92px;
}
.diff small { display: block; color: var(--muted); }
.diff.is-on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.diff.is-on small { color: rgba(246, 255, 253, 0.8); }

h2 { font-size: 16px; font-weight: 600; margin: 0 0 12px; }
.style-hint { margin: -8px 0 8px; color: var(--muted); font-size: 12px; }
.styles { margin-bottom: 18px; }
.empty { color: var(--muted); padding: 28px 4px; }

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 12px;
}
.card {
  padding: 0; border: 1px solid var(--line); background: var(--card);
  text-align: left; overflow: hidden;
}
.card img { width: 100%; aspect-ratio: 4 / 3; object-fit: contain; display: block; background: #eee8dc; }
.card span { display: block; padding: 8px 10px 10px; font-size: 13px; }
.card:hover { border-color: var(--accent); }

#play { height: 100%; display: flex; flex-direction: column; overflow: auto; }
#play.is-hidden { display: none !important; }

.board-wrap {
  flex: 1; position: relative; min-height: 0;
  display: flex; width: 100%;
}
.pause-mask {
  position: absolute; inset: 0; z-index: 4;
  display: grid; place-items: center;
  overflow: hidden;
  background: #1d1a16;
  cursor: pointer;
}
#pauseBtn.is-on {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
}
.pause-mask canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
}
.pause-mask span {
  position: relative; z-index: 1;
  color: #fffdf8; font-size: 22px; letter-spacing: 0.28em; font-weight: 650;
  text-shadow: 0 2px 16px rgba(0, 0, 0, 0.7);
  background: rgba(29, 26, 22, 0.45);
  padding: 10px 22px;
}

.play-bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; flex-wrap: nowrap;
  padding: 10px 14px;
  background: rgba(255, 253, 248, 0.92);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 30; flex-shrink: 0;
  overflow-x: auto;
}
#backBtn, #playSession { flex-shrink: 0; }
.play-meta {
  display: flex; gap: 14px; align-items: center;
  flex: 1; min-width: 0; flex-wrap: nowrap;
}
.play-identity {
  display: flex; flex-direction: column; justify-content: center;
  align-items: center;
  text-align: center;
  min-width: 0; flex: 1; gap: 1px; line-height: 1.25;
}
.play-identity strong {
  letter-spacing: 0.06em;
  width: 100%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.play-credit {
  font-size: 12px; color: var(--muted);
  width: 100%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.play-credit:empty { display: none; }
#timer, #progress { flex-shrink: 0; white-space: nowrap; }
#timer { font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 700; }
.session-stat {
  font-size: 13px; color: var(--accent); font-variant-numeric: tabular-nums;
  white-space: nowrap; font-weight: 650;
}
.play-actions { display: flex; gap: 8px; flex-wrap: nowrap; flex-shrink: 0; }
.play-actions-gap { width: 20px; flex: 0 0 20px; }
.ghost {
  background: var(--card); border: 1px solid var(--line);
  color: var(--ink); padding: 8px 12px;
}
.ghost:hover { border-color: var(--accent); }

#board {
  flex: 1; width: 100%; height: 100%; display: block;
  background:
    linear-gradient(90deg, rgba(0,0,0,0.03) 1px, transparent 1px),
    linear-gradient(rgba(0,0,0,0.03) 1px, transparent 1px),
    #efe8da;
  background-size: 32px 32px;
  touch-action: none;
}

.hint-line {
  margin: 0; padding: 8px 14px;
  font-size: 12px; color: var(--muted);
  background: var(--card); border-top: 1px solid var(--line);
}

.done-bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  padding: 10px 14px;
  background: var(--card); border-top: 1px solid var(--line);
}
.done-copy { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.done-copy strong { color: var(--accent); }
.done-copy span { color: var(--muted); font-size: 13px; }
.done-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.ref-win {
  position: fixed; right: 16px; top: 72px; width: 180px;
  background: var(--card); border: 1px solid var(--line);
  box-shadow: 0 12px 32px rgba(29, 26, 22, 0.12); z-index: 8;
}
.ref-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 8px; cursor: move; font-size: 12px; background: #f7f2e8;
}
  .ref-head button {
    border: 0; background: transparent; color: var(--accent);
    cursor: pointer; pointer-events: auto; position: relative; z-index: 1;
    padding: 4px 6px;
  }
.ref-win img { width: 100%; display: block; }
.ref-win.is-min img { display: none; }

.intro-win {
  position: fixed; left: 12px; right: auto; top: 76px; width: min(280px, calc(100vw - 24px));
  background: var(--card); border: 2px solid var(--accent);
  box-shadow: 0 12px 32px rgba(29, 26, 22, 0.18); z-index: 9;
  max-height: min(48vh, 360px); overflow: auto;
}
.intro-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 8px; cursor: move; font-size: 12px; background: #f7f2e8;
}
.intro-head button {
  border: 0; background: transparent; color: var(--accent);
  cursor: pointer; pointer-events: auto; position: relative; z-index: 1;
  padding: 4px 6px;
}
.intro-body {
  margin: 0; padding: 10px 12px 14px;
  font-size: 13px; line-height: 1.65; color: var(--ink);
}

.intro-source {
  margin: 0; padding: 0 12px 14px;
  font-size: 12px; line-height: 1.5; color: var(--muted);
}

.overlay {
  position: fixed; inset: 0; background: rgba(29, 26, 22, 0.45);
  display: grid; place-items: center; z-index: 40; padding: 16px;
}
.dialog {
  width: min(420px, 100%); background: var(--card); padding: 24px;
  border: 1px solid var(--line);
}
.dialog h3 { margin: 0 0 8px; font-size: 22px; }
.dialog p { margin: 0 0 18px; color: var(--muted); line-height: 1.6; }
.dialog-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.load-dialog {
  width: min(320px, 100%);
  text-align: center;
  padding: 28px 28px 22px;
}
.load-dialog h3 { margin: 0 0 6px; }
.load-dialog p { margin: 0; }
.load-board {
  width: 132px;
  height: 132px;
  margin: 0 auto 18px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 3px;
}
.load-board span {
  display: block;
  border-radius: 5px;
  background: linear-gradient(160deg, #3aa392, #1f7a6c);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.22) inset;
  opacity: 0;
  transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(0.72);
  animation: load-snap 2.1s ease-in-out infinite;
}
.load-board span:nth-child(1) { --dx: -36px; --dy: -30px; --rot: -22deg; animation-delay: 0s; }
.load-board span:nth-child(2) { --dx: 0; --dy: -38px; --rot: 8deg; animation-delay: 0.08s; background: #2c8f7f; }
.load-board span:nth-child(3) { --dx: 34px; --dy: -28px; --rot: 18deg; animation-delay: 0.16s; }
.load-board span:nth-child(4) { --dx: -40px; --dy: 4px; --rot: -12deg; animation-delay: 0.24s; background: #247a6c; }
.load-board span:nth-child(5) { --dx: 8px; --dy: 10px; --rot: 6deg; animation-delay: 0.32s; background: #c4a35a; }
.load-board span:nth-child(6) { --dx: 38px; --dy: 6px; --rot: 14deg; animation-delay: 0.4s; background: #2c8f7f; }
.load-board span:nth-child(7) { --dx: -32px; --dy: 32px; --rot: -16deg; animation-delay: 0.48s; }
.load-board span:nth-child(8) { --dx: 2px; --dy: 36px; --rot: -6deg; animation-delay: 0.56s; background: #247a6c; }
.load-board span:nth-child(9) { --dx: 30px; --dy: 30px; --rot: 20deg; animation-delay: 0.64s; }
@keyframes load-snap {
  0%, 8% {
    opacity: 0;
    transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(0.72);
  }
  22%, 70% {
    opacity: 1;
    transform: none;
  }
  86%, 100% {
    opacity: 0;
    transform: scale(0.9);
  }
}
.primary {
  background: var(--accent); color: var(--accent-ink);
  border: 0; padding: 10px 16px;
}

@media (max-width: 767px) {
  .top { align-items: flex-start; }
  .gallery { grid-template-columns: repeat(2, 1fr); }
  .play-bar { padding: 8px; }
  .ref-win { width: 120px; right: 8px; top: auto; bottom: 48px; }
  .intro-win { left: 8px; top: 72px; width: min(240px, calc(100vw - 16px)); }
  .hint-line { display: none; }
}
