@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300;400;500;600&family=Space+Grotesk:wght@400;600;700&display=swap");

:root {
  --bg: #f4efe8;
  --panel: #ffffff;
  --panel-border: #e1d7c9;
  --ink: #1c1c1c;
  --muted: #5c5c5c;
  --accent: #0b6b5f;
  --accent-2: #d65b2a;
  --shadow: rgba(0, 0, 0, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "IBM Plex Sans", "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at top left, #fef9f3 0%, #f4efe8 45%, #efe7db 100%);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 1.4rem 0.7rem;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(6px);
}

.app-kicker {
  font-size: 0.75rem;
  letter-spacing: 0.2rem;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 0.5rem;
  font-weight: 600;
}

h1 {
  font-family: "Space Grotesk", "IBM Plex Sans", sans-serif;
  font-size: 1.2rem;
  line-height: 1.2;
  margin: 0 0 0.25rem;
}

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

.app-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.4rem;
  font-weight: 600;
}

.app-meta span {
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  box-shadow: 0 6px 18px var(--shadow);
  min-width: 118px;
  font-size: 0.85rem;
  text-align: center;
}

.app-grid {
  display: grid;
  grid-template-columns: minmax(540px, 58vw) minmax(0, 1fr);
  gap: 1rem;
  padding: 0.8rem 1.4rem 1rem;
  flex: 1;
  min-height: 0;
}

.controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.7rem;
  align-content: start;
  min-height: 0;
  overflow-y: auto;
  padding-right: 0.2rem;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  padding: 0.65rem 0.75rem 0.6rem;
  border-radius: 12px;
  box-shadow: 0 6px 14px var(--shadow);
}

.panel h2 {
  margin: 0 0 0.5rem;
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.09rem;
  color: var(--muted);
}

.field {
  display: grid;
  gap: 0.2rem;
  margin-bottom: 0.45rem;
}

.field span {
  font-size: 0.74rem;
  color: var(--muted);
}

.field input[type="range"],
.field select {
  width: 100%;
}

.value {
  font-weight: 600;
  color: var(--ink);
  font-size: 0.76rem;
}

.button-row {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.4rem;
}

button {
  flex: 1;
  border: 1px solid var(--panel-border);
  background: #f9f6f1;
  padding: 0.42rem 0.55rem;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.78rem;
  cursor: pointer;
}

button.primary {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.canvas-panel {
  background: var(--panel);
  border-radius: 16px;
  padding: 0.85rem;
  box-shadow: 0 12px 24px var(--shadow);
  border: 1px solid var(--panel-border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0.7rem;
  min-height: 0;
}

.legend {
  display: flex;
  gap: 0.7rem;
  font-size: 0.72rem;
  color: var(--muted);
  flex-wrap: wrap;
}

.legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  margin-right: 0.4rem;
  vertical-align: middle;
}

.supplier-x { background: #f4c430; }
.supplier-y { background: #d9492a; }
.retailer-x { background: #f8e26a; }
.retailer-y { background: #ff6b4a; }

canvas {
  width: min(100%, clamp(340px, 41vw, 560px));
  height: auto;
  max-height: calc(100vh - 220px);
  aspect-ratio: 1 / 1;
  background: #fbfaf7;
  border-radius: 12px;
  border: 1px solid #efe3d2;
}

.controls .panel:nth-child(1),
.controls .panel:nth-child(5) {
  grid-column: 1 / -1;
}

#statsDialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  width: min(720px, 90vw);
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}

#statsDialog::backdrop {
  background: rgba(0,0,0,0.4);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--panel-border);
  background: #f8f4ee;
}

#statsText {
  margin: 0;
  padding: 1.5rem;
  font-family: "IBM Plex Mono", "Courier New", monospace;
  white-space: pre-wrap;
  max-height: 70vh;
  overflow-y: auto;
}

.hidden {
  display: none;
}

@media (max-width: 1100px) {
  .app-grid {
    grid-template-columns: 1fr;
    padding: 1rem;
  }

  .app-header {
    flex-direction: column;
    align-items: flex-start;
    padding: 1rem;
  }

  .app-meta {
    flex-direction: row;
  }

  .controls {
    grid-template-columns: 1fr;
    overflow: visible;
    padding-right: 0;
  }

  .controls .panel:nth-child(1),
  .controls .panel:nth-child(5) {
    grid-column: auto;
  }
}

@media (max-height: 860px) and (min-width: 1101px) {
  h1 {
    font-size: 1.08rem;
  }

  .panel {
    padding: 0.55rem 0.65rem 0.5rem;
  }

  .field {
    margin-bottom: 0.35rem;
  }

  canvas {
    max-height: calc(100vh - 205px);
  }
}
