:root {
  --bg-0: #1a120c;
  --bg-1: #2a1e14;
  --bg-2: #3a2a1d;
  --ink: #f4e8d3;
  --ink-dim: #c8b295;
  --ink-faint: #9c866c;
  --gold: #e9b761;
  --gold-deep: #c98d35;
  --cream: #f6ead1;
  --wood-line: #b98a52;
  --radius: 16px;
  --shadow: 0 18px 40px rgba(0, 0, 0, .45);
  --font-ui: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", system-ui, -apple-system, sans-serif;
  --font-serif: "Songti SC", "STSong", "Noto Serif SC", "SimSun", Georgia, serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: var(--font-ui);
  touch-action: manipulation;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  color: var(--ink);
  background:
    radial-gradient(120% 80% at 50% -10%, #5a4029 0%, rgba(90, 64, 41, 0) 55%),
    radial-gradient(90% 60% at 50% 105%, #3b2a1c 0%, rgba(59, 42, 28, 0) 60%),
    linear-gradient(180deg, #2b2016 0%, #1a120c 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: .05;
  background-image:
    repeating-linear-gradient(0deg, transparent 0 2px, #fff 2px 3px),
    repeating-linear-gradient(90deg, transparent 0 3px, #000 3px 4px);
  mix-blend-mode: overlay;
}

.app {
  width: min(100%, 640px);
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 18px calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ---------------- header ---------------- */
.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 2px 0;
}

.brand h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: clamp(26px, 6.4vw, 34px);
  font-weight: 700;
  letter-spacing: .16em;
  color: var(--ink);
  text-shadow: 0 2px 0 rgba(0, 0, 0, .35);
}

.brand p {
  margin: 4px 0 0 2px;
  font-size: 12px;
  letter-spacing: .32em;
  color: var(--ink-faint);
}

.timer {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 8px 12px;
  border-radius: 999px;
  background: rgba(20, 13, 8, .55);
  border: 1px solid rgba(233, 183, 97, .25);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .06);
  font-variant-numeric: tabular-nums;
  font-family: "SFMono-Regular", Consolas, "Roboto Mono", monospace;
  font-size: 18px;
  letter-spacing: .08em;
  color: #f7d79b;
}

.timer-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #7a6248;
  transition: background .3s;
}

.timer.running .timer-dot {
  background: #8fd694;
  box-shadow: 0 0 10px rgba(143, 214, 148, .8);
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .45; }
}

/* ---------------- date bar ---------------- */
.datebar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 2px 0 2px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(233, 183, 97, .22);
  background: rgba(255, 246, 230, .05);
  color: var(--ink-dim);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform .15s, background .2s, color .2s;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover { background: rgba(255, 246, 230, .1); color: var(--ink); }

.today-btn {
  border: 1px solid rgba(233, 183, 97, .3);
  background: rgba(233, 183, 97, .1);
  color: #e9c27c;
  border-radius: 999px;
  padding: 5px 11px;
  font-family: inherit;
  font-size: 11px;
  letter-spacing: .06em;
  cursor: pointer;
  white-space: nowrap;
}

.today-btn:hover { background: rgba(233, 183, 97, .18); }
.icon-btn:active { transform: scale(.92); }

.date-btn {
  display: flex;
  align-items: baseline;
  gap: 9px;
  padding: 6px 18px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}

.date-btn:hover { background: rgba(255, 246, 230, .06); }

.date-main {
  font-family: var(--font-serif);
  font-size: clamp(20px, 5vw, 25px);
  font-weight: 700;
  letter-spacing: .06em;
}

.date-sub {
  font-size: 13px;
  color: var(--gold);
  letter-spacing: .14em;
}

.date-pick-wrap {
  position: relative;
  display: inline-flex;
  justify-content: center;
}

.date-picker {
  position: absolute;
  inset: 0;
  font-size: 16px;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  touch-action: manipulation;
}

/* ---------------- board ---------------- */
.stage {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1 1 0;
  min-height: 110px;
  padding: 2px 0 0;
}

.board-wrap {
  position: relative;
  width: min(100%, 560px);
  aspect-ratio: 744 / 844;
}

#board {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell-label {
  font-family: var(--font-serif);
  font-weight: 600;
  fill: #5c3d1e;
  letter-spacing: .02em;
  pointer-events: none;
  user-select: none;
}

.cell-label.month { font-size: 33px; }
.cell-label.day { font-size: 33px; }
.cell-label.week { font-size: 23px; letter-spacing: .06em; }

.cell-label.is-target {
  fill: #a35f12;
  font-weight: 700;
}

.target-cell {
  fill: #ffe3a9;
  opacity: .55;
}

.target-ring {
  fill: none;
  stroke: #d99a34;
  stroke-width: 4;
  stroke-dasharray: 10 8;
  opacity: .95;
}

.target-glow {
  filter: url(#glow);
}

.piece {
  cursor: grab;
  filter: url(#pieceShadow);
}

.piece:active { cursor: grabbing; }

.piece-body {
  stroke: #6f4218;
  stroke-width: 5;
  stroke-linejoin: round;
}

.piece-bevel {
  fill: none;
  stroke: rgba(255, 252, 243, .35);
  stroke-width: 2;
  stroke-linejoin: round;
  pointer-events: none;
}

.preview-piece {
  pointer-events: none;
}

.preview-piece .piece-side { display: none; }

.preview-ok .piece-body {
  fill: rgba(122, 199, 138, .52);
  stroke: #4f915d;
  stroke-width: 5;
}

.preview-bad .piece-body {
  fill: rgba(224, 110, 88, .42);
  stroke: #a83d28;
  stroke-width: 5;
}

.preview-cell {
  fill: #8fd694;
  opacity: .26;
  pointer-events: none;
}

.preview-cell.bad { fill: #e2705a; opacity: .3; }

/* ---------------- status ---------------- */
.status {
  margin: 0;
  text-align: center;
  font-size: 12.5px;
  letter-spacing: .04em;
  color: var(--ink-faint);
  min-height: 18px;
}

.status b {
  color: var(--gold);
  font-weight: 600;
}

.status.flash {
  color: #f0d19a;
}

/* ---------------- tray ---------------- */
.tray-section {
  border-radius: var(--radius);
  padding: 10px 12px 12px;
  background: linear-gradient(180deg, rgba(255, 246, 230, .06), rgba(255, 246, 230, .02));
  border: 1px solid rgba(233, 183, 97, .14);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .05), 0 10px 24px rgba(0, 0, 0, .18);
}

.tray-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2px 6px;
  font-size: 11px;
  letter-spacing: .28em;
  color: var(--ink-faint);
}

.tray-title {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  letter-spacing: .2em;
}

.tray-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.tray-count {
  letter-spacing: .1em;
  font-variant-numeric: tabular-nums;
  color: var(--ink-dim);
}

.diff-select {
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid rgba(233, 183, 97, .24);
  background-color: rgba(255, 246, 230, .05);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%23c8a97a' stroke-width='1.4' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 7px center;
  color: #e5c184;
  font-family: inherit;
  font-size: 11px;
  letter-spacing: .08em;
  padding: 3px 20px 3px 9px;
  border-radius: 999px;
  cursor: pointer;
  outline: none;
}

.diff-select:hover { background-color: rgba(255, 246, 230, .1); }

.diff-select option {
  background: #2b2016;
  color: #f4e8d3;
  letter-spacing: 0;
}

.reroll-btn {
  border: 0;
  background: transparent;
  color: #c8a97a;
  font-family: inherit;
  font-size: 11px;
  letter-spacing: .06em;
  padding: 4px 2px;
  cursor: pointer;
  white-space: nowrap;
}

.reroll-btn:hover { color: #f2d49b; }
.reroll-btn:active { transform: scale(.96); }

.tray {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
}

.piece-btn {
  position: relative;
  flex: 0 0 calc((100% - 32px) / 9);
  min-width: 0;
  min-height: 0;
  aspect-ratio: 1 / 1;
  border: 0;
  border-radius: 12px;
  background: transparent;
  padding: 4px;
  cursor: grab;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  transition: background .18s, transform .18s, opacity .3s;
  display: grid;
  place-items: center;
}

.piece-btn svg {
  position: absolute;
  left: 4px;
  top: 4px;
  width: calc(100% - 8px);
  height: calc(100% - 8px);
  display: block;
  overflow: visible;
  filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .35));
}

.piece-btn:hover { background: rgba(255, 246, 230, .07); }
.piece-btn:active { cursor: grabbing; }

.piece-btn.is-placed {
  opacity: .16;
  pointer-events: none;
  filter: saturate(.4);
}

.piece-btn.is-active {
  background: rgba(233, 183, 97, .14);
  box-shadow: inset 0 0 0 1px rgba(233, 183, 97, .35);
}

/* ---------------- controls ---------------- */
.controls {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.ctl {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 9px 2px 8px;
  border-radius: 12px;
  border: 1px solid rgba(233, 183, 97, .12);
  background: rgba(255, 246, 230, .04);
  color: var(--ink-dim);
  font-family: inherit;
  font-size: 11px;
  letter-spacing: .08em;
  cursor: pointer;
  transition: background .18s, color .18s, transform .12s;
  -webkit-tap-highlight-color: transparent;
}

.ctl:hover { background: rgba(255, 246, 230, .09); color: var(--ink); }
.ctl:active { transform: scale(.95); }

.ctl-ico {
  font-size: 16px;
  line-height: 1;
  color: var(--gold);
}

.ctl:disabled {
  opacity: .35;
  pointer-events: none;
}

/* ---------------- drag ghost ---------------- */
.ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 60;
  pointer-events: none;
  opacity: 0;
  transition: opacity .12s;
  will-change: transform;
}

.ghost.on { opacity: 1; }

.ghost svg {
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 14px 18px rgba(0, 0, 0, .45));
}

/* ---------------- overlays ---------------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px;
  overflow-y: auto;
  background: rgba(14, 9, 6, .62);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.overlay[hidden] { display: none; }

.win-card, .help-card {
  position: relative;
  z-index: 2;
  max-height: calc(100dvh - 44px);
  overflow-y: auto;
  margin: auto;
  width: min(100%, 360px);
  border-radius: 24px;
  padding: 26px 24px 22px;
  background:
    radial-gradient(120% 80% at 50% 0%, #fff8e8 0%, #f3e2c1 55%, #e9d3ab 100%);
  color: #4a3218;
  box-shadow: 0 30px 70px rgba(0, 0, 0, .55), inset 0 1px 0 #fff;
  text-align: center;
  animation: pop .35s cubic-bezier(.2, .9, .3, 1.2);
}

@keyframes pop {
  from { transform: scale(.92) translateY(8px); opacity: 0; }
  to { transform: scale(1) translateY(0); opacity: 1; }
}

.win-spark {
  font-size: 26px;
  color: #d99a34;
  line-height: 1;
  margin-bottom: 6px;
}

.win-card h2, .help-card h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 26px;
  letter-spacing: .18em;
}

.win-date {
  margin: 8px 0 0;
  font-size: 13px;
  letter-spacing: .12em;
  color: #8a6a44;
}

.win-time {
  margin: 12px 0 4px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 44px;
  letter-spacing: .06em;
  color: #7a4f1c;
  font-variant-numeric: tabular-nums;
}

.win-note {
  margin: 0 0 18px;
  font-size: 12.5px;
  color: #8a6a44;
  min-height: 18px;
}

.win-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.btn {
  border: 1px solid rgba(90, 60, 25, .22);
  background: rgba(255, 255, 255, .5);
  color: #5a3f1e;
  padding: 10px 18px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 13px;
  letter-spacing: .08em;
  cursor: pointer;
  transition: transform .12s, background .2s;
}

.btn:hover { background: rgba(255, 255, 255, .8); }
.btn:active { transform: scale(.96); }

.btn.primary {
  background: linear-gradient(180deg, #e9b761, #d99a34);
  border-color: #b8802a;
  color: #3d2708;
  font-weight: 700;
  box-shadow: 0 6px 16px rgba(180, 120, 30, .35);
}

.help-card {
  width: min(100%, 420px);
  text-align: left;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.help-card h2 { text-align: center; margin-bottom: 14px; }

.rules {
  margin: 0 0 14px;
  padding-left: 20px;
  font-size: 13.5px;
  line-height: 1.85;
  color: #5a4326;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.rules li { margin-bottom: 4px; }
.rules b { color: #8a5a14; }

kbd {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid #c5a878;
  background: #fffaf0;
  font-family: inherit;
  font-size: 11px;
  color: #6b4d24;
  box-shadow: 0 1px 0 #d8c1a0;
}

.help-card .btn {
  display: block;
  margin: 0 auto;
  flex: 0 0 auto;
}

/* confetti */
.confetti {
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.confetti i {
  position: absolute;
  top: -12px;
  width: 7px;
  height: 11px;
  border-radius: 2px;
  opacity: 0;
  animation: fall 1.8s ease-in forwards;
}

@keyframes fall {
  0% { opacity: 1; transform: translateY(0) rotate(0deg); }
  100% { opacity: 0; transform: translateY(70vh) rotate(540deg); }
}

/* ---------------- responsive ---------------- */
@media (max-width: 360px) {
  .tray { flex-wrap: wrap; }
  .piece-btn { flex-basis: calc((100% - 8px) / 5); }
}

@media (max-width: 520px) {
  .app { padding-left: 10px; padding-right: 10px; gap: 6px; }
  .tray { gap: 2px; }
  .tray-section { padding: 7px 9px 9px; border-radius: 14px; }
  .tray-head { padding-bottom: 4px; }
  .controls { grid-template-columns: repeat(6, 1fr); gap: 4px; }
  .ctl { padding: 6px 0 5px; font-size: 10px; border-radius: 10px; }
  .ctl-ico { font-size: 14px; }
  .brand h1 { letter-spacing: .1em; font-size: clamp(24px, 6.4vw, 30px); }
  .brand p { font-size: 10px; letter-spacing: .24em; margin-top: 2px; }
  .timer { font-size: 15px; padding: 6px 11px; }
  .datebar { gap: 6px; padding: 0; }
  .icon-btn { width: 34px; height: 34px; font-size: 21px; }
  .date-main { font-size: 20px; }
  .date-sub { font-size: 12px; }
  .status { font-size: 11.5px; min-height: 16px; }
  .top { padding-top: 2px; }
}

/* 高度够用的手机：木块恢复两排，操作更从容 */
@media (max-width: 520px) and (min-height: 740px) {
  .tray { flex-wrap: wrap; }
  .piece-btn { flex-basis: calc((100% - 8px) / 5); }
}

@media (min-width: 521px) {
  .board-wrap { max-width: min(100%, 560px); }
}

@media (min-width: 521px) and (max-height: 639px) {
  .app { gap: 6px; padding-top: 8px; padding-bottom: 8px; }
  .brand h1 { font-size: 24px; }
  .brand p { display: none; }
  .datebar { padding: 0; }
  .tray-section { padding: 6px 10px 8px; }
  .piece-btn { flex-basis: calc((100% - 32px) / 9); }
}

@media (max-height: 520px) and (orientation: landscape) {
  .stage { min-height: 320px; }
  .app { gap: 6px; padding-top: 8px; padding-bottom: 8px; }
  .brand p { display: none; }
  .datebar { padding: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}
