* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, "Microsoft JhengHei", sans-serif;
  background: #f6f8f7;
  color: #222;
}

button {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(300px, 340px) minmax(0, 1fr);
  min-height: 100vh;
}

/* 左側面板 */
.sidebar {
  background: #ffffff;
  border-right: 1px solid #e8ece9;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.logo {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
}

/* 分類 tabs */
.category-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.category-tabs button {
  border: none;
  background: #f1f4f2;
  color: #4f5b55;
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
  transition: 0.15s ease;
}

.category-tabs button:hover {
  background: #e4efe9;
}

.category-tabs button.selected-category {
  background: #43d394;
  color: #fff;
  outline: none;
}

/* 素材區 */
.item-grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
  align-content: start;
  padding-right: 4px;
}

.item-grid::-webkit-scrollbar {
  width: 8px;
}

.item-grid::-webkit-scrollbar-thumb {
  background: #d8dfdb;
  border-radius: 999px;
}

.thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  background: #fff;
  border: 2px solid #e7ece8;
  border-radius: 18px;
  cursor: pointer;
  padding: 10px;
  transition: 0.15s ease;
}

.thumb:hover {
  border-color: #7fe2b6;
  background: #f9fffb;
}

.thumb.selected,
.selected {
  border-color: #43d394;
  box-shadow: 0 0 0 3px rgba(67, 211, 148, 0.15);
}

/* 白圈調整區 */
.tool-panel {
  background: #f8faf9;
  border: 1px solid #e6ece8;
  border-radius: 18px;
  padding: 14px;
}

.tool-title {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 12px;
  text-align: center;
  color: #53605a;
}

.game-pad {
  display: grid;
  grid-template-columns: 48px 56px 48px;
  grid-template-rows: 48px 36px 48px;
  gap: 8px;
  align-items: center;
  justify-items: center;
  justify-content: center;
}

.pad-btn {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 14px;
  background: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  font-size: 20px;
  cursor: pointer;
  transition: 0.12s ease;
}

.pad-reset {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 12px;
  background: #edf7f1;
  color: #2e8f67;
  font-weight: 700;
  cursor: pointer;
  transition: 0.12s ease;
}

.pad-btn:hover,
.pad-reset:hover {
  transform: translateY(-1px);
}

.pad-btn:active,
.pad-reset:active {
  transform: translateY(1px);
}

/* 右側預覽 */
.preview-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: clamp(20px, 4vw, 32px);
}

.preview-stage {
  width: 100%;
  display: flex;
  justify-content: center;
}

.avatar-card {
  width: min(420px, 100%);
  aspect-ratio: 4 / 5;
  background: #ffffff;
  border-radius: 28px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.canvas {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  border-radius: 20px;
  background: linear-gradient(180deg, #f8fffb 0%, #f0fbf6 100%);
}

.canvas img {
  position: absolute;
  width: min(300px, 78%);
  max-width: 100%;
}

/* 底部按鈕 */
.action-bar {
  display: flex;
  justify-content: center;
  gap: 14px;
  flex-wrap: wrap;
  width: min(680px, 100%);
}

.action-btn {
  flex: 1 1 150px;
  min-width: 150px;
  border: none;
  border-radius: 999px;
  padding: 14px 22px;
  cursor: pointer;
  font-weight: 700;
  transition: 0.15s ease;
}

.action-btn.icon-btn {
  flex: 0 0 60px;
  min-width: 60px;
  width: 60px;
  aspect-ratio: 1 / 1;
  border-radius: 20px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.action-btn.primary {
  background: #43d394;
  color: white;
  box-shadow: 0 8px 18px rgba(67, 211, 148, 0.24);
}

.action-btn.primary:hover {
  background: #35bf82;
}

.action-btn.secondary {
  background: #ffffff;
  color: #4d5a54;
  border: 1px solid #dce6e1;
}

.action-btn.secondary:hover {
  background: #f7faf8;
}

.dice-icon {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform 0.18s ease;
}

.icon-btn:hover .dice-icon {
  transform: rotate(12deg) scale(1.06);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* 手機或窄螢幕 */
@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    order: 1;
    border-right: none;
    border-bottom: 1px solid #e8ece9;
    padding: 16px;
  }

  .preview-panel {
    order: 2;
    padding: 20px 16px 8px;
  }

  .avatar-card {
    width: min(420px, 100%);
  }

  .item-grid {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  }
}

@media (max-width: 640px) {
  .sidebar {
    gap: 12px;
  }

  .logo {
    font-size: 18px;
  }

  .category-tabs {
    gap: 6px;
  }

  .category-tabs button {
    flex: 1 1 calc(50% - 6px);
    padding: 10px 12px;
  }

  .item-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
  }

  .thumb {
    border-radius: 14px;
    padding: 8px;
  }

  .avatar-card {
    border-radius: 22px;
    padding: 16px;
  }

  .canvas {
    border-radius: 16px;
  }

  .canvas img {
    width: min(260px, 82%);
  }

  .action-bar {
    gap: 10px;
  }

  .action-btn {
    flex-basis: 100%;
    min-width: 0;
    width: 100%;
  }

  .action-btn.icon-btn {
    flex-basis: 60px;
    width: 60px;
  }
}

@media (max-width: 420px) {
  .item-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .game-pad {
    transform: scale(0.92);
    transform-origin: top center;
  }
}
