/* Cactus Tracker — green nature theme */
:root {
  --primary: #2D5F2D;
  --primary-dark: #1f4520;
  --accent: #8BC34A;
  --accent-soft: #C5E1A5;
  --bg: #FAFAFA;
  --card: #FFFFFF;
  --border: #E0E0E0;
  --text: #1a2a1a;
  --text-muted: #6b7a6b;
  --danger: #C62828;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.08);
  --radius: 12px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Noto Sans Thai', 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; cursor: pointer; }
a { color: var(--primary); }

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--primary);
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow-sm);
}
.topbar__title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.2px;
}
/* Back button rendered inside <main>, not topbar — needs dark text on light bg. */
.topbar__back {
  background: var(--card);
  border: 1.5px solid var(--border);
  color: var(--primary);
  font-size: 14px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.topbar__back:active { background: var(--accent-soft); }
.topbar__back:hover { border-color: var(--accent); }
.topbar__left { display: flex; align-items: center; }
.user-chip {
  font-size: 12px;
  background: rgba(255,255,255,0.15);
  padding: 4px 10px;
  border-radius: 999px;
}

/* Main */
main {
  flex: 1;
  padding: 16px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.loading, .empty {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 16px;
}

/* Home: group grid */
.home-intro {
  margin: 0 0 16px;
  color: var(--text-muted);
  font-size: 14px;
}
.group-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.group-btn {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 10px;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: transform 0.05s, border-color 0.15s, box-shadow 0.15s;
  min-height: 78px;
}
.group-btn:active {
  transform: scale(0.97);
}
.group-btn:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
}
.group-btn__code {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
  font-family: 'Inter', sans-serif;
  letter-spacing: 0.5px;
}
.group-btn__name {
  font-size: 11px;
  color: var(--text);
  line-height: 1.25;
}
.group-btn__count {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: auto;
}
.group-btn__count strong { color: var(--accent); font-weight: 600; }

/* Form */
.form {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.form__row { display: flex; flex-direction: column; gap: 6px; }
.form__row label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.form__row label .req { color: var(--danger); }
.form__row input,
.form__row select,
.form__row textarea {
  font-family: inherit;
  font-size: 16px;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
}
.form__row input:focus,
.form__row select:focus,
.form__row textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(139,195,74,0.18);
}
.form__row input[readonly] {
  background: #f5f5f5;
  color: var(--text-muted);
}
.form__row textarea {
  resize: vertical;
  min-height: 70px;
}
.form__hint { font-size: 12px; color: var(--text-muted); }
.form__code-preview {
  background: linear-gradient(135deg, var(--accent-soft), #fff);
  border: 1.5px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px 16px;
  text-align: center;
}
.form__code-preview span {
  display: block;
  font-size: 12px;
  color: var(--primary-dark);
  font-weight: 600;
  margin-bottom: 4px;
}
.form__code-preview strong {
  font-size: 28px;
  color: var(--primary);
  font-family: 'Inter', sans-serif;
  letter-spacing: 1px;
}

/* Photo input */
.photo-input { display: flex; flex-direction: column; gap: 8px; }
.photo-input__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--accent-soft);
  color: var(--primary-dark);
  border: 1.5px dashed var(--accent);
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
}
.photo-input__btn input { display: none; }
.photo-preview {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.photo-preview img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

/* Buttons */
.btn-primary, .btn-secondary {
  font-size: 16px;
  font-weight: 700;
  border-radius: var(--radius);
  padding: 14px 20px;
  border: none;
  width: 100%;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn-primary svg { display: block; }
.btn-primary:active { background: var(--primary-dark); }
.btn-primary:disabled {
  background: var(--text-muted);
  cursor: not-allowed;
  opacity: 0.7;
}
.btn-secondary {
  background: #fff;
  color: var(--primary);
  border: 1.5px solid var(--primary);
}

/* List view */
.list-tools {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.list-tools input {
  flex: 1;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-family: inherit;
}
.list-tools input:focus { outline: none; border-color: var(--accent); }
.record-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.record-card {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.05s, box-shadow 0.15s;
}
.record-card:active { transform: scale(0.98); }
.record-card:hover { box-shadow: var(--shadow-md); }
.record-card__img {
  width: 100%;
  aspect-ratio: 1;
  background: #f0f0f0 center/cover no-repeat;
  position: relative;
}
.record-card__img--empty {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7f2, #eaf0e3);
  color: #9aa899;
  font-size: 12px;
}
.record-card__body { padding: 10px 12px; }
.record-card__code {
  font-weight: 700;
  font-size: 16px;
  color: var(--primary);
  font-family: 'Inter', sans-serif;
}
.record-card__meta {
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
  margin-top: 2px;
}
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge--เก็บ  { background: #e8f5e9; color: #2e7d32; }
.badge--ขาย  { background: #fff3e0; color: #ef6c00; }
.badge--ตาย  { background: #ffebee; color: #c62828; }
.badge--อื่นๆ { background: #f5f5f5; color: #616161; }

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary-dark);
  color: #fff;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  z-index: 100;
  max-width: calc(100% - 32px);
}
.toast--err { background: var(--danger); }

/* Sticky save bar */
.save-bar {
  position: sticky;
  bottom: 0;
  margin: 16px -16px -16px;
  padding: 12px 16px env(safe-area-inset-bottom, 12px);
  background: rgba(250,250,250,0.95);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
}

/* Detail */
.detail-img {
  width: 100%;
  max-height: 360px;
  object-fit: cover;
  border-radius: var(--radius);
  background: #f0f0f0;
  display: block;
}
.detail-img--empty {
  height: 240px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7f2, #eaf0e3);
  color: #9aa899;
  font-size: 14px;
}

/* LQIP overlay: blurred base64 placeholder paints in 0ms; real image fades over it. */
.cover-frame {
  position: relative;
  width: 100%;
  max-height: 360px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #f0f0f0;
  aspect-ratio: 1 / 1;
}
@media (min-width: 768px) {
  .cover-frame { aspect-ratio: 4 / 3; max-height: 480px; }
}
.cover-frame .cover-lqip {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: blur(14px);
  transform: scale(1.08);
}
.cover-frame .cover-real {
  position: relative;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.25s ease-out;
  display: block;
}
.cover-frame .cover-real.loaded { opacity: 1; }

/* Embla swipe carousel (Instagram-style photo navigation) */
.embla {
  position: relative;
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
}
.embla__viewport {
  overflow: hidden;
  width: 100%;
}
.embla__container {
  display: flex;
  touch-action: pan-y pinch-zoom;
}
.embla__slide {
  flex: 0 0 100%;
  min-width: 0;
  position: relative;
}
.embla__slide .cover-frame {
  border-radius: 0;
  aspect-ratio: 1 / 1;
  max-height: none;
}
@media (min-width: 768px) {
  .embla__slide .cover-frame { aspect-ratio: 4 / 3; }
}
.embla__dots {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin: 10px 0 4px;
}
.embla__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--border);
  border: 0;
  padding: 0;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s;
}
.embla__dot--selected {
  background: var(--primary);
  transform: scale(1.3);
}
.embla__counter {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}
.slide-delete {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: rgba(0,0,0,0.45);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 2;
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  transition: background 0.15s, transform 0.1s;
}
.slide-delete:hover { background: rgba(198,40,40,0.85); }
.slide-delete:active { transform: scale(0.92); }
.slide-delete:disabled { opacity: 0.4; cursor: not-allowed; }
.slide-delete svg { display: block; }

/* List card LQIP — same idea on the small cards in list view. */
.record-card__img-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  background: #f0f0f0;
}
.record-card__img-frame .record-card__img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  aspect-ratio: auto;
}
.record-card__img--lqip {
  background-size: cover;
  background-position: center;
  filter: blur(8px);
  transform: scale(1.06);
}
.record-card__img--real {
  background-size: cover;
  background-position: center;
  opacity: 0;
  transition: opacity 0.2s ease-out;
}
.record-card__img--real.loaded { opacity: 1; }
.gallery-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 2px 4px;
  scroll-snap-type: x mandatory;
  scrollbar-width: thin;
}
.gallery-thumb {
  flex: 0 0 auto;
  width: 72px;
  height: 72px;
  padding: 0;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  overflow: hidden;
  cursor: pointer;
  scroll-snap-align: start;
  transition: border-color 0.15s, transform 0.05s;
}
.gallery-thumb:hover { border-color: var(--accent); }
.gallery-thumb:active { transform: scale(0.95); }
.gallery-thumb.is-active {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(45,95,45,0.18);
}
.gallery-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery-folder-link {
  display: inline-block;
  margin-top: 6px;
  font-size: 13px;
  color: var(--primary);
  text-decoration: none;
}
.gallery-folder-link:hover { text-decoration: underline; }
.detail-meta {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-top: 12px;
}
.detail-meta dl { margin: 0; display: grid; grid-template-columns: 100px 1fr; gap: 8px 12px; font-size: 14px; align-items: start; }
.detail-meta dt { color: var(--text-muted); font-weight: 500; padding-top: 2px; }
.detail-meta dd { margin: 0; min-width: 0; }

/* Inline edit row: value left, minimal pencil icon pinned to far right */
.dd-editable {
  display: flex;
  align-items: center;
  gap: 6px;
}
.dd-editable .dd-value { min-width: 0; flex: 1; word-break: break-word; }
.dd-empty { color: var(--text-muted); }
.dd-edit-btn {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  padding: 4px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.55;
  cursor: pointer;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
  flex-shrink: 0;
  margin-left: auto;
}
.dd-edit-btn:hover { opacity: 1; color: var(--primary); background: rgba(45,95,45,0.06); }
.dd-edit-btn:active { background: rgba(45,95,45,0.12); }

/* Edit mode — input takes the row width */
.dd-editing { flex-direction: column; align-items: stretch; gap: 6px; }
.dd-input {
  width: 100%;
  padding: 8px 10px;
  border: 1.5px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  background: #fff;
  color: var(--text);
}
.dd-input:focus { outline: none; box-shadow: 0 0 0 3px rgba(139,195,74,0.2); }
textarea.dd-input { resize: vertical; min-height: 60px; }
.dd-edit-actions { display: flex; gap: 6px; }
.dd-save, .dd-cancel {
  flex: 1;
  padding: 7px 10px;
  font-size: 16px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: #fff;
  cursor: pointer;
  font-weight: 600;
}
.dd-save { background: var(--primary); color: #fff; border-color: var(--primary); }
.dd-cancel { color: var(--text-muted); }

/* Detail action row — primary action (Edit) + minimal trash */
.detail-actions {
  display: flex;
  gap: 10px;
  align-items: stretch;
  margin-top: 16px;
}
.detail-actions .btn-primary { flex: 1; width: auto; }
.btn-trash {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius);
  width: 52px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.btn-trash:hover { color: var(--danger); border-color: var(--danger); }
.btn-trash:active { background: rgba(198,40,40,0.08); }
.btn-trash:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-trash svg { display: block; }

/* ─── Tablet (≥768px) — 4 cols per spec ─── */
@media (min-width: 768px) {
  main { padding: 24px; }
  .group-grid { grid-template-columns: repeat(4, 1fr); gap: 14px; }
  .group-btn { min-height: 96px; padding: 16px; }
  .group-btn__code { font-size: 26px; }
  .group-btn__name { font-size: 13px; }
  .group-btn__count { font-size: 12px; }
  .record-grid { grid-template-columns: repeat(3, 1fr); gap: 14px; }
  .form { padding: 24px; max-width: 640px; margin: 0 auto; }
}

/* ─── Desktop (≥1200px) — 6 cols per spec ─── */
@media (min-width: 1200px) {
  .group-grid { grid-template-columns: repeat(6, 1fr); gap: 16px; }
  .group-btn { min-height: 104px; }
  .group-btn__code { font-size: 28px; }
  .record-grid { grid-template-columns: repeat(5, 1fr); }
}
