:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --panel: #ffffff;
  --ink: #16211f;
  --muted: #61706d;
  --line: #d7dfdc;
  --accent: #0f766e;
  --accent-dark: #0b5953;
  --amber: #d68b17;
  --canvas: #fbfcfc;
  --shadow: 0 18px 45px rgba(22, 33, 31, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  background:
    linear-gradient(135deg, rgba(15, 118, 110, 0.08), transparent 34%),
    linear-gradient(315deg, rgba(214, 139, 23, 0.08), transparent 32%),
    var(--bg);
  color: var(--ink);
  font-family: Inter, "Noto Sans JP", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
}

button,
input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(380px, 440px) minmax(0, 1fr);
  gap: 18px;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  padding: 18px;
}

body.controls-collapsed .app-shell {
  grid-template-columns: minmax(250px, 300px) minmax(0, 1fr);
}

.controls,
.plot-area {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-height: 0;
  padding: 18px;
  overflow: hidden;
}

.brand {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex: 0 0 auto;
}

.brand h1 {
  margin: 0;
  font-size: 2rem;
  line-height: 1;
}

.brand-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand p,
.plot-toolbar span,
.readout dt {
  color: var(--muted);
}

.brand p {
  margin: 8px 0 0;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--accent-dark);
  cursor: pointer;
}

.icon-button:hover {
  border-color: var(--accent);
}

.auto-scale-button,
.toggle-button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--accent-dark);
  cursor: pointer;
  font-weight: 700;
  padding: 0 12px;
  white-space: nowrap;
}

.auto-scale-button {
  background: var(--accent);
  color: #fff;
}

.auto-scale-button:hover {
  background: var(--accent-dark);
}

.toggle-button {
  display: none;
}

.toggle-button:hover {
  border-color: var(--accent);
}

.controls-panel {
  display: grid;
  gap: 18px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.controls-panel[hidden] {
  display: none;
}

.control-form,
.control-section {
  display: grid;
  gap: 14px;
}

.control-section {
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.control-section h2 {
  margin: 0;
  font-size: 0.9rem;
  letter-spacing: 0;
}

label {
  display: grid;
  grid-template-columns: 1fr minmax(120px, 150px) 34px;
  align-items: center;
  gap: 10px;
  color: var(--ink);
}

label span {
  min-width: 0;
  font-size: 0.92rem;
}

label small {
  color: var(--muted);
}

input {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 9px 10px;
}

input:focus {
  border-color: var(--accent);
  outline: 3px solid rgba(15, 118, 110, 0.16);
}

.vector-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.vector-grid label {
  grid-template-columns: 24px minmax(0, 1fr);
}

.range-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.range-grid label {
  grid-template-columns: 1fr minmax(0, 108px) 24px;
}

.primary-button {
  width: 100%;
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
}

.primary-button:hover {
  background: var(--accent-dark);
}

.readout {
  display: grid;
  gap: 10px;
  margin: 0;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.readout div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.readout dt,
.readout dd {
  margin: 0;
}

.readout dd {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.plot-area {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.plot-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 64px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.plot-toolbar strong {
  display: block;
  margin-bottom: 4px;
}

.legend {
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}

.plot-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 0 4px rgba(214, 139, 23, 0.18);
}

#plotCanvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  background: var(--canvas);
  cursor: grab;
  touch-action: none;
}

#plotCanvas.is-panning {
  cursor: grabbing;
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 10px;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    padding: 10px;
  }

  body.controls-collapsed .app-shell {
    grid-template-columns: 1fr;
  }

  .controls,
  .plot-area {
    box-shadow: 0 10px 28px rgba(22, 33, 31, 0.1);
  }

  .controls {
    gap: 12px;
    max-height: 58dvh;
    padding: 12px;
    overflow: hidden;
  }

  body.controls-collapsed .controls {
    max-height: none;
    overflow: visible;
  }

  .brand {
    align-items: center;
  }

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

  .controls-panel {
    gap: 12px;
    overflow-y: auto;
  }

  .control-section {
    padding-top: 12px;
  }

  .plot-toolbar {
    min-height: 52px;
    padding: 10px 12px;
  }

  .toggle-button {
    display: inline-block;
  }

  #plotCanvas {
    min-height: 0;
  }
}

@media (max-width: 520px) {
  .brand {
    align-items: stretch;
    flex-direction: column;
  }

  .brand-actions {
    justify-content: space-between;
  }

  .brand-actions button {
    flex: 1;
  }

  label,
  .range-grid,
  .vector-grid {
    grid-template-columns: 1fr;
  }

  .range-grid label,
  .vector-grid label {
    grid-template-columns: 1fr;
  }

  .plot-toolbar {
    align-items: flex-start;
    flex-direction: row;
  }

  .plot-toolbar span {
    display: none;
  }

  .plot-toolbar strong {
    margin-bottom: 0;
  }

  .legend {
    display: none;
  }

  .plot-actions {
    flex: 0 0 auto;
  }

  .toggle-button {
    max-width: none;
    padding: 0 10px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
