*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  
  :root {
    --cream: #f5f0e8;
    --warm-white: #faf8f3;
    --linen: #e8dfd0;
    --thread-red: #c0392b;
    --thread-blue: #2980b9;
    --thread-gold: #d4a017;
    --thread-green: #27ae60;
    --ink: #1a1410;
    --mid: #6b5c4c;
    --soft: #a89b8c;
    --grid-line: rgba(100, 80, 60, 0.15);
  }

  body {
    font-family: 'DM Sans', sans-serif;
    background: var(--cream);
    color: var(--ink);
    min-height: 100vh;
    overflow-x: hidden;
  }

  /* Embroidery hoop background pattern */
  body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image: 
      repeating-linear-gradient(0deg, var(--grid-line) 0, var(--grid-line) 1px, transparent 1px, transparent 24px),
      repeating-linear-gradient(90deg, var(--grid-line) 0, var(--grid-line) 1px, transparent 1px, transparent 24px);
    pointer-events: none;
    z-index: 0;
  }

  .container {
    position: relative;
    z-index: 1;
    max-width: 1100px;
    margin: 0 auto;
    padding: 0 24px 80px;
  }

  /* ─── Header ─── */
  header {
    padding: 48px 0 40px;
    text-align: center;
    position: relative;
  }

  .logo-thread {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 12px;
  }

  .thread-icon {
    width: 36px; height: 36px;
    position: relative;
  }

  .thread-icon svg { width: 100%; height: 100%; }

  h1 {
    font-family: 'Playfair Display', serif;
    font-size: clamp(2.2rem, 5vw, 3.4rem);
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.1;
    color: var(--ink);
  }

  h1 em {
    font-style: italic;
    color: var(--thread-red);
  }

  .tagline {
    margin-top: 10px;
    font-family: 'DM Mono', monospace;
    font-size: 0.78rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--soft);
  }

  /* ─── Upload area ─── */
  .upload-section {
    margin: 0 auto 36px;
    max-width: 640px;
  }

  .upload-zone {
    border: 2px dashed var(--linen);
    border-radius: 4px;
    padding: 48px 32px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--warm-white);
    position: relative;
    overflow: hidden;
  }

  .upload-zone:hover, .upload-zone.drag-over {
    border-color: var(--thread-red);
    background: #fff;
    transform: translateY(-1px);
    box-shadow: 0 8px 32px rgba(0,0,0,0.08);
  }

  .upload-icon {
    width: 56px; height: 56px;
    margin: 0 auto 16px;
    background: var(--linen);
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.2s;
  }

  .upload-zone:hover .upload-icon { background: #fde8e6; }

  .upload-title {
    font-family: 'Playfair Display', serif;
    font-size: 1.2rem;
    margin-bottom: 6px;
  }

  .upload-sub {
    font-size: 0.82rem;
    color: var(--soft);
    font-family: 'DM Mono', monospace;
  }

  .upload-preview {
    width: 100%;
    max-height: 200px;
    object-fit: contain;
    border-radius: 2px;
    display: none;
  }

  input[type=file] { display: none; }

  /* ─── Crop Tool ─── */
  .crop-section {
    display: none;
    max-width: 640px;
    margin: 0 auto 36px;
    background: var(--warm-white);
    border: 1px solid var(--linen);
    border-radius: 4px;
    overflow: hidden;
  }

  .crop-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--linen);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
  }

  .crop-title {
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--soft);
  }

  .crop-ratios {
    display: flex;
    gap: 4px;
  }

  .crop-ratio-btn {
    padding: 4px 10px;
    border: 1px solid var(--linen);
    background: white;
    border-radius: 2px;
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    color: var(--mid);
    cursor: pointer;
    transition: all 0.15s;
  }

  .crop-ratio-btn:hover {
    border-color: var(--mid);
    color: var(--ink);
  }

  .crop-ratio-btn.active {
    background: var(--ink);
    color: var(--cream);
    border-color: var(--ink);
  }

  .crop-container {
    position: relative;
    padding: 16px;
    display: flex;
    justify-content: center;
    background: #e0dbd3;
  }

  #cropCanvas {
    display: block;
    cursor: crosshair;
    max-width: 100%;
  }

  .crop-actions {
    padding: 14px 18px;
    border-top: 1px solid var(--linen);
    display: flex;
    gap: 10px;
    justify-content: flex-end;
  }

  .crop-action-btn {
    padding: 10px 20px;
    border: none;
    border-radius: 3px;
    font-family: 'DM Mono', monospace;
    font-size: 0.75rem;
    letter-spacing: 0.04em;
    cursor: pointer;
    transition: all 0.2s;
  }

  .crop-skip {
    background: transparent;
    border: 1px solid var(--linen);
    color: var(--mid);
  }

  .crop-skip:hover {
    border-color: var(--mid);
    color: var(--ink);
  }

  .crop-apply {
    background: var(--ink);
    color: var(--cream);
  }

  .crop-apply:hover {
    background: var(--thread-red);
  }

  /* ─── Settings bar ─── */
  .settings-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    gap: 12px;
    margin-bottom: 20px;
    max-width: 640px;
    margin-left: auto;
    margin-right: auto;
  }

  .setting-group label {
    display: block;
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--soft);
    margin-bottom: 5px;
  }

  .setting-group select, .setting-group input[type=number] {
    width: 100%;
    padding: 9px 12px;
    border: 1.5px solid var(--linen);
    border-radius: 3px;
    background: var(--warm-white);
    font-family: 'DM Sans', sans-serif;
    font-size: 0.88rem;
    color: var(--ink);
    appearance: none;
    cursor: pointer;
    transition: border-color 0.2s;
  }

  .setting-group select:focus, .setting-group input:focus {
    outline: none;
    border-color: var(--thread-red);
  }

  /* ─── Generate button ─── */
  .generate-btn {
    display: block;
    width: 100%;
    max-width: 640px;
    margin: 0 auto 48px;
    padding: 16px;
    background: var(--ink);
    color: var(--cream);
    border: none;
    border-radius: 3px;
    font-family: 'Playfair Display', serif;
    font-size: 1.05rem;
    font-style: italic;
    letter-spacing: 0.02em;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
  }

  .generate-btn:hover:not(:disabled) {
    background: var(--thread-red);
    transform: translateY(-1px);
    box-shadow: 0 6px 24px rgba(192,57,43,0.3);
  }

  .generate-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  /* ─── Loading ─── */
  .loading {
    display: none;
    text-align: center;
    padding: 60px 0;
  }

  .needle-anim {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-bottom: 20px;
  }

  .stitch-dot {
    width: 10px; height: 10px;
    border-radius: 50%;
    animation: stitch-pulse 1.2s ease-in-out infinite;
  }
  .stitch-dot:nth-child(1) { background: var(--thread-red); animation-delay: 0s; }
  .stitch-dot:nth-child(2) { background: var(--thread-blue); animation-delay: 0.2s; }
  .stitch-dot:nth-child(3) { background: var(--thread-gold); animation-delay: 0.4s; }
  .stitch-dot:nth-child(4) { background: var(--thread-green); animation-delay: 0.6s; }

  @keyframes stitch-pulse {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1.2); opacity: 1; }
  }

  .loading-text {
    font-family: 'DM Mono', monospace;
    font-size: 0.82rem;
    color: var(--soft);
    letter-spacing: 0.06em;
  }

  /* ─── Results ─── */
  #results { display: none; }

  .results-header {
    display: flex;
    align-items: baseline;
    gap: 16px;
    margin-bottom: 28px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--linen);
  }

  .results-title {
    font-family: 'Playfair Display', serif;
    font-size: 1.6rem;
  }

  .results-meta {
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    color: var(--soft);
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .results-grid {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 28px;
    align-items: start;
  }

  @media (max-width: 768px) {
    .results-grid { grid-template-columns: 1fr; }
    .settings-row { grid-template-columns: 1fr 1fr; }
    .crop-header { flex-direction: column; align-items: flex-start; }
  }

  /* ─── Pattern Canvas ─── */
  .pattern-panel {
    background: var(--warm-white);
    border: 1px solid var(--linen);
    border-radius: 4px;
    overflow: hidden;
  }

  .panel-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--linen);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .panel-title {
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--soft);
  }

  .canvas-wrap {
    overflow: auto;
    padding: 16px;
    max-height: 520px;
    cursor: grab;
  }

  .canvas-wrap:active { cursor: grabbing; }

  #patternCanvas {
    display: block;
    image-rendering: pixelated;
    max-width: 100%;
  }

  /* ─── Side panels ─── */
  .side-panels { display: flex; flex-direction: column; gap: 16px; }

  .info-card {
    background: var(--warm-white);
    border: 1px solid var(--linen);
    border-radius: 4px;
    overflow: hidden;
  }

  .card-header {
    padding: 12px 16px;
    background: var(--linen);
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--mid);
  }

  .card-body { padding: 16px; }

  /* Stats */
  .stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .stat-item {}
  .stat-val {
    font-family: 'Playfair Display', serif;
    font-size: 1.4rem;
    color: var(--ink);
    line-height: 1;
  }
  .stat-label {
    font-family: 'DM Mono', monospace;
    font-size: 0.65rem;
    color: var(--soft);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-top: 2px;
  }

  /* Thread palette */
  .thread-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .thread-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 3px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: all 0.15s;
  }

  .thread-item:hover {
    border-color: var(--linen);
    background: var(--cream);
  }

  .thread-swatch {
    width: 28px; height: 28px;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: inset 0 0 0 1px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    color: white;
    font-weight: 600;
    font-family: 'DM Mono', monospace;
  }

  .thread-info { flex: 1; min-width: 0; }
  .thread-name {
    font-size: 0.82rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .thread-dmc {
    font-family: 'DM Mono', monospace;
    font-size: 0.65rem;
    color: var(--soft);
    margin-top: 1px;
  }
  .thread-count {
    font-family: 'DM Mono', monospace;
    font-size: 0.65rem;
    color: var(--soft);
    flex-shrink: 0;
  }

  /* Instructions */
  .instructions-content {
    font-size: 0.84rem;
    line-height: 1.65;
    color: var(--mid);
  }

  .instructions-content h3 {
    font-family: 'Playfair Display', serif;
    font-size: 0.95rem;
    color: var(--ink);
    margin: 14px 0 5px;
  }

  .instructions-content h3:first-child { margin-top: 0; }

  .instructions-content ol {
    padding-left: 18px;
  }

  .instructions-content li { margin-bottom: 4px; }

  /* Zoom controls */
  .zoom-controls {
    display: flex;
    gap: 6px;
  }

  .zoom-btn {
    width: 28px; height: 28px;
    border: 1px solid var(--linen);
    background: white;
    border-radius: 3px;
    font-size: 0.9rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    font-family: 'DM Mono', monospace;
    color: var(--mid);
  }

  .zoom-btn:hover { border-color: var(--mid); color: var(--ink); }

  /* Download */
  .download-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: var(--ink);
    color: var(--cream);
    border: none;
    border-radius: 3px;
    font-family: 'DM Mono', monospace;
    font-size: 0.75rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    cursor: pointer;
    width: 100%;
    justify-content: center;
    margin-top: 8px;
    transition: background 0.2s;
  }

  .download-btn:hover { background: var(--thread-red); }

  /* Error */
  .error-box {
    display: none;
    background: #fde8e6;
    border: 1px solid #f5c0bc;
    border-radius: 4px;
    padding: 16px 20px;
    font-size: 0.88rem;
    color: #7a1a12;
    max-width: 640px;
    margin: 0 auto 24px;
  }

  /* Highlight on hover - show stitch position */
  .stitch-highlight { 
    position: absolute;
    pointer-events: none;
    background: rgba(255,255,255,0.3);
    border: 1px solid rgba(255,255,255,0.6);
  }

  /* Legend overlay on canvas */
  .legend-toggle {
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--soft);
    cursor: pointer;
    padding: 4px 8px;
    border: 1px solid var(--linen);
    border-radius: 2px;
    transition: all 0.15s;
  }

  .legend-toggle:hover { color: var(--ink); border-color: var(--mid); }

  /* Adjust Image Panel */
  .adjust-section {
    display: none;
    max-width: 640px;
    margin: 0 auto 24px;
    background: var(--warm-white);
    border: 1px solid var(--linen);
    border-radius: 4px;
    overflow: hidden;
  }

  .adjust-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--linen);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .adjust-title {
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--soft);
  }

  .adjust-toggle {
    border: 1px solid var(--linen);
    background: white;
    border-radius: 2px;
    padding: 6px 10px;
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    cursor: pointer;
    color: var(--mid);
  }

  .adjust-body {
    padding: 16px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
  }

  .adjust-preview {
    background: #efe9df;
    border: 1px solid var(--linen);
    border-radius: 3px;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #adjustCanvas {
    max-width: 100%;
    height: auto;
    display: block;
    image-rendering: pixelated;
  }

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

  .adjust-control label {
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--soft);
    display: flex;
    justify-content: space-between;
  }

  .adjust-control input[type=range] {
    width: 100%;
  }

  .adjust-actions {
    grid-column: 1 / -1;
    display: flex;
    justify-content: flex-end;
  }

  .adjust-reset {
    border: 1px solid var(--linen);
    background: white;
    border-radius: 2px;
    padding: 6px 12px;
    font-family: 'DM Mono', monospace;
    font-size: 0.7rem;
    cursor: pointer;
    color: var(--mid);
  }

  /* Panel toggles */
  .panel-toggles {
    display: flex;
    gap: 8px;
    align-items: center;
  }

  .toggle-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--linen);
    padding: 4px 8px;
    border-radius: 999px;
    font-family: 'DM Mono', monospace;
    font-size: 0.65rem;
    color: var(--mid);
    background: white;
  }

  .toggle-pill input {
    accent-color: var(--thread-red);
  }

  /* Palette controls */
  .palette-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 10px;
  }

  .toggle-group {
    display: flex;
    gap: 6px;
  }

  .toggle-btn {
    padding: 4px 10px;
    border: 1px solid var(--linen);
    background: white;
    border-radius: 2px;
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    cursor: pointer;
    color: var(--mid);
  }

  .toggle-btn.active {
    background: var(--ink);
    color: var(--cream);
    border-color: var(--ink);
  }

  .merge-btn {
    border: 1px solid var(--linen);
    background: white;
    border-radius: 2px;
    padding: 4px 10px;
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    cursor: pointer;
    color: var(--mid);
  }

  .thread-item.active {
    border-color: var(--thread-red);
    box-shadow: 0 0 0 2px rgba(192,57,43,0.2);
  }

  .thread-item .thread-actions {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .remove-btn {
    border: 1px solid #e1b2ae;
    background: #fde8e6;
    color: #7a1a12;
    border-radius: 2px;
    font-family: 'DM Mono', monospace;
    font-size: 0.65rem;
    padding: 2px 6px;
    cursor: pointer;
  }

  /* Modal */
  .modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1000;
    align-items: center;
    justify-content: center;
  }

  .modal.show { display: flex; }

  .modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.45);
  }

  .modal-card {
    position: relative;
    z-index: 1;
    width: min(720px, 92vw);
    max-height: 80vh;
    background: var(--warm-white);
    border: 1px solid var(--linen);
    border-radius: 4px;
    display: flex;
    flex-direction: column;
  }

  .modal-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--linen);
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--mid);
  }

  .modal-close {
    border: 1px solid var(--linen);
    background: white;
    border-radius: 2px;
    padding: 4px 8px;
    font-family: 'DM Mono', monospace;
    font-size: 0.68rem;
    cursor: pointer;
  }

  .modal-body {
    padding: 12px 16px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: hidden;
  }

  #colorSearch {
    border: 1px solid var(--linen);
    border-radius: 2px;
    padding: 8px 10px;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.88rem;
  }

  .color-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
    overflow: auto;
    padding-right: 6px;
  }

  .color-card {
    border: 1px solid var(--linen);
    border-radius: 3px;
    padding: 6px;
    cursor: pointer;
    background: white;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .color-card .swatch {
    height: 26px;
    border-radius: 2px;
  }

  .color-card .label {
    font-family: 'DM Mono', monospace;
    font-size: 0.64rem;
    color: var(--mid);
  }

  @media (max-width: 768px) {
    .adjust-body { grid-template-columns: 1fr; }
  }

  .site-footer {
    margin-top: 48px;
    text-align: center;
    font-family: 'DM Mono', monospace;
    font-size: 0.72rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--soft);
  }
