/* Woolhouse PWA — mobile-first styles. No frameworks. */

:root {
  --bg:          #faf7f2;
  --surface:     #ffffff;
  --surface-2:   #f2ece3;
  --border:      #e4dccf;
  --text:        #2b2622;
  --muted:       #7d7268;
  --brand:       #6b4f3a;
  --brand-600:   #5a4130;
  --ok:          #2e7d54;
  --warn:        #b8860b;
  --danger:      #b3402f;
  --radius:      14px;
  --radius-sm:   10px;
  --shadow:      0 1px 2px rgba(43,38,34,.06), 0 4px 14px rgba(43,38,34,.05);
  --tap:         46px;
  --safe-b:      env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

body {
  min-height: 100dvh;
  padding-bottom: calc(64px + var(--safe-b)); /* room for tabbar */
}

/* ---------------------------------------------------------------- topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--brand);
  color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand__mark { font-size: 1.25rem; line-height: 1; }
.brand__name { font-weight: 650; letter-spacing: .2px; font-size: 1.05rem; }
.topbar__right { display: flex; align-items: center; gap: 10px; }

.badge {
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: .78rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.badge.hidden, .hidden { display: none !important; }

/* ------------------------------------------------------------- status bars */
.strip {
  padding: 8px 14px;
  font-size: .85rem;
  text-align: center;
  background: #fff4d6;
  color: #6a4d00;
  border-bottom: 1px solid var(--border);
}
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(78px + var(--safe-b));
  transform: translate(-50%, 16px);
  max-width: min(92vw, 460px);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(43,38,34,.94);
  color: #fff;
  font-size: .9rem;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
  z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok    { background: rgba(46,125,84,.96); }
.toast.warn  { background: rgba(184,134,11,.96); }
.toast.error { background: rgba(179,64,47,.96); }

/* ---------------------------------------------------------------- layout */
.content { padding: 12px 12px 20px; max-width: 720px; margin: 0 auto; }
.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fade .16s ease; }
@keyframes fade { from { opacity: .4 } to { opacity: 1 } }

.section-title {
  margin: 4px 2px 12px;
  font-size: 1.05rem;
  font-weight: 650;
}
.muted { color: var(--muted); }
.small { font-size: .82rem; }

.panel-head { margin-bottom: 6px; }

/* --------------------------------------------------------------- searchbar */
.searchbar { margin-bottom: 10px; }
.searchbar input {
  width: 100%;
  height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 1rem;
  color: var(--text);
  box-shadow: var(--shadow);
}
.searchbar input:focus-visible,
.form input:focus-visible,
.form select:focus-visible,
.quick input:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}

.list-actions { display: flex; justify-content: flex-end; margin-bottom: 8px; }

/* ------------------------------------------------------------- yarn cards */
.yarn-list { display: flex; flex-direction: column; gap: 10px; }

.card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform .08s ease, box-shadow .12s ease;
}
.card:active { transform: scale(.985); }

.swatch {
  flex: 0 0 auto;
  width: 40px; height: 40px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.swatch.small { width: 40px; height: 40px; }
.swatch--lg { width: 54px; height: 54px; border-radius: 14px; }
.swatch--empty {
  background-image: repeating-linear-gradient(45deg, #eee 0 6px, #e2d9cc 6px 12px);
}

.card__body { flex: 1 1 auto; min-width: 0; }
.card__title {
  font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card__meta {
  color: var(--muted); font-size: .84rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card__right { flex: 0 0 auto; text-align: right; }
.grams { font-weight: 680; font-variant-numeric: tabular-nums; }

.gauge {
  margin-top: 8px;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}
.gauge__fill { height: 100%; background: var(--ok); transition: width .25s ease; }
.gauge__fill.warn { background: var(--warn); }
.gauge__fill.danger { background: var(--danger); }

.empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty p { margin: 6px 0; }

/* ------------------------------------------------------------------ forms */
.form { display: flex; flex-direction: column; gap: 12px; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; }
.field > span { color: var(--muted); font-weight: 600; font-size: .82rem; }
.field-row { display: flex; gap: 10px; }
.field-row > .field { flex: 1 1 0; min-width: 0; }

.form input, .form select, .quick input {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 1rem;
  color: var(--text);
}
.form-actions { display: flex; gap: 10px; margin-top: 4px; }

/* --------------------------------------------------------- colorway picker */
.colorway-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 54px;
  align-items: center;
}
.swatch-btn {
  width: 38px; height: 38px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid transparent;
  background: var(--surface);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.swatch-btn .dot {
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 1px solid rgba(0,0,0,.12);
  display: block;
}
.swatch-btn.selected {
  border-color: var(--brand);
  box-shadow: 0 0 0 2px rgba(107,79,58,.18);
}

/* ----------------------------------------------------------------- buttons */
.btn {
  min-height: var(--tap);
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background .12s ease, transform .08s ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:active { background: var(--brand-600); }
.btn.ghost { background: transparent; }
.btn.ok { background: rgba(46,125,84,.12); border-color: rgba(46,125,84,.4); color: var(--ok); }
.btn.danger { background: rgba(179,64,47,.10); border-color: rgba(179,64,47,.4); color: var(--danger); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .85rem; font-weight: 600; }
.btn:disabled { opacity: .5; }

/* ------------------------------------------------------------------ detail */
.detail-body { display: flex; flex-direction: column; gap: 16px; }
.detail-head { display: flex; align-items: center; gap: 14px; }
.detail-title { margin: 0 0 4px; font-size: 1.2rem; font-weight: 680; }

.balance {
  padding: 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  text-align: center;
}
.balance__num {
  font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums;
}
.balance__num.warn { color: var(--warn); }
.balance__num.danger { color: var(--danger); }

.quick {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.quick__field { display: flex; flex-direction: column; gap: 6px; }
.quick__field label { font-size: .82rem; font-weight: 600; color: var(--muted); }
.quick__note { width: 100%; }
.quick__btns { display: flex; gap: 10px; }
.quick__btns .btn { flex: 1 1 0; }

.meta-grid {
  display: grid;
  grid-template-columns: minmax(96px, auto) 1fr;
  gap: 6px 14px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: .9rem;
}
.meta-key { color: var(--muted); font-weight: 600; }
.meta-val { word-break: break-word; }

.qr {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.qr__code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.15rem;
  letter-spacing: 2px;
  padding: 6px 10px;
  background: var(--surface-2);
  border-radius: 8px;
}

.history { display: flex; flex-direction: column; gap: 8px; }
.events { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.event {
  display: grid;
  grid-template-columns: auto auto 1fr;
  grid-template-areas: "delta kind when" "note note note";
  gap: 4px 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: .88rem;
}
.event__delta { grid-area: delta; font-weight: 700; font-variant-numeric: tabular-nums; }
.event__delta.ok { color: var(--ok); }
.event__delta.danger { color: var(--danger); }
.event__kind { grid-area: kind; color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .5px; }
.event__when { grid-area: when; text-align: right; }
.event__note { grid-area: note; }

/* ------------------------------------------------------------------ tabbar */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
  box-shadow: 0 -2px 12px rgba(43,38,34,.06);
}
.tabbar button {
  flex: 1 1 0;
  min-height: 58px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: .72rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.tabbar button.active { color: var(--brand); }
.tabbar__icon { font-size: 1.25rem; line-height: 1; }
.tabbar__label { font-weight: 600; }

/* ------------------------------------------------------------- responsiveness */
@media (min-width: 620px) {
  .content { padding: 18px 18px 28px; }
  body { padding-bottom: 0; }
  .tabbar {
    position: sticky;
    bottom: 0;
    margin: 0 auto;
    max-width: 720px;
    border-radius: var(--radius) var(--radius) 0 0;
  }
}

/* ---- Scan button (native QR) ---- */
.searchbar { display: flex; gap: 8px; align-items: center; }
.searchbar input[type="search"] { flex: 1 1 auto; }
.scan-btn {
  flex: 0 0 auto;
  width: 44px; height: 44px;
  padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 20px; line-height: 1;
  border-radius: 10px;
}
.scan-ico { transform: translateY(1px); }
