:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1f2a44;
  --primary: #4458ff;
  --secondary: #8891a7;
  --ok: #1f9d66;
  --warn: #f57c00;
  --border: #e2e5ef;
  --shadow: 0 12px 30px rgba(31, 42, 68, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 20px;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, #f8f9ff, #f2f6fb 40%, #eef6ff);
}

.app {
  max-width: 1440px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar {
  display: flex;
  justify-content: flex-start;
  gap: 16px;
  align-items: flex-start;
}

h1 {
  margin: 0;
  font-size: 28px;
}

.topbar p {
  margin: 6px 0 0;
  color: #5e6b85;
}

.stats {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  min-width: 0;
}

.stat-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  box-shadow: var(--shadow);
}

.stat-item .label {
  font-size: 12px;
  color: #73819e;
  display: block;
}

.stat-item strong {
  font-size: 18px;
}

.workspace {
  display: grid;
  grid-template-columns: 360px minmax(980px, 1fr);
  gap: 16px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.left-panel h2,
.right-panel h2 {
  margin: 0 0 12px;
  font-size: 20px;
}

.sample-wrapper {
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 2px dashed #d5daea;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: #f9fbff;
}

.controls {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

button {
  border: none;
  border-radius: 10px;
  height: 38px;
  font-weight: 600;
  color: #fff;
  background: var(--primary);
  cursor: pointer;
}

button.secondary {
  background: var(--secondary);
}

button:hover {
  filter: brightness(1.05);
}

.tip {
  margin: 12px 0 0;
  color: #4f5d7a;
  min-height: 42px;
  line-height: 1.45;
}

.playfield {
  width: 620px;
  position: relative;
  border-radius: 12px;
  flex: 0 0 auto;
}

.right-layout {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  width: 100%;
}

.record-panel {
  flex: 1 1 auto;
  min-width: 240px;
  min-height: 280px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fafcff;
  padding: 10px;
}

.record-panel h3 {
  margin: 0 0 10px;
  font-size: 14px;
  color: #58657f;
}

.stats-vertical .stat-item {
  box-shadow: none;
}

.stats-vertical .stat-item strong {
  font-size: 17px;
}

.tray,
.board {
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fafcff;
  position: relative;
}

.tray {
  height: 240px;
  padding: 8px 12px;
}

.board {
  margin-top: 10px;
  height: 580px;
  padding: 8px 12px;
  overflow: hidden;
  background: repeating-linear-gradient(
    45deg,
    #f5f8ff,
    #f5f8ff 16px,
    #f7faff 16px,
    #f7faff 32px
  );
}

.tray h3,
.board h3 {
  margin: 0;
  font-size: 14px;
  color: #58657f;
}

.board-hints {
  position: absolute;
  left: 12px;
  top: 34px;
  right: 12px;
  bottom: 12px;
  pointer-events: none;
}

.pieces-layer {
  position: absolute;
  left: 0;
  top: 0;
  width: 620px;
  height: 840px;
}

.piece {
  position: absolute;
  cursor: grab;
  transform-origin: center center;
  user-select: none;
  touch-action: none;
}

.piece.dragging {
  cursor: grabbing;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.22));
  z-index: 999;
}

.piece svg {
  overflow: visible;
}

.piece polygon {
  stroke: rgba(0, 0, 0, 0.35);
  stroke-width: 2;
}

.toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  min-width: 240px;
  max-width: 360px;
  padding: 12px 14px;
  border-radius: 12px;
  color: #fff;
  background: rgba(25, 32, 47, 0.9);
  opacity: 0;
  transform: translateY(10px);
  transition: all 0.25s ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast.warn {
  background: rgba(245, 124, 0, 0.95);
}

.toast.ok {
  background: rgba(31, 157, 102, 0.95);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 26, 40, 0.45);
  display: grid;
  place-items: center;
}

.modal.hidden {
  display: none;
}

.modal-content {
  width: min(420px, calc(100vw - 32px));
  border-radius: 14px;
  background: #fff;
  padding: 18px;
}

.modal-content h3 {
  margin: 0;
}

.modal-content p {
  margin: 10px 0 16px;
  line-height: 1.5;
}

.category-content {
  width: min(760px, calc(100vw - 28px));
  max-height: calc(100vh - 28px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.category-header h3 {
  margin: 0;
}

#start-category-btn {
  min-width: 96px;
  transform: translate(-10px, 10px);
}

.category-subtitle {
  color: #5e6b85;
  margin: 8px 0 10px;
}

.category-list {
  margin: 12px 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  gap: 10px;
}

.category-chip {
  border: 1px solid #ccd3e8;
  background: #f7f9ff;
  color: #2c3a58;
}

.category-chip.active {
  border-color: var(--primary);
  background: #e9edff;
  color: #2338eb;
}

.pattern-preview-wrap {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  background: #fafcff;
  min-height: 0;
}

.pattern-preview-wrap h4 {
  margin: 0 0 10px;
  color: #4f5d7a;
}

.pattern-preview {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  gap: 7px;
}

.pattern-item {
  border-radius: 10px;
  border: 1px solid #d9dfef;
  padding: 6px;
  background: #fff;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  text-align: center;
}

.pattern-thumb {
  width: auto;
  max-width: 100%;
  height: 72px;
  display: block;
}

.pattern-item .name {
  font-weight: 600;
  color: #263552;
  font-size: 14px;
  width: 100%;
}

.pattern-item .meta {
  font-size: 12px;
  color: #6d7b97;
  width: 100%;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 12px;
}

@media (max-width: 1240px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .right-layout {
    flex-direction: column;
  }

  .playfield {
    width: 620px;
    max-width: 100%;
  }

  .record-panel {
    width: 100%;
  }

  .pieces-layer {
    width: 100%;
  }
}
