:root {
  --accent: #2a9d8f;
  --primary: #d96c6c;
  --secondary: #f5e1d3;
  --ink: #4a3f3a;
  --paper: #f3e8da;
  --canvas: #e9d7c4;
  --green: #7fb069;
  --yellow: #e8b64c;
  --red: #c95d5d;
  --shadow: 0 6px 14px rgba(74, 63, 58, 0.22);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--canvas);
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  overscroll-behavior-y: none;
}

/* Canvas-textured binder background */
.binder {
  position: relative;
  min-height: 100%;
  padding: 20px 14px calc(88px + env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 15% 10%, rgba(217,108,108,0.06) 0, transparent 40%),
    radial-gradient(circle at 85% 20%, rgba(42,157,143,0.06) 0, transparent 40%),
    repeating-linear-gradient(0deg, rgba(0,0,0,0.015) 0 2px, transparent 2px 4px),
    repeating-linear-gradient(90deg, rgba(0,0,0,0.015) 0 2px, transparent 2px 4px),
    var(--canvas);
  padding-top: 54px;
}

/* Metal rings at top */
.rings {
  position: absolute;
  top: 12px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-around;
  padding: 0 70px;
}
.ring {
  width: 34px;
  height: 44px;
  border-radius: 50%;
  border: 6px solid #8d8580;
  background: linear-gradient(180deg, #c9c2bb, #9c948c);
  box-shadow: inset 0 0 0 3px #b7afa7, 0 3px 6px rgba(0,0,0,0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.ring-hole {
  width: 10px;
  height: 12px;
  border-radius: 50%;
  background: var(--canvas);
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
}

/* Page title */
.page-title {
  margin: 6px 0 2px;
  font-size: 30px;
  text-align: center;
  letter-spacing: 4px;
  color: var(--primary);
  font-style: italic;
  text-shadow: 1px 1px 0 rgba(255,255,255,0.6);
}
.page-subtitle {
  margin: 0 0 20px;
  text-align: center;
  font-size: 12px;
  letter-spacing: 2px;
  color: #8a7a6e;
  text-transform: uppercase;
}

/* VIEWS */
.view { display: none; }
.view.active { display: block; animation: fade 0.25s ease; }
@keyframes fade { from { opacity: 0.4; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ===== SPOOLS (Home counters) ===== */
.spools {
  display: flex;
  align-items: center;
  justify-content: space-around;
  margin: 10px 0 26px;
}
.spool {
  position: relative;
  border-radius: 14px;
  box-shadow: var(--shadow);
  transition: transform 0.2s;
}
.spool-large {
  width: 132px;
  height: 152px;
  background: linear-gradient(180deg, #fff7ec, #efe0cf);
  border: 3px solid #cbb39c;
}
.spool-small {
  width: 96px;
  height: 112px;
  background: linear-gradient(180deg, #fff7ec, #efe0cf);
  border: 3px solid #cbb39c;
}
.spool::before, .spool::after {
  content: "";
  position: absolute;
  left: -4px;
  right: -4px;
  height: 16px;
  background: linear-gradient(180deg, #d8c8b6, #bfae9a);
  border: 3px solid #cbb39c;
  z-index: 2;
}
.spool::before { top: 12px; border-radius: 6px; }
.spool::after { bottom: 12px; border-radius: 6px; }
.spool .thread-body {
  position: absolute;
  left: 10px;
  right: 10px;
  top: 30px;
  bottom: 30px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.spool .thread-body.thread-white {
  background: repeating-linear-gradient(0deg, #fff 0 4px, #e9e4da 4px 8px);
  box-shadow: inset 0 0 0 3px rgba(0,0,0,0.06);
}
.spool .thread-body.thread-color {
  background: repeating-linear-gradient(0deg, var(--primary) 0 4px, #e8a08f 4px 8px);
  box-shadow: inset 0 0 0 3px rgba(0,0,0,0.08);
}
.spool-count {
  font-family: Georgia, serif;
  font-weight: bold;
  font-style: italic;
  color: var(--accent);
  text-shadow: 0 1px 0 rgba(0,0,0,0.15);
  position: relative;
  z-index: 3;
  letter-spacing: 1px;
}
.spool-large .spool-count { font-size: 52px; }
.spool-small .spool-count { font-size: 40px; }

.section-label {
  margin: 4px 4px 12px;
  font-size: 13px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: #8a7a6e;
}

/* Recently added strip */
.swatch-strip {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 6px 2px 16px;
  scroll-snap-type: x mandatory;
}
.mini-swatch {
  flex: 0 0 auto;
  width: 104px;
  height: 104px;
  scroll-snap-align: start;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  text-align: center;
  font-size: 12px;
  font-style: italic;
  font-weight: bold;
  color: #5a463b;
  box-shadow: var(--shadow);
  border: 1px solid rgba(74,63,58,0.18);
  cursor: pointer;
}
.mini-swatch span {
  position: relative;
  z-index: 2;
  background: rgba(255,255,255,0.75);
  padding: 3px 6px;
  border-radius: 3px;
  font-size: 11px;
}
.swatch-fabric {
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.18) 0 6px, transparent 6px 12px),
    var(--fabric, #f0d9c8);
}

/* ===== CARD (swatch) ===== */
.card-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 40px;
}
.pattern-card {
  position: relative;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.swatch {
  position: relative;
  padding: 26px 18px 20px;
  border: 1px solid rgba(74,63,58,0.18);
  background:
    radial-gradient(circle at 20% 20%, rgba(255,255,255,0.25), transparent 50%),
    repeating-linear-gradient(45deg, rgba(255,255,255,0.14) 0 7px, transparent 7px 14px),
    var(--fabric, #f0d9c8);
}
.swatch-emb {
  font-size: 19px;
  font-weight: bold;
  font-style: italic;
  color: #5a463b;
  text-align: center;
  letter-spacing: 1px;
  margin-bottom: 12px;
  text-shadow: 0 0 0 #fff, 1px 1px 0 rgba(255,255,255,0.4);
  position: relative;
  z-index: 2;
}
.swatch-emb::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -2px;
  height: 3px;
  background: repeating-linear-gradient(90deg, rgba(74,63,58,0.5) 0 3px, transparent 3px 6px);
  border-radius: 2px;
  z-index: -1;
}
.card-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  position: relative;
  z-index: 2;
}
.diff-btn {
  font-size: 11px;
  font-weight: bold;
  letter-spacing: 1px;
  padding: 5px 10px;
  border-radius: 14px;
  border: 1px solid rgba(74,63,58,0.35);
  color: #fff;
  text-transform: uppercase;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.25);
}
.diff-btn.beginner { background: var(--green); }
.diff-btn.intermediate { background: var(--yellow); }
.diff-btn.advanced { background: var(--red); }
.craft-label {
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #6b5749;
  background: rgba(255,255,255,0.6);
  padding: 3px 7px;
  border-radius: 3px;
  border: 1px solid rgba(74,63,58,0.15);
}
.fav-fold {
  margin-left: auto;
  width: 26px;
  height: 26px;
  border-radius: 50% 50% 50% 4px;
  background: var(--accent);
  opacity: 0.85;
  box-shadow: inset -3px -3px 6px rgba(0,0,0,0.2);
  transform: rotate(-8deg);
}
.card-notes {
  margin-top: 12px;
  font-size: 12px;
  font-style: italic;
  color: #7a6a5c;
  position: relative;
  z-index: 2;
}

/* Card action menu */
.card-menu {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 5;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.7);
  color: #6b5749;
  font-size: 15px;
  font-weight: bold;
  line-height: 1;
  cursor: pointer;
}
.card-actions {
  position: absolute;
  top: 30px;
  right: 6px;
  z-index: 6;
  background: #fff;
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
  display: none;
}
.card-actions.show { display: block; }
.card-actions button {
  display: block;
  width: 100%;
  border: none;
  background: transparent;
  padding: 10px 16px;
  font-size: 13px;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}
.card-actions button:active { background: var(--secondary); }

/* Zigzag edges (pinking shear) */
.zigzag {
  --zig: 6px;
  clip-path: polygon(
    0 calc(var(--zig)*1) var(--zig) 0, calc(var(--zig)*2) calc(var(--zig)*1),
    calc(var(--zig)*3) 0, calc(var(--zig)*4) calc(var(--zig)*1),
    calc(100% - var(--zig)*4) calc(var(--zig)*1), calc(100% - var(--zig)*3) 0,
    calc(100% - var(--zig)*2) calc(var(--zig)*1), calc(100% - var(--zig)) 0, 100% var(--zig),
    100% calc(100% - var(--zig)), calc(100% - var(--zig)) 100%, calc(100% - var(--zig)*2) calc(100% - var(--zig)),
    calc(100% - var(--zig)*3) 100%, calc(100% - var(--zig)*4) calc(100% - var(--zig)),
    calc(var(--zig)*4) calc(100% - var(--zig)), calc(var(--zig)*3) 100%,
    calc(var(--zig)*2) calc(100% - var(--zig)), var(--zig) 100%, 0 calc(100% - var(--zig))
  );
}

/* FAB */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(96px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 6px 16px rgba(0,0,0,0.3);
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
}
.fab-plus { position: relative; width: 22px; height: 22px; }
.fab-plus::before, .fab-plus::after {
  content: "";
  position: absolute;
  background: #fff;
  border-radius: 2px;
}
.fab-plus::before { left: 50%; top: 0; width: 4px; height: 22px; transform: translateX(-50%); }
.fab-plus::after { top: 50%; left: 0; height: 4px; width: 22px; transform: translateY(-50%); }

/* Empty state */
.empty {
  text-align: center;
  padding: 40px 20px;
  color: #8a7a6e;
  font-style: italic;
  font-size: 14px;
}

/* ===== SEARCH ===== */
.search-box {
  margin-bottom: 16px;
}
.search-box input {
  width: 100%;
  padding: 14px 16px;
  font-size: 16px;
  border-radius: 10px;
  border: 2px solid #d0bea8;
  background: #fbf4ea;
  color: var(--ink);
  font-family: inherit;
}

/* ===== TAB BAR ===== */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(74px + env(safe-area-inset-bottom));
  padding: 8px 8px env(safe-area-inset-bottom);
  display: flex;
  align-items: stretch;
  gap: 6px;
  background: #cfb8a2;
  border-top: 1px solid rgba(74,63,58,0.2);
  z-index: 30;
}
.tab {
  flex: 1;
  position: relative;
  border: none;
  background: #b9a48c;
  color: #5d4a3d;
  font-family: Georgia, serif;
  font-style: italic;
  font-weight: bold;
  font-size: 13px;
  cursor: pointer;
  box-shadow: inset 0 -6px 10px rgba(0,0,0,0.12);
  transition: transform 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
}
.tab-name { position: relative; z-index: 2; }
.tab::before {
  content: "";
  position: absolute;
  left: 0; right: 0; top: -8px;
  height: 16px;
  background: repeating-linear-gradient(-45deg, rgba(255,255,255,0.2) 0 4px, transparent 4px 8px),
    currentColor;
  opacity: 0.35;
}
.tab.active {
  background: var(--secondary);
  color: #4a3f3a;
  transform: translateY(-6px);
  box-shadow: 0 -4px 10px rgba(0,0,0,0.25);
  z-index: 2;
}
.tab.active::before { opacity: 1; }

/* ===== MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(58,44,32,0.55);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.modal.show { display: flex; }
.modal-sheet {
  width: 100%;
  max-width: 480px;
  background: #fbf4ea;
  border-radius: 20px 20px 0 0;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  max-height: 92vh;
  overflow-y: auto;
  animation: slideUp 0.25s ease;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: 0.5; } to { transform: none; opacity: 1; } }
.modal-sheet h2 {
  margin: 0 0 16px;
  font-size: 20px;
  color: var(--primary);
  font-style: italic;
  letter-spacing: 1px;
  text-align: center;
}
#patternForm label {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
  color: #6b5749;
  font-weight: bold;
}
#patternForm input[type="text"],
#patternForm textarea,
#patternForm select {
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  font-size: 16px;
  border: 2px solid #d0bea8;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
}
.req { color: var(--red); }
.error { display: block; color: var(--red); font-size: 12px; min-height: 15px; margin-top: 3px; }
.fav-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.toggle { position: relative; }
.toggle input { display: none; }
.toggle-slider {
  display: inline-block;
  width: 52px;
  height: 30px;
  border-radius: 30px;
  background: #d8c6b0;
  position: relative;
  transition: background 0.2s;
}
.toggle-slider::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  transition: transform 0.2s;
}
.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider::after { transform: translateX(22px); }

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.btn {
  flex: 1;
  padding: 13px;
  font-size: 15px;
  border-radius: 10px;
  border: none;
  font-family: inherit;
  font-weight: bold;
  cursor: pointer;
}
.btn.solid { background: var(--primary); color: #fff; }
.btn.ghost { background: transparent; border: 2px solid #d0bea8; color: #6b5749; }
.btn:active { transform: scale(0.98); }