:root {
  color-scheme: light;
  --bg: #f7f4ed;
  --panel: #fffdfa;
  --panel-strong: #ffffff;
  --ink: #17202a;
  --muted: #667085;
  --line: #ded6c8;
  --line-strong: #cfc3b2;
  --accent: #17202a;
  --blue: #3976f6;
  --mint: #27b89a;
  --danger: #d92d20;
  --warning: #946200;
  --shadow: 0 18px 48px rgba(40, 31, 20, 0.08);
  --cell-size: 24px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(23, 32, 42, 0.045) 1px, transparent 1px) 0 0 / 28px 28px,
    linear-gradient(rgba(23, 32, 42, 0.045) 1px, transparent 1px) 0 0 / 28px 28px,
    var(--bg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.app-shell {
  width: min(1600px, calc(100% - 32px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 82px;
  padding: 18px 20px;
  border: 1px solid rgba(23, 32, 42, 0.1);
  border-radius: 8px;
  background: rgba(255, 253, 250, 0.9);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--warning);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(26px, 3.4vw, 42px);
  line-height: 1.1;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 16px;
  line-height: 1.3;
  letter-spacing: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--ink);
  border-radius: 8px;
  color: var(--ink);
  background: var(--panel-strong);
  font-weight: 800;
  white-space: nowrap;
  box-shadow: 0 4px 0 rgba(23, 32, 42, 0.08);
}

.button-primary {
  color: #fff;
  background: var(--accent);
}

.button-compact {
  min-width: 72px;
  padding: 0 12px;
}

.button-full {
  width: 100%;
}

.empty-state {
  display: grid;
  min-height: calc(100vh - 240px);
  place-items: center;
  padding: 52px 0;
}

.empty-card {
  width: min(620px, 100%);
  padding: 42px;
  border: 1px solid rgba(23, 32, 42, 0.1);
  border-radius: 8px;
  background: rgba(255, 253, 250, 0.9);
  text-align: center;
  box-shadow: var(--shadow);
}

.empty-card h2 {
  margin: 18px 0 12px;
  font-size: clamp(24px, 4vw, 36px);
}

.empty-card p {
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.8;
}

.empty-mark {
  display: grid;
  grid-template-columns: repeat(2, 32px);
  justify-content: center;
  gap: 10px;
}

.empty-mark span {
  aspect-ratio: 1;
  border-radius: 50%;
  box-shadow: inset -5px -6px 0 rgba(23, 32, 42, 0.12), inset 4px 4px 0 rgba(255, 255, 255, 0.62);
}

.empty-mark span:nth-child(1) {
  background: #ff6fae;
}

.empty-mark span:nth-child(2) {
  background: #ffc857;
}

.empty-mark span:nth-child(3) {
  background: #3fc7a7;
}

.empty-mark span:nth-child(4) {
  background: #4f8cff;
}

.editor {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 320px;
  gap: 14px;
  margin-top: 14px;
  min-height: calc(100vh - 132px);
}

.pattern-list-panel,
.canvas-panel,
.tools-panel {
  min-height: 0;
  border: 1px solid rgba(23, 32, 42, 0.1);
  border-radius: 8px;
  background: rgba(255, 253, 250, 0.94);
  box-shadow: var(--shadow);
}

.pattern-list-panel,
.tools-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-head,
.canvas-toolbar {
  border-bottom: 1px solid var(--line);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 62px;
  padding: 14px 16px;
}

.panel-head span,
.file-name,
.meta-row,
.muted {
  color: var(--muted);
  font-size: 13px;
}

.pattern-list {
  overflow: auto;
  padding: 8px;
}

.pattern-item {
  width: 100%;
  display: grid;
  gap: 6px;
  min-height: 76px;
  padding: 12px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  text-align: left;
  box-shadow: none;
}

.pattern-item:hover {
  background: rgba(23, 32, 42, 0.04);
}

.pattern-item.active {
  border-color: rgba(23, 32, 42, 0.18);
  background: #fff;
}

.pattern-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pattern-item span {
  color: var(--muted);
  font-size: 12px;
}

.dirty-dot::after {
  content: "已修改";
  display: inline-flex;
  margin-left: 8px;
  padding: 2px 6px;
  border-radius: 999px;
  color: #fff;
  background: var(--blue);
  font-size: 11px;
}

.canvas-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.canvas-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 86px;
  padding: 16px 18px;
}

.file-name {
  margin-bottom: 5px;
}

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

.meta-row span {
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
}

.view-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 12px;
}

.control-check,
.slider-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #344054;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.slider-control input {
  width: 116px;
}

.grid-scroll {
  overflow: auto;
  padding: 16px;
  outline: none;
  background:
    linear-gradient(90deg, rgba(23, 32, 42, 0.045) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(rgba(23, 32, 42, 0.045) 1px, transparent 1px) 0 0 / 24px 24px,
    #fbf8f2;
}

.grid-scroll:focus-visible {
  box-shadow: inset 0 0 0 2px var(--blue);
}

.bead-grid {
  display: grid;
  width: max-content;
  gap: 1px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eee6db;
}

.grid-index,
.grid-corner,
.bead-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  min-width: var(--cell-size);
}

.grid-index,
.grid-corner {
  display: grid;
  place-items: center;
  color: #697586;
  background: #f6f0e8;
  font-size: max(8px, calc(var(--cell-size) * 0.34));
  font-weight: 800;
  user-select: none;
}

.bead-cell {
  position: relative;
  border: 0;
  border-radius: 0;
  background: rgba(255, 255, 255, 0.24);
  box-shadow: none;
}

.bead-cell::before {
  content: "";
  position: absolute;
  inset: 13%;
  border-radius: 50%;
  background: var(--cell-color, transparent);
  box-shadow:
    inset -3px -4px 0 rgba(23, 32, 42, 0.12),
    inset 2px 2px 0 rgba(255, 255, 255, 0.55);
}

.bead-cell.empty::before {
  display: none;
}

.bead-cell.empty.show-empty::before {
  display: block;
  background: rgba(255, 255, 255, 0.62);
  box-shadow: inset 0 0 0 1px rgba(23, 32, 42, 0.08);
}

.bead-code {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--label-color, rgba(23, 32, 42, 0.78));
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: max(7px, calc(var(--cell-size) * 0.3));
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25);
}

.hide-codes .bead-code {
  display: none;
}

.bead-cell.selected {
  outline: 2px solid var(--blue);
  outline-offset: -2px;
  z-index: 2;
}

.bead-cell.multi-selected {
  outline: 2px solid var(--mint);
  outline-offset: -2px;
  z-index: 2;
}

.tools-panel {
  overflow: auto;
  padding: 14px;
}

.tool-section {
  padding: 0 0 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.tool-section:last-child {
  border-bottom: 0;
  margin-bottom: 0;
}

.tool-section h2 {
  margin-bottom: 10px;
}

.tool-section p {
  margin-bottom: 10px;
  line-height: 1.65;
}

input[type="text"],
input[type="search"] {
  width: 100%;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

input[type="text"]:focus,
input[type="search"]:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(57, 118, 246, 0.12);
}

.field-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.palette-list {
  display: grid;
  gap: 6px;
  max-height: 320px;
  overflow: auto;
  padding-right: 4px;
}

.color-option {
  display: grid;
  grid-template-columns: 22px 52px 1fr;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 38px;
  padding: 7px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  text-align: left;
  box-shadow: none;
}

.color-option:hover {
  border-color: var(--line);
  background: #fff;
}

.color-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--swatch-color);
  box-shadow: inset -3px -3px 0 rgba(23, 32, 42, 0.12), inset 2px 2px 0 rgba(255, 255, 255, 0.52);
}

.color-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-weight: 900;
}

.color-name {
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-section {
  position: sticky;
  bottom: -14px;
  padding-top: 12px;
  background: rgba(255, 253, 250, 0.96);
}

.status-text {
  min-height: 42px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
}

.status-text.error {
  color: var(--danger);
}

.status-text.ok {
  color: #067647;
}

.app-footer {
  display: grid;
  gap: 4px;
  padding: 24px 0 8px;
  color: #5f6b7a;
  font-size: 13px;
  font-weight: 700;
  text-align: center;
}

.app-footer p {
  margin: 0;
}

@media (max-width: 1180px) {
  .editor {
    grid-template-columns: minmax(190px, 240px) minmax(0, 1fr);
  }

  .tools-panel {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
  }

  .tool-section {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: 0;
  }
}

@media (max-width: 820px) {
  .app-shell {
    width: min(100% - 20px, 1600px);
    padding-top: 10px;
  }

  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .header-actions {
    width: 100%;
  }

  .header-actions .button {
    flex: 1;
  }

  .editor {
    grid-template-columns: 1fr;
  }

  .pattern-list-panel {
    max-height: 260px;
  }

  .canvas-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .view-controls {
    justify-content: flex-start;
  }

  .tools-panel {
    grid-template-columns: 1fr;
  }

  .field-row {
    flex-direction: column;
  }

  .button-compact {
    width: 100%;
  }
}
