:root {
  --bg: #fbf5ef;
  --surface: #ffffff;
  --surface-2: #f3e8dd;
  --text: #2b211c;
  --muted: #8a7565;
  --accent: #c4643a;
  --accent-soft: #f3ddd1;
  --danger: #b3261e;
  --line: #e6d6c8;
  --radius: 20px;
  --shadow: 0 2px 10px rgba(43, 33, 28, .08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1614;
    --surface: #241e1b;
    --surface-2: #2f2723;
    --text: #f2e9e2;
    --muted: #a3907f;
    --accent: #e08556;
    --accent-soft: #3a2a22;
    --danger: #f2b8b5;
    --line: #3a302b;
    --shadow: 0 2px 10px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ── Sync status strip ───────────────────────────────────── */
.syncstrip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: max(7px, env(safe-area-inset-top)) 12px 7px;
  border: none;
  background: var(--surface-2);
  color: var(--muted);
  font-size: .78rem;
  font-weight: 600;
  text-align: center;
}

.syncdot {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: currentColor;
}

.syncstrip.ok { color: var(--muted); }
.syncstrip.ok .syncdot { background: #2e9e5b; }

.syncstrip.busy .syncdot { animation: pulse 1s ease-in-out infinite; }

.syncstrip.warn {
  background: var(--accent-soft);
  color: var(--accent);
}

.syncstrip.bad {
  background: var(--accent-soft);
  color: var(--danger);
}

.syncstrip.off .syncdot { background: var(--muted); opacity: .5; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .25; }
}

/* ── Views and tabs ──────────────────────────────────────── */
.view {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
  min-height: 0;
  padding: 8px 10px 4px;
}

.view[hidden] { display: none; }

#chartView { grid-template-rows: auto 1fr auto auto auto; }

.tabbar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 6px 10px max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 8px 4px;
  border: none;
  border-radius: 14px;
  background: none;
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
}

.tab svg { width: 22px; height: 22px; }

.tab[aria-selected="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ── Top bar ─────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chip {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 52px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 1.05rem;
  font-weight: 600;
  text-align: left;
  overflow: hidden;
}

.chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

/* ── Main ────────────────────────────────────────────────── */
main {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 10px;
  min-height: 0;
}

/* Grid children default to their content's minimum height, which on a short
   landscape screen makes them overflow and overlap instead of shrinking. */
main > * { min-height: 0; }

.rowcard {
  display: grid;
  grid-template-columns: 84px 1fr 84px;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.step {
  height: 72px;
  border: none;
  border-radius: 16px;
  background: var(--surface-2);
  font-size: 2rem;
  font-weight: 600;
  line-height: 1;
}

.step:active { background: var(--accent-soft); transform: scale(.96); }

.rowinfo { text-align: center; }

.label {
  display: block;
  font-size: .8rem;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}

.rowinfo .value {
  display: block;
  font-size: clamp(2.5rem, 9vw, 4rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

/* ── The big tap target ──────────────────────────────────── */
.stitchpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 0;
  padding: 16px;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: background .12s ease, transform .06s ease;
}

.stitchpad:active { background: var(--accent-soft); transform: scale(.995); }

.bignum {
  font-size: clamp(5rem, 30vh, 14rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--accent);
}

.target {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--muted);
}

.progress {
  width: min(100%, 420px);
  height: 10px;
  margin-top: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress .fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width .15s ease;
}

.hint {
  margin-top: 10px;
  font-size: .85rem;
  color: var(--muted);
}

/* Target reached */
body.celebrate .stitchpad { background: var(--accent-soft); }
body.celebrate .bignum { animation: pop .4s ease; }

@keyframes pop {
  0%, 100% { transform: scale(1); }
  40% { transform: scale(1.12); }
}

/* ── Bottom actions ──────────────────────────────────────── */
.actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.action {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 66px;
  padding: 8px 4px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: .8rem;
  font-weight: 600;
}

.action:active:not(:disabled) { background: var(--surface-2); transform: scale(.97); }
.action:disabled { opacity: .4; cursor: default; }

/* ── Sheets ──────────────────────────────────────────────── */
.sheet {
  width: min(560px, calc(100vw - 24px));
  max-height: 88dvh;
  margin: auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px;
  border: none;
  border-radius: 24px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}

.sheet::backdrop { background: rgba(0, 0, 0, .45); }

.sheet h2 {
  margin: 0 0 16px;
  font-size: 1.3rem;
}

.sheet hr {
  margin: 18px 0;
  border: none;
  border-top: 1px solid var(--line);
}

.field {
  display: block;
  margin-bottom: 16px;
}

.field > span {
  display: block;
  margin-bottom: 6px;
  font-size: .9rem;
  font-weight: 600;
}

.field em {
  font-style: normal;
  font-weight: 400;
  color: var(--muted);
}

.field input {
  width: 100%;
  min-height: 50px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  font-size: .95rem;
  line-height: 1.35;
}

.toggle input {
  flex: none;
  width: 24px;
  height: 24px;
  accent-color: var(--accent);
}

.sheet-actions {
  display: flex;
  gap: 8px;
  margin-top: 18px;
}

.sheet-actions.wrap { flex-wrap: wrap; }

.sheet-actions button {
  flex: 1;
  min-height: 50px;
  min-width: 120px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  font-weight: 600;
}

.sheet-actions .primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.sheet-actions .danger { color: var(--danger); }

/* ── Project list ────────────────────────────────────────── */
.projectlist {
  max-height: 45dvh;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.projectlist li {
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}

.projectlist .pick {
  flex: 1;
  padding: 14px 4px;
  border: none;
  background: none;
  text-align: left;
}

.projectlist .pick strong {
  display: block;
  font-size: 1rem;
}

.projectlist .pick small {
  color: var(--muted);
}

.projectlist li[aria-current="true"] .pick strong { color: var(--accent); }

.projectlist .del {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 12px;
  background: none;
  color: var(--muted);
}

/* ── Chart canvas ────────────────────────────────────────── */
.canvaswrap {
  position: relative;
  min-height: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}

#chartCanvas {
  display: block;
  touch-action: none;
}

.coords {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: .75rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

/* ── Palette strip ───────────────────────────────────────── */
.palettebar {
  display: flex;
  align-items: center;
  gap: 8px;
}

.palette {
  flex: 1;
  display: flex;
  gap: 6px;
  padding: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}

.palette::-webkit-scrollbar { display: none; }

.palette-hint {
  padding: 14px 4px;
  color: var(--muted);
  font-size: .85rem;
}

.swatch {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 58px;
  padding: 6px 2px;
  border: 2px solid transparent;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.swatch[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* A colour used on the chart that is not in your thread stash. */
.swatch.missing { position: relative; }

.swatch.missing::after {
  content: "!";
  position: absolute;
  top: 2px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 15px;
  height: 15px;
  border-radius: 999px;
  background: var(--danger);
  color: var(--surface);
  font-size: .62rem;
  font-weight: 800;
  line-height: 1;
}

.swatch .dot {
  width: 26px;
  height: 26px;
  border: 1px solid rgba(0, 0, 0, .18);
  border-radius: 8px;
}

.sw-label {
  font-size: .68rem;
  font-weight: 700;
}

.sw-count {
  min-height: .85em;
  color: var(--muted);
  font-size: .6rem;
  font-variant-numeric: tabular-nums;
}

.addcolour {
  flex: none;
  width: 54px;
  height: 54px;
  border: 1px dashed var(--line);
  border-radius: 14px;
  background: var(--surface);
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--muted);
}

/* ── Tools ───────────────────────────────────────────────── */
.tools {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

/* ── Selection bar ───────────────────────────────────────── */
.selbar {
  display: flex;
  align-items: center;
  gap: 5px;
}

.selbar[hidden] { display: none; }

.selinfo {
  flex: none;
  min-width: 58px;
  padding: 0 4px;
  color: var(--accent);
  font-size: .78rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.selbar button {
  flex: 1;
  min-height: 46px;
  padding: 0 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: .8rem;
  font-weight: 600;
}

.selbar button:active:not(:disabled) { background: var(--surface-2); transform: scale(.97); }
.selbar button:disabled { opacity: .35; cursor: default; }

.tool {
  display: grid;
  place-items: center;
  min-height: 56px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.tool[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.tool:active:not(:disabled) { transform: scale(.96); }
.tool:disabled { opacity: .35; cursor: default; }

/* ── Colour dialog ───────────────────────────────────────── */
.pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.note {
  margin: -4px 0 14px;
  color: var(--muted);
  font-size: .82rem;
  line-height: 1.45;
}

.subhead {
  margin: 16px 0 8px;
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}

.colourlist {
  max-height: 22dvh;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  list-style: none;
}

/* Chrome makes these scrollers keyboard-focusable; keep the ring on-brand. */
.colourlist:focus-visible,
.dmclist:focus-visible,
.stitchlist:focus-visible,
.projectlist:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}

.colourlist li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 2px;
  border-bottom: 1px solid var(--line);
}

.colourlist .empty-row {
  border: none;
  color: var(--muted);
}

.colourlist .dot,
.dmc-row .dot {
  flex: none;
  width: 30px;
  height: 30px;
  border: 1px solid rgba(0, 0, 0, .2);
  border-radius: 8px;
}

.colour-text {
  flex: 1;
  min-width: 0;
}

.colour-text strong {
  display: block;
  font-size: .95rem;
}

.colour-text small { color: var(--muted); }

.dmclist {
  max-height: 26dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.dmclist .empty {
  padding: 20px 4px;
  color: var(--muted);
  text-align: center;
}

.dmc-row {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  border-bottom: 1px solid var(--line);
}

.dmc-pick {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 8px 2px;
  border: none;
  background: none;
  text-align: left;
}

.dmc-pick:active { background: var(--surface-2); }

/* The tick that marks a thread as one you own. */
.own {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: none;
  color: var(--muted);
}

.own svg { width: 20px; height: 20px; opacity: .35; }

.own.owned {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.own.owned svg { opacity: 1; }

.colour-text small.warn { color: var(--danger); }

.colourlist.tall { max-height: 34dvh; }

.subhead.flush { margin: 0; }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 16px 0 8px;
}

.segmented {
  display: flex;
  flex: none;
  padding: 3px;
  border-radius: 999px;
  background: var(--surface-2);
}

.segmented button {
  min-width: 52px;
  padding: 8px 12px;
  border: none;
  border-radius: 999px;
  background: none;
  font-size: .85rem;
  font-weight: 700;
  color: var(--muted);
}

.segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
}

.custom-colour summary {
  padding: 10px 2px;
  font-weight: 600;
  cursor: pointer;
}

.custom-colour input[type="color"] {
  min-height: 50px;
  padding: 4px;
}

/* ── My stitches ─────────────────────────────────────────── */
.search {
  width: 100%;
  min-height: 48px;
  margin-bottom: 12px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.stitchlist {
  max-height: 48dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.stitchlist .empty {
  padding: 28px 12px;
  color: var(--muted);
  line-height: 1.5;
  text-align: center;
}

.stitch {
  border-bottom: 1px solid var(--line);
}

.stitch summary {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 14px 4px;
  cursor: pointer;
  list-style: none;
  font-weight: 600;
}

.stitch summary::-webkit-details-marker { display: none; }

.stitch summary .tag {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: .75rem;
  font-weight: 700;
  color: var(--muted);
}

.stitch ol {
  margin: 0 0 10px;
  padding: 0 4px 0 24px;
  line-height: 1.5;
}

.stitch ol li { margin-bottom: 6px; }

.stitch .tip {
  margin: 0 4px 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface-2);
  font-size: .9rem;
  line-height: 1.45;
}

.stitch .own-actions {
  display: flex;
  gap: 8px;
  margin: 0 4px 12px;
}

.stitch .own-actions button {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: none;
  font-size: .85rem;
  font-weight: 600;
}

.sheet textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  line-height: 1.5;
  resize: vertical;
}

/* ── Toast ───────────────────────────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: .9rem;
  font-weight: 600;
  opacity: 0;
  transform: translate(-50%, 10px);
  transition: opacity .2s ease, transform .2s ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ── Tablet / landscape ──────────────────────────────────── */
/* ── Sync panel ──────────────────────────────────────────── */
.codebox {
  margin: 0 0 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-align: center;
  user-select: all;
  word-break: break-all;
}

.codefield {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .04em;
}

/* This one follows a row of buttons, so it needs room above it. */
#syncLast { margin-top: 14px; }

.choicelist {
  margin: 0 0 4px;
  padding: 0;
  list-style: none;
}

.choicelist li {
  padding: 12px 2px;
  border-bottom: 1px solid var(--line);
}

.choicelist strong { display: block; }
.choicelist small { color: var(--muted); }

@media (min-width: 700px) {
  .view { padding: 16px 16px 6px; gap: 14px; }
  .rowcard { grid-template-columns: 120px 1fr 120px; padding: 14px; }
  .step { height: 88px; }
  .action { min-height: 76px; font-size: .95rem; }
  .swatch { width: 66px; }
  .tool { min-height: 64px; }
  .tabbar { padding: 8px 16px max(10px, env(safe-area-inset-bottom)); }
}

/* ── Landscape ───────────────────────────────────────────── */
/* Height is the scarce dimension here, so the row counter moves alongside the
   tap area rather than above it, and text scales from height not width. */
@media (orientation: landscape) {
  #crochetView main {
    grid-template-columns: minmax(140px, 24%) 1fr;
    grid-template-rows: 1fr;
  }

  #crochetView .rowcard {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
  }

  .step {
    height: auto;
    min-height: 58px;
  }

  .rowinfo .value { font-size: clamp(2rem, 15vh, 4rem); }
  .bignum { font-size: clamp(3.5rem, 34vh, 12rem); }

  .action { min-height: 54px; }
  .hint { margin-top: 4px; }

  /* Claw back vertical room on the chart side. */
  .tool { min-height: 46px; }
  .selbar button { min-height: 40px; }
  .swatch { width: 52px; padding: 4px 2px; }
  .swatch .dot { width: 22px; height: 22px; }
  .addcolour { width: 46px; height: 46px; }

  /* A row rather than a stack, so the tab bar is half the height. */
  .tab {
    flex-direction: row;
    gap: 8px;
    padding: 7px 4px;
  }
}

/* A phone on its side — very little height indeed. */
@media (orientation: landscape) and (max-height: 470px) {
  .view { gap: 6px; padding: 6px 8px 2px; }

  .step { min-height: 42px; font-size: 1.5rem; }
  .rowinfo .value { font-size: clamp(1.5rem, 13vh, 2.4rem); }
  .bignum { font-size: clamp(2.5rem, 30vh, 6rem); }

  .stitchpad { padding: 8px; gap: 0; }
  .label { font-size: .68rem; }
  .target { font-size: .9rem; }
  .hint { display: none; }

  .action { min-height: 42px; font-size: .72rem; gap: 2px; }
  .action svg { width: 18px; height: 18px; }

  .chip, .icon-btn { min-height: 44px; height: 44px; }
  .tabbar { padding: 4px 8px max(4px, env(safe-area-inset-bottom)); }
  .tab { padding: 5px 4px; font-size: .74rem; }
}
