:root {
  color-scheme: light;
  --linen: #f3eee4;
  --linen-deep: #e7dfd1;
  --paper: #fffdf8;
  --ink: #2a241e;
  --muted: #6f655c;
  --line: #e3d8c8;
  --thread: #8d3a32;
  --thread-dark: #732f28;
  --shadow: 0 18px 50px rgba(60, 40, 20, 0.12);
  --radius: 14px;
  font-family: "Segoe UI", Candara, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

body {
  display: flex;
  flex-direction: column;
  background: var(--linen);
  color: var(--ink);
  overflow: hidden;
}

button, input, select { font: inherit; color: inherit; }

button { cursor: pointer; }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  height: 68px;
  padding: 0 18px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  flex: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 180px;
}

.mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background:
    linear-gradient(var(--thread), var(--thread)) center/16px 2px no-repeat,
    linear-gradient(var(--thread), var(--thread)) center/2px 16px no-repeat,
    #f6efe6;
  transform: rotate(45deg);
}

.wordmark {
  font-family: Georgia, "Palatino Linotype", serif;
  font-size: 20px;
  letter-spacing: -0.03em;
  line-height: 1;
}

.tagline {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.name-field { flex: 1; max-width: 360px; }

.name-field input,
dialog input[type="text"],
dialog input[type="number"],
.search input,
.fabric select {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 8px 10px;
}

.name-field input:focus,
.search input:focus,
dialog input:focus,
.fabric select:focus,
button:focus-visible {
  outline: 2px solid var(--thread);
  outline-offset: 2px;
}

.top-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.short { display: none; }

.ghost, .primary {
  border-radius: 10px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  background: #fff;
}

.ghost:hover { background: #faf6ef; }

.primary {
  background: var(--thread);
  border-color: var(--thread);
  color: white;
}

.primary:hover { background: var(--thread-dark); }

.primary:disabled, .ghost:disabled, .tool:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.large { padding: 12px 18px; font-size: 16px; }

.split { position: relative; display: flex; }

.split .primary { border-radius: 10px 0 0 10px; }

.split .more {
  border-radius: 0 10px 10px 0;
  border-left: 1px solid rgba(255, 255, 255, 0.35);
  padding-inline: 8px;
  display: grid;
  place-items: center;
}

.menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 5;
  min-width: 220px;
  padding: 6px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 8px 10px;
}

.menu button:hover { background: #f6efe6; }

#welcome {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
}

#welcome[hidden], #workspace[hidden], .name-field[hidden] { display: none; }

.welcome-card {
  width: min(640px, 100%);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 36px 36px 28px;
  box-shadow: var(--shadow);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--thread);
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.welcome-card h1 {
  margin: 0;
  font-family: Georgia, "Palatino Linotype", serif;
  font-size: 42px;
  font-weight: 500;
  letter-spacing: -0.03em;
  line-height: 1.05;
}

.lede {
  color: var(--muted);
  font-size: 17px;
  line-height: 1.45;
  margin: 14px 0 22px;
}

.welcome-actions { display: flex; gap: 10px; flex-wrap: wrap; }

#workspace {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 76px minmax(0, 1fr) 300px;
}

.tools {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
  background: var(--paper);
  border-right: 1px solid var(--line);
  overflow: auto;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: 0;
  background: transparent;
  border-radius: 10px;
  padding: 6px 2px 5px;
  color: var(--ink);
}

.tool svg { width: 22px; height: 22px; }

.tool span { font-size: 10px; color: var(--muted); }

.tool:hover { background: #f6efe6; }

.tool[aria-pressed="true"] {
  background: #f4e4df;
  color: var(--thread);
}

.tool[aria-pressed="true"] span { color: var(--thread); }

.tool-gap { height: 8px; }

#stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: var(--linen-deep);
  outline: none;
  touch-action: none;
}

#grid, #overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#overlay { pointer-events: none; }

#stage.stitch { cursor: crosshair; }
#stage.unstitch { cursor: cell; }
#stage.fill, #stage.pick { cursor: crosshair; }
#stage.pan, #stage.spacepan { cursor: grab; }
#stage.panning { cursor: grabbing; }

.side {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background: var(--paper);
  border-left: 1px solid var(--line);
}

#current-color {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
}

.current-swatch {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.18);
  flex: none;
}

.current-code { font-weight: 650; }
.current-name { color: var(--muted); font-size: 13px; }

.recent { display: flex; gap: 6px; min-height: 22px; }

.swatch, .recent button, .used button {
  border: 1px solid rgba(0, 0, 0, 0.16);
  padding: 0;
  border-radius: 5px;
}

.swatch {
  width: 22px;
  height: 22px;
}

.swatch[aria-pressed="true"], .recent button[aria-pressed="true"] {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.search input { background: #fff; }

.palette {
  flex: 1;
  min-height: 80px;
  overflow: auto;
  padding-right: 4px;
}

.family {
  margin: 10px 0 6px;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.swatches { display: flex; flex-wrap: wrap; gap: 6px; }

.used-wrap h2 {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 650;
}

#used {
  max-height: 180px;
  overflow: auto;
}

.used-empty { margin: 0; color: var(--muted); font-size: 13px; }

.used button {
  width: 100%;
  display: grid;
  grid-template-columns: 18px 1fr auto;
  gap: 8px;
  align-items: center;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 4px;
  margin-bottom: 2px;
}

.used button:hover, .used button[aria-pressed="true"] { background: #f6efe6; }

.used i {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.16);
  display: block;
}

.used small { color: var(--muted); }

.fabric {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}

.fabric select { width: auto; }

.statusbar {
  min-height: 34px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 6px 14px calc(6px + env(safe-area-inset-bottom));
  background: var(--paper);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}

#status-size {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

dialog {
  border: 0;
  padding: 0;
  border-radius: 18px;
  box-shadow: var(--shadow);
  width: min(480px, calc(100vw - 32px));
}

dialog::backdrop { background: rgba(42, 36, 30, 0.45); }

dialog form { padding: 22px; }

dialog h2 {
  margin: 0 0 6px;
  font-family: Georgia, "Palatino Linotype", serif;
  font-weight: 500;
  font-size: 28px;
}

.dialog-copy { margin: 0 0 14px; color: var(--muted); }

dialog label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }

dialog label input[type="text"],
dialog label input[type="number"] { margin-top: 4px; color: var(--ink); }

.size-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }

.presets button, .check {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 6px 10px;
}

.size-note { min-height: 1.2em; color: var(--muted); font-size: 13px; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

.drop {
  border: 1.5px dashed #cbbba6;
  border-radius: 14px;
  background: #faf6ef;
  padding: 22px;
  text-align: center;
  margin-bottom: 12px;
}

.drop.hot { border-color: var(--thread); background: #f8ece8; }

.drop p { margin: 0 0 10px; }

#photo-controls label { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center; }

#photo-controls input[type="range"] { grid-column: 1 / -1; }

.check { display: flex; align-items: center; gap: 8px; color: var(--ink); }

#photo-preview,
#suggest-preview {
  width: 100%;
  height: 180px;
  background: var(--linen);
  border-radius: 10px;
  border: 1px solid var(--line);
}

.suggest-row { justify-content: flex-start; }

#suggest-dialog { width: min(520px, calc(100vw - 32px)); }

.colors-toggle { display: none; }

.colors-backdrop {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 3;
  border: 0;
  padding: 0;
  background: rgba(42, 36, 30, 0.28);
}

.colors-backdrop[hidden] { display: none !important; }

@media (max-width: 980px) {
  .tagline, .statusbar span:last-child { display: none; }
  .colors-toggle { display: flex; }
  #workspace { grid-template-columns: 68px minmax(0, 1fr); }
  .side { display: none; }
  body.colors-open .colors-backdrop { display: block; }
  body.colors-open .side {
    display: flex;
    position: absolute;
    z-index: 4;
    top: 8px;
    right: 8px;
    bottom: 8px;
    width: min(320px, calc(100% - 16px));
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: var(--shadow);
  }
}

@media (max-width: 760px) {
  .topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    height: auto;
    gap: 8px;
    padding: 8px 10px;
  }
  .brand { min-width: 0; }
  .wordmark { font-size: 18px; }
  .name-field { grid-column: 1 / -1; max-width: none; }
  .top-actions {
    margin-left: 0;
    min-width: 0;
    overflow-x: auto;
    justify-content: flex-start;
    padding-bottom: 2px;
  }
  .top-actions button, .split { flex: none; }
  .full { display: none; }
  .short { display: inline; }
  .ghost, .primary { padding: 8px 10px; }
  #welcome { overflow: auto; place-items: start center; padding: 12px; }
  .welcome-card { padding: 22px 16px 16px; border-radius: 18px; }
  .welcome-card h1 { font-size: 32px; }
  .lede { font-size: 16px; }
  .welcome-actions { flex-direction: column; }
  .welcome-actions .large { width: 100%; }
  #workspace { display: flex; flex-direction: column; }
  #stage { flex: 1; }
  .tools {
    order: 2;
    flex-direction: row;
    align-items: stretch;
    gap: 2px;
    overflow-x: auto;
    border-right: 0;
    border-top: 1px solid var(--line);
    padding: 4px 6px;
  }
  .tool { flex: none; min-width: 56px; min-height: 48px; justify-content: center; }
  .tool-gap { width: 8px; height: auto; flex: none; }
  body.colors-open .side {
    top: auto;
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
    height: min(62dvh, 520px);
  }
  dialog {
    width: calc(100vw - 16px);
    max-height: calc(100dvh - 16px);
  }
  dialog form {
    max-height: calc(100dvh - 16px);
    overflow: auto;
    padding: 16px;
  }
  dialog h2 { font-size: 24px; }
  .dialog-actions { flex-wrap: wrap; }
  .dialog-actions button { flex: 1 1 auto; }
  input, select, textarea { font-size: 16px; }
}
