:root {
  --primary: #8B4513;
  --primary-light: #D2691E;
  --accent: #2E7D32;
  --bg: #FEFCF8;
  --card: #FFF;
  --text: #333;
  --muted: #888;
  --border: #E0D5C5;
  --pass: #2E7D32;
  --fail: #C62828;
  --unsure: #E65100;
  --depth: #0288D1;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ── Access Gate ── */
.gate-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 24px;
}
.gate-overlay.hidden { display: none; }
.gate-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 24px;
  max-width: 340px;
  width: 100%;
  text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}
.gate-box h2 {
  font-size: 1.3em;
  color: var(--primary);
  margin-bottom: 6px;
}
.gate-box p {
  font-size: 0.85em;
  color: var(--muted);
  margin-bottom: 20px;
}
.gate-box input {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 1.1em;
  text-align: center;
  letter-spacing: 2px;
  margin-bottom: 14px;
  -webkit-appearance: none;
}
.gate-box input:focus {
  outline: none;
  border-color: var(--primary);
}
.gate-box button {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1em;
  font-weight: 600;
  cursor: pointer;
}
.gate-box button:active { opacity: 0.85; }
.gate-error {
  color: var(--fail);
  font-size: 0.82em;
  margin-top: 10px;
  min-height: 1.2em;
}
.app-hidden { display: none !important; }
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding-bottom: calc(70px + var(--safe-bottom));
  overflow-x: hidden;
}

/* ── Header ── */
.header {
  background: var(--primary);
  color: white;
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.header h1 { font-size: 1.15em; font-weight: 700; }
.header .badge-count {
  background: var(--primary-light);
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 0.85em;
  font-weight: 600;
}

/* ── Bottom Nav ── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  border-top: 1px solid var(--border);
  display: flex;
  padding-bottom: var(--safe-bottom);
  z-index: 100;
}
.nav-btn {
  flex: 1;
  padding: 8px 4px 6px;
  text-align: center;
  background: none;
  border: none;
  font-size: 0.7em;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.nav-btn.active { color: var(--primary); }
.nav-btn svg { width: 24px; height: 24px; }

/* ── Pages ── */
.page { display: none; padding: 16px; }
.page.active { display: block; }

/* ── Cards ── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.card-title {
  font-size: 0.8em;
  font-weight: 720;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--primary);
  margin-bottom: 2px;
}

/* ── Capture Buttons ── */
.capture-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}
.capture-btn {
  background: var(--card);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 6px 3px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
}
.capture-btn:active { transform: scale(0.97); }
.capture-btn.has-file {
  border-style: solid;
  border-color: var(--accent);
  background: #F1F8E9;
}
.capture-btn svg { width: 26px; height: 26px; color: var(--primary-light); margin-bottom: 2px; }
.capture-btn.has-file svg { color: var(--accent); }
.capture-btn .label { font-size: 0.82em; font-weight: 600; color: var(--text); }
.capture-btn .sublabel { font-size: 0.7em; color: var(--muted); margin-top: 1px; }
.capture-btn .check-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 22px;
  height: 22px;
  background: var(--accent);
  color: white;
  border-radius: 50%;
  font-size: 14px;
  line-height: 22px;
  display: none;
}
.capture-btn.has-file .check-badge { display: block; }
.capture-btn input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

/* ── Form Elements ── */
.form-row { margin-bottom: 4px; }
.form-row label {
  display: block;
  font-size: 0.8em;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 4px;
}
.form-row input, .form-row select, .form-row textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1em;
  background: white;
  -webkit-appearance: none;
}
.form-row textarea { min-height: 70px; resize: vertical; }
.form-row-inline {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* ── Code Assessment (3-state toggle) ── */
.code-row {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid #F0EBE3;
  gap: 10px;
}
.code-row:last-child { border-bottom: none; }
.code-info { flex: 1; min-width: 0; }
.code-info .code-id { font-weight: 700; font-size: 0.9em; color: var(--primary); }
.code-info .code-desc { font-size: 0.78em; color: var(--muted); line-height: 1.3; }
.toggle-group {
  display: flex;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.toggle-group button {
  padding: 8px 11px;
  border: none;
  background: white;
  font-size: 0.78em;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
  border-right: 1px solid var(--border);
  transition: all 0.15s;
}
.toggle-group button:last-child { border-right: none; }
.toggle-group button.sel-pass { background: #E8F5E9; color: var(--pass); }
.toggle-group button.sel-fail { background: #FFEBEE; color: var(--fail); }
.toggle-group button.sel-unsure { background: #FFF3E0; color: var(--unsure); }

/* ── Action Buttons ── */
.btn {
  display: block;
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  font-size: 1em;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--primary); color: white; }
.btn-accent { background: var(--accent); color: white; }
.btn-outline {
  background: white;
  color: var(--primary);
  border: 2px solid var(--primary);
}
.btn-danger { background: var(--fail); color: white; }
.btn-muted { background: #E0E0E0; color: var(--text); }
.btn + .btn { margin-top: 8px; }
.btn:disabled { opacity: 0.5; pointer-events: none; }

/* ── Entry List ── */
.entry-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.15s;
}
.entry-item:active { transform: scale(0.99); background: #FAF6F0; }
.entry-thumb {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  object-fit: cover;
  background: #EEE;
  flex-shrink: 0;
}
.entry-thumb.video-thumb {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #333;
}
.entry-details { flex: 1; min-width: 0; }
.entry-details .entry-id {
  font-weight: 700;
  font-size: 0.95em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-details .entry-meta {
  font-size: 0.78em;
  color: var(--muted);
  margin-top: 2px;
}
.entry-badges { display: flex; gap: 3px; flex-wrap: wrap; margin-top: 4px; }
.mini-badge {
  font-size: 0.68em;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 3px;
}
.mini-badge.pass { background: #E8F5E9; color: var(--pass); }
.mini-badge.fail { background: #FFEBEE; color: var(--fail); }
.mini-badge.unsure { background: #FFF3E0; color: var(--unsure); }
.entry-arrow { color: var(--muted); font-size: 1.2em; flex-shrink: 0; }

/* ── Preview strip ── */
.preview-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 0;
  margin-bottom: 12px;
  -webkit-overflow-scrolling: touch;
}
.preview-strip img, .preview-strip video {
  height: 80px;
  border-radius: 8px;
  object-fit: cover;
  border: 2px solid var(--border);
  flex-shrink: 0;
}

/* ── Empty State ── */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}
.empty-state svg { width: 48px; height: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 0.9em; margin-top: 8px; }

/* ── Export page ── */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}
.stat-box {
  text-align: center;
  padding: 8px 6px;
  background: #FAF6F0;
  border-radius: 8px;
}
.stat-box .stat-num { font-size: 1.25em; font-weight: 800; color: var(--primary); }
.stat-box .stat-label { font-size: 0.72em; color: var(--muted); margin-top: 2px; }

/* ── Auto-download toggle ── */
.auto-dl-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  margin-bottom: 6px;
}
.auto-dl-bar .label-wrap { display: flex; flex-direction: column; }
.auto-dl-bar .label-main { font-size: 0.88em; font-weight: 600; color: var(--text); }
.auto-dl-bar .label-sub { font-size: 0.75em; color: var(--muted); margin-top: 2px; }

/* iOS-style toggle switch */
.toggle-switch { position: relative; width: 44px; height: 26px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle-track {
  position: absolute; inset: 0; border-radius: 26px;
  background: #ccc; cursor: pointer; transition: background 0.2s;
}
.toggle-track::after {
  content: ''; position: absolute; width: 20px; height: 20px;
  border-radius: 50%; background: white; top: 3px; left: 3px;
  transition: transform 0.2s; box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.toggle-switch input:checked + .toggle-track { background: var(--accent); }
.toggle-switch input:checked + .toggle-track::after { transform: translateX(18px); }

/* Recording indicator */
.rec-status { display: none; align-items: center; gap: 6px; margin-top: 4px; }
.rec-status.active { display: flex; }
.rec-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--fail);
  animation: recPulse 1s ease-in-out infinite;
}
.rec-timer { font-size: 0.72em; color: var(--fail); font-variant-numeric: tabular-nums; font-weight: 600; }
@keyframes recPulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ── iOS-specific adjustments ── */
@supports (-webkit-touch-callout: none) {
  input[type="file"] { font-size: 16px; }
  select { font-size: 16px; }
}

/* ── Modal ── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: flex-end;
  justify-content: center;
}
.modal-overlay.show { display: flex; }
.modal-sheet {
  background: white;
  border-radius: 16px 16px 0 0;
  padding: 20px 16px;
  padding-bottom: calc(20px + var(--safe-bottom));
  width: 100%;
  max-width: 500px;
  max-height: 85dvh;
  overflow-y: auto;
  animation: slideUp 0.25s ease-out;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.modal-handle {
  width: 36px;
  height: 4px;
  background: #CCC;
  border-radius: 2px;
  margin: 0 auto 16px;
}

/* ── Toast ── */
.toast {
  position: fixed;
  top: calc(60px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%) translateY(calc(-100% - 80px));
  background: #333;
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.9em;
  font-weight: 600;
  z-index: 300;
  transition: transform 0.3s ease;
  pointer-events: none;
  max-width: calc(100vw - 32px);
  text-align: center;
  word-break: break-word;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ── Depth Data Card ── */
.depth-title { color: var(--depth) !important; }
.depth-info {
  font-size: 0.82em;
  color: var(--muted);
  margin-bottom: 10px;
}
#card-depth { border-color: #B3E5FC; }
#card-depth .capture-btn {
  border-color: var(--depth);
  border-style: dashed;
}
#card-depth .capture-btn.has-file {
  border-style: solid;
  border-color: var(--depth);
  background: #E1F5FE;
}
#card-depth .capture-btn svg { color: var(--depth); }
#card-depth .capture-btn .check-badge { background: var(--depth); }
.depth-preview-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 0;
  margin-bottom: 8px;
  -webkit-overflow-scrolling: touch;
}
.depth-thumb-wrap {
  flex-shrink: 0;
  text-align: center;
}
.depth-thumb {
  width: 80px;
  height: 60px;
  border-radius: 8px;
  border: 2px solid var(--depth);
  display: block;
}
.depth-thumb-label {
  font-size: 0.68em;
  color: var(--depth);
  font-weight: 600;
  margin-top: 2px;
  display: block;
  text-transform: capitalize;
}
.depth-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  gap: 8px;
}
.depth-clear-btn {
  font-size: 0.78em !important;
  padding: 6px 12px !important;
  color: var(--muted) !important;
  border-color: var(--border) !important;
  width: auto !important;
  display: inline-block !important;
}
.depth-app-btn {
  font-size: 0.78em;
  padding: 6px 12px;
  background: none;
  border: 2px solid var(--depth);
  border-radius: 8px;
  color: var(--depth);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  transition: all 0.15s;
}
.depth-app-btn:active { opacity: 0.7; }
.btn-small {
  padding: 6px 14px !important;
  font-size: 0.82em !important;
  width: auto !important;
  display: inline-block !important;
}
.mini-badge.depth { background: #E1F5FE; color: var(--depth); }

/* ── Add More Media (detail modal) ── */
.add-media-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}
.add-media-btn {
  display: inline-flex;
  align-items: center;
  padding: 7px 14px;
  background: white;
  border: 2px solid var(--primary);
  border-radius: 20px;
  color: var(--primary);
  font-size: 0.82em;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  user-select: none;
}
.add-media-btn:active {
  background: var(--primary);
  color: white;
}

/* ── Animal ID 3-part input ── */
.animalid-row {
  display: flex;
  align-items: flex-end;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.animalid-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.animalid-field:last-child { border-right: none; }
.animalid-field--num { flex: 1.4; }
.animalid-field input {
  width: 100%;
  padding: 10px 10px 4px;
  border: none;
  font-size: 1em;
  font-weight: 700;
  background: white;
  text-align: center;
  text-transform: uppercase;
  -webkit-appearance: none;
  outline: none;
  letter-spacing: 1px;
}
.animalid-field input:focus { background: #FDFAF5; }
/* hide number spinners */
.animalid-field input[type="number"]::-webkit-inner-spin-button,
.animalid-field input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; }
.animalid-field input[type="number"] { -moz-appearance: textfield; }
.animalid-field-label {
  font-size: 0.65em;
  color: var(--muted);
  text-align: center;
  padding: 2px 4px 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.animalid-sep { display: none; }
.animalid-preview {
  margin-top: 2px;
  font-size: 0.78em;
  color: var(--muted);
  text-align: center;
  letter-spacing: 0.5px;
  min-height: 1em;
}
.animalid-preview.ready {
  color: var(--primary);
  font-weight: 700;
}
.animalid-field input::placeholder {
  opacity: 0.3;
}

/* ── Code Assessment: Category Pills ── */
.code-hint {
  font-size: 0.78em;
  color: var(--muted);
  margin-bottom: 10px;
}
.code-category-pills {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
}
.cat-pill {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 2px solid var(--border);
  background: white;
  font-size: 0.85em;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
}
.cat-pill:active { transform: scale(0.93); }
.cat-pill.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.cat-pill.has-fail {
  border-color: var(--fail);
  color: var(--fail);
}
.cat-pill.has-fail.active {
  background: var(--fail);
  color: white;
}
.cat-pill.has-unsure {
  border-color: var(--unsure);
  color: var(--unsure);
}
.cat-pill.has-unsure.active {
  background: var(--unsure);
  color: white;
}
.cat-pill[data-badge]:not([data-badge=""])::after {
  content: attr(data-badge);
  position: absolute;
  top: -6px;
  right: -6px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-size: 0.65em;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  color: white;
}
.cat-pill.has-fail[data-badge]:not([data-badge=""])::after { background: var(--fail); }
.cat-pill.has-unsure[data-badge]:not([data-badge=""])::after { background: var(--unsure); }

/* ── Code Assessment: Category Panel ── */
.code-category {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 4px;
}
.cat-header {
  background: var(--primary);
  color: white;
  padding: 8px 12px;
  font-size: 0.82em;
  font-weight: 700;
}

/* ── Guide: Collapsible Categories ── */
.guide-category {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 8px;
}
.guide-cat-btn {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 10px 12px;
  background: white;
  border: none;
  cursor: pointer;
  gap: 8px;
  text-align: left;
  transition: background 0.15s;
}
.guide-cat-btn:active { background: #FAF6F0; }
.guide-cat-letter {
  background: var(--primary);
  color: white;
  border-radius: 6px;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95em;
  flex-shrink: 0;
}
.guide-cat-name {
  flex: 1;
  font-weight: 600;
  font-size: 1em;
  color: var(--text);
}
.guide-cat-count {
  font-size: 0.72em;
  color: var(--muted);
  flex-shrink: 0;
}
.guide-cat-arrow {
  font-size: 0.8em;
  color: var(--muted);
  flex-shrink: 0;
}
.guide-cat-panel { overflow: hidden; }
.guide-code-row {
  display: grid;
  grid-template-columns: 40px 1fr;
  padding: 8px 12px;
  gap: 8px;
  border-bottom: 1px solid #F0EBE3;
  align-items: start;
}
.guide-code-row:last-child { border-bottom: none; }
.guide-code-id {
  background: var(--primary);
  color: white;
  border-radius: 4px;
  padding: 2px 0;
  font-weight: 700;
  font-size: 0.78em;
  text-align: center;
  display: block;
  margin-top: 2px;
}
.guide-code-detail { min-width: 0; }
.guide-code-short {
  font-weight: 600;
  font-size: 0.85em;
  color: var(--text);
  margin-bottom: 1px;
}
.guide-code-desc {
  font-size: 0.78em;
  color: var(--muted);
  line-height: 1.4;
}
