:root {
  --bg-light: #f4f6f5;
  --bg-dark: #0e141b;
  --surface-light: #ffffff;
  --surface-dark: #161f29;
  --surface-alt-light: #ebf0ee;
  --surface-alt-dark: #1e2a36;
  --text-light: #152026;
  --text-dark: #eaf1f4;
  --muted-light: #51616b;
  --muted-dark: #9cb0bc;
  --border-light: #cfd9d6;
  --border-dark: #2c3b4a;
  --brand-light: #1b5e54;
  --brand-dark: #5ad2ba;
  --brand-text-light: #ffffff;
  --brand-text-dark: #07221d;
  --track-light: #dde6e3;
  --track-dark: #253341;
  --thumb-light: #6a7d78;
  --thumb-dark: #88a0ae;

  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-alt: var(--surface-alt-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --border: var(--border-light);
  --brand: var(--brand-light);
  --brand-text: var(--brand-text-light);
  --track: var(--track-light);
  --thumb: var(--thumb-light);

  --target-size: 24px;
  color-scheme: light dark;
  accent-color: var(--brand);
  scrollbar-color: var(--thumb) var(--track);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-alt: var(--surface-alt-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --border: var(--border-dark);
    --brand: var(--brand-dark);
    --brand-text: var(--brand-text-dark);
    --track: var(--track-dark);
    --thumb: var(--thumb-dark);
  }
}

@supports (color: light-dark(white, black)) {
  :root {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-alt: light-dark(var(--surface-alt-light), var(--surface-alt-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --brand: light-dark(var(--brand-light), var(--brand-dark));
    --brand-text: light-dark(var(--brand-text-light), var(--brand-text-dark));
    --track: light-dark(var(--track-light), var(--track-dark));
    --thumb: light-dark(var(--thumb-light), var(--thumb-dark));
  }
}

@supports not (scrollbar-color: auto) {
  .table-wrapper::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }
  .table-wrapper::-webkit-scrollbar-thumb {
    background: var(--thumb);
  }
  .table-wrapper::-webkit-scrollbar-track {
    background: var(--track);
  }
}

@media (pointer: coarse) {
  :root {
    --target-size: 44px;
  }
}

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

html {
  scroll-padding-bottom: calc(2.5rem + env(safe-area-inset-bottom, 0px));
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  overflow-wrap: break-word;
  padding-bottom: max(1.25rem, env(safe-area-inset-bottom, 0px));
}

h1, h2, h3 {
  text-wrap: balance;
  margin: 0;
}

p, li {
  text-wrap: pretty;
}

:where(a, code) {
  overflow-wrap: anywhere;
}

img, svg, canvas {
  max-inline-size: 100%;
  block-size: auto;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

:where(button, select, input:not([type="checkbox"], [type="radio"]), .file-btn, label:has(input[type="checkbox"])) {
  min-block-size: var(--target-size);
  min-inline-size: var(--target-size);
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: max(0.75rem, env(safe-area-inset-top, 0px)) max(1rem, env(safe-area-inset-right, 0px)) 0.75rem max(1rem, env(safe-area-inset-left, 0px));
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  gap: 0.65rem;
  align-items: center;
}

.brand-icon {
  flex: none;
  border-radius: 8px;
}

.brand h1 {
  font-size: 1.2rem;
}

.subtitle {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.toolbar > :not(button, input, select, a, label) {
  min-inline-size: 0;
}

.workspace {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem max(1rem, env(safe-area-inset-right, 0px)) 1.5rem max(1rem, env(safe-area-inset-left, 0px));
  container: workspace / inline-size;
}

.workspace-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@container workspace (inline-size > 54rem) {
  .workspace-grid {
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
    align-items: start;
  }
  .table-card {
    grid-column: 1 / -1;
  }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.card-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.card-head h2 {
  font-size: 1.05rem;
}

.pill {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.65rem;
  font-size: 0.85rem;
  font-weight: 600;
}

.btn, select, input[type="number"] {
  font: inherit;
  font-size: max(1rem, 1em);
  max-inline-size: 100%;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  padding: 0.45rem 0.75rem;
}

:where(button, select, input, summary, [tabindex]):focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
}

.engine-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.engine-note {
  margin-top: -0.25rem;
}

.drop-hint {
  margin-inline-start: 0.2rem;
}

.sample-strip {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.sample-heading {
  margin: 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.sample-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
  gap: 0.5rem;
}

.sample-card {
  appearance: none;
  font: inherit;
  color: var(--text);
  background: var(--surface-alt);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  cursor: pointer;
  text-align: start;
  transition: border-color 0.15s ease, transform 0.1s ease;
}

.sample-card:hover {
  border-color: var(--brand);
}

.sample-card.is-active {
  border-color: var(--brand);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--brand) 25%, transparent);
}

.sample-card img {
  width: 100%;
  aspect-ratio: 3 / 2;
  object-fit: cover;
  border-radius: 7px;
  display: block;
}

.sample-title {
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.2;
}

.canvas-stage.is-dragover {
  border-color: var(--brand);
  border-style: solid;
  box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--brand) 25%, transparent);
}

.site-footer {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.25rem max(1rem, env(safe-area-inset-right, 0px)) max(1.25rem, env(safe-area-inset-bottom, 0px)) max(1rem, env(safe-area-inset-left, 0px));
  font-size: 0.82rem;
  color: var(--muted);
}

.site-footer p {
  margin: 0;
}

.btn {
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}

.btn-primary {
  background: var(--brand);
  color: var(--brand-text);
  border-color: transparent;
  padding: 0.6rem 1.1rem;
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-ghost {
  background: transparent;
}

.btn-chip {
  font-size: 0.82rem;
  padding: 0.3rem 0.6rem;
}

.file-btn {
  position: relative;
  overflow: hidden;
}

.file-btn input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.canvas-stage {
  position: relative;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--surface-alt);
  overflow: hidden;
  min-block-size: 190px;
  display: grid;
  place-items: center;
}

#calib-canvas {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
  cursor: crosshair;
}

.canvas-placeholder {
  position: absolute;
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  margin: 0;
  pointer-events: none;
}

.calib-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.calib-box legend {
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 0.35rem;
}

.field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 0.6rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
}

.predict-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.65rem;
}

.check-label {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  cursor: pointer;
}

.hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.status-text {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 500;
}

.progress-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.progress-wrap[hidden], .card[hidden], [hidden] {
  display: none !important;
}

progress {
  appearance: none;
  border: 0 solid;
  inline-size: 100%;
  block-size: 10px;
  --progress-bar-radius: 1e5px;
  border-radius: var(--progress-bar-radius);
  overflow: clip;
  background: var(--track);
  container-type: inline-size;
  --progress-bar-indeterminate-size: 32%;
  --progress-bar-indeterminate-animation-duration: 1.4s;
}

@media (prefers-reduced-motion: reduce) {
  progress {
    --progress-bar-indeterminate-animation-duration: 10s;
  }
}

progress:dir(rtl) {
  --progress-bar-indeterminate-animation: progress-bar-indeterminate var(--progress-bar-indeterminate-animation-duration) infinite linear reverse;
}

progress:indeterminate {
  --progress-bar-animation: var(
    --progress-bar-indeterminate-animation,
    progress-bar-indeterminate var(--progress-bar-indeterminate-animation-duration) infinite linear
  );
}

progress::-webkit-progress-bar {
  background: transparent;
}

progress::-webkit-progress-value {
  background: var(--brand);
  border-radius: var(--progress-bar-radius);
  animation: var(--progress-bar-animation, revert-rule);
}

progress::-moz-progress-bar {
  background: var(--brand);
  border-radius: var(--progress-bar-radius);
  animation: var(--progress-bar-animation, revert-rule);
}

@keyframes progress-bar-indeterminate {
  from, to {
    inline-size: var(--progress-bar-indeterminate-size);
    margin-right: auto;
  }
  from { translate: -100%; }
  to { translate: 100cqi; }
}

.uscs-banner {
  background: var(--surface-alt);
  border-left: 4px solid var(--brand);
  border-radius: 8px;
  padding: 0.65rem 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 0.5rem;
}

.kpi {
  background: var(--surface-alt);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
  display: flex;
  flex-direction: column;
}

.kpi span {
  font-size: 0.78rem;
  color: var(--muted);
}

.kpi strong {
  font-size: 1.05rem;
}

.comp-bar {
  display: flex;
  block-size: 14px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface-alt);
}

.comp-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.4rem;
  font-size: 0.82rem;
}

.chart-wrap {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.35rem;
  background: var(--surface);
}

#psd-svg {
  width: 100%;
  height: auto;
  display: block;
}

/* Responsive table (sticky headers desktop, stacked cards mobile) */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(460px, 70vh);
  container-type: inline-size;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  --label-1: "Sieve (mm)";
  --label-2: "Passing (%)";
  --label-3: "Fraction";
}

caption {
  text-align: start;
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 0.6rem;
}

th, td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: start;
}

thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 1;
  background: var(--surface-alt);
}

tbody th {
  position: sticky;
  inset-inline-start: 0;
  background: var(--surface);
  font-weight: 600;
}

@container (width < 520px) {
  table, caption, thead, tbody, tr, th, td {
    display: block;
  }
  thead {
    display: none;
  }
  tr {
    margin-block-end: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
  }
  tbody th {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--surface-alt);
  }
  td:nth-child(2)::before {
    content: var(--label-2) ": ";
    color: var(--muted);
    font-weight: 600;
  }
  td:nth-child(3)::before {
    content: var(--label-3) ": ";
    color: var(--muted);
    font-weight: 600;
  }
}
