:root {
  color-scheme: light;
  --bg: #f6f4ee;
  --panel: #fffdfa;
  --ink: #1d211f;
  --muted: #68706a;
  --line: #d9ded4;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --accent-soft: #d8f1ec;
  --warn: #b7791f;
  --shadow: 0 18px 42px rgba(18, 27, 22, 0.1);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.84), rgba(255, 255, 255, 0) 280px),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

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

.icon-sprite {
  display: none;
}

.icon {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 22px clamp(18px, 4vw, 42px);
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 250, 0.92);
  backdrop-filter: blur(16px);
}

.topbar h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.15;
  letter-spacing: 0;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
}

.topbar__stats {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar__stats span,
.paint-state {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--muted);
}

.topbar__stats span {
  color: var(--ink);
  font-weight: 700;
}

#difficultyBadge {
  font-size: 13px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  white-space: nowrap;
}

#difficultyBadge[data-level="easy"] { border-color: #16a34a; color: #16a34a; background: #f0fdf4; }
#difficultyBadge[data-level="medium"] { border-color: var(--warn); color: var(--warn); background: #fffbeb; }
#difficultyBadge[data-level="hard"] { border-color: #dc2626; color: #dc2626; background: #fef2f2; }
#difficultyBadge[data-level="extreme"] { border-color: #7c3aed; color: #7c3aed; background: #f5f3ff; }

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, 410px) minmax(0, 1fr);
  min-height: 0;
}

.tool-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 22px;
  border-right: 1px solid var(--line);
  background: rgba(255, 253, 250, 0.78);
  overflow: auto;
}

.upload-zone {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  align-items: center;
  padding: 16px;
  min-height: 78px;
  border: 1px dashed #a7b0a7;
  border-radius: var(--radius);
  background: #fff;
}

.upload-zone:hover,
.upload-zone.is-dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.upload-zone .icon {
  grid-row: span 2;
  width: 26px;
  height: 26px;
  color: var(--accent);
}

.upload-zone span {
  font-weight: 800;
}

.upload-zone small {
  min-width: 0;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.crop-area {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    linear-gradient(45deg, rgba(15, 118, 110, 0.07) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(15, 118, 110, 0.07) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(15, 118, 110, 0.07) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(15, 118, 110, 0.07) 75%);
  background-size: 22px 22px;
  background-position:
    0 0,
    0 11px,
    11px -11px,
    -11px 0;
  overflow: hidden;
}

#cropCanvas {
  width: 100%;
  height: auto;
  display: block;
  touch-action: none;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

fieldset {
  margin: 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

legend {
  padding: 0 6px;
  font-weight: 800;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 14px;
}

label span {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

input[type="number"],
select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfbf8;
  color: var(--ink);
  padding: 8px 10px;
}

select {
  margin-top: 10px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

#colorCountNumber {
  margin-top: 10px;
}

.range-field strong {
  color: var(--ink);
}

.check-field {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  gap: 9px;
  margin-top: 10px;
  color: var(--ink);
  font-weight: 700;
}

.check-field input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.check-field span {
  display: inline;
}

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

.button-row--compact {
  justify-content: flex-end;
}

.button {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  font-weight: 800;
}

.button:hover:not(:disabled) {
  border-color: #9ca79f;
  background: #f9faf5;
}

.button--primary {
  flex: 1 1 160px;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.button--primary:hover:not(:disabled) {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.button--ghost {
  width: 100%;
  margin-top: 10px;
}

.button--quiet {
  min-height: 32px;
  padding: 5px 10px;
  font-size: 13px;
}

.pattern-panel {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 18px;
  padding: 22px;
  overflow: hidden;
}

.pattern-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.paint-state strong {
  color: var(--accent-strong);
  font-size: 18px;
}

.preview-canvas {
  width: 96px;
  height: 96px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  image-rendering: pixelated;
  cursor: pointer;
}

.preview-canvas:hover {
  border-color: var(--accent);
}

.preview-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
  padding: 4px 8px;
  border-radius: var(--radius);
}

.preview-toggle:hover {
  background: rgba(0,0,0,0.05);
}

.pattern-stage {
  position: relative;
  min-height: 360px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow);
}

.empty-pattern {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 800;
}

.pattern-board {
  --cols: 48;
  --rows: 48;
  --guide-size: 34px;
  --cell-font-size: 10px;
  --guide-font-size: 9px;
  display: grid;
  grid-template-columns: var(--guide-size) minmax(0, 1fr) var(--guide-size);
  grid-template-rows: var(--guide-size) auto var(--guide-size);
  gap: 0;
  width: min(100%, 1120px);
  min-width: 620px;
  margin: 0 auto;
  padding: 16px;
}

.pattern-board.is-empty {
  display: none;
}

.pattern-corner,
.guide-cell {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  background: #f5f8f2;
  border: 1px solid rgba(25, 30, 27, 0.2);
  color: #3d4841;
  font-size: var(--guide-font-size);
  line-height: 1;
  font-weight: 900;
  letter-spacing: 0;
  user-select: none;
}

.pattern-corner--top-left {
  grid-column: 1;
  grid-row: 1;
  border-right-width: 2px;
  border-bottom-width: 2px;
}

.pattern-corner--top-right {
  grid-column: 3;
  grid-row: 1;
  border-left-width: 2px;
  border-bottom-width: 2px;
}

.pattern-corner--bottom-left {
  grid-column: 1;
  grid-row: 3;
  border-top-width: 2px;
  border-right-width: 2px;
}

.pattern-corner--bottom-right {
  grid-column: 3;
  grid-row: 3;
  border-top-width: 2px;
  border-left-width: 2px;
}

.pattern-guide {
  display: grid;
  min-width: 0;
  min-height: 0;
}

.pattern-guide--columns {
  grid-column: 2;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
}

.pattern-guide--rows {
  grid-row: 2;
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
}

.pattern-guide--top {
  grid-row: 1;
}

.pattern-guide--bottom {
  grid-row: 3;
}

.pattern-guide--left {
  grid-column: 1;
}

.pattern-guide--right {
  grid-column: 3;
}

.guide-cell.is-group-left,
.guide-cell.is-group-top {
  background: #e7f4ee;
}

.guide-cell.is-group-left {
  border-left-width: 3px;
  border-left-color: rgba(25, 30, 27, 0.45);
}

.guide-cell.is-group-top {
  border-top-width: 3px;
  border-top-color: rgba(25, 30, 27, 0.45);
}

.guide-cell.is-group-right {
  border-right-width: 3px;
  border-right-color: rgba(25, 30, 27, 0.45);
}

.guide-cell.is-group-bottom {
  border-bottom-width: 3px;
  border-bottom-color: rgba(25, 30, 27, 0.45);
}

.pattern-grid {
  grid-column: 2;
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  width: 100%;
  min-width: 0;
}

.bead-cell {
  aspect-ratio: 1 / 1;
  min-width: 0;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(25, 30, 27, 0.2);
  border-radius: 0;
  font-size: var(--cell-font-size);
  line-height: 1;
  font-weight: 800;
  letter-spacing: 0;
  overflow: hidden;
  user-select: none;
}

.bead-cell.is-group-left {
  border-left-width: 3px;
  border-left-color: rgba(25, 30, 27, 0.45);
}

.bead-cell.is-group-top {
  border-top-width: 3px;
  border-top-color: rgba(25, 30, 27, 0.45);
}

.bead-cell.is-group-right {
  border-right-width: 3px;
  border-right-color: rgba(25, 30, 27, 0.45);
}

.bead-cell.is-group-bottom {
  border-bottom-width: 3px;
  border-bottom-color: rgba(25, 30, 27, 0.45);
}

.bead-cell:hover {
  outline: 2px solid rgba(15, 118, 110, 0.72);
  outline-offset: -2px;
  z-index: 1;
}

.palette-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(96px, auto);
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 253, 250, 0.9);
  padding: 14px;
}

.palette-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.palette-header h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.palette-header span {
  color: var(--muted);
  font-size: 13px;
}

.palette-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(184px, 1fr));
  gap: 10px;
  max-height: 220px;
  overflow: auto;
  padding-right: 2px;
}

.palette-item {
  display: grid;
  grid-template-columns: minmax(34px, auto) minmax(0, 1fr) auto;
  align-items: center;
  width: 100%;
  gap: 10px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: inherit;
  text-align: left;
}

.palette-item.is-changed {
  border-color: #b7791f;
  background: #fffaf0;
}

.palette-item.is-active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.14);
}

.palette-swatch {
  width: 34px;
  aspect-ratio: 1 / 1;
  border: 1px solid rgba(0, 0, 0, 0.18);
}

.palette-swatch-flow {
  display: grid;
  grid-template-columns: 24px 16px 24px;
  align-items: center;
  gap: 4px;
}

.palette-swatch-flow .palette-swatch {
  width: 24px;
}

.palette-arrow {
  color: var(--warn);
  font-size: 12px;
  font-weight: 900;
}

.palette-meta {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.palette-meta strong {
  color: var(--ink);
}

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

.palette-undo {
  min-width: 32px;
  min-height: 30px;
  padding: 4px 8px;
  border: 1px solid #e2c27d;
  border-radius: 6px;
  background: #fff6db;
  color: #7a4d00;
  font-size: 12px;
  font-weight: 800;
}

.palette-undo:hover {
  background: #fdeab8;
}

.color-editor {
  display: grid;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.color-editor[hidden] {
  display: none;
}

.color-editor__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.color-editor__header strong {
  font-size: 16px;
}

.color-editor__label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.common-colors {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(30px, 1fr));
  gap: 7px;
}

.common-color {
  aspect-ratio: 1 / 1;
  min-height: 30px;
  border: 1px solid rgba(0, 0, 0, 0.22);
  border-radius: 6px;
  background: var(--swatch);
}

.common-color:hover,
.common-color:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.inventory-panel {
  display: grid;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  max-height: 300px;
  overflow: auto;
}

.inventory-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.inventory-warnings {
  padding: 8px 12px;
  border: 1px solid #dc2626;
  border-radius: var(--radius);
  background: #fef2f2;
  color: #dc2626;
  font-size: 13px;
  font-weight: 700;
}

.inventory-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
}

.inventory-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.inventory-item .palette-swatch {
  width: 20px;
  height: 20px;
  border: 1px solid rgba(0,0,0,0.18);
  border-radius: 3px;
  flex-shrink: 0;
}

.inventory-qty {
  width: 60px;
  min-height: 28px;
  padding: 2px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  text-align: center;
  font-size: 13px;
  margin-left: auto;
}

.rgb-editor {
  display: grid;
  grid-template-columns: minmax(90px, 0.7fr) repeat(3, minmax(120px, 1fr));
  gap: 10px;
}

.rgb-editor label {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  align-items: center;
  gap: 6px;
}

.rgb-editor label input[type="range"] {
  grid-column: 1 / -1;
}

.rgb-editor input[type="number"] {
  min-height: 34px;
  padding: 6px 8px;
}

.color-preview {
  grid-template-columns: 1fr !important;
}

.color-preview input[type="color"] {
  width: 100%;
  min-height: 74px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: transparent;
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .tool-panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    overflow: visible;
  }

  .pattern-panel {
    overflow: visible;
  }
}

@media (max-width: 640px) {
  .topbar,
  .pattern-toolbar,
  .palette-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar {
    gap: 14px;
  }

  .topbar h1 {
    font-size: 24px;
  }

  .tool-panel,
  .pattern-panel {
    padding: 16px;
  }

  .pattern-board {
    min-width: 420px;
    padding: 10px;
    --guide-size: 30px;
  }

  .palette-list {
    grid-template-columns: 1fr;
  }

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

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .tool-panel,
  .pattern-toolbar,
  .palette-header span {
    display: none;
  }

  .workspace,
  .pattern-panel {
    display: block;
    padding: 0;
  }

  .pattern-stage {
    overflow: visible;
    box-shadow: none;
    border: 0;
  }

  .pattern-board {
    width: 100%;
    min-width: 0;
  }

  .palette-panel {
    margin-top: 14px;
    border: 0;
    padding: 0;
  }
}
