/* 仓库番主题：纸色底、墨线描边、硬阴影 */
:root {
  --paper: #ece2cb;
  --paper-line: rgba(90, 70, 50, 0.07);
  --card: #faf4e4;
  --ink: #2a211b;
  --ink-soft: #5d4e40;
  --brick: #9b4331;
  --wood: #c9893d;
  --wood-dark: #8e5a24;
  --hat: #f0b429;
  --green: #5d7f2e;
  --green-soft: #dfe7c6;
  --red: #b3321f;
  --blue: #2f5d8a;
  --shadow: 4px 4px 0 var(--ink);
  --shadow-sm: 2px 2px 0 var(--ink);
  --font: "Microsoft YaHei UI", "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", sans-serif;
  --mono: Consolas, "Cascadia Mono", "SFMono-Regular", Menlo, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background-color: var(--paper);
  /* 仓库地面的方格线 */
  background-image:
    linear-gradient(var(--paper-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--paper-line) 1px, transparent 1px);
  background-size: 40px 40px;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

h1, h2, h3, h4 {
  margin: 0;
}

/* ---------- 返回游戏中心 ---------- */
.home-link {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 1000;
  padding: 7px 14px;
  border: 2px solid var(--ink);
  border-radius: 3px;
  background: var(--card);
  color: var(--ink);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  box-shadow: var(--shadow-sm);
}

.home-link:hover {
  background: #fff;
}

.home-link:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.home-link:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 3px;
}

/* ---------- 按钮 ---------- */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 6px 14px;
  border: 2px solid var(--ink);
  border-radius: 3px;
  background: var(--card);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  box-shadow: var(--shadow-sm);
}

.btn:hover:not(:disabled) {
  background: #fff;
}

.btn:active:not(:disabled) {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.btn:focus-visible,
.modal-close:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 3px;
}

.btn:disabled {
  color: rgba(42, 33, 27, 0.4);
  border-color: rgba(42, 33, 27, 0.35);
  box-shadow: none;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--hat);
}

.btn-primary:hover:not(:disabled) {
  background: #f7c64e;
}

.btn-lg {
  min-height: 50px;
  justify-content: flex-start;
  padding: 10px 18px;
  font-size: 17px;
  box-shadow: var(--shadow);
}

.btn-lg:active:not(:disabled) {
  transform: translate(4px, 4px);
}

.btn-icon {
  width: 38px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

/* ---------- 通用屏幕 ---------- */
.screen {
  min-height: 100vh;
  min-height: 100dvh;
  padding: 64px 16px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.bar {
  width: 100%;
  max-width: 960px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.bar h2 {
  font-size: 22px;
  font-weight: 800;
}

.bar-info {
  font-family: var(--mono);
  font-size: 14px;
  color: var(--ink-soft);
}

.bar-group {
  display: flex;
  gap: 10px;
}

/* ---------- 主界面 ---------- */
.screen-menu {
  justify-content: center;
}

.menu-card {
  width: 100%;
  max-width: 380px;
  padding: 24px 24px 26px;
  border: 3px solid var(--ink);
  border-radius: 4px;
  background: var(--card);
  box-shadow: 8px 8px 0 var(--ink);
}

.menu-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 14px;
  border-bottom: 2px solid var(--ink);
}

.menu-head h1 {
  font-size: 40px;
  font-weight: 900;
  letter-spacing: 4px;
}

.menu-sub {
  margin: 0;
  font-size: 13px;
  color: var(--ink-soft);
}

.menu-preview {
  margin: 16px 0 0;
  padding: 12px;
  border: 2px solid var(--ink);
  background: var(--paper);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#menuPreview {
  display: block;
}

.menu-preview figcaption {
  font-size: 13px;
  font-weight: 700;
  color: var(--ink-soft);
}

.menu-progress {
  margin: 16px 0 20px;
}

.menu-progress-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  font-size: 14px;
}

.menu-progress-row b {
  font-family: var(--mono);
}

.progress-bar {
  height: 12px;
  border: 2px solid var(--ink);
  background: var(--card);
}

.progress-bar span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--green);
  transition: width 0.3s;
}

.menu-actions {
  display: grid;
  gap: 12px;
}

/* ---------- 选关界面 ---------- */
.page-tabs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-bottom: 18px;
}

.page-tab {
  min-width: 80px;
  font-family: var(--mono);
}

.page-tab[aria-selected="true"] {
  background: var(--ink);
  color: var(--card);
}

.level-grid {
  width: 100%;
  max-width: 600px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
}

.level-btn {
  aspect-ratio: 1 / 1;
  min-height: 0;
  padding: 4px;
  flex-direction: column;
  gap: 2px;
  font-family: var(--mono);
  font-size: 24px;
}

.level-btn small {
  font-family: var(--font);
  font-size: 12px;
  font-weight: 400;
}

.level-btn.is-done {
  background: var(--green-soft);
}

.level-btn.is-done small {
  color: var(--green);
  font-weight: 700;
}

.level-btn.is-next {
  background: var(--hat);
}

.level-btn.is-locked,
.level-btn.is-locked:disabled {
  color: rgba(42, 33, 27, 0.45);
  border-color: rgba(42, 33, 27, 0.35);
  background: repeating-linear-gradient(-45deg, transparent 0 6px, rgba(42, 33, 27, 0.08) 6px 12px);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 18px;
  margin: 22px 0 0;
  font-size: 13px;
  color: var(--ink-soft);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 14px;
  height: 14px;
  display: inline-block;
  border: 2px solid var(--ink);
}

.swatch-done { background: var(--green-soft); }
.swatch-next { background: var(--hat); }
.swatch-open { background: var(--card); }
.swatch-locked {
  border-color: rgba(42, 33, 27, 0.35);
  background: repeating-linear-gradient(-45deg, transparent 0 3px, rgba(42, 33, 27, 0.2) 3px 5px);
}

/* ---------- 游戏界面 ---------- */
.screen-game {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  padding-bottom: 10px;
}

.game-bar {
  margin-bottom: 10px;
}

.level-nav {
  display: flex;
  align-items: center;
  gap: 10px;
}

.level-nav h2 {
  min-width: 104px;
  text-align: center;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 10px;
  border: 2px solid var(--ink);
  background: var(--card);
  font-size: 14px;
}

.stats span {
  padding: 4px 14px;
  border-right: 2px solid var(--ink);
}

.stats span:last-child {
  border-right: 0;
}

.stats b {
  font-family: var(--mono);
  font-size: 15px;
  margin-left: 4px;
}

.board-wrap {
  flex: 1 1 0;
  width: 100%;
  max-width: 960px;
  min-height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  overflow: hidden;
}

#board {
  display: block;
}

.hint {
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--ink-soft);
}

.dpad {
  display: none;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(2, 50px);
  gap: 8px;
  margin-top: 10px;
}

.dpad-btn {
  min-height: 0;
  padding: 0;
  font-size: 18px;
  touch-action: manipulation;
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }

@media (pointer: coarse), (max-width: 640px) {
  .dpad {
    display: grid;
  }

  .hint {
    display: none;
  }
}

@media (max-width: 640px) {
  .screen {
    padding: 56px 10px 10px;
  }

  .home-link {
    top: 10px;
    right: 10px;
    padding: 6px 10px;
    font-size: 13px;
  }

  .game-bar {
    justify-content: center;
  }

  .game-bar .level-nav {
    order: -1;
    width: 100%;
    justify-content: center;
  }

  .btn {
    min-height: 36px;
    padding: 5px 11px;
    font-size: 14px;
  }

  .btn-lg {
    min-height: 46px;
    font-size: 16px;
  }

  .stats span {
    padding: 3px 9px;
  }

  .level-grid {
    gap: 8px;
  }

  .page-tabs {
    width: 100%;
    flex-wrap: nowrap;
    gap: 6px;
  }

  .page-tab {
    flex: 1 1 0;
    min-width: 0;
    padding: 5px 2px;
    font-size: 13px;
  }

  .level-btn {
    font-size: 19px;
  }

  .level-btn small {
    font-size: 10px;
  }

  .menu-head h1 {
    font-size: 34px;
  }
}

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(42, 33, 27, 0.5);
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 400px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 22px 22px 20px;
  border: 3px solid var(--ink);
  border-radius: 4px;
  background: var(--card);
  box-shadow: 8px 8px 0 var(--ink);
}

.modal-wide {
  max-width: 540px;
}

.modal-card h3 {
  font-size: 21px;
  font-weight: 800;
  margin-bottom: 12px;
}

.modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 34px;
  height: 34px;
  border: 2px solid transparent;
  border-radius: 3px;
  background: transparent;
  color: var(--ink);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.modal-close:hover {
  border-color: var(--ink);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

/* 过关：盖章 */
.modal-win h3 {
  font-size: 24px;
}

.stamp {
  position: absolute;
  top: 18px;
  right: 20px;
  padding: 2px 12px;
  border: 4px double var(--red);
  color: var(--red);
  font-size: 26px;
  font-weight: 900;
  letter-spacing: 6px;
  transform: rotate(-12deg);
  opacity: 0.85;
  animation: stamp 0.28s ease-out;
}

@keyframes stamp {
  from { transform: rotate(-12deg) scale(2.2); opacity: 0; }
  to { transform: rotate(-12deg) scale(1); opacity: 0.85; }
}

.win-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 6px 0 0;
  border: 2px solid var(--ink);
}

.win-stats div {
  padding: 8px 10px;
  border-right: 2px solid var(--ink);
}

.win-stats div:last-child {
  border-right: 0;
}

.win-stats dt {
  font-size: 12px;
  color: var(--ink-soft);
}

.win-stats dd {
  margin: 2px 0 0;
  font-family: var(--mono);
  font-size: 22px;
  font-weight: 700;
}

.win-note {
  min-height: 1.2em;
  margin: 12px 0 0;
  font-size: 14px;
  line-height: 1.7;
}

.save-block {
  padding: 14px 0 4px;
  border-top: 2px dashed rgba(42, 33, 27, 0.35);
}

.save-block h4 {
  font-size: 16px;
  margin-bottom: 6px;
}

.muted {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--ink-soft);
}

.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--ink-soft);
}

textarea {
  width: 100%;
  resize: vertical;
  padding: 8px 10px;
  border: 2px solid var(--ink);
  border-radius: 2px;
  background: #fff;
  color: var(--ink);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.5;
  word-break: break-all;
}

textarea[readonly] {
  background: var(--paper);
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}

.form-msg {
  min-height: 1.4em;
  margin: 10px 0 0;
  font-size: 14px;
}

.form-msg.is-error { color: var(--red); }
.form-msg.is-ok { color: var(--green); }

.help-list {
  margin: 0;
  padding-left: 20px;
  font-size: 15px;
  line-height: 1.8;
}

/* ---------- 提示条 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 3000;
  max-width: calc(100vw - 32px);
  padding: 8px 16px;
  border: 2px solid var(--ink);
  background: var(--ink);
  color: var(--card);
  font-size: 14px;
  transform: translate(-50%, 12px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.noscript {
  position: fixed;
  inset: auto 0 0;
  margin: 0;
  padding: 16px;
  text-align: center;
  background: var(--red);
  color: #fff;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
