:root {
  color-scheme: light;
  --ink: #22201d;
  --muted: #706b63;
  --line: #ded7ce;
  --paper: #fbfaf7;
  --panel: #ffffff;
  --accent: #167f78;
  --accent-strong: #0f5f5a;
  --coffee: #5b3927;
  --coffee-soft: #d7b28b;
  --warn: #b4661d;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(120deg, rgba(22, 127, 120, .08), transparent 38%),
    linear-gradient(320deg, rgba(183, 117, 52, .12), transparent 42%),
    var(--paper);
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 700;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(28px, 5vw, 48px);
  line-height: 1.05;
}

h2 {
  font-size: 20px;
}

.status-pill {
  min-width: 96px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, .76);
  text-align: center;
  white-space: nowrap;
}

.workspace {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 18px;
  align-items: stretch;
}

.control-panel,
.chart-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .86);
  box-shadow: 0 18px 38px rgba(52, 38, 25, .08);
}

.control-panel {
  padding: 16px;
}

#imageInput {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.upload-zone {
  display: grid;
  gap: 6px;
  place-items: center;
  min-height: 132px;
  border: 1.5px dashed #b7aaa0;
  border-radius: 8px;
  padding: 18px;
  text-align: center;
  cursor: pointer;
  background: #fffdf9;
}

.upload-zone:hover {
  border-color: var(--accent);
}

.upload-icon {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 50%;
  color: white;
  background: var(--accent);
  font-size: 28px;
  line-height: 1;
}

.upload-zone small,
.hint,
.readouts,
.chart-header span {
  color: var(--muted);
  font-size: 13px;
}

.control-group {
  padding-top: 18px;
}

.group-title,
.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  font-weight: 800;
}

label {
  display: grid;
  gap: 8px;
  margin: 12px 0;
  color: #37322d;
  font-size: 14px;
  font-weight: 650;
}

input[type="number"],
select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: white;
}

input[type="range"] {
  accent-color: var(--accent);
}

.inline-fields {
  display: grid;
  grid-template-columns: 1fr 88px;
  gap: 8px;
}

.icon-button,
.text-button,
.primary-button {
  border: 1px solid var(--line);
  border-radius: 7px;
  min-height: 38px;
  background: white;
  color: var(--ink);
  cursor: pointer;
}

.icon-button {
  width: 42px;
  font-size: 20px;
}

.text-button {
  padding: 0 14px;
  font-weight: 750;
}

.primary-button {
  width: 100%;
  min-height: 48px;
  border-color: var(--accent);
  color: white;
  background: var(--accent);
  font-weight: 850;
}

.primary-button:hover,
.icon-button:hover,
.text-button:hover {
  filter: brightness(.96);
}

.readouts {
  display: grid;
  gap: 7px;
  padding: 10px;
  border-radius: 7px;
  background: #f5f1eb;
}

.image-stage {
  position: relative;
  display: grid;
  min-height: 620px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background:
    linear-gradient(45deg, #f2eee8 25%, transparent 25%),
    linear-gradient(-45deg, #f2eee8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #f2eee8 75%),
    linear-gradient(-45deg, transparent 75%, #f2eee8 75%),
    #fbf8f3;
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}

#imageCanvas,
#overlayCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#overlayCanvas {
  cursor: crosshair;
  touch-action: none;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

.empty-state strong {
  color: var(--ink);
  font-size: 22px;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin: 18px 0;
}

.metric-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  background: rgba(255, 255, 255, .88);
}

.metric-card span {
  display: block;
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 8px;
}

.metric-card strong {
  font-size: clamp(24px, 4vw, 38px);
  color: var(--coffee);
}

.chart-section {
  padding: 16px;
}

#histogramCanvas {
  display: block;
  width: 100%;
  height: auto;
  max-height: 440px;
}

@media (max-width: 920px) {
  .app-shell {
    padding: 14px;
  }

  .topbar,
  .workspace {
    display: grid;
  }

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

  .image-stage {
    min-height: 58vh;
  }

  .results-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 520px) {
  .results-grid {
    grid-template-columns: 1fr;
  }

  .status-pill {
    justify-self: start;
  }
}
