</div>

<div class="toast" id="toast"><i class="ti ti-circle-check" aria-hidden="true"></i> <span id="toastMsg"></span></div>

<script>
function toggleSection(id){
  var body=document.getElementById(id);
  var toggle=document.getElementById('toggleSec'+id.replace('sec',''));
  var isOpen=body.classList.contains('open');
  body.classList.toggle('open',!isOpen);
  toggle.classList.toggle('open',!isOpen);
}

function now(){
  var d=new Date();
  var pad=function(n){return n.toString().padStart(2,'0')};
  return d.getFullYear()+'-'+pad(d.getMonth()+1)+'-'+pad(d.getDate())+' '+pad(d.getHours())+':'+pad(d.getMinutes());
}
document.getElementById('autoDate').textContent=now();

function captureGPS(){
  document.getElementById('eastingField').value='378452';
  document.getElementById('northingField').value='1198304';
  document.getElementById('elevationField').value='2134';
  document.getElementById('coordStatus').style.display='flex';
  showToast('GPS coordinates captured · UTM Zone 37N');
}

function updateStrengthBar(sel){
  var v=parseInt(sel.value);
  var cells=document.querySelectorAll('#strengthBar .rs-cell');
  cells.forEach(function(c,i){c.classList.toggle('active',i<v)});
}

function updateWeatherBar(sel){}

function addSoilRow(){
  var letters=['A','B','C','D','E','F','G','H'];
  var tbody=document.getElementById('soilBody');
  var n=tbody.rows.length;
  var row=document.createElement('tr');
  row.innerHTML='<td><input type="text" value="'+letters[Math.min(n,7)]+'" style="width:36px"></td><td><input type="number" value="'+(n*0.5).toFixed(1)+'" step="0.1"></td><td><input type="number" value="'+((n+1)*0.5).toFixed(1)+'" step="0.1"></td><td><select><option>Dark Brown</option><option>Brown</option><option>Reddish Brown</option><option>Yellow</option><option>Grey</option><option>Black</option></select></td><td><select><option>Soft</option><option>Firm</option><option>Stiff</option><option>Very Stiff</option><option>Hard</option></select></td><td><select><option>Dry</option><option>Slightly Moist</option><option>Moist</option><option>Wet</option></select></td><td><select><option>Clay</option><option>Silt</option><option>Fine Sand</option><option>Medium Sand</option><option>Coarse Sand</option><option>Gravel</option></select></td><td><select><option>Low (CL)</option><option>Medium (CI)</option><option>High (CH)</option><option>Non-plastic</option></select></td><td><select><option>CL</option><option>CI</option><option>CH</option><option>SM</option><option>SC</option><option>SP</option><option>SW</option><option>GC</option><option>GM</option></select></td><td><button class="del-btn" onclick="delRow(this)"><i class="ti ti-x"></i></button></td>';
  tbody.appendChild(row);
}

function addDiscRow(){
  var tbody=document.getElementById('discBody');
  var n=tbody.rows.length+1;
  var label='J'+n;
  var row=document.createElement('tr');
  row.innerHTML='<td><input type="text" value="'+label+'" style="width:36px"></td><td><select><option>Joint</option><option>Fault</option><option>Bedding</option><option>Foliation</option><option>Shear Zone</option><option>Tension Crack</option></select></td><td><input type="number" placeholder="0–360" min="0" max="360" style="width:64px"></td><td><input type="number" placeholder="0–90" min="0" max="90" style="width:54px"></td><td><input type="number" placeholder="m" step="0.01" style="width:54px"></td><td><input type="number" placeholder="mm" step="0.1" style="width:54px"></td><td><select><option>Very Rough</option><option>Rough</option><option>Slightly Rough</option><option>Smooth</option><option>Slickensided</option></select></td><td><select><option>None</option><option>Clay</option><option>Calcite</option><option>Quartz</option><option>Chlorite</option></select></td><td><input type="number" placeholder="m" style="width:54px"></td><td><button class="del-btn" onclick="delRow(this)"><i class="ti ti-x"></i></button></td>';
  tbody.appendChild(row);
}

function addSampleRow(){
  var tbody=document.getElementById('sampleBody');
  var n=tbody.rows.length+1;
  var row=document.createElement('tr');
  row.innerHTML='<td><input type="text" value="S-0'+n+'" style="width:52px"></td><td><input type="number" value="'+(n*0.5).toFixed(1)+'" step="0.1" style="width:54px"></td><td><select><option>Disturbed</option><option>Undisturbed</option><option>Bulk</option><option>Rock Core</option></select></td><td><select><option>Atterberg Limits</option><option>Grain Size Analysis</option><option>Compaction</option><option>Shear Strength</option><option>UCS</option><option>Permeability</option></select></td><td><input type="text" placeholder="A" style="width:40px"></td><td><input type="text" placeholder="Notes…"></td><td><button class="del-btn" onclick="delRow(this)"><i class="ti ti-x"></i></button></td>';
  tbody.appendChild(row);
}

function delRow(btn){btn.closest('tr').remove();}

function showToast(msg){
  var t=document.getElementById('toast');
  document.getElementById('toastMsg').textContent=msg;
  t.style.display='flex';
  setTimeout(function(){t.style.display='none'},2800);
}

function saveDraft(){showToast('Draft saved · EXP-2026-0047');}

function submitRecord(){showToast('Record submitted to PostgreSQL · PostGIS geometry written');}
</script>
